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;
  }
}

27 Temmuz 2021 Salı

sftp komutu

Örnek
Şöyle yaparız. Kullanıcı ismi user, şifre ise xx şeklindedir. Her ikisi arasında da : karakteri bulunur
sftp://user:xx@test.com:22

25 Temmuz 2021 Pazar

cpulimit komutu - Kullanmayın

Giriş
Açıklaması şöyle
cpulimit is, in 2021, really not the kind of tool that you want to use, especially not with interactive software: it "throttles" processes (or unsuccessfully tries to, in your case) by stopping and resuming them via signals. Um. That's a terrible hack, and it leads to unreliability you really don't want in a modern browser that might well be processing audio and video, or trying to scroll smoothly.
Örnek
Şöyle yaparız
cpulimit -l 30 -- chromium --incognito

22 Temmuz 2021 Perşembe

chmod gid bit - Dosya Paylaşmak İçindir

Giriş
Açıklaması şöyle. Dizin için kullanılırsa, dizinde dosya oluşturulunca sahibinin grubuna değil, dizinin grubuna ait olur
chmod preserves a directory's set-user-ID and set-group-ID bits unless you explicitly specify otherwise. You can set or clear the bits with symbolic modes like u+s and g-s, and you can set (but not clear) the bits with a numeric mode.
Örnek - grup hakkı vermek
Gruba guid hakkı vermek için şöyle yaparız.
chmod g+s [path]
Örnek - grup hakkını almak
Şöyle yaparız.
% stat -c '%04a' ~/testdir
2700

% chmod g-w ~/testdir
% stat -c '%04a' ~/testdir
0700
Örnek - grup hakkı vermek
Elimizde bir grup olsun ve buna kullanıcıları ekleyelim
addgroup webimage

adduser user1 webimage
adduser user2 webimage
adduser www-data webimage
Gruptaki herkese yazma hakkı vermek için şöyle yaparız
sudo chmod g+s image/
Açıklaması şöyle
This will cause any file created in that directory to be owned by the same group as the directory. Thus, if image is owned by group webimage, any file created there will be owned by the group webimage. If also read and write permissions are set for the group, all members of the group will be able to update the file.

19 Temmuz 2021 Pazartesi

pthread_setaffinity_np metodu

Giriş
Bu metod standart olmayan GNU fonksiyonudur (_np uzantısı nonportable demek) ve threadin hangi mantıksal işlemciye bağlanmak istendiği belirtilir. Açıklaması şöyle
Platform specific API's do have the ability to specify specific logical core (or a set of such cores) for a software thread. For example, GNU has pthread_setaffinity_np.
Örnek
Şöyle yaparız.
int cpuNum = ...
cpu_set_t cpus;
CPU_ZERO(&cpus);
CPU_SET(cpuNum, &cpus);

if(pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpus) != 0)  {
...Error...
}

13 Temmuz 2021 Salı

openssl speed aracı

Örnek
Şöyle yaparız
OpenSSL> speed rsa
Örnek
Şöyle yaparız
openssl speed -evp aes-128-cbc aes-128-ctr

1 Temmuz 2021 Perşembe

isatty metodu

Örnek
Şöyle yaparız. Eğer terminale bağlıysa true döner.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
  printf("%d\n", isatty(STDOUT_FILENO));

  return EXIT_SUCCESS;
}