@kyanny's blog

My thoughts, my life. Views/opinions are my own.

Memcached のチューニングを考える (2)

ちょうどコンビニで買った湾岸ミッドナイト41巻に「チューニングとは楽器の調律が本来の意味だ」とか書いてあって、厳密に言うと今やってることはチューニングと呼べないんじゃないだろうかとか思ったけど他にいいタイトルも思いつかない。「Memcached のチューニング」といったら Memcached 本体のコードに手を入れることだよなあ。でもじゃあ今からやろうとしてるのは何だろうな。最適化?も違うかなぁ。

仕様書最強

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

プロトコル仕様書の存在は前から知っていたけど、「プロトコルとか何か難しそうだし、そんな深いことを知りたいわけじゃないよ」と思って敬遠していた。けど昨日、これ以上検索しても(日本語では)新しい情報は見つけられなさそうだ、本家のメーリングリストのアーカイブを読めばもっといろいろわかりそうだけど、どうせ英語なら公式に配布されてるドキュメントを先に読む方がいいか、メーリングリストに比べたら断然短いし、と覚悟を決めて印刷して電車の中で読んだら一時間かからずにほぼ読めた。良くわかんなかったところもいくつかあるけどわかったことも多かった。

で、 telnet で接続して stats コマンドを発行するといろいろでてくるのだが、 stats items とか stats sizes とか stats slabs とか引数を指定すると詳細な情報がでてくる、というのはしらなかった。そして、

Item size statistics
--------------------
CAVEAT: This section describes statistics which are subject to change in the
future.

The "stats" command with the argument of "sizes" returns information about the
general size and count of all items stored in the cache.
WARNING: This command WILL lock up your cache! It iterates over *every item*
and examines the size. While the operation is fast, if you have many items 
you could prevent memcached from serving requests for several seconds.

The data is returned in the following format:

<size> <count>\r\n

The server terminates this list with the line

END\r\n

'size' is an approximate size of the item, within 32 bytes.
'count' is the amount of items that exist within that 32-byte range.

This is essentially a display of all of your items if there was a slab class
for every 32 bytes. You can use this to determine if adjusting the slab growth
factor would save memory overhead. For example: generating more classes in the 
lower range could allow items to fit more snugly into their slab classes, if
most of your items are less than 200 bytes in size.

ってちゃんと書いてあった。 memcached-tool の出力を加工して利用されてるであろうメモリ量を計算して・・・なんて遠回りなことをする意味も必要もなかった。 stats sizes を発行して size * count を合計したものが実際にキャッシュオブジェクトが保存されてるメモリ総量・・・と考えていいのかな?

stats sizes は memcached をロックするので注意、みたいなことが書いてある。確かに一瞬(一秒はかからないくらいの短い時間)、止まる感じがする。