Merge in conversion to C++.
[awesomized/libmemcached] / clients / memslap.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38
39 #include <config.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 #include <sys/mman.h>
47 #include <fcntl.h>
48 #include <sys/time.h>
49 #include <getopt.h>
50 #include <pthread.h>
51 #include <assert.h>
52
53 #include <libmemcached/memcached.h>
54
55 #include "client_options.h"
56 #include "utilities.h"
57 #include "generator.h"
58 #include "execute.h"
59
60 #define DEFAULT_INITIAL_LOAD 10000
61 #define DEFAULT_EXECUTE_NUMBER 10000
62 #define DEFAULT_CONCURRENCY 1
63
64 #define PROGRAM_NAME "memslap"
65 #define PROGRAM_DESCRIPTION "Generates a load against a memcached custer of servers."
66
67 /* Global Thread counter */
68 volatile unsigned int thread_counter;
69 pthread_mutex_t counter_mutex;
70 pthread_cond_t count_threshhold;
71 volatile unsigned int master_wakeup;
72 pthread_mutex_t sleeper_mutex;
73 pthread_cond_t sleep_threshhold;
74
75 void *run_task(void *p);
76
77 /* Types */
78 typedef struct conclusions_st conclusions_st;
79 typedef struct thread_context_st thread_context_st;
80 typedef enum {
81 SET_TEST,
82 GET_TEST,
83 MGET_TEST
84 } test_type;
85
86 struct thread_context_st {
87 unsigned int key_count;
88 pairs_st *initial_pairs;
89 unsigned int initial_number;
90 pairs_st *execute_pairs;
91 unsigned int execute_number;
92 char **keys;
93 size_t *key_lengths;
94 test_type test;
95 memcached_st *memc;
96 };
97
98 struct conclusions_st {
99 long int load_time;
100 long int read_time;
101 unsigned int rows_loaded;
102 unsigned int rows_read;
103 };
104
105 /* Prototypes */
106 void options_parse(int argc, char *argv[]);
107 void conclusions_print(conclusions_st *conclusion);
108 void scheduler(memcached_server_st *servers, conclusions_st *conclusion);
109 pairs_st *load_create_data(memcached_st *memc, unsigned int number_of,
110 unsigned int *actual_loaded);
111 void flush_all(memcached_st *memc);
112
113 static int opt_binary= 0;
114 static int opt_verbose= 0;
115 static int opt_flush= 0;
116 static int opt_non_blocking_io= 0;
117 static int opt_tcp_nodelay= 0;
118 static unsigned int opt_execute_number= 0;
119 static unsigned int opt_createial_load= 0;
120 static unsigned int opt_concurrency= 0;
121 static int opt_displayflag= 0;
122 static char *opt_servers= NULL;
123 static int opt_udp_io= 0;
124 test_type opt_test= SET_TEST;
125
126 int main(int argc, char *argv[])
127 {
128 conclusions_st conclusion;
129 memcached_server_st *servers;
130
131 memset(&conclusion, 0, sizeof(conclusions_st));
132
133 srandom((unsigned int)time(NULL));
134 options_parse(argc, argv);
135
136 if (!opt_servers)
137 {
138 char *temp;
139
140 if ((temp= getenv("MEMCACHED_SERVERS")))
141 opt_servers= strdup(temp);
142 else
143 {
144 fprintf(stderr, "No Servers provided\n");
145 exit(1);
146 }
147 }
148
149 servers= memcached_servers_parse(opt_servers);
150
151 pthread_mutex_init(&counter_mutex, NULL);
152 pthread_cond_init(&count_threshhold, NULL);
153 pthread_mutex_init(&sleeper_mutex, NULL);
154 pthread_cond_init(&sleep_threshhold, NULL);
155
156 scheduler(servers, &conclusion);
157
158 free(opt_servers);
159
160 (void)pthread_mutex_destroy(&counter_mutex);
161 (void)pthread_cond_destroy(&count_threshhold);
162 (void)pthread_mutex_destroy(&sleeper_mutex);
163 (void)pthread_cond_destroy(&sleep_threshhold);
164 conclusions_print(&conclusion);
165 memcached_server_list_free(servers);
166
167 return 0;
168 }
169
170 void scheduler(memcached_server_st *servers, conclusions_st *conclusion)
171 {
172 unsigned int actual_loaded= 0; /* Fix warning */
173 memcached_st *memc;
174
175 struct timeval start_time, end_time;
176 pthread_t mainthread; /* Thread descriptor */
177 pthread_attr_t attr; /* Thread attributes */
178 pairs_st *pairs= NULL;
179
180 pthread_attr_init(&attr);
181 pthread_attr_setdetachstate(&attr,
182 PTHREAD_CREATE_DETACHED);
183
184 memc= memcached_create(NULL);
185
186 /* We need to set udp behavior before adding servers to the client */
187 if (opt_udp_io)
188 {
189 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP,
190 (uint64_t)opt_udp_io);
191 for (uint32_t x= 0; x < memcached_server_list_count(servers); x++ )
192 {
193 servers[x].type= MEMCACHED_CONNECTION_UDP;
194 }
195 }
196 memcached_server_push(memc, servers);
197
198 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
199 (uint64_t)opt_binary);
200
201 if (opt_flush)
202 flush_all(memc);
203 if (opt_createial_load)
204 pairs= load_create_data(memc, opt_createial_load, &actual_loaded);
205
206 char **keys= static_cast<char **>(calloc(actual_loaded, sizeof(char*)));
207 size_t *key_lengths= static_cast<size_t *>(calloc(actual_loaded, sizeof(size_t)));
208
209 if (keys == NULL || key_lengths == NULL)
210 {
211 free(keys);
212 free(key_lengths);
213 keys= NULL;
214 key_lengths= NULL;
215 }
216 else
217 {
218 for (uint32_t x= 0; x < actual_loaded; ++x)
219 {
220 keys[x]= pairs[x].key;
221 key_lengths[x]= pairs[x].key_length;
222 }
223 }
224
225 /* We set this after we have loaded */
226 {
227 if (opt_non_blocking_io)
228 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
229 if (opt_tcp_nodelay)
230 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 1);
231 }
232
233 pthread_mutex_lock(&counter_mutex);
234 thread_counter= 0;
235
236 pthread_mutex_lock(&sleeper_mutex);
237 master_wakeup= 1;
238 pthread_mutex_unlock(&sleeper_mutex);
239
240 for (uint32_t x= 0; x < opt_concurrency; x++)
241 {
242 thread_context_st *context;
243 context= (thread_context_st *)calloc(1, sizeof(thread_context_st));
244
245 context->memc= memcached_clone(NULL, memc);
246 context->test= opt_test;
247
248 context->initial_pairs= pairs;
249 context->initial_number= actual_loaded;
250 context->keys= keys;
251 context->key_lengths= key_lengths;
252
253 if (opt_test == SET_TEST)
254 {
255 context->execute_pairs= pairs_generate(opt_execute_number, 400);
256 context->execute_number= opt_execute_number;
257 }
258
259 /* now you create the thread */
260 if (pthread_create(&mainthread, &attr, run_task,
261 (void *)context) != 0)
262 {
263 fprintf(stderr,"Could not create thread\n");
264 exit(1);
265 }
266 thread_counter++;
267 }
268
269 pthread_mutex_unlock(&counter_mutex);
270 pthread_attr_destroy(&attr);
271
272 pthread_mutex_lock(&sleeper_mutex);
273 master_wakeup= 0;
274 pthread_mutex_unlock(&sleeper_mutex);
275 pthread_cond_broadcast(&sleep_threshhold);
276
277 gettimeofday(&start_time, NULL);
278 /*
279 We loop until we know that all children have cleaned up.
280 */
281 pthread_mutex_lock(&counter_mutex);
282 while (thread_counter)
283 pthread_cond_wait(&count_threshhold, &counter_mutex);
284 pthread_mutex_unlock(&counter_mutex);
285
286 gettimeofday(&end_time, NULL);
287
288 conclusion->load_time= timedif(end_time, start_time);
289 conclusion->read_time= timedif(end_time, start_time);
290 free(keys);
291 free(key_lengths);
292 pairs_free(pairs);
293 memcached_free(memc);
294 }
295
296 void options_parse(int argc, char *argv[])
297 {
298 memcached_programs_help_st help_options[]=
299 {
300 {0},
301 };
302
303 static struct option long_options[]=
304 {
305 {(OPTIONSTRING)"concurrency", required_argument, NULL, OPT_SLAP_CONCURRENCY},
306 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
307 {(OPTIONSTRING)"execute-number", required_argument, NULL, OPT_SLAP_EXECUTE_NUMBER},
308 {(OPTIONSTRING)"flag", no_argument, &opt_displayflag, OPT_FLAG},
309 {(OPTIONSTRING)"flush", no_argument, &opt_flush, OPT_FLUSH},
310 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
311 {(OPTIONSTRING)"initial-load", required_argument, NULL, OPT_SLAP_INITIAL_LOAD}, /* Number to load initially */
312 {(OPTIONSTRING)"non-blocking", no_argument, &opt_non_blocking_io, OPT_SLAP_NON_BLOCK},
313 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
314 {(OPTIONSTRING)"tcp-nodelay", no_argument, &opt_tcp_nodelay, OPT_SLAP_TCP_NODELAY},
315 {(OPTIONSTRING)"test", required_argument, NULL, OPT_SLAP_TEST},
316 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
317 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
318 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
319 {(OPTIONSTRING)"udp", no_argument, NULL, OPT_UDP},
320 {0, 0, 0, 0},
321 };
322
323 int option_index= 0;
324 int option_rv;
325
326 while (1)
327 {
328 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
329 if (option_rv == -1) break;
330 switch (option_rv)
331 {
332 case 0:
333 break;
334 case OPT_UDP:
335 if (opt_test == GET_TEST)
336 {
337 fprintf(stderr, "You can not run a get test in UDP mode. UDP mode "
338 "does not currently support get ops.\n");
339 exit(1);
340 }
341 opt_udp_io= 1;
342 break;
343 case OPT_BINARY:
344 opt_binary = 1;
345 break;
346 case OPT_VERBOSE: /* --verbose or -v */
347 opt_verbose = OPT_VERBOSE;
348 break;
349 case OPT_DEBUG: /* --debug or -d */
350 opt_verbose = OPT_DEBUG;
351 break;
352 case OPT_VERSION: /* --version or -V */
353 version_command(PROGRAM_NAME);
354 break;
355 case OPT_HELP: /* --help or -h */
356 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
357 break;
358 case OPT_SERVERS: /* --servers or -s */
359 opt_servers= strdup(optarg);
360 break;
361 case OPT_SLAP_TEST:
362 if (!strcmp(optarg, "get"))
363 {
364 if (opt_udp_io == 1)
365 {
366 fprintf(stderr, "You can not run a get test in UDP mode. UDP mode "
367 "does not currently support get ops.\n");
368 exit(1);
369 }
370 opt_test= GET_TEST ;
371 }
372 else if (!strcmp(optarg, "set"))
373 opt_test= SET_TEST;
374 else if (!strcmp(optarg, "mget"))
375 {
376 opt_test= MGET_TEST;
377 }
378 else
379 {
380 fprintf(stderr, "Your test, %s, is not a known test\n", optarg);
381 exit(1);
382 }
383 break;
384 case OPT_SLAP_CONCURRENCY:
385 opt_concurrency= (unsigned int)strtoul(optarg, (char **)NULL, 10);
386 break;
387 case OPT_SLAP_EXECUTE_NUMBER:
388 opt_execute_number= (unsigned int)strtoul(optarg, (char **)NULL, 10);
389 break;
390 case OPT_SLAP_INITIAL_LOAD:
391 opt_createial_load= (unsigned int)strtoul(optarg, (char **)NULL, 10);
392 break;
393 case '?':
394 /* getopt_long already printed an error message. */
395 exit(1);
396 default:
397 abort();
398 }
399 }
400
401 if ((opt_test == GET_TEST || opt_test == MGET_TEST) && opt_createial_load == 0)
402 opt_createial_load= DEFAULT_INITIAL_LOAD;
403
404 if (opt_execute_number == 0)
405 opt_execute_number= DEFAULT_EXECUTE_NUMBER;
406
407 if (opt_concurrency == 0)
408 opt_concurrency= DEFAULT_CONCURRENCY;
409 }
410
411 void conclusions_print(conclusions_st *conclusion)
412 {
413 printf("\tThreads connecting to servers %u\n", opt_concurrency);
414 #ifdef NOT_FINISHED
415 printf("\tLoaded %u rows\n", conclusion->rows_loaded);
416 printf("\tRead %u rows\n", conclusion->rows_read);
417 #endif
418 if (opt_test == SET_TEST)
419 printf("\tTook %ld.%03ld seconds to load data\n", conclusion->load_time / 1000,
420 conclusion->load_time % 1000);
421 else
422 printf("\tTook %ld.%03ld seconds to read data\n", conclusion->read_time / 1000,
423 conclusion->read_time % 1000);
424 }
425
426 void *run_task(void *p)
427 {
428 thread_context_st *context= (thread_context_st *)p;
429 memcached_st *memc;
430
431 memc= context->memc;
432
433 pthread_mutex_lock(&sleeper_mutex);
434 while (master_wakeup)
435 {
436 pthread_cond_wait(&sleep_threshhold, &sleeper_mutex);
437 }
438 pthread_mutex_unlock(&sleeper_mutex);
439
440 /* Do Stuff */
441 switch (context->test)
442 {
443 case SET_TEST:
444 assert(context->execute_pairs);
445 execute_set(memc, context->execute_pairs, context->execute_number);
446 break;
447 case GET_TEST:
448 execute_get(memc, context->initial_pairs, context->initial_number);
449 break;
450 case MGET_TEST:
451 execute_mget(memc, (const char*const*)context->keys, context->key_lengths,
452 context->initial_number);
453 break;
454 default:
455 WATCHPOINT_ASSERT(context->test);
456 break;
457 }
458
459 memcached_free(memc);
460
461 if (context->execute_pairs)
462 pairs_free(context->execute_pairs);
463
464 free(context);
465
466 pthread_mutex_lock(&counter_mutex);
467 thread_counter--;
468 pthread_cond_signal(&count_threshhold);
469 pthread_mutex_unlock(&counter_mutex);
470
471 return NULL;
472 }
473
474 void flush_all(memcached_st *memc)
475 {
476 memcached_flush(memc, 0);
477 }
478
479 pairs_st *load_create_data(memcached_st *memc, unsigned int number_of,
480 unsigned int *actual_loaded)
481 {
482 memcached_st *memc_clone;
483 pairs_st *pairs;
484
485 memc_clone= memcached_clone(NULL, memc);
486 /* We always used non-blocking IO for load since it is faster */
487 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
488
489 pairs= pairs_generate(number_of, 400);
490 *actual_loaded= execute_set(memc_clone, pairs, number_of);
491
492 memcached_free(memc_clone);
493
494 return pairs;
495 }