查看、分析memcached使用状态 (Linux/Unix)
发布于 2009-02-12 22:44 阅读:27,266 评论:0 标签: memcached stats 缓存

    访问量上升,数据库压力大,怎么办?好办法是在中间挡一层缓存!这个缓存要求高效,不能比数据库慢,否则服务质量受影响;如果能把数据用hash打散存储到硬盘,也是可以的,不过在内存越来越便宜的今天,还是使用内存吧!

    mysql也有自己的缓存,也是存储在内存的,但是有一个说法是:

以下是引用片段:

只能有一个实例
意味着你能存储内容的上限就是你服务器的可用内存,一台服务器能有多少内存?你又能存多少呢?

只要有写操作,mysql的query cache就失效
只要数据库内容稍有改变,那怕改变的是其他行,mysql的query cache也会失效

    再说,如果mysql都抗不住了,怎么还能指望它提供的缓存呢?

    所以我可以使用memcached了!他的好处和如何用可以参考:

以下是引用片段:

1:《Memcache和mysql交互流程操作原理

2:《让memcached和mysql更好的工作

    开发时面对需求是个麻烦事,更漫长而闹心的是维护,所以我更关心的是memcached运行中的情况。还好的是,memcached的作者给我们提供查看运行情况的命令。主要是“stats”,使用方法为 “telnet ip 端口号”,登录后使用“stats”命令。

    然后你可以看见很多内容,具体可以参考:《memcacche stats

以下是引用片段:

pid = process id
uptime = number of seconds since the process was started
time = current time
version = memcached version
rusage_user = seconds the cpu has devoted to the process as the user
rusage_system = seconds the cpu has devoted to the process as the system
curr_items = total number of items currently in memcache
total_items = total number of items that have passed through the cache
bytes = total number of bytes currently in use by curr_items
curr_connections = total number of open connections to memcached
connection_structures = ???
cmd_get = total GET commands issued to the server
cmd_set = total SET commands issued to the server
get_hits = total number of times a GET command was able to retrieve and
return data
get_misses = total number of times a GET command was unable to retrieve and
return data
bytes_read = total number of bytes input into the server
bytes_written = total number of bytes written by the server
limit_maxbytes = total storage bytes available to the server.

    着重说一下几个对观测很有用的项。

    limit_maxbytes、bytes

    memcached在存储的时候是可以设置失效时间的,但如果存储已经满了,那旧数据即使没有到过期时间,也会被移除。所以需要观察memcached存储是否已经满了,同时这对扩容也是有意义的参考。limit_maxbytes即总的存储大小,而bytes就是已经使用的大小,从这两个数据就可以看出在memcached启动时,我们为它分配的内存是否足够使用。

    cmd_get、cmd_set

    memcached启动后,我们对它一共做了多少次读取操作呢?从这两个参数可以观察出来。

    get_hits、get_misses

    使用memcached后,我们需要评估我们使用的策略是否合理。不能够使用中间缓存后,后端的数据库还是有较大的访问量,这样的话中间缓存就变得没有意义了。get_hits表示命中了多少次读取,即来memcached取到了多少有效数据;get_misses表示没有命中的次数,即此次来取数据的时候,memcached并没有你所查询的数据。如果没有清零统计数据的话,cmd_get = get_hits + get_misses。

    memcached 的状态查询还有其它的命令,可以参考:《Memcached的stats命令

    如下:

stats reset
清空统计数据

stats malloc
显示内存分配数据

stats maps
这个不太确定,看源代码是把/proc/self/maps的数据显示出来。

stats cachedump slab_id limit_num
显示某个slab中的前limit_num个key列表,显示格式如下
ITEM key_name [ value_length b; expire_time|access_time s]
其中,memcached 1.2.2及以前版本显示的是  访问时间(timestamp)
1.2.4以上版本,包括1.2.4显示 过期时间(timestamp)
如果是永不过期的key,expire_time会显示为服务器启动的时间

stats cachedump 7 2
ITEM copy_test1 [250 b; 1207795754 s]
ITEM copy_test [248 b; 1207793649 s]

