Memcached source code analysis ----- memcached startup parameters and default values of key configurations

Source: Internet
Author: User

Memcached source code analysis ----- memcached startup parameters and default values of key configurations


Reprinted please indicate the source: http://blog.csdn.net/luotuo44/article/details/42672913


This article starts code analysis for this series of blog posts. In this series of blog posts, the memcached version is 1.4.21.

This article provides detailed explanations of each parameter during memcached startup and the default values of some key configurations. This allows you to view the source code of memcached at any time. Of course, you can also view the meaning of each parameter at any time when using memcached. "How to read memcached source code" says memcached has many global variables (that is, key configurations). These global variables will cause some troubles for source code analysis. A good solution is to assume that these global variables (Key configurations) Take the default value, and their values will not change.


Default values of key configurations:


Many key configuration variables are member variables of the global struct variable settings. The settings_init function assigns the key configuration variables to the default values. The variables are listed and explained below.

Static void settings_init (void) {// enable the CAS service. If the CAS service is enabled, an additional field for CAS is added to the item. You can use the-C option to disable settings when starting memcached. use_cas = true; settings. access = 0700; // unix socket permission bit information settings. port = 11211; // The tcp port settings of the memcached listener. udpport = 11211; // udp port for memcached listening // ip address bound to memcached. If the value is NULL, It is INADDR_ANY. Otherwise, the value points to an ip string settings. inter = NULL; settings. maxbytes = 64*1024*1024; // maximum memory settings available for memcached. maxconns = 1024; // The maximum number of clients allowed to be online simultaneously. Different from settings. backlog settings. verbose = 0; // output level of the running information. the larger the value, the more detailed the output information is. oldest_live = 0; // time limit of the flush_all command. Delete an item whose insertion time is earlier than this time. Settings. evict_to_free = 1; // mark whether memcached allows LRU elimination. Yes by default. You can use the-M option to disable settings. socketpath = NULL; // socket path of the unix socket listener. unix socket settings are not used by default. factor = 1.25; // item's expansion factor settings. chunk_size = 48; // The number of bytes of data that can be stored by the smallest item (data in the set and add commands) settings. num_threads = 4; // number of worker threads is a udp socket service number of worker threads serving each udp socket settings. num_threads_per_udp = 0; settings. prefix_delimiter = ':'; // delimiter settings. detail_enabled = 0; // Yes Whether to automatically collect status information // The maximum number of commands that a worker thread continuously executes for a client. This is mainly to prevent a client from occupying the entire worker thread //, And the commands of other clients of the worker thread cannot be processed. reqs_per_event = 20; settings. backlog = 1024; // The second parameter of the listen function, unlike settings. maxconns // USER command protocol, which can be file or binary. Negotiating_prot is a negotiation that automatically determines settings. binding_protocol = negotiating_prot Based on the command content; settings. item_size_max = 1024*1024; // size of the slab Memory Page. The Unit is byte settings. maxconns_fast = false; // if the number of connections exceeds the maximum number of concurrent connections (specified by the-c option), whether to immediately close the Client Connected to the new connection. // Indicates whether the memcached crawler thread LRU is enabled. The default value is false. The LRU crawler thread is not started. // When starting memcached, you can use-o lru_crawler to assign the value of the variable to true and start the LRU crawler thread settings. lru_crawler = false; settings. lru_crawler_sleep = 100; // The sleep interval when the LRU crawler thread is working. The Unit is microsecond settings. lru_crawler_tocrawl = 0; // The LRU crawler checks the number of items in each LRU queue. If you want the LRU crawler to work, you must modify this value. // The hash table length is 2 ^ n. This value is the initial value of n. You can set it through-o hashpower_init when starting memcached. The value must be in the range of [12, 64. If this parameter is not set, the value is 0. The hash table's power will take the default value 16 settings. hashpower_init = 0;/* Starting hash power level */settings. slab_reassign = false; // whether to enable memory adjustment for different types of items. You can enable settings through the-o slab_reassign option. slab_automove = 0; // automatically checks whether memory adjustment for different types of items is required, depending on settings. slab_reassign enabled settings. shutdown_command = false; // whether the client-side shutdown command is supported. This command will shut down the memcached process // The number of references used to fix the item. If a worker thread references an item, the thread will be suspended before it can be removed. // The item will always be referenced by the dead thread and cannot be released. Memcached uses this value to check whether this is the case. Because this rarely happens, the default value of this variable is 0 (that is, no detection ). // When starting memcached, set it through-o tail_repair_time xxx. The value must be greater than 10 (in seconds) // TAIL_REPAIR_TIME_DEFAULT is equal to 0. Settings. tail_repair_time = TAIL_REPAIR_TIME_DEFAULT; settings. flush_enabled = true; // whether to run the flush_all command on the client}

Of course, this function does not assign values to all member variables in settings. The remaining member variables are not that important.



Command line parameters:

Memcached uses getopt and getsubopt to parse command line parameters. The following command line options are related to the two parsing functions: if the option is followed by a colon, it indicates that this option must have a parameter; if there is no colon, there is no parameter; memcached does not use the double colon option. For details about getopt and getsubopt, refer to getopt and getsubopt command line parsing functions.


  • ""Whether to run the shutdown command on the client. It is not allowed by default. This option is allowed. The shutdown command of the client will kill the memcached process. This option will assign settings. shutdown_command to false.
  • ":"Information of the unix socket permission bit (access mask ). Parameters of this option are assigned to settings. access.
  • "U :"Uppercase U. The UDP port value of the memcached listener. The default port is 11211. The parameter value of this option is settings. udpport.
  • "P :"Tcp port listened by memcached in lower case. The default port is 11211. The parameter value of this option is settings. port.
  • "S :"Lowercase S. The socket path of the unix socket listener. The parameter value of this option is settings. socketpath.
  • "M :"Lowercase m. Maximum memory value available for memcached. The default value is 64 MB. The unit of the parameter is MB. This parameter is assigned to settings. maxbytes.
  • "M"Uppercase M. By default, when memcached memory is used up, the LRU mechanism will be used out of items to free up space. If this option is used, the LRU function is disabled. Of course, disabling LRU does not mean that new data cannot be stored. If memcached contains expired items, new data can be stored. Otherwise, it cannot be stored. This option assigns settings. evict_to_free to 0.
  • "C :"Lowercase c. The maximum number of clients allowed to be online simultaneously (this value is not equivalent to the second parameter of the listen function). This option is different from the following option B. The default value is 1024. This option parameter is assigned to settings. maxconns.
  • "H"Show Help Information
  • "I"Display the copyright information of memcached and libevent
  • "K"Lowercase k. Lock the memory used by memcached in the memory. The OS is not allowed to move the memory of memcached to the virtual memory. When the OS moves the memcached memory to the virtual memory, page errors may occur, reducing the memcached response time.
  • "V"Lowercase v. Output some information about the memcached runtime. The output information of-v-vv-vvv is added in sequence. This option will increase the value of settings. verbose
  • "L :"Lowercase L. The IP address bound to memcached. If this option is not set, memcached uses INADDR_ANY. If you want to specify multiple ip addresses, the parameters of this option can be composed of multiple ip addresses separated by commas. You can also use this option multiple times. In this case, the port should follow the ip address instead of using the-p option separately. For example,-l 127.0.0.1: 8888,192.168 .1.112: 9999 or-l 127.0.0.1: 8888-l 192.168.1.112: 9999. This option parameter is assigned to settings. inter.
  • "D"Run memcached as a daemon
  • "R"Set the core file size to unrestricted
  • "R :"The maximum number of commands that a worker thread can run on a client consecutively. The parameter value of this option is settings. reqs_per_event.
  • "U :"Lowercase u. When starting memcached as a root user, you need to specify the user of memcached. This option is not required for other users to start memcached.
  • "P :"Uppercase p. This option specifies the memcached pid to save the file. It must be used with the-d option. Check whether the running user has the permission to write the corresponding file.
  • "F :"The expansion factor of item. The default value is 1.25. The parameter value of this option can be decimal but must be greater than 1.0. This option parameter is assigned to settings. factor.
  • "N :"Sets the maximum number of bytes of data that can be stored by the smallest item. This option parameter is assigned to settings. chunk_size.
  • "T :"The parameter of this option is used to specify the number of worker threads. It is not recommended to exceed 64. If this option is not set, there are four threads by default. This parameter is assigned to settings. num_threads.
  • "D :"The parameter character is used as the delimiter between the prefix and ID. This option is used to automatically collect status information. You can also enable memcached by running the stats detail on command on the client. The default Delimiter is colon ":". This option parameter is assigned with settings. prefix_delimiter and set settings. detail_enabled to 1.
  • "L"If the OS permits, apply for a larger memory page from the OS. The default Memory Page of the OS is 4 kb. Large Memory pages can effectively reduce the size of page tables and improve efficiency. This option will enable memcached to preemptively apply for all memory required by the OS. Of course, the memory should be allocated with a large memory page as much as possible.
  • "C :"Uppercase C. Memcached uses CAS by default. This option disables CAS. This option will assign settings. use_cas a value of false.
  • "B :"The second parameter of the listen function. Parameters of this option are assigned to settings. backlog. If this option is not set, the default value is 1024. This option is different from the previous c option.
  • "B :"Memcached supports both text and binary protocols. This option is used to specify the protocol used. By default, it is automatically determined (also called negotiation) based on the client command. The parameter can only be set to three string values: auto, binary, and ascii. Assign parameters to settings. binding_protocol.
  • "I :"Uppercase I. The size of each page in the slab distributor. The parameter value of this option indicates the page size. The default unit is B. It can also be K or M (case sensitive) after the value, indicating KB and MB. The page size is smaller than 1 kb or greater than MB. This option is not recommended. This option parameter is assigned to settings. item_size_max.
  • "S"Uppercase S. Open the sasl security protocol. The settings. sasl value is true.
  • "F"Disable the flush_all command on the client. The flush_all command of the client is allowed by default. Set settings. flush_enabled to false.
  • "O :"Lowercase o. You can set the following sub-options. This option is used for optimization.
  • Maxconns_fast:If the number of connections exceeds the maximum number of concurrent connections (specified by the-c option), immediately close the newly connected client. This option assigns settings. maxconns_fast to true.
  • Hashpower:The hash table length is 2 ^ n. You can set the initial value of index n by selecting hashpower. If this parameter is not set, the default value is 16. This option must have parameters. The value range can only be [12, 64]. The parameter value of this option is assigned to settings. hashpower_init.
  • Slab_reassign:This option has no parameters. Used to adjust the memory occupied by different types of items. Different Types indicate different sizes. A type of item is rarely used, but still occupies memory. You can enable the slab_reassign scheduling memory to reduce the memory of such items. If this option is used, the value of settings. slab_reassign is true.
  • Slab_automove:Depends on slab_reassign. It is used to actively detect whether memory scheduling is required. This option is optional. The value range of a parameter can only be 0, 1, or 2. Parameter 2 is not recommended. This option parameter is assigned to settings. slab_automove. If this option does not have a parameter, the settings. slab_automove value is 1.
  • Hash_algorithm:Used to specify a hash algorithm. This option must contain parameters. And the parameter can only be a string jenkins or murmur3
  • Tail_repair_time:Used to check whether any item is referenced by a dead thread. This is generally not the case, so this check is disabled by default. To enable this check, use this option. This option requires a parameter with a value not less than 10. This parameter is assigned to settings. tail_repair_time.
  • Lru_crawler:This option is used to start the LRU crawler thread. This option does not require parameters. This option causes the settings. lru_crawler to be assigned a value of true.
  • Lru_crawler_sleep:The sleep interval when the LRU crawler thread is working. This option requires a parameter as the sleep time in microseconds. The value range is [0, 1000000]. This parameter is assigned to settings. lru_crawler_sleep.
  • Lru_crawler_tocrawl:The LRU crawler checks the number of items in each LRU queue. This option has a parameter. The parameter is assigned to settings. lru_crawler_tocrawl.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.