5d761e3c94729c03dca3931b005ec381f359abb0
[m6w6/libmemcached] / clients / utilities.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 #include "libmemcached/common.h"
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include "utilities.h"
17
18
19 long int timedif(struct timeval a, struct timeval b)
20 {
21 long us, s;
22
23 us = (int)(a.tv_usec - b.tv_usec);
24 us /= 1000;
25 s = (int)(a.tv_sec - b.tv_sec);
26 s *= 1000;
27 return s + us;
28 }
29
30 void version_command(const char *command_name)
31 {
32 printf("%s v%u.%u\n", command_name, 1U, 0U);
33 exit(0);
34 }
35
36 static const char *lookup_help(memcached_options option)
37 {
38 switch (option)
39 {
40 case OPT_SERVERS: return("List which servers you wish to connect to.");
41 case OPT_VERSION: return("Display the version of the application and then exit.");
42 case OPT_HELP: return("Display this message and then exit.");
43 case OPT_VERBOSE: return("Give more details on the progression of the application.");
44 case OPT_DEBUG: return("Provide output only useful for debugging.");
45 case OPT_FLAG: return("Provide flag information for storage operation.");
46 case OPT_EXPIRE: return("Set the expire option for the object.");
47 case OPT_SET: return("Use set command with memcached when storing.");
48 case OPT_REPLACE: return("Use replace command with memcached when storing.");
49 case OPT_ADD: return("Use add command with memcached when storing.");
50 case OPT_SLAP_EXECUTE_NUMBER: return("Number of times to execute the given test.");
51 case OPT_SLAP_INITIAL_LOAD: return("Number of key pairs to load before executing tests.");
52 case OPT_SLAP_TEST: return("Test to run (currently \"get\" or \"set\").");
53 case OPT_SLAP_CONCURRENCY: return("Number of users to simulate with load.");
54 case OPT_SLAP_NON_BLOCK: return("Set TCP up to use non-blocking IO.");
55 case OPT_SLAP_TCP_NODELAY: return("Set TCP socket up to use nodelay.");
56 case OPT_FLUSH: return("Flush servers before running tests.");
57 case OPT_HASH: return("Select hash type.");
58 case OPT_BINARY: return("Switch to binary protocol.");
59 case OPT_ANALYZE: return("Analyze the provided servers.");
60 case OPT_UDP: return("Use UDP protocol when communicating with server.");
61 default: WATCHPOINT_ASSERT(0);
62 };
63
64 WATCHPOINT_ASSERT(0);
65 return "forgot to document this function :)";
66 }
67
68 void help_command(const char *command_name, const char *description,
69 const struct option *long_options,
70 memcached_programs_help_st *options __attribute__((unused)))
71 {
72 unsigned int x;
73
74 printf("%s v%u.%u\n\n", command_name, 1U, 0U);
75 printf("\t%s\n\n", description);
76 printf("Current options. A '=' means the option takes a value.\n\n");
77
78 for (x= 0; long_options[x].name; x++)
79 {
80 const char *help_message;
81
82 printf("\t --%s%c\n", long_options[x].name,
83 long_options[x].has_arg ? '=' : ' ');
84 if ((help_message= lookup_help(long_options[x].val)))
85 printf("\t\t%s\n", help_message);
86 }
87
88 printf("\n");
89 exit(0);
90 }
91
92 void process_hash_option(memcached_st *memc, char *opt_hash)
93 {
94 uint64_t set;
95 memcached_return_t rc;
96
97 if (opt_hash == NULL)
98 return;
99
100 set= MEMCACHED_HASH_DEFAULT; /* Just here to solve warning */
101 if (!strcasecmp(opt_hash, "CRC"))
102 set= MEMCACHED_HASH_CRC;
103 else if (!strcasecmp(opt_hash, "FNV1_64"))
104 set= MEMCACHED_HASH_FNV1_64;
105 else if (!strcasecmp(opt_hash, "FNV1A_64"))
106 set= MEMCACHED_HASH_FNV1A_64;
107 else if (!strcasecmp(opt_hash, "FNV1_32"))
108 set= MEMCACHED_HASH_FNV1_32;
109 else if (!strcasecmp(opt_hash, "FNV1A_32"))
110 set= MEMCACHED_HASH_FNV1A_32;
111 else
112 {
113 fprintf(stderr, "hash: type not recognized %s\n", opt_hash);
114 exit(1);
115 }
116
117 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
118 if (rc != MEMCACHED_SUCCESS)
119 {
120 fprintf(stderr, "hash: memcache error %s\n", memcached_strerror(memc, rc));
121 exit(1);
122 }
123 }
124