redis 소스를 살펴보면, 리눅스일 때는 메모리 관련 체크 설정이 들어가 있다.


https://github.com/antirez/redis/blob/3.2/src/server.c



int main(int argc, char **argv) {

...


    #ifdef __linux__


       linuxMemoryWarnings();

  #endif

...


}




linuxMemoryWarnings() 함수는 두 가지를 확인하고 이슈가 있을 것 같으면 warning을 출력한다.

1 번째는 memory overcommit 를 체크한다. (/proc/sys/vm/overcommit_memory)

2 번째는 Transparent Huge Table 정보를 확인한다.(vm.overcommit_memory)




https://github.com/antirez/redis/blob/3.2/src/server.c


#ifdef __linux__

int linuxOvercommitMemoryValue(void) {

    FILE *fp = fopen("/proc/sys/vm/overcommit_memory","r");

    char buf[64];


    if (!fp) return -1;

    if (fgets(buf,64,fp) == NULL) {

        fclose(fp);

        return -1;

    }

    fclose(fp);


    return atoi(buf);

}


void linuxMemoryWarnings(void) {

    if (linuxOvercommitMemoryValue() == 0) {

        serverLog(LL_WARNING,"WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.");

    }

    if (THPIsEnabled()) {

        serverLog(LL_WARNING,"WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.");

    }

}

#endif /* __linux__ */




https://github.com/antirez/redis/blob/3.2/src/latency.c




#ifdef __linux__

/* Returns 1 if Transparent Huge Pages support is enabled in the kernel.

 * Otherwise (or if we are unable to check) 0 is returned. */

int THPIsEnabled(void) {

    char buf[1024];


    FILE *fp = fopen("/sys/kernel/mm/transparent_hugepage/enabled","r");

    if (!fp) return 0;

    if (fgets(buf,sizeof(buf),fp) == NULL) {

        fclose(fp);

        return 0;

    }

    fclose(fp);

    return (strstr(buf,"[never]") == NULL) ? 1 : 0;

}

#endif





* 메모리 오버커밋에 대한 설명이다.

메모리 오버커밋(memory overcommit)은 물리 메모리 공간 이상을 쓸 수 있는 방법으로, 가상 메모리(virtual memory)를 함께 사용하여 더 많은 메모리를 할당할 수 있는 기법을 의미한다. 파일에 존재하는 디스크의 공간을 스왑 메모리로 설정하면, 사용할 수 있는 메모리 공간이 커진다


백그라운드 저장을 빨리 하기 위해 메모리 오버커밋(overcommit) 설정을 1로 명세한다. /etc/sysctl.conf 파일에 다음을 추가한다.

vm.overcommit_memory=1


/etc/sysctl.conf를 저장한 후에는 리눅스 서버를 재시작한다.

 

또한, 레디스 설정에는 레디스가 사용할 수 있는 메모리의 용량(바이트)를 제한하는 maxmemory라는 지시자가 있다.




* THP  (Transparent Huge Pages)에 대한 설명이다.


CentOS 운영체제는 메모리의 관리를 페이지(page) 단위로 한다. 페이지는 작은 단위(4KB)이지만, 애플리케이션이 대용량 메모리를 필요로 하면 운영체제가 특별한 설정 없이 페이지의 크기를 아주 크게 할당(2MB 또는 1GB)하려 애플리케이션에서 쓸 수 있게 한다. 이를 THP 기능이라 한다




THP리눅스 커널 기능을 쓰지 않으려면, 다음과 같이 설정한다. 


sudo echo never > /sys/kernel/mm/transparent_hugepage/enabled






Posted by '김용환'
,