stats slabs
显示各个slab的信息,包括chunk的大小、数目、使用情况等

stats items
显示各个slab中item的数目和最老item的年龄(最后一次访问距离现在的秒数)

stats detail [on|off|dump]
设置或者显示详细操作记录

参数为on,打开详细操作记录
参数为off,关闭详细操作记录
参数为dump,显示详细操作记录(每一个键值get、set、hit、del的次数)

stats detail dump
PREFIX copy_test2 get 1 hit 1 set 0 del 0
PREFIX copy_test1 get 1 hit 1 set 0 del 0
PREFIX cpy get 1 hit 0 set 0 del 0

展开全文  
收起全文  
libeven、memcached、libmemcache安装 (Linux/Unix)
发布于 2007-12-21 19:31 1 阅读:96,808 评论:1 标签: libeven libmemcache Linux memcached 安装

    前端时间玩了一下memcached,由于工作关系得过一段时间在玩它。哎,工作就是这样,老变!

    libeven

    memcached的使用需要libeven的支持,我们得先装上libeven。
    官方网站:http://www.monkey.org/~provos/libevent/
    下载地址:http://www.monkey.org/~provos/libevent-1.4.0-beta.tar.gz
    libevent安装方式比较简单:

以下是代码片段:
./configure && make
make install
    这样就好

    检查如下:

以下是引用片段:
[root@localhost memcached-1.2.2]# ls -l /usr/local/lib/ | grep even
lrwxrwxrwx   1 root root      21 Nov 26 11:32 libevent-1.4.so.2 -> libevent-1.4.so.2.0.0
-rwxr-xr-x   1 root root  267334 Nov 26 11:32 libevent-1.4.so.2.0.0
-rw-r--r--   1 root root  351694 Nov 26 11:32 libevent.a
lrwxrwxrwx   1 root root      26 Nov 26 11:32 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.0.0
-rwxr-xr-x   1 root root  101485 Nov 26 11:32 libevent_core-1.4.so.2.0.0
-rw-r--r--   1 root root  139080 Nov 26 11:32 libevent_core.a
-rwxr-xr-x   1 root root     866 Nov 26 11:32 libevent_core.la
lrwxrwxrwx   1 root root      26 Nov 26 11:32 libevent_core.so -> libevent_core-1.4.so.2.0.0
lrwxrwxrwx   1 root root      27 Nov 26 11:32 libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.0.0
-rwxr-xr-x   1 root root  214596 Nov 26 11:32 libevent_extra-1.4.so.2.0.0
-rw-r--r--   1 root root  273270 Nov 26 11:32 libevent_extra.a
-rwxr-xr-x   1 root root     873 Nov 26 11:32 libevent_extra.la
lrwxrwxrwx   1 root root      27 Nov 26 11:32 libevent_extra.so -> libevent_extra-1.4.so.2.0.0
-rwxr-xr-x   1 root root     831 Nov 26 11:32 libevent.la
lrwxrwxrwx   1 root root      21 Nov 26 11:32 libevent.so -> libevent-1.4.so.2.0.0

    看见有很多libevent,就可以放心了。

    memcached

    官方网站:http://www.danga.com/memcached/download.bml
    下载地址:http://www.danga.com/memcached/dist/memcached-1.2.2.tar.gz

    memcached安装需要些参数:

以下是代码片段:
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local
make
make install

   我们来运行看看:
   运行:

以下是代码片段:
/usr/local/memcached/bin/memcached -d -m 10 -p 11211 -u root

    结果:
以下是引用片段:
[root@localhost memcached-1.2.2]# /usr/local/memcached/bin/memcached -d -m 10 -p 11211 -u root
/usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory

    查找原因,解决方案如下:

以下是代码片段:
LD_DEBUG=libs /usr/local/memcached/bin/memcached -v

    有如下显示:

