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 +--------------------------------------------------------------------+
16 #include "mem_config.h"
30 #include <sys/types.h>
35 #include "libmemcached-1.0/memcached.h"
37 #include "client_options.h"
38 #include "utilities.h"
39 #include "generator.h"
42 #define DEFAULT_INITIAL_LOAD 10000
43 #define DEFAULT_EXECUTE_NUMBER 10000
44 #define DEFAULT_CONCURRENCY 1
46 #define VALUE_BYTES 4096
48 #define PROGRAM_NAME "memslap"
49 #define PROGRAM_DESCRIPTION "Generates a load against a memcached custer of servers."
51 /* Global Thread counter */
52 volatile unsigned int master_wakeup
;
53 pthread_mutex_t sleeper_mutex
;
54 pthread_cond_t sleep_threshhold
;
57 enum test_t
{ SET_TEST
, GET_TEST
, MGET_TEST
};
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
;
69 const memcached_st
*root
;
71 thread_context_st(const memcached_st
*memc_arg
, test_t test_arg
)
84 memc
= memcached_clone(NULL
, root
);
87 ~thread_context_st() {
89 pairs_free(execute_pairs
);
95 struct conclusions_st
{
98 unsigned int rows_loaded
;
99 unsigned int rows_read
;
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
);
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
;
130 static __attribute__((noreturn
)) void *run_task(void *p
) {
131 thread_context_st
*context
= (thread_context_st
*) p
;
135 pthread_mutex_lock(&sleeper_mutex
);
136 while (master_wakeup
) {
137 pthread_cond_wait(&sleep_threshhold
, &sleeper_mutex
);
139 pthread_mutex_unlock(&sleeper_mutex
);
142 switch (context
->test
) {
144 assert(context
->execute_pairs
);
145 execute_set(context
->memc
, context
->execute_pairs
, context
->execute_number
);
149 execute_get(context
->memc
, context
->initial_pairs
, context
->initial_number
);
153 execute_mget(context
->memc
, (const char *const *) context
->keys
, context
->key_lengths
,
154 context
->initial_number
);
164 int main(int argc
, char *argv
[]) {
165 conclusions_st conclusion
;
167 srandom((unsigned int) time(NULL
));
168 options_parse(argc
, argv
);
170 if (opt_servers
== NULL
) {
173 if ((temp
= getenv("MEMCACHED_SERVERS"))) {
174 opt_servers
= strdup(temp
);
177 if (opt_servers
== NULL
) {
178 std::cerr
<< "No servers provided" << std::endl
;
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
;
189 pthread_mutex_init(&sleeper_mutex
, NULL
);
190 pthread_cond_init(&sleep_threshhold
, NULL
);
192 int error_code
= EXIT_SUCCESS
;
194 scheduler(servers
, &conclusion
);
195 } catch (std::exception
&e
) {
196 std::cerr
<< "Died with exception: " << e
.what() << std::endl
;
197 error_code
= EXIT_FAILURE
;
202 (void) pthread_mutex_destroy(&sleeper_mutex
);
203 (void) pthread_cond_destroy(&sleep_threshhold
);
204 conclusions_print(&conclusion
);
205 memcached_server_list_free(servers
);
210 void scheduler(memcached_server_st
*servers
, conclusions_st
*conclusion
) {
211 unsigned int actual_loaded
= 0; /* Fix warning */
213 struct timeval start_time
, end_time
;
214 pairs_st
*pairs
= NULL
;
216 memcached_st
*memc
= memcached_create(NULL
);
218 memcached_server_push(memc
, servers
);
220 /* We need to set udp behavior before adding servers to the client */
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
);
229 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, (uint64_t) opt_binary
);
235 if (opt_createial_load
) {
236 pairs
= load_create_data(memc
, opt_createial_load
, &actual_loaded
);
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)));
242 if (keys
== NULL
or key_lengths
== NULL
) {
248 for (uint32_t x
= 0; x
< actual_loaded
; ++x
) {
249 keys
[x
] = pairs
[x
].key
;
250 key_lengths
[x
] = pairs
[x
].key_length
;
254 /* We set this after we have loaded */
256 if (opt_non_blocking_io
)
257 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
260 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
263 pthread_mutex_lock(&sleeper_mutex
);
265 pthread_mutex_unlock(&sleeper_mutex
);
267 pthread_t
*threads
= new (std::nothrow
) pthread_t
[opt_concurrency
];
269 if (threads
== NULL
) {
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
;
277 context
->initial_pairs
= pairs
;
278 context
->initial_number
= actual_loaded
;
279 context
->keys
= keys
;
280 context
->key_lengths
= key_lengths
;
282 if (opt_test
== SET_TEST
) {
283 context
->execute_pairs
= pairs_generate(opt_execute_number
, VALUE_BYTES
);
284 context
->execute_number
= opt_execute_number
;
287 /* now you create the thread */
288 if (pthread_create(threads
+ x
, NULL
, run_task
, (void *) context
)) {
289 fprintf(stderr
, "Could not create thread\n");
294 pthread_mutex_lock(&sleeper_mutex
);
296 pthread_mutex_unlock(&sleeper_mutex
);
297 pthread_cond_broadcast(&sleep_threshhold
);
298 gettimeofday(&start_time
, NULL
);
300 for (uint32_t x
= 0; x
< opt_concurrency
; x
++) {
302 pthread_join(threads
[x
], &retval
);
306 gettimeofday(&end_time
, NULL
);
308 conclusion
->load_time
= timedif(end_time
, start_time
);
309 conclusion
->read_time
= timedif(end_time
, start_time
);
313 memcached_free(memc
);
316 void options_parse(int argc
, char *argv
[]) {
317 memcached_programs_help_st help_options
[] = {
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
},
342 bool opt_help
= false;
343 bool opt_version
= false;
344 int option_index
= 0;
346 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
356 if (opt_test
== GET_TEST
) {
358 "You can not run a get test in UDP mode. UDP mode "
359 "does not currently support get ops.\n");
369 case OPT_VERBOSE
: /* --verbose or -v */
370 opt_verbose
= OPT_VERBOSE
;
373 case OPT_DEBUG
: /* --debug or -d */
374 opt_verbose
= OPT_DEBUG
;
377 case OPT_VERSION
: /* --version or -V */
381 case OPT_HELP
: /* --help or -h */
385 case OPT_SERVERS
: /* --servers or -s */
386 opt_servers
= strdup(optarg
);
390 if (strcmp(optarg
, "get") == 0) {
391 if (opt_udp_io
== 1) {
393 "You can not run a get test in UDP mode. UDP mode "
394 "does not currently support get ops.\n");
398 } else if (strcmp(optarg
, "set") == 0) {
400 } else if (strcmp(optarg
, "mget") == 0) {
401 opt_test
= MGET_TEST
;
403 fprintf(stderr
, "Your test, %s, is not a known test\n", optarg
);
408 case OPT_SLAP_CONCURRENCY
:
410 opt_concurrency
= (unsigned int) strtoul(optarg
, (char **) NULL
, 10);
412 fprintf(stderr
, "Invalid value for concurrency: %s\n", optarg
);
417 case OPT_SLAP_EXECUTE_NUMBER
:
419 opt_execute_number
= (unsigned int) strtoul(optarg
, (char **) NULL
, 10);
421 fprintf(stderr
, "Invalid value for execute: %s\n", optarg
);
426 case OPT_SLAP_INITIAL_LOAD
:
428 opt_createial_load
= (unsigned int) strtoul(optarg
, (char **) NULL
, 10);
430 fprintf(stderr
, "Invalid value for initial load: %s\n", optarg
);
440 /* getopt_long already printed an error message. */
449 version_command(PROGRAM_NAME
);
454 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
458 if ((opt_test
== GET_TEST
or opt_test
== MGET_TEST
) and opt_createial_load
== 0)
459 opt_createial_load
= DEFAULT_INITIAL_LOAD
;
461 if (opt_execute_number
== 0)
462 opt_execute_number
= DEFAULT_EXECUTE_NUMBER
;
464 if (opt_concurrency
== 0)
465 opt_concurrency
= DEFAULT_CONCURRENCY
;
468 void conclusions_print(conclusions_st
*conclusion
) {
469 printf("\tThreads connecting to servers %u\n", opt_concurrency
);
471 printf("\tLoaded %u rows\n", conclusion
->rows_loaded
);
472 printf("\tRead %u rows\n", conclusion
->rows_read
);
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);
478 printf("\tTook %ld.%03ld seconds to read data\n", conclusion
->read_time
/ 1000,
479 conclusion
->read_time
% 1000);
482 void flush_all(memcached_st
*memc
) {
483 memcached_flush(memc
, 0);
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);
492 pairs_st
*pairs
= pairs_generate(number_of
, VALUE_BYTES
);
493 *actual_loaded
= execute_set(memc_clone
, pairs
, number_of
);
495 memcached_free(memc_clone
);