Trond Norbye's Weblog

« Previous day (Nov 30, 2008) | Main | Next day (Dec 1, 2008) »

http://blogs.sun.com/trond/date/20081201 Monday December 01, 2008

Displaying the 10 most popular keys in your memcached instance?

If you run your memcached-server on Solaris you can easily display the 10 most popular keys in you memcached server by using dtrace. The following little script will display the 10 most popular keys every 10th second.

#! /usr/bin/ksh 
pid=`pgrep -x memcached` 
if [ -z "${pid}" ] 
then 
   echo memcached not running 
   exit 1 
fi 

file /proc/${pid}/path/a.out | grep "ELF 64" > /dev/zero 2>&1 
if [ $? -ne 0 ] 
then 
  mode="-32" 
else 
  mode="-64" 
fi 

/usr/sbin/dtrace ${mode} -n ' 
#pragma D option quiet 

memcached'${pid}':::assoc-find 
{ 
    @assK[copyinstr(arg0)] = count(); 
} 

tick-10s 
{ 
    printf("Top 10 keys\n"); 
    trunc(@assK, 10); 
    printa(@assK); 
    trunc(@assK, 0); 
} 

END { 
    trunc(@assK, 10); 
    printa(@assK); 
    trunc(@assK, 0); 
} 
'

You may find the probe name assoc-find strange, but that is a function that is called for all commands when the memcached server tries to locate an item in the cache. By using that probe, we can count all get/add/set/replace/delete with one single probe :-)


Valid HTML! Valid CSS!

This is a personal weblog, I do not speak for my employer.