3 Ağustos 2021 Salı

Linux ve Overcommit

Giriş
Linux uygulamaların talep ettikleri bellek miktarını aslında kullanmayacaklarını farz ederek, fiziksel ve sanal bellek üst sınırından çok daha fazlasını ayırıyormuş gibi çalışabilir. 

Buna overcommit deniliyor. Linux'ta "Out Of Memory (OOM) Killer" bulunur. Bu algoritma sistem konfigürasyonuna göre devreye girip çalışıyor

Overcommit Ayarı
Sistem ayarı /proc/sys/vm/overcommit_memory dosyasında ve çoğu sistem 0 değeri yani overcommit etkin olarak başlıyor. Açıklaması şöyle
/proc/sys/vm/overcommit_memory
       This file contains the kernel virtual memory accounting mode.
       Values are:

              0: heuristic overcommit (this is the default)
              1: always overcommit, never check
              2: always check, never overcommit

       In mode 0, calls of mmap(2) with MAP_NORESERVE are not
       checked, and the default check is very weak, leading to the
       risk of getting a process "OOM-killed".

       In mode 2 (available since Linux 2.6), the total virtual
       address space that can be allocated (CommitLimit in /proc/mem‐
       info) is calculated as

           CommitLimit = (total_RAM - total_huge_TLB) *
                         overcommit_ratio / 100 + total_swap
Bu dosyadaki değerler bilgisayarı yeniden başlatınca kaybolur. Esas değeri "/etc/sysctl.conf" dosyasına yazmak lazım

overcommit_memory = 0 İse OOM-Killer Devrededir
Bu durumda overcommit yapılır ve işletim sistemi çok fazla bellek isteyen bazı uygulamaları öldürür.
Yani /var/log/kern.log veya /var/log dosyasında "Out of Memory: Kill Process or Sacrifice Child" şeklinde hatalar görebiliriz.

Örnek
Şöyle bir satır görebiliriz
[ 1584.087068] Out of memory: Kill process 3070 (java) score 547 or sacrifice child 
[ 1584.094170] Killed process 3070 (java) total-vm:56994588kB, anon-rss:35690996kB, file-rss:0kB, shmem-rss:0kB
Koyu renkli sayıların açıklaması şöyle. 3070 Process Id (PID) anlamına gelir. 547 ise OOM algoritmasının bulduğu değerdir. totalvm sanırım 56GB civarında bellek kullandığını gösteriyor.
Above is the kernel log message printed on this device. You can note that the Operating System is killing our BuggyApp java process (whose Process Id is 3070). Note Operating System has internal algorithms and heuristics and keeps track of score for each process. Score this BuggyApp got was 547, thus it killed this process.
Örnek
Şöyle bir satır görebiliriz
kernel: [28244264.657076] Out of memory: Kill process 28854 (java) score 977 or sacrifice child

kernel: [28244264.657146] Killed process 28854, UID 500, (java) total-vm:138204008kB, 
  anon-rss:128965112kB, file-rss:144kB rss:0kB
overcommit_memory = 1 İse
İşletim sistemi hiç bir kontrol yapmaz. Açıklaması şöyle
... setting overcommit to 1, will set the stage so that when a program calls something like malloc() to allocate a chunk of memory (man 3 malloc), it will always succeed regardless if the system knows it will not have all the memory that is being asked for.

overcommit_memory = 2 İse
Halen overcommit yapılır ancak bu sefer üst sınır konulmuştur. Eldeki toplam bellek alanı * vm.overcommit_ratio değeri kadar bellek isteği yapılabilir. Açıklaması şöyle
When you set vm.overcommit_memory to 2, the vm.overcommit_ratio value becomes relevant. By default, this value is set to 50, which means the system would only allocate up to 50% of your RAM (plus swap).
Bir açıklama şöyle
If you set vm.overcommitmemory=2 then it will overcommit up to the percentage of physical RAM configured in vm.overcommitratio (default is 50%).

Overcommit Kod Örnekleri

Örnek
Aşağıdaki örnek döngü içinde çağrılarak 50GB bellek istense bile sorun çıkartmıyor.
unsigned long long memory_to_eat = 1024 * 50000;
size_t eaten_memory = 0;
void *memory = NULL;

int eat_kilobyte()
{
  if (memory == NULL)
    memory = malloc(1024);
  else
    memory = realloc(memory, (eaten_memory * 1024) + 1024);
  if (memory == NULL)
  {
    return 1;
  }
  else
  {
    eaten_memory++;
    return 0;
  }
}
Ancak ne zamanki kod sayfalara dokunmaya başlar, o zaman OOM hatası alırız.
int eat_kilobyte()
{
  if (memory == NULL)
    memory = malloc(1024);
  else
    memory = realloc(memory, (eaten_memory * 1024) + 1024);
  if (memory == NULL)
  {
    return 1;
  }
  else
  {
    //Force the kernel to map the containing memory page.
    ((char*)memory)[1024*eaten_memory] = 42;

    eaten_memory++;
    return 0;
  }
}

Hiç yorum yok:

Yorum Gönder