タイトル

Need for Answer

2016年4月6日水曜日

NGINX Luaのshared dictionaryの使用容量を調べたいのまき

nginx lua使ってますか!
worker間でメモリ共有するのに、shared dictionary使いますよね!

こんな感じで定義します
http {
        lua_shared_dict HOGE 30m;
}

…なのですが、これって実際どれくらいメモリ確保すればいいのかわかんないですよね。
ということで、こちらを調べようという話です。agentzh氏が作ってくださったngx-shmを使います。

まずはnginxのworkerのプロセスIDを調べます。masterじゃないですよ、workerですよ!

#ps aux | grep nginx
root     46581  0.0  0.0 103304   856 pts/0    S+   12:50   0:00 grep nginx
root     56272  0.0  0.1 118384 19492 ?        Ss   Mar30   0:00 nginx: master process
nobody   56273  2.2  0.7 215004 125824 ?       S    Mar30 180:48 nginx: worker process
nobody   56274  2.2  0.6 191208 102056 ?       S    Mar30 182:14 nginx: worker process
nobody   56276  2.2  0.6 195812 106524 ?       S    Mar30 181:37 nginx: worker process
この場合56273ということがわかりましたので、このPIDをngx-shmに渡します。こんな感じです
#./ngx-shm -p 56273
Tracing 56273 (/usr/local/openresty/nginx/sbin/nginx)...

shm zone "HOGE"
    owner: ngx_http_lua_shdict
    total size: 3072 KB

Use the -n <zone> option to see more details about each zone. 30 microseconds elapsed in the probe.
これだと確保容量だけしかわからないので、メモリ使用量の詳細を調べます
#./ngx-shm -p 56273 -n HOGE
Tracing 56273 (/usr/local/openresty/nginx/sbin/nginx)...

shm zone "HOGE"
    owner: ngx_http_lua_shdict
    total size: 30720 KB
    free pages: 27912 KB (6978 pages, 176 blocks)

32 microseconds elapsed in the probe handler.
これが調べたかった結果です!メモリ確保しすぎですね!