以下是引用片段:
[root@localhost memcached-1.2.2]# LD_DEBUG=libs /usr/local/memcached/bin/memcached -v
     18999:     find library=libevent-1.4.so.2 [0]; searching
     18999:      search cache=/etc/ld.so.cache
     18999:      search path=/lib/tls/i686/sse2:/lib/tls/i686:/lib/tls/sse2:/lib/tls:/lib/i686/sse2:/lib/i686:/lib/sse2:/lib:/usr/lib/tls/i686/sse2:/usr/lib/tls/i686:/usr/lib/tls/sse2:/usr/lib/tls:/usr/lib/i686/sse2:/usr/lib/i686:/usr/lib/sse2:/usr/lib          (system search path)
     18999:       trying file=/lib/tls/i686/sse2/libevent-1.4.so.2
     18999:       trying file=/lib/tls/i686/libevent-1.4.so.2
     18999:       trying file=/lib/tls/sse2/libevent-1.4.so.2
     18999:       trying file=/lib/tls/libevent-1.4.so.2
     18999:       trying file=/lib/i686/sse2/libevent-1.4.so.2
     18999:       trying file=/lib/i686/libevent-1.4.so.2
     18999:       trying file=/lib/sse2/libevent-1.4.so.2
     18999:       trying file=/lib/libevent-1.4.so.2
     18999:       trying file=/usr/lib/tls/i686/sse2/libevent-1.4.so.2
     18999:       trying file=/usr/lib/tls/i686/libevent-1.4.so.2
     18999:       trying file=/usr/lib/tls/sse2/libevent-1.4.so.2
     18999:       trying file=/usr/lib/tls/libevent-1.4.so.2
     18999:       trying file=/usr/lib/i686/sse2/libevent-1.4.so.2
     18999:       trying file=/usr/lib/i686/libevent-1.4.so.2
     18999:       trying file=/usr/lib/sse2/libevent-1.4.so.2
     18999:       trying file=/usr/lib/libevent-1.4.so.2
     18999:
/usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory

    主要是看最后一行提示的错误,那么我们:

以下是代码片段:
ln -s /usr/local/lib/libevent-1.4.so.2 /usr/lib/libevent-1.4.so.2

    OK!再次运行:

以下是代码片段:
/usr/local/memcached/bin/memcached -d -m 10 -p 11211 -u root
    成功!

    貌似用“ldconfig”也可以解决这个问题

     libmemcache   

    libmemcache是memcached C的客户端之一。我准备使用这个玩意。
    官网:http://people.freebsd.org/~seanc/libmemcache/
    下载:http://people.freebsd.org/~seanc/libmemcache/libmemcache-1.4.0.rc2.tar.bz2
    编译:

以下是代码片段:
./configure
make
make install

     写一个有libmemcache函数的程序
     然后    

以下是代码片段:
gcc a.c -o a.cgi -L/usr/local/include -lmemcache


    运行

以下是代码片段:
./a.cgi

    提示类似上面的“error while loading shared libraries”的错误,查了一下可用"ldconfig"解决这个问题,那上面的可否用这个命令解决呢?

    上面的gcc编译的时候,带有参数“-lmemcache”。
    -l参数后面跟的库名有规则的,库的命名方式有libxxxx.so或libxxxx.a,编译时要用-lxxxx就可以了。
    而“-l”的意思就是代表是个“lib”了。

    所以在你自己编写动态库或者静态库时,命名还是得按照 libxxxx.so的形式来。

    不得不认为libmemcache有点糟糕,编译它自带的一个程序编译竟然有问题,很无语!

