Cleanup of spelling error in option.
[m6w6/libmemcached] / src / memslap.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <sys/mman.h>
8 #include <fcntl.h>
9 #include <assert.h>
10 #include <sys/time.h>
11 #include <getopt.h>
12 #include <pthread.h>
13
14 #include <memcached.h>
15
16 #include "client_options.h"
17 #include "utilities.h"
18 #include "generator.h"
19 #include "execute.h"
20
21 #define DEFAULT_INITIAL_LOAD 10000
22 #define DEFAULT_EXECUTE_NUMBER 10000
23 #define DEFAULT_CONCURRENCY 1
24
25 #define PROGRAM_NAME "memslap"
26 #define PROGRAM_DESCRIPTION "Generates a load against a memcached custer of servers."
27
28 /* Global Thread counter */
29 unsigned int thread_counter;
30 pthread_mutex_t counter_mutex;
31 pthread_cond_t count_threshhold;
32 unsigned int master_wakeup;
33 pthread_mutex_t sleeper_mutex;
34 pthread_cond_t sleep_threshhold;
35
36 void *run_task(void *p);
37
38 /* Types */
39 typedef struct conclusions_st conclusions_st;
40 typedef struct thread_context_st thread_context_st;
41 typedef enum {
42 SET_TEST,
43 GET_TEST,
44 } test_type;
45
46 struct thread_context_st {
47 unsigned int key_count;
48 pairs_st *initial_pairs;
49 unsigned int initial_number;
50 pairs_st *execute_pairs;
51 unsigned int execute_number;
52 test_type test;
53 memcached_st *memc;
54 };
55
56 struct conclusions_st {
57 long int load_time;
58 long int read_time;
59 unsigned int rows_loaded;
60 unsigned int rows_read;
61 };
62
63 /* Prototypes */
64 void options_parse(int argc, char *argv[]);
65 void conclusions_print(conclusions_st *conclusion);
66 void scheduler(memcached_server_st *servers, conclusions_st *conclusion);
67 pairs_st *load_create_data(memcached_st *memc, unsigned int number_of,
68 unsigned int *actual_loaded);
69 void flush_all(memcached_st *memc);
70
71 static int opt_verbose= 0;
72 static int opt_flush= 0;
73 static int opt_non_blocking_io= 0;
74 static int opt_tcp_nodelay= 0;
75 static unsigned int opt_execute_number= 0;
76 static unsigned int opt_createial_load= 0;
77 static unsigned int opt_concurrency= 0;
78 static int opt_displayflag= 0;
79 static char *opt_servers= NULL;
80 test_type opt_test= SET_TEST;
81
82 int main(int argc, char *argv[])
83 {
84 conclusions_st conclusion;
85 memcached_server_st *servers;
86
87 memset(&conclusion, 0, sizeof(conclusions_st));
88
89 srandom(time(NULL));
90 options_parse(argc, argv);
91
92 if (!opt_servers)
93 {
94 char *temp;
95
96 if ((temp= getenv("MEMCACHED_SERVERS")))
97 opt_servers= strdup(temp);
98 else
99 exit(1);
100 }
101
102 servers= memcached_servers_parse(opt_servers);
103
104 pthread_mutex_init(&counter_mutex, NULL);
105 pthread_cond_init(&count_threshhold, NULL);
106 pthread_mutex_init(&sleeper_mutex, NULL);
107 pthread_cond_init(&sleep_threshhold, NULL);
108
109 scheduler(servers, &conclusion);
110
111 free(opt_servers);
112
113 (void)pthread_mutex_destroy(&counter_mutex);
114 (void)pthread_cond_destroy(&count_threshhold);
115 (void)pthread_mutex_destroy(&sleeper_mutex);
116 (void)pthread_cond_destroy(&sleep_threshhold);
117 conclusions_print(&conclusion);
118 memcached_server_list_free(servers);
119
120 return 0;
121 }
122
123 void scheduler(memcached_server_st *servers, conclusions_st *conclusion)
124 {
125 unsigned int x;
126 unsigned int actual_loaded= 0; /* Fix warning */
127 memcached_st *memc;
128
129 struct timeval start_time, end_time;
130 pthread_t mainthread; /* Thread descriptor */
131 pthread_attr_t attr; /* Thread attributes */
132 pairs_st *pairs= NULL;
133
134 pthread_attr_init(&attr);
135 pthread_attr_setdetachstate(&attr,
136 PTHREAD_CREATE_DETACHED);
137
138 memc= memcached_create(NULL);
139 memcached_server_push(memc, servers);
140
141 if (opt_flush)
142 flush_all(memc);
143 if (opt_createial_load)
144 pairs= load_create_data(memc, opt_createial_load, &actual_loaded);
145
146 /* We set this after we have loaded */
147 {
148 unsigned int value= 1;
149 if (opt_non_blocking_io)
150 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &value);
151 if (opt_tcp_nodelay)
152 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &value);
153 }
154
155
156 pthread_mutex_lock(&counter_mutex);
157 thread_counter= 0;
158
159 pthread_mutex_lock(&sleeper_mutex);
160 master_wakeup= 1;
161 pthread_mutex_unlock(&sleeper_mutex);
162
163 for (x= 0; x < opt_concurrency; x++)
164 {
165 thread_context_st *context;
166 context= (thread_context_st *)malloc(sizeof(thread_context_st));
167 memset(context, 0, sizeof(thread_context_st));
168
169 context->memc= memcached_clone(NULL, memc);
170 context->test= opt_test;
171
172 context->initial_pairs= pairs;
173 context->initial_number= actual_loaded;
174
175 if (opt_test == SET_TEST)
176 {
177 context->execute_pairs= pairs_generate(opt_execute_number);
178 context->execute_number= opt_execute_number;
179 }
180
181 /* now you create the thread */
182 if (pthread_create(&mainthread, &attr, run_task,
183 (void *)context) != 0)
184 {
185 fprintf(stderr,"Could not create thread\n");
186 exit(1);
187 }
188 thread_counter++;
189 }
190
191 pthread_mutex_unlock(&counter_mutex);
192 pthread_attr_destroy(&attr);
193
194 pthread_mutex_lock(&sleeper_mutex);
195 master_wakeup= 0;
196 pthread_mutex_unlock(&sleeper_mutex);
197 pthread_cond_broadcast(&sleep_threshhold);
198
199 gettimeofday(&start_time, NULL);
200 /*
201 We loop until we know that all children have cleaned up.
202 */
203 pthread_mutex_lock(&counter_mutex);
204 while (thread_counter)
205 {
206 struct timespec abstime;
207
208 memset(&abstime, 0, sizeof(struct timespec));
209 abstime.tv_sec= 1;
210
211 pthread_cond_timedwait(&count_threshhold, &counter_mutex, &abstime);
212 }
213 pthread_mutex_unlock(&counter_mutex);
214
215 gettimeofday(&end_time, NULL);
216
217 conclusion->load_time= timedif(end_time, start_time);
218 conclusion->read_time= timedif(end_time, start_time);
219 pairs_free(pairs);
220 }
221
222 void options_parse(int argc, char *argv[])
223 {
224 memcached_programs_help_st help_options[]=
225 {
226 {0},
227 };
228
229 static struct option long_options[]=
230 {
231 {"concurrency", required_argument, NULL, OPT_SLAP_CONCURRENCY},
232 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
233 {"execute-number", required_argument, NULL, OPT_SLAP_EXECUTE_NUMBER},
234 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
235 {"flush", no_argument, &opt_flush, OPT_FLUSH},
236 {"help", no_argument, NULL, OPT_HELP},
237 {"initial-load", required_argument, NULL, OPT_SLAP_INITIAL_LOAD}, /* Number to load initially */
238 {"non-blocking", no_argument, &opt_non_blocking_io, OPT_SLAP_NON_BLOCK},
239 {"servers", required_argument, NULL, OPT_SERVERS},
240 {"tcp-nodelay", no_argument, &opt_tcp_nodelay, OPT_SLAP_TCP_NODELAY},
241 {"test", required_argument, NULL, OPT_SLAP_TEST},
242 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
243 {"version", no_argument, NULL, OPT_VERSION},
244 {0, 0, 0, 0},
245 };
246
247 int option_index= 0;
248 int option_rv;
249
250 while (1)
251 {
252 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
253 if (option_rv == -1) break;
254 switch (option_rv)
255 {
256 case 0:
257 break;
258 case OPT_VERBOSE: /* --verbose or -v */
259 opt_verbose = OPT_VERBOSE;
260 break;
261 case OPT_DEBUG: /* --debug or -d */
262 opt_verbose = OPT_DEBUG;
263 break;
264 case OPT_VERSION: /* --version or -V */
265 version_command(PROGRAM_NAME);
266 break;
267 case OPT_HELP: /* --help or -h */
268 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
269 break;
270 case OPT_SERVERS: /* --servers or -s */
271 opt_servers= strdup(optarg);
272 break;
273 case OPT_SLAP_TEST:
274 if (!strcmp(optarg, "get"))
275 opt_test= GET_TEST ;
276 else if (!strcmp(optarg, "set"))
277 opt_test= SET_TEST;
278 else
279 {
280 fprintf(stderr, "Your test, %s, is not a known test\n", optarg);
281 exit(1);
282 }
283 break;
284 case OPT_SLAP_CONCURRENCY:
285 opt_concurrency= strtol(optarg, (char **)NULL, 10);
286 case OPT_SLAP_EXECUTE_NUMBER:
287 opt_execute_number= strtol(optarg, (char **)NULL, 10);
288 break;
289 case OPT_SLAP_INITIAL_LOAD:
290 opt_createial_load= strtol(optarg, (char **)NULL, 10);
291 break;
292 case '?':
293 /* getopt_long already printed an error message. */
294 exit(1);
295 default:
296 abort();
297 }
298 }
299
300 if (opt_test == GET_TEST && opt_createial_load == 0)
301 opt_createial_load= DEFAULT_INITIAL_LOAD;
302
303 if (opt_execute_number == 0)
304 opt_execute_number= DEFAULT_EXECUTE_NUMBER;
305
306 if (opt_concurrency == 0)
307 opt_concurrency= DEFAULT_CONCURRENCY;
308 }
309
310 void conclusions_print(conclusions_st *conclusion)
311 {
312 printf("\tThreads connecting to servers %u\n", opt_concurrency);
313 #ifdef NOT_FINISHED
314 printf("\tLoaded %u rows\n", conclusion->rows_loaded);
315 printf("\tRead %u rows\n", conclusion->rows_read);
316 #endif
317 if (opt_test == SET_TEST)
318 printf("\tTook %ld.%03ld seconds to load data\n", conclusion->load_time / 1000,
319 conclusion->load_time % 1000);
320 else
321 printf("\tTook %ld.%03ld seconds to read data\n", conclusion->read_time / 1000,
322 conclusion->read_time % 1000);
323 }
324
325 void *run_task(void *p)
326 {
327 thread_context_st *context= (thread_context_st *)p;
328 memcached_st *memc;
329
330 memc= context->memc;
331
332 pthread_mutex_lock(&sleeper_mutex);
333 while (master_wakeup)
334 {
335 pthread_cond_wait(&sleep_threshhold, &sleeper_mutex);
336 }
337 pthread_mutex_unlock(&sleeper_mutex);
338
339 /* Do Stuff */
340 switch (context->test)
341 {
342 case SET_TEST:
343 execute_set(memc, context->execute_pairs, context->execute_number);
344 break;
345 case GET_TEST:
346 execute_get(memc, context->initial_pairs, context->initial_number);
347 break;
348 }
349
350 memcached_free(memc);
351
352 if (context->execute_pairs)
353 pairs_free(context->execute_pairs);
354 free(context);
355
356 pthread_mutex_lock(&counter_mutex);
357 thread_counter--;
358 pthread_cond_signal(&count_threshhold);
359 pthread_mutex_unlock(&counter_mutex);
360
361 return NULL;
362 }
363
364 void flush_all(memcached_st *memc)
365 {
366 memcached_flush(memc, 0);
367 }
368
369 pairs_st *load_create_data(memcached_st *memc, unsigned int number_of,
370 unsigned int *actual_loaded)
371 {
372 memcached_st *clone;
373 pairs_st *pairs;
374
375 clone= memcached_clone(NULL, memc);
376 /* We always used non-blocking IO for load since it is faster */
377 memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL );
378
379 pairs= pairs_generate(number_of);
380 *actual_loaded= execute_set(clone, pairs, number_of);
381
382 memcached_free(clone);
383
384 return pairs;
385 }