memcached Source Analysis-----Installation, commissioning and how to read memcached source code

Source: Internet
Author: User


Reprint Please specify source: http://blog.csdn.net/luotuo44/article/details/42639131


Installation:

Install libevent before installing memcached. Now assume that Libevent is installed in the/usr/local/libevent directory.

Because Memcached does not have a bunch of header files and library files after installation, like Libevent. The post-installation memcached is not intended to be used for programming and running directly. Therefore, there is no need to create a directory specifically for memcached under the/usr/local directory. Install the memcached directly to the/usr/local.

Unzip the memcached-1.4.21.tar.gz in Linux and enter the extracted directory. Enter the command below to configure the installation.

$./configure--with-libevent=/usr/local/libevent

$make

$sudo make Install


Memcached's default installation directory is/usr/local, if you want to install in their own directory, then you can configure with--prefix=xxx to specify a directory. --with-libevent is used to indicate the installation directory of the Libevent.

If the installation directory is/usr/local then you can now memcached this executable program, enter memcached directly on the command line, and at this point you can use the command $man memcached Query the Help manual.

If it is installed in a different directory, such as the/usr/local/memcached directory, then you need some configuration. To first configure the executable file's lookup directory, you need to modify the PATH environment variable. At the end of the/ETC/BASH.BASHRC file, add:

Exportpath= $PATH:/usr/local/memcached/bin

At this point, you can run the memcached command in the shell. But you can't use man to query memcached's help manual. Because the command's help manual is not on the man's query directory. In the/usr/local/memcached/share/man/man1 directory, there is a Memcached.1 file. Copy this file to the/usr/local/share/man/man1 directory. At this point, you can man this memcached.



Debugging:

If memcached is already installed, uninstall it using the $sudo make uninstall command first. The Configure configuration is the same as before and does not need to be changed. Then enter the following command to generate the debug version of memcached.

$make cflags= "-g-o0"

$sudo make Install

That's all. Then directly inside the shell input $gdb memcached command to start memcached. The memcached parameter is entered after GDB is started, using the Run command. As shown in the following:


Remember to set a breakpoint before using the R or Run command.



Start command:

The simple Start command is as follows, setting the-l and-p respectively to set the IP and listening ports. -VV is the output of some running information.

$memcached-L 192.168.1.112-p 8888-vv



The relationship with Libevent:

Memcached relies on libevent. From the compilation of memcached can be known. Read memcached code needs to understand the basic use of libevent, about libevent use examples can refer to the libevent use examples, from simple to complex. Of course read memcached source code does not need to read libevent source as the basis, understand the basic use of libevent can. If the reader wants to read the source code of Libevent, refer to the Libevent Source Analysis series article.


and Libevent source reading difficulty comparison:

Although memcached has much less code than libevent, it is much harder to read than Libevent. The main reasons are: Memcached uses a large number of global variables, the correlation between the modules is very strong, using a lot of locks and many threads. And the libevent of each module is relatively good, the module is very small association, basically the individual modules to complete their own independent functions, and then to the outside world to provide the use of interfaces. So read Memcached's code more patiently. In addition, reading memcached source code in addition to the need to use libevent also need to understand the multi-threading, sockets, pipelines and other things basic use. Of course, if you've seen advanced programming for the UNIX environment, then there's no problem.



How to read memcached source code:

1. Find an article describing memcached in general. Learn what technologies memcached uses and the modules inside the memcached (such as a hash table inside, a slab memory allocator, an LRU queue, semi-synchronous semi-async, and so on). A Japanese-written "memcached Complete anatomy" can look at, there is no code involved in the simple introduction of memcached.

2. Sub-module reading code, you can read slabs.c files, assoc.c files, items.c files separately. After reading the basic functions of a module, read another module. remember, don't try to get the whole module fully understood at once. memcached modules are too large to be understood at once. Always look back at the modules you've seen before, especially when you see something associated (such as a global variable).

3. Although it is easier to read the modules, it is still a bit difficult for memcached. Because the correlation between the memcached modules is relatively large, unlike libevent. Associations are generally through some global variables. But memcached uses a lot of lots and lots of global variables, so read the code to see if there is a static modifier on the global variable (if something is Anxi). Also, when reading a module, assume that these global variables take the default values and do not change, so it is easier to read them. A significant part of the global variable is the member variable of the struct variable settings. Many of the members of the global variable settings can be set by the parameter when the memcached is started, but it is assumed to be not set when reading. All members take the default values. The Settings_init function is used to assign default values to the individual members of the global variable settings. Each time you read the code, it is recommended to open this function to see the default values for each member at any time.

4.memcached uses a large number of locks and multiple threads (non-worker threads), ignoring these locks and threads while reading. See that the lock is skipped, assuming no locks are thread safe. For those non-worker threads, it is assumed that there is no such thread. Ha ha!

5. Some functions are to be opened only, such as the LRU crawler function. When reading the code it is assumed that this function is not turned on. This function is generally determined by the value of the global variable to open or not open. Then when you see this global variable, it is directly taken as a non-open value.

As for the reading order of the code, you can refer to the sequence of posts in this series. In simple terms, from simple to complex, from a single structure to the general.




Reference:

http://blog.csdn.net/unix21/article/details/15501049




memcached Source Analysis-----Installation, commissioning and how to read memcached source code

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.