以下是引用片段:
[root@login benchmark]# gcc benchmark.c -L/usr/local/include -lmemcache
benchmark.c: In function `main':
benchmark.c:100: warning: passing arg 2 of `mc_set' discards qualifiers from pointer target type
benchmark.c:108: warning: passing arg 2 of `mc_set' discards qualifiers from pointer target type
benchmark.c:121: warning: passing arg 2 of `mc_req_add' discards qualifiers from pointer target type
benchmark.c:145: warning: passing arg 2 of `mc_delete' discards qualifiers from pointer target type
benchmark.c:153: warning: passing arg 2 of `mc_add' discards qualifiers from pointer target type
benchmark.c:167: warning: passing arg 2 of `mc_delete' discards qualifiers from pointer target type

    解决方法:

    找到passing arg 2这个参数的定义:

以下是引用片段:
const char *key;

    把const去了:
以下是引用片段:
char *key;

    这样就好了!

    然后:

以下是代码片段:
[root@login memcache-test]# ./a.out

    执行时可能报以下的错误:

以下是引用片段:
./a.out: error while loading shared libraries: libmemcache.so.0: cannot open shared object file: No such file or directory

    解决方法:

    执行ldconfig命令即可!


    附:

    http://kapoc.blogdriver.com/kapoc/1200549.html
    ldconfig命令 - - 动态链接库管理命令

    为了让动态链接库为系统所共享,还需运行动态链接库的管理命令--ldconfig.此执行程序存放在/sbin目录下.

    ldconfig命令的用途,主要是在默认搜寻目录(/lib和/usr/lib)以及动态库配置文件/etc/ld.so.conf内所列的目录下,搜索出可共享的动态链接库(格式如前介绍,lib*.so*),进而创建出动态装入程序(ld.so)所需的连接和缓存文件.缓存文件默认为/etc/ld.so.cache,此文件保存已排好序的动态链接库名字列表.

    ldconfig通常在系统启动时运行,而当用户安装了一个新的动态链接库时,就需要手工运行这个命令.

展开全文  
收起全文  
memcached英文文档学习 (C/C++学习)
发布于 2007-12-04 17:58 1 阅读:66,135 评论:1 标签: Linux memcached 学习 英文

      今天看了memcached wiki的FAQ,觉得有几句话对了解memcached很有作用,与大家共享一下。

      服务器的内存如何分配问题:

以下是引用片段:
Because a 32-bit process can only address 4GB of virtual memory (usually significantly less, depending on your operating system), if you have a 32-bit server with 4-64GB of memory using PAE you can just run multiple processes on the machine, each using 2 or 3GB of memory.

     memcached之get过程,看点在于client/server的hash key处理:

以下是引用片段:
When doing a memcached lookup, first the client hashes the key against the whole list of servers. Once it has chosen a server, the client then sends its request, and the server does an internal hash key lookup for the actual item data.

      client/server的hash key算法差异:

以下是引用片段:
Different client implementations store data into memcached differently (perl Storable, php serialize, java hibernate, JSON, etc). Some clients also implement the hashing algorithm differently. The server is always the same however.

      当一个memcached down后的情况:

以下是引用片段:
 Remove dead nodes from your list. Be very careful! With default clients adding or removing servers will invalidate all of your cache! Since the list of servers to hash against has changed, most of your keys will likely hash to different servers. It's like restarting all of your nodes at the same time.

      key/value大小的说明:

以下是引用片段:
 Keys are restricted to 250 characters. Stored data cannot exceed 1 megabyte in size, since that is the largest typical slab size.

      数据存储的处理方式:

以下是引用片段:
 At first memcached did just use malloc/free for everything. However this does not play very well with OS memory managers. You get fragmentation, and your OS ends up spending more time trying to find contiguous blocks of memory to feed malloc() than it does running the memcached process.

      如何处理数据过期的问题:

以下是引用片段:
 When do expired cached items get deleted from the cache?

 memcached uses a lazy expiration, which means it uses no extra cpu
 expiring items. When an item is requested (a get request) it checks
 the expiration time to see if the item is still valid before returning
 it to the client.

 Similarly when adding a new item to the cache, if the cache is full,
 it will look at for expired items to replace before replacing the
 least used items in the cache.

      尤其是对数据过期的处理让偶意外,虽然省CPU,但很浪费空间。

       下面是几个常用的链接:

以下是引用片段:
 memcached'home
http://danga.com/memcached/

memcached'wiki
http://www.socialtext.net/memcached/index.cgi

memcached'faq
http://www.socialtext.net/memcached/index.cgi?faq

memcached'Protocol
http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt

memcached'consistent hashing
http://www.last.fm/user/RJ/journal/2007/04/10/392555/

展开全文  
收起全文