当前位置:网站首页>Memcached comprehensive analysis – 2 Understand memcached memory storage

Memcached comprehensive analysis – 2 Understand memcached memory storage

2022-06-24 21:27:00 Hello,C++!

I am a mixi Co., Ltd Maesaka of the research and Development Group . The last time The article introduces memcached Is a distributed cache server . This will introduce memcached Implementation of the internal structure of , And memory management . in addition ,memcached The weaknesses caused by the internal structure of the will also be explained .

Slab Allocation Mechanism : Organize memory for reuse

Current memcached By default, it uses the name Slab Allocator The mechanism of distribution 、 Manage memory . Before this mechanism appeared , The allocation of memory is simple through all records malloc and free To carry out . however , This leads to memory fragmentation , Increase the burden of operating system memory manager , At worst , Will cause the operating system to be better than memcached The process itself is slow .Slab Allocator It was born to solve the problem .

So let's see Slab Allocator Principle . Here is memcached In document slab allocator The goal of :

the primary goal of the slabs subsystem in memcached was to eliminate memory fragmentation issues totally by using fixed-size memory chunks coming from a few predetermined size classes.

in other words ,Slab Allocator The basic principle is to follow a predetermined size , Splits allocated memory into blocks of a specific length , To completely solve the memory fragmentation problem .

Slab Allocation The principle is quite simple . Divide the allocated memory into blocks of various sizes (chunk), And divide blocks of the same size into groups (chunk Set )( chart 1).

img

chart 1 Slab Allocation Construction drawing of

and ,slab allocator There is also the purpose of reusing allocated memory . in other words , The allocated memory will not be released , It's recycling .

Slab Allocation Main terms of

Page

Assigned to Slab Of memory space , The default is 1MB. Assigned to Slab Then according to slab Size cut of chunk.

Chunk

Memory space for caching records .

Slab Class

Of a certain size chunk Group .

stay Slab The principle of caching records in

The following instructions memcached How to select the data sent by the client slab And cache to chunk in .

memcached Depending on the size of the data received , Choose the one that best fits the data size slab( chart 2). memcached There is slab I'm free inside chunk A list of , Select... From the list chunk, Then cache the data in it .

img

chart 2 How to select a group to store records

actually ,Slab Allocator There are also advantages and disadvantages . Let's introduce its disadvantages .

Slab Allocator The shortcomings of

Slab Allocator Solved the original memory fragmentation problem , But the new mechanism also gives memcached Brought new problems .

The question is , Because a specific length of memory is allocated , Therefore, the allocated memory cannot be used effectively . for example , take 100 Bytes of data to 128 Bytes of chunk in , remainder 28 Bytes are wasted ( chart 3).

img

chart 3 chunk The use of space

There is no perfect solution to this problem , But more effective solutions are documented .

The most efficient way to reduce the waste is to use a list of size classes that closely matches (if that’s at all possible) common sizes of objects that the clients of this particular installation of memcached are likely to store.

That is to say , If the common size of the data sent by the client is known in advance , Or only cache data of the same size , Just use a list of groups that fit the data size , You can reduce waste .

But unfortunately , You can't do any tuning yet , I can only look forward to future versions . however , We can adjust slab class The difference in size . Next growth factor Options .

Use Growth Factor tuning

memcached Specified at startup Growth Factor factor ( adopt -f Options ), You can control... To some extent slab Differences between . The default value is 1.25. however , Before this option appears , This factor was once fixed to 2, be called “powers of 2” Strategy .

Let's use the previous settings , With verbose mode memcached Give it a try :

$ memcached -f 2 -vv

Here's what happens after startup verbose Output :

slab class   1: chunk size    128 perslab  8192
slab class   2: chunk size    256 perslab  4096
slab class   3: chunk size    512 perslab  2048
slab class   4: chunk size   1024 perslab  1024
slab class   5: chunk size   2048 perslab   512
slab class   6: chunk size   4096 perslab   256
slab class   7: chunk size   8192 perslab   128
slab class   8: chunk size  16384 perslab    64
slab class   9: chunk size  32768 perslab    32
slab class  10: chunk size  65536 perslab    16
slab class  11: chunk size 131072 perslab     8
slab class  12: chunk size 262144 perslab     4
slab class  13: chunk size 524288 perslab     2

so , from 128 The group of bytes begins , The size of the group increases to the original 2 times . The problem with this setting is ,slab There is a big difference between them , In some cases, it is quite a waste of memory . therefore , To minimize memory waste , Added... Two years ago growth factor This option .

Let's look at the current default settings (f=1.25) Time output ( And the limit of the length , Only the first 10 Group ):

