developer's diary

最近はc#のエントリが多いです

C初心者がmemcached-1.4.5を追いかける。(10)

C初心者がmemcached-1.4.5を追いかける。(9)の続き
ソースはコチラにてDLしました。

getoptで受け取ったパラメータによって処理を行っている部分を追いかける。

memcached.c(4338行目あたり)

        case 'h':
            usage();
            exit(EXIT_SUCCESS);
          "hi"  /* help, licence info */

hは引数なしのオプション。

usage()を呼び出している。

memcached.c(4043行目あたり)

static void usage(void) {
    printf(PACKAGE " " VERSION "\n");
    printf("-p <num>      TCP port number to listen on (default: 11211)\n"
           "-U <num>      UDP port number to listen on (default: 11211, 0 is off)\n"
           "-s <file>     UNIX socket path to listen on (disables network support)\n"
           "-a <mask>     access mask for UNIX socket, in octal (default: 0700)\n"
           "-l <ip_addr>  interface to listen on (default: INADDR_ANY, all addresses)\n"
           "-d            run as a daemon\n"
           "-r            maximize core file limit\n"
           "-u <username> assume identity of <username> (only when run as root)\n"
           "-m <num>      max memory to use for items in megabytes (default: 64 MB)\n"
           "-M            return error on memory exhausted (rather than removing items)\n"
           "-c <num>      max simultaneous connections (default: 1024)\n"
           "-k            lock down all paged memory.  Note that there is a\n"
           "              limit on how much memory you may lock.  Trying to\n"
           "              allocate more than that would fail, so be sure you\n"
           "              set the limit correctly for the user you started\n"
           "              the daemon with (not for -u <username> user;\n"
           "              under sh this is done with 'ulimit -S -l NUM_KB').\n"
           "-v            verbose (print errors/warnings while in event loop)\n"
           "-vv           very verbose (also print client commands/reponses)\n"
           "-vvv          extremely verbose (also print internal state transitions)\n"
           "-h            print this help and exit\n"
           "-i            print memcached and libevent license\n"
           "-P <file>     save PID in <file>, only used with -d option\n"
           "-f <factor>   chunk size growth factor (default: 1.25)\n"
           "-n <bytes>    minimum space allocated for key+value+flags (default: 48)\n");
    printf("-L            Try to use large memory pages (if available). Increasing\n"
           "              the memory page size could reduce the number of TLB misses\n"
           "              and improve the performance. In order to get large pages\n"
           "              from the OS, memcached will allocate the total item-cache\n"
           "              in one large chunk.\n");
    printf("-D <char>     Use <char> as the delimiter between key prefixes and IDs.\n"
           "              This is used for per-prefix stats reporting. The default is\n"
           "              \":\" (colon). If this option is specified, stats collection\n"
           "              is turned on automatically; if not, then it may be turned on\n"
           "              by sending the \"stats detail on\" command to the server.\n");
    printf("-t <num>      number of threads to use (default: 4)\n");
    printf("-R            Maximum number of requests per event, limits the number of\n"
           "              requests process for a given connection to prevent \n"
           "              starvation (default: 20)\n");
    printf("-C            Disable use of CAS\n");
    printf("-b            Set the backlog queue limit (default: 1024)\n");
    printf("-B            Binding protocol - one of ascii, binary, or auto (default)\n");
    printf("-I            Override the size of each slab page. Adjusts max item size\n"
           "              (default: 1mb, min: 1k, max: 128m)\n");
#ifdef ENABLE_SASL
    printf("-S            Turn on Sasl authentication\n");
#endif
    return;
}

いわゆるHelpってやつすか。

関数の宣言方法を紐解く

  • static void usage(void)
    • static宣言された関数名は、そのファイルの中だけでのみ有効になる。
    • 静的関数やstatic関数と呼ばれている。
    • private関数ということでしょうか。
    • ちなみにmemcached.cはint mainです。(4253行目あたり)
int main (int argc, char **argv) {

PACKAGE とVERSION

どこで設定しているのかわかりませんが。マクロとかいうやつでしょうか。
configureとMakefileにありました。

configure(2350行目あたり)

# Define the identity of the package.
 PACKAGE=memcached
 VERSION=1.4.5

Makefile(88行目あたり)

PACKAGE = memcached
PACKAGE_BUGREPORT = memcached@googlegroups.com
PACKAGE_NAME = memcached
PACKAGE_STRING = memcached 1.4.5
PACKAGE_TARNAME = memcached
PACKAGE_VERSION = 1.4.5
PATH_SEPARATOR = :
PROFILER = /usr/bin/gcov
PROFILER_FLAGS = -fprofile-arcs -ftest-coverage
PROFILER_LDFLAGS = -lgcov
SET_MAKE = 
SHELL = /bin/sh
STRIP = 
VERSION = 1.4.5

↓この辺を学習するべきでしょうか?
Autotools - Wikipedia
http://www.unixuser.org/~euske/doc/makefile/#macrodef

とりあえず、printf(PACKAGE " " VERSION "\n");で
memcached 1.4.5
が出力されるということで進む。

#ifdefってなあに?

#ifdef ENABLE_SASL
    printf("-S            Turn on Sasl authentication\n");
#endif

#から始まるifはプリプロセッサにおける条件文だそう。
この場合、ENABLE_SASLというマクロが定義されていたならば、 #ifdef と #endif の間の行が有効になり

-S            Turn on Sasl authentication

が出力される。

configureにそれらしきものがありました。
configure(4521行目)

# Check whether --enable-sasl was given.
if test "${enable_sasl+set}" = set; then
  enableval=$enable_sasl;
fi


# Check whether --enable-sasl_pwdb was given.
if test "${enable_sasl_pwdb+set}" = set; then
  enableval=$enable_sasl_pwdb;
fi


if test "x$enable_sasl_pwdb" = "xyes"; then
  enable_sasl=yes
fi

内容はわかりませんが、コンパイル時に関係ありそう。
Sasl*2が使える場合って事でしょうか。

続きは↓
C初心者がmemcached-1.4.5を追いかける。(11){iオプション}

*1:引数に使うvoid

*2:Simple Authentication and Security Layer (インターネットプロトコルにおける認証とデータセキュリティのためのフレームワーク)http://ja.wikipedia.org/wiki/Simple_Authentication_and_Security_Layer