clang-format: no single-line case
[awesomized/libmemcached] / src / bin / memslap.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "mem_config.h"
17
18 #include <cassert>
19 #include <cerrno>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cstring>
23 #include <fcntl.h>
24 #include <getopt.h>
25 #include <memory>
26 #include <pthread.h>
27 #include <sys/mman.h>
28 #include <sys/stat.h>
29 #include <sys/time.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32
33 #include <iostream>
34
35 #include "libmemcached-1.0/memcached.h"
36
37 #include "client_options.h"
38 #include "utilities.h"
39 #include "generator.h"
40 #include "execute.h"
41
42 #define DEFAULT_INITIAL_LOAD 10000
43 #define DEFAULT_EXECUTE_NUMBER 10000
44 #define DEFAULT_CONCURRENCY 1
45
46 #define VALUE_BYTES 4096
47
48 #define PROGRAM_NAME "memslap"
49 #define PROGRAM_DESCRIPTION "Generates a load against a memcached custer of servers."
50
51 /* Global Thread counter */
52 volatile unsigned int master_wakeup;
53 pthread_mutex_t sleeper_mutex;
54 pthread_cond_t sleep_threshhold;
55
56 /* Types */
57 enum test_t { SET_TEST, GET_TEST, MGET_TEST };
58
59 struct thread_context_st {
60 unsigned int key_count;
61 pairs_st *initial_pairs;
62 unsigned int initial_number;
63 pairs_st *execute_pairs;
64 unsigned int execute_number;
65 char **keys;
66 size_t *key_lengths;
67 test_t test;
68 memcached_st *memc;
69 const memcached_st *root;
70
71 thread_context_st(const memcached_st *memc_arg, test_t test_arg)
72 : key_count(0)
73 , initial_pairs(NULL)
74 , initial_number(0)
75 , execute_pairs(NULL)
76 , execute_number(0)
77 , keys(0)
78 , key_lengths(NULL)
79 , test(test_arg)
80 , memc(NULL)
81 , root(memc_arg) {}
82
83 void init() {
84 memc = memcached_clone(NULL, root);
85 }
86
87 ~thread_context_st() {
88 if (execute_pairs) {
89 pairs_free(execute_pairs);
90 }
91 memcached_free(memc);
92 }
93 };
94
95 struct conclusions_st {
96 long int load_time;
97 long int read_time;
98 unsigned int rows_loaded;
99 unsigned int rows_read;
100
101 conclusions_st()
102 : load_time(0)
103 , read_time(0)
104 , rows_loaded(0)
105 , rows_read() {}
106 };
107
108 /* Prototypes */
109 void options_parse(int argc, char *argv[]);
110 void conclusions_print(conclusions_st *conclusion);
111 void scheduler(memcached_server_st *servers, conclusions_st *conclusion);
112 pairs_st *load_create_data(memcached_st *memc, unsigned int number_of, unsigned int *actual_loaded);
113 void flush_all(memcached_st *memc);
114
115 static bool opt_binary = 0;
116 static int opt_verbose = 0;
117 static int opt_flush = 0;
118 static int opt_non_blocking_io = 0;
119 static int opt_tcp_nodelay = 0;
120 static unsigned int opt_execute_number = 0;
121 static unsigned int opt_createial_load = 0;
122 static unsigned int opt_concurrency = 0;
123 static int opt_displayflag = 0;
124 static char *opt_servers = NULL;
125 static bool opt_udp_io = false;
126 test_t opt_test = SET_TEST;
127
128 extern "C" {
129
130 static __attribute__((noreturn)) void *run_task(void *p) {
131 thread_context_st *context = (thread_context_st *) p;
132
133 context->init();
134
135 pthread_mutex_lock(&sleeper_mutex);
136 while (master_wakeup) {
137 pthread_cond_wait(&sleep_threshhold, &sleeper_mutex);
138 }
139 pthread_mutex_unlock(&sleeper_mutex);
140
141 /* Do Stuff */
142 switch (context->test) {
143 case SET_TEST:
144 assert(context->execute_pairs);
145 execute_set(context->memc, context->execute_pairs, context->execute_number);
146 break;
147
148 case GET_TEST:
149 execute_get(context->memc, context->initial_pairs, context->initial_number);
150 break;
151
152 case MGET_TEST:
153 execute_mget(context->memc, (const char *const *) context->keys, context->key_lengths,
154 context->initial_number);
155 break;
156 }
157
158 delete context;
159
160 pthread_exit(0);
161 }
162 }
163
164 int main(int argc, char *argv[]) {
165 conclusions_st conclusion;
166
167 srandom((unsigned int) time(NULL));
168 options_parse(argc, argv);
169
170 if (opt_servers == NULL) {
171 char *temp;
172
173 if ((temp = getenv("MEMCACHED_SERVERS"))) {
174 opt_servers = strdup(temp);
175 }
176
177 if (opt_servers == NULL) {
178 std::cerr << "No servers provided" << std::endl;
179 exit(EXIT_FAILURE);
180 }
181 }
182
183 memcached_server_st *servers = memcached_servers_parse(opt_servers);
184 if (servers == NULL or memcached_server_list_count(servers) == 0) {
185 std::cerr << "Invalid server list provided:" << opt_servers << std::endl;
186 return EXIT_FAILURE;
187 }
188
189 pthread_mutex_init(&sleeper_mutex, NULL);
190 pthread_cond_init(&sleep_threshhold, NULL);
191
192 int error_code = EXIT_SUCCESS;
193 try {
194 scheduler(servers, &conclusion);
195 } catch (std::exception &e) {
196 std::cerr << "Died with exception: " << e.what() << std::endl;
197 error_code = EXIT_FAILURE;
198 }
199
200 free(opt_servers);
201
202 (void) pthread_mutex_destroy(&sleeper_mutex);
203 (void) pthread_cond_destroy(&sleep_threshhold);
204 conclusions_print(&conclusion);
205 memcached_server_list_free(servers);
206
207 return error_code;
208 }
209
210 void scheduler(memcached_server_st *servers, conclusions_st *conclusion) {
211 unsigned int actual_loaded = 0; /* Fix warning */
212
213 struct timeval start_time, end_time;
214 pairs_st *pairs = NULL;
215
216 memcached_st *memc = memcached_create(NULL);
217
218 memcached_server_push(memc, servers);
219
220 /* We need to set udp behavior before adding servers to the client */
221 if (opt_udp_io) {
222 if (memcached_failed(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, opt_udp_io))) {
223 std::cerr << "Failed to enable UDP." << std::endl;
224 memcached_free(memc);
225 exit(EXIT_FAILURE);
226 }
227 }
228
229 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t) opt_binary);
230
231 if (opt_flush) {
232 flush_all(memc);
233 }
234
235 if (opt_createial_load) {
236 pairs = load_create_data(memc, opt_createial_load, &actual_loaded);
237 }
238
239 char **keys = static_cast<char **>(calloc(actual_loaded, sizeof(char *)));
240 size_t *key_lengths = static_cast<size_t *>(calloc(actual_loaded, sizeof(size_t)));
241
242 if (keys == NULL or key_lengths == NULL) {
243 free(keys);
244 free(key_lengths);
245 keys = NULL;
246 key_lengths = NULL;
247 } else {
248 for (uint32_t x = 0; x < actual_loaded; ++x) {
249 keys[x] = pairs[x].key;
250 key_lengths[x] = pairs[x].key_length;
251 }
252 }
253
254 /* We set this after we have loaded */
255 {
256 if (opt_non_blocking_io)
257 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
258
259 if (opt_tcp_nodelay)
260 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 1);
261 }
262
263 pthread_mutex_lock(&sleeper_mutex);
264 master_wakeup = 1;
265 pthread_mutex_unlock(&sleeper_mutex);
266
267 pthread_t *threads = new (std::nothrow) pthread_t[opt_concurrency];
268
269 if (threads == NULL) {
270 exit(EXIT_FAILURE);
271 }
272
273 for (uint32_t x = 0; x < opt_concurrency; x++) {
274 thread_context_st *context = new thread_context_st(memc, opt_test);
275 context->test = opt_test;
276
277 context->initial_pairs = pairs;
278 context->initial_number = actual_loaded;
279 context->keys = keys;
280 context->key_lengths = key_lengths;
281
282 if (opt_test == SET_TEST) {
283 context->execute_pairs = pairs_generate(opt_execute_number, VALUE_BYTES);
284 context->execute_number = opt_execute_number;
285 }
286
287 /* now you create the thread */
288 if (pthread_create(threads + x, NULL, run_task, (void *) context) != 0) {
289 fprintf(stderr, "Could not create thread\n");
290 exit(1);
291 }
292 }
293
294 pthread_mutex_lock(&sleeper_mutex);
295 master_wakeup = 0;
296 pthread_mutex_unlock(&sleeper_mutex);
297 pthread_cond_broadcast(&sleep_threshhold);
298 gettimeofday(&start_time, NULL);
299
300 for (uint32_t x = 0; x < opt_concurrency; x++) {
301 void *retval;
302 pthread_join(threads[x], &retval);
303 }
304 delete[] threads;
305
306 gettimeofday(&end_time, NULL);
307
308 conclusion->load_time = timedif(end_time, start_time);
309 conclusion->read_time = timedif(end_time, start_time);
310 free(keys);
311 free(key_lengths);
312 pairs_free(pairs);
313 memcached_free(memc);
314 }
315
316 void options_parse(int argc, char *argv[]) {
317 memcached_programs_help_st help_options[] = {
318 {0},
319 };
320
321 static struct option long_options[] = {
322 {(OPTIONSTRING) "concurrency", required_argument, NULL, OPT_SLAP_CONCURRENCY},
323 {(OPTIONSTRING) "debug", no_argument, &opt_verbose, OPT_DEBUG},
324 {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET},
325 {(OPTIONSTRING) "execute-number", required_argument, NULL, OPT_SLAP_EXECUTE_NUMBER},
326 {(OPTIONSTRING) "flag", no_argument, &opt_displayflag, OPT_FLAG},
327 {(OPTIONSTRING) "flush", no_argument, &opt_flush, OPT_FLUSH},
328 {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP},
329 {(OPTIONSTRING) "initial-load", required_argument, NULL,
330 OPT_SLAP_INITIAL_LOAD}, /* Number to load initially */
331 {(OPTIONSTRING) "non-blocking", no_argument, &opt_non_blocking_io, OPT_SLAP_NON_BLOCK},
332 {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS},
333 {(OPTIONSTRING) "tcp-nodelay", no_argument, &opt_tcp_nodelay, OPT_SLAP_TCP_NODELAY},
334 {(OPTIONSTRING) "test", required_argument, NULL, OPT_SLAP_TEST},
335 {(OPTIONSTRING) "verbose", no_argument, &opt_verbose, OPT_VERBOSE},
336 {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION},
337 {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY},
338 {(OPTIONSTRING) "udp", no_argument, NULL, OPT_UDP},
339 {0, 0, 0, 0},
340 };
341
342 bool opt_help = false;
343 bool opt_version = false;
344 int option_index = 0;
345 while (1) {
346 int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
347
348 if (option_rv == -1)
349 break;
350
351 switch (option_rv) {
352 case 0:
353 break;
354
355 case OPT_UDP:
356 if (opt_test == GET_TEST) {
357 fprintf(stderr,
358 "You can not run a get test in UDP mode. UDP mode "
359 "does not currently support get ops.\n");
360 exit(1);
361 }
362 opt_udp_io = true;
363 break;
364
365 case OPT_BINARY:
366 opt_binary = true;
367 break;
368
369 case OPT_VERBOSE: /* --verbose or -v */
370 opt_verbose = OPT_VERBOSE;
371 break;
372
373 case OPT_DEBUG: /* --debug or -d */
374 opt_verbose = OPT_DEBUG;
375 break;
376
377 case OPT_VERSION: /* --version or -V */
378 opt_version = true;
379 break;
380
381 case OPT_HELP: /* --help or -h */
382 opt_help = true;
383 break;
384
385 case OPT_SERVERS: /* --servers or -s */
386 opt_servers = strdup(optarg);
387 break;
388
389 case OPT_SLAP_TEST:
390 if (strcmp(optarg, "get") == 0) {
391 if (opt_udp_io == 1) {
392 fprintf(stderr,
393 "You can not run a get test in UDP mode. UDP mode "
394 "does not currently support get ops.\n");
395 exit(EXIT_FAILURE);
396 }
397 opt_test = GET_TEST;
398 } else if (strcmp(optarg, "set") == 0) {
399 opt_test = SET_TEST;
400 } else if (strcmp(optarg, "mget") == 0) {
401 opt_test = MGET_TEST;
402 } else {
403 fprintf(stderr, "Your test, %s, is not a known test\n", optarg);
404 exit(EXIT_FAILURE);
405 }
406 break;
407
408 case OPT_SLAP_CONCURRENCY:
409 errno = 0;
410 opt_concurrency = (unsigned int) strtoul(optarg, (char **) NULL, 10);
411 if (errno != 0) {
412 fprintf(stderr, "Invalid value for concurrency: %s\n", optarg);
413 exit(EXIT_FAILURE);
414 }
415 break;
416
417 case OPT_SLAP_EXECUTE_NUMBER:
418 errno = 0;
419 opt_execute_number = (unsigned int) strtoul(optarg, (char **) NULL, 10);
420 if (errno != 0) {
421 fprintf(stderr, "Invalid value for execute: %s\n", optarg);
422 exit(EXIT_FAILURE);
423 }
424 break;
425
426 case OPT_SLAP_INITIAL_LOAD:
427 errno = 0;
428 opt_createial_load = (unsigned int) strtoul(optarg, (char **) NULL, 10);
429 if (errno != 0) {
430 fprintf(stderr, "Invalid value for initial load: %s\n", optarg);
431 exit(EXIT_FAILURE);
432 }
433 break;
434
435 case OPT_QUIET:
436 close_stdio();
437 break;
438
439 case '?':
440 /* getopt_long already printed an error message. */
441 exit(EXIT_FAILURE);
442
443 default:
444 abort();
445 }
446 }
447
448 if (opt_version) {
449 version_command(PROGRAM_NAME);
450 exit(EXIT_SUCCESS);
451 }
452
453 if (opt_help) {
454 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
455 exit(EXIT_SUCCESS);
456 }
457
458 if ((opt_test == GET_TEST or opt_test == MGET_TEST) and opt_createial_load == 0)
459 opt_createial_load = DEFAULT_INITIAL_LOAD;
460
461 if (opt_execute_number == 0)
462 opt_execute_number = DEFAULT_EXECUTE_NUMBER;
463
464 if (opt_concurrency == 0)
465 opt_concurrency = DEFAULT_CONCURRENCY;
466 }
467
468 void conclusions_print(conclusions_st *conclusion) {
469 printf("\tThreads connecting to servers %u\n", opt_concurrency);
470 #ifdef NOT_FINISHED
471 printf("\tLoaded %u rows\n", conclusion->rows_loaded);
472 printf("\tRead %u rows\n", conclusion->rows_read);
473 #endif
474 if (opt_test == SET_TEST)
475 printf("\tTook %ld.%03ld seconds to load data\n", conclusion->load_time / 1000,
476 conclusion->load_time % 1000);
477 else
478 printf("\tTook %ld.%03ld seconds to read data\n", conclusion->read_time / 1000,
479 conclusion->read_time % 1000);
480 }
481
482 void flush_all(memcached_st *memc) {
483 memcached_flush(memc, 0);
484 }
485
486 pairs_st *load_create_data(memcached_st *memc, unsigned int number_of,
487 unsigned int *actual_loaded) {
488 memcached_st *memc_clone = memcached_clone(NULL, memc);
489 /* We always used non-blocking IO for load since it is faster */
490 memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
491
492 pairs_st *pairs = pairs_generate(number_of, VALUE_BYTES);
493 *actual_loaded = execute_set(memc_clone, pairs, number_of);
494
495 memcached_free(memc_clone);
496
497 return pairs;
498 }