Redis0.1 source code reading notes

preparation

The code of redis-0.1 is downloaded from the network. This version has a small amount of code and is easy to understand. It also contains many basic ideas and theories of the author in developing redis. It is a good source code reading material. from Redis source code download address After downloading, unzip it to the directory specified by yourself. Enter the directory and execute make to generate three executable files: redis benchmark (redis stress test program), redis server (redis service program) and redis cli (redis client program)

Catalog introduction

file namebrief introduction
client-librariesImplementation of API interface support for various languages
docIntroduction documents attached to redis
utilsunknown

Program initial operation

redis-server

. / redis server generates the following output

04 Feb 04:33:40 * WARNING overcommit_memory is set to 0! Background save may fail under low condition memory. To fix this issue add 'echo 1 > /proc/sys/vm/overcommit_memory' in your init scripts.
04 Feb 04:33:40 * Warning: no config file specified, using the default config. In order to specify a config file use 'redis-server /path/to/redis.conf'
04 Feb 04:33:40 - Server started, Redis version 0.100
04 Feb 04:33:40 - The server is now ready to accept connections on port 6379
04 Feb 04:33:41 . 0 clients connected (0 slaves), 2521 bytes in use
04 Feb 04:33:46 . 0 clients connected (0 slaves), 2521 bytes in use
04 Feb 04:33:51 . 0 clients connected (0 slaves), 2521 bytes in use
04 Feb 04:33:56 . 0 clients connected (0 slaves), 2521 bytes in use

Question one

Warning: no config file specified, using the default config.In order to specify a config file use 'redis-server /path/to/redis.conf'
Warning: there is no explicit config file, use the default configuration. To specify the configuration file, please use 'redis server / path / to / redis conf’
#./redis-server /usr/local/src/redis-0.1/redis.conf

Question two

WARNING overcommit_memory is set to 0! Background save may fail under low condition memory. To fix this issue add 'echo 1 > /proc/sys/vm/overcommit_memory' in your init scripts.

overcommit_memory (kernel parameter)

This is the memory allocation strategy of the kernel. The optional values are 0, 1 and 2

valuesignificance
0Indicates that the kernel will check whether there is enough available memory for the process to use; If there is enough available memory, the memory application is allowed; Otherwise, the memory request fails and the error is returned to the application process
1Indicates that the kernel allows all physical memory to be allocated regardless of the current memory state.
2Indicates that the kernel is allowed to allocate more memory than the sum of all physical memory and swap space

What are Overcommit and OOM

Linux replies "yes" to most requests for memory so that it can run more and larger programs. Because after applying for memory, memory will not be used immediately. This technology is called Overcommit. When Linux finds that there is insufficient memory, oom killer (OOM = out of memory) will occur. It will choose to kill some processes (user state processes, not kernel threads) in order to free up memory.
When oom killer happens, which processes will linux choose to kill? The function to select a process is oom_badness function (in mm/oom_kill.c), which calculates the number of points (0 ~ 1000) for each process. The higher the number of points, the more likely the process is to be killed. The number of points per process is the same as oom_score_adj related, and oom_score_adj can be set (- 1000 min, 1000 max).

terms of settlement

It's very simple. Just follow the prompt (set vm.overcommit_memory to 1). There are three ways to modify kernel parameters, but with root permission:

  • vim /etc/sysctl.conf, change VM overcommit_ Memory = 1, then sysctl -p makes the configuration file effective
  • sysctl vm.overcommit_memory=1
  • echo 1 > /proc/sys/vm/overcommit_memory

redis-cli

. / redis cli produces the following output

usage: redis-cli [-h host] [-p port] cmd arg1 arg2 arg3 ... argN
usage: echo "argN" | redis-cli [-h host] [-p port] cmd arg1 arg2 ... arg(N-1)

If a pipe from standard input is detected this data is used as last argument.

example: cat /etc/passwd | redis-cli set my_passwd
example: redis-cli get my_passwd

Unfinished to be continued

Tags: C Redis

Posted by erupt on Mon, 25 Apr 2022 12:30:55 +0300