slab class   1: chunk size     88 perslab 11915
slab class   2: chunk size    112 perslab  9362
slab class   3: chunk size    144 perslab  7281
slab class   4: chunk size    184 perslab  5698
slab class   5: chunk size    232 perslab  4519
slab class   6: chunk size    296 perslab  3542
slab class   7: chunk size    376 perslab  2788
slab class   8: chunk size    472 perslab  2221
slab class   9: chunk size    592 perslab  1771
slab class  10: chunk size    744 perslab  1409

so , The factor of gap ratio between groups is 2 It's much smaller , More suitable for caching hundreds of bytes of records . From the above output , You may feel that there is some calculation error , These errors are deliberately set to maintain byte alignment .

take memcached Introducing products , Or when deploying directly using default values , It's better to recalculate the expected average length of the data , adjustment growth factor, To get the most appropriate settings . Memory is a precious resource , It's a pity to waste .

Next, let's introduce how to use memcached Of stats Command view slabs Various information such as utilization rate of .

see memcached Internal state of

memcached A name is stats The order of , Using it, you can get all kinds of information . There are many ways to execute commands , use telnet The simplest :

$ telnet  Host name   Port number 

Connect to memcached after , Input stats Press enter again , You can get all kinds of information including resource utilization . Besides , Input "stats slabs" or "stats items" You can also get information about cached records . To end the program, please enter quit.

For details of these commands, refer to memcached In the package protocol.txt file .

$ telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
stats
STAT pid 481
STAT uptime 16574
STAT time 1213687612
STAT version 1.2.5
STAT pointer_size 32
STAT rusage_user 0.102297
STAT rusage_system 0.214317
STAT curr_items 0
STAT total_items 0
STAT bytes 0
STAT curr_connections 6
STAT total_connections 8
STAT connection_structures 7
STAT cmd_get 0
STAT cmd_set 0
STAT get_hits 0
STAT get_misses 0
STAT evictions 0
STAT bytes_read 20
STAT bytes_written 465
STAT limit_maxbytes 67108864
STAT threads 4
END
quit

in addition , If installed libmemcached This aspect C/C++ Language client library , Will install memstat This command . The method is simple to use , Can be obtained in fewer steps with telnet The same information , You can also get information from multiple servers at one time .

$ memstat --servers=server1,server2,server3,...

libmemcached It can be obtained from the following address :

see slabs The usage of

Use memcached The creation of Brad It's called memcached-tool Of Perl Script , You can easily get slab Usage situation ( It will memcached The return values of are sorted into an easy to read format ). You can get the script from the following address :

The method of use is also extremely simple :

$ memcached-tool  Host name : port   Options 

see slabs There is no need to specify options for usage , So use the following command :

$ memcached-tool  Host name : port 

The information obtained is as follows :

 #  Item_Size   Max_age  1MB_pages Count   Full?
 1     104 B  1394292 s    1215 12249628    yes
 2     136 B  1456795 s      52  400919     yes
 3     176 B  1339587 s      33  196567     yes
 4     224 B  1360926 s     109  510221     yes
 5     280 B  1570071 s      49  183452     yes
 6     352 B  1592051 s      77  229197     yes
 7     440 B  1517732 s      66  157183     yes
 8     552 B  1460821 s      62  117697     yes
 9     696 B  1521917 s     143  215308     yes
10     872 B  1695035 s     205  246162     yes
11     1.1 kB 1681650 s     233  221968     yes
12     1.3 kB 1603363 s     241  183621     yes
13     1.7 kB 1634218 s      94   57197     yes
14     2.1 kB 1695038 s      75   36488     yes
15     2.6 kB 1747075 s      65   25203     yes
16     3.3 kB 1760661 s      78   24167     yes

The meaning of each column is :

Column meaning
#slab class Number
Item_SizeChunk size
Max_ageLRU The lifetime of the oldest record in
1MB_pages Assigned to Slab Pages of
CountSlab Number of records in
Full?Slab Whether there is free chunk

The information obtained from this script is very convenient for tuning , Highly recommended .

Summary of memory storage

This is a brief description of memcached Cache mechanism and tuning method . I hope the reader can understand memcached Memory management principle and its advantages and disadvantages .

Next time, we will continue to explain LRU and Expire Equal principle , as well as memcached The latest development direction of —— Extensible architecture (pluggable architecher)).

Series navigation :

memcached A complete analysis of –1. memcached The basis of

memcached Comprehensive analysis –2. understand memcached Memory storage

memcached Comprehensive analysis –3. memcached The deletion mechanism and development direction of

memcached Comprehensive analysis –4. memcached Distributed algorithm

memcached Comprehensive analysis –5. memcached Applications and compatible programs

原网站

版权声明
本文为[Hello,C++!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241432585997.html