The following notes focus on the memcached Startup Process in TCP mode.
Main ()
Set the signal processing function to sig_handler ()
Initialize system settings and save the settings in the global variables.
Settings_init ();
Parse the startup parameters and use the settings variable to save the configuration.
Initializes the hash function used by the system based on the startup parameters. The default value is jenkins_hash ().
Hash_init ();
Set the network listening mode based on the value of tcp_specified. The default mode is TCP.
Then, set the number of available CPU cores and the maximum number of connections.
Restrict the start permission. Generally, the root permission is required to start the application.
Set whether to start as a daemon
Call the libevent library to obtain an event listening instance. The basic usage of libevent is event_init () => evtimer_set () => event_add () => event_dispatch ()
Event_init ();
Initialize system global status information Variables
Stats_init ();
Initialize the hash table. The parameter is the level of the buckets hash table, that is, buckets = (unsigned long INT) 1 <(n ))
Assoc_init (INT hashpower_init );
Sets and initializes the link array to save the FD of each link. By default, the maximum FD is not greater than max_conn + 10.
Conn_init ();
Memory Management initialization. The parameter is the size of the total applied memory. The default value is 64 mb. The block size variation factor determines whether to pre-divide the memory.
The use of factor is mainly used when chunck is divided in each memory page. the maximum size of each page is 1 MB. For example, if each chunck of slab_class1 is 80 bytes, the size of each chunck of slab_class2 is 80 * factor.
The default value of factor in 1.4.20 is 1.25, and the default value of chunck is 48 bytes.
The default value of preallocate is false. If it is true, the system applies for an instance for each type of slab_class by default, but some instances may not be used at all times. Therefore, the default value is false at startup.
Slabs_init (maxbytes, factor, preallocate );
Start and initialize the worker thread
Num_threads indicates the number of worker threads. The main thread is responsible for accepting and creating links, while the worker thread is responsible for I/O operations on linked commands.
Initializes various multi-thread synchronization locks and creates a pipe channel for the master thread to communicate with the worker thread. It is mainly used to allocate new links. During the allocation process, Server Load balancer uses the round-robin round robin scheduling algorithm.
Use the pthread library to create a thread and use the synchronization lock to ensure that all worker threads are started to end the function.
Thread_init (settings. num_threads, main_base );
Start the maintenance thread of the hash table, mainly responsible for resizing the hash table
Start_assoc_maintenance_thread ();
Determine whether to start the management thread of the Memory Module Based on the startup parameters. The main reason is that the value of data storage is extreme. For example, a chunck is not accessed for a long time after it is allocated, other frequently accessed applications cannot be applied for storage. Based on the LRU algorithm, you can re-divide the memory blocks with the least recent access. slab_maintenance_thread is mainly used for this purpose.
If (settings. slab_reassign &&
Start_slab_maintenance_thread () =-1)
Init_lru_crawler ();
Start the timer and use a global variable to maintain a record of the current time. In this way, multiple threads access the global variable to obtain the current time, reducing the acquisition time. system calls
Clock_handler ();
Start socket listening
Server_sockets ();
Enable event loop listening
Eventevent_base_loop ();