1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
43 #include <sys/types.h>
45 #include <sys/types.h>
53 #include <libmemcached/memcached.h>
55 #include "client_options.h"
56 #include "utilities.h"
57 #include "generator.h"
60 #define DEFAULT_INITIAL_LOAD 10000
61 #define DEFAULT_EXECUTE_NUMBER 10000
62 #define DEFAULT_CONCURRENCY 1
64 #define PROGRAM_NAME "memslap"
65 #define PROGRAM_DESCRIPTION "Generates a load against a memcached custer of servers."
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
;
75 void *run_task(void *p
);
78 typedef struct conclusions_st conclusions_st
;
79 typedef struct thread_context_st thread_context_st
;
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
;
98 struct conclusions_st
{
101 unsigned int rows_loaded
;
102 unsigned int rows_read
;
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
);
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
;
126 int main(int argc
, char *argv
[])
128 conclusions_st conclusion
;
129 memcached_server_st
*servers
;
131 memset(&conclusion
, 0, sizeof(conclusions_st
));
133 srandom((unsigned int)time(NULL
));
134 options_parse(argc
, argv
);
140 if ((temp
= getenv("MEMCACHED_SERVERS")))
141 opt_servers
= strdup(temp
);
144 fprintf(stderr
, "No Servers provided\n");
149 servers
= memcached_servers_parse(opt_servers
);
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
);
156 scheduler(servers
, &conclusion
);
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
);
170 void scheduler(memcached_server_st
*servers
, conclusions_st
*conclusion
)
172 unsigned int actual_loaded
= 0; /* Fix warning */
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
;
180 pthread_attr_init(&attr
);
181 pthread_attr_setdetachstate(&attr
,
182 PTHREAD_CREATE_DETACHED
);
184 memc
= memcached_create(NULL
);
186 /* We need to set udp behavior before adding servers to the client */
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
++ )
193 servers
[x
].type
= MEMCACHED_CONNECTION_UDP
;
196 memcached_server_push(memc
, servers
);
198 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
199 (uint64_t)opt_binary
);
203 if (opt_createial_load
)
204 pairs
= load_create_data(memc
, opt_createial_load
, &actual_loaded
);
206 char **keys
= calloc(actual_loaded
, sizeof(char*));
207 size_t *key_lengths
= calloc(actual_loaded
, sizeof(size_t));
209 if (keys
== NULL
|| key_lengths
== NULL
)
218 for (uint32_t x
= 0; x
< actual_loaded
; ++x
)
220 keys
[x
]= pairs
[x
].key
;
221 key_lengths
[x
]= pairs
[x
].key_length
;
225 /* We set this after we have loaded */
227 if (opt_non_blocking_io
)
228 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
230 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
233 pthread_mutex_lock(&counter_mutex
);
236 pthread_mutex_lock(&sleeper_mutex
);
238 pthread_mutex_unlock(&sleeper_mutex
);
240 for (uint32_t x
= 0; x
< opt_concurrency
; x
++)
242 thread_context_st
*context
;
243 context
= (thread_context_st
*)calloc(1, sizeof(thread_context_st
));
245 context
->memc
= memcached_clone(NULL
, memc
);
246 context
->test
= opt_test
;
248 context
->initial_pairs
= pairs
;
249 context
->initial_number
= actual_loaded
;
251 context
->key_lengths
= key_lengths
;
253 if (opt_test
== SET_TEST
)
255 context
->execute_pairs
= pairs_generate(opt_execute_number
, 400);
256 context
->execute_number
= opt_execute_number
;
259 /* now you create the thread */
260 if (pthread_create(&mainthread
, &attr
, run_task
,
261 (void *)context
) != 0)
263 fprintf(stderr
,"Could not create thread\n");
269 pthread_mutex_unlock(&counter_mutex
);
270 pthread_attr_destroy(&attr
);
272 pthread_mutex_lock(&sleeper_mutex
);
274 pthread_mutex_unlock(&sleeper_mutex
);
275 pthread_cond_broadcast(&sleep_threshhold
);
277 gettimeofday(&start_time
, NULL
);
279 We loop until we know that all children have cleaned up.
281 pthread_mutex_lock(&counter_mutex
);
282 while (thread_counter
)
283 pthread_cond_wait(&count_threshhold
, &counter_mutex
);
284 pthread_mutex_unlock(&counter_mutex
);
286 gettimeofday(&end_time
, NULL
);
288 conclusion
->load_time
= timedif(end_time
, start_time
);
289 conclusion
->read_time
= timedif(end_time
, start_time
);
293 memcached_free(memc
);
296 void options_parse(int argc
, char *argv
[])
298 memcached_programs_help_st help_options
[]=
303 static struct option long_options
[]=
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
},
328 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
329 if (option_rv
== -1) break;
335 if (opt_test
== GET_TEST
)
337 fprintf(stderr
, "You can not run a get test in UDP mode. UDP mode "
338 "does not currently support get ops.\n");
346 case OPT_VERBOSE
: /* --verbose or -v */
347 opt_verbose
= OPT_VERBOSE
;
349 case OPT_DEBUG
: /* --debug or -d */
350 opt_verbose
= OPT_DEBUG
;
352 case OPT_VERSION
: /* --version or -V */
353 version_command(PROGRAM_NAME
);
355 case OPT_HELP
: /* --help or -h */
356 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
358 case OPT_SERVERS
: /* --servers or -s */
359 opt_servers
= strdup(optarg
);
362 if (!strcmp(optarg
, "get"))
366 fprintf(stderr
, "You can not run a get test in UDP mode. UDP mode "
367 "does not currently support get ops.\n");
372 else if (!strcmp(optarg
, "set"))
374 else if (!strcmp(optarg
, "mget"))
380 fprintf(stderr
, "Your test, %s, is not a known test\n", optarg
);
384 case OPT_SLAP_CONCURRENCY
:
385 opt_concurrency
= (unsigned int)strtoul(optarg
, (char **)NULL
, 10);
387 case OPT_SLAP_EXECUTE_NUMBER
:
388 opt_execute_number
= (unsigned int)strtoul(optarg
, (char **)NULL
, 10);
390 case OPT_SLAP_INITIAL_LOAD
:
391 opt_createial_load
= (unsigned int)strtoul(optarg
, (char **)NULL
, 10);
394 /* getopt_long already printed an error message. */
401 if ((opt_test
== GET_TEST
|| opt_test
== MGET_TEST
) && opt_createial_load
== 0)
402 opt_createial_load
= DEFAULT_INITIAL_LOAD
;
404 if (opt_execute_number
== 0)
405 opt_execute_number
= DEFAULT_EXECUTE_NUMBER
;
407 if (opt_concurrency
== 0)
408 opt_concurrency
= DEFAULT_CONCURRENCY
;
411 void conclusions_print(conclusions_st
*conclusion
)
413 printf("\tThreads connecting to servers %u\n", opt_concurrency
);
415 printf("\tLoaded %u rows\n", conclusion
->rows_loaded
);
416 printf("\tRead %u rows\n", conclusion
->rows_read
);
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);
422 printf("\tTook %ld.%03ld seconds to read data\n", conclusion
->read_time
/ 1000,
423 conclusion
->read_time
% 1000);
426 void *run_task(void *p
)
428 thread_context_st
*context
= (thread_context_st
*)p
;
433 pthread_mutex_lock(&sleeper_mutex
);
434 while (master_wakeup
)
436 pthread_cond_wait(&sleep_threshhold
, &sleeper_mutex
);
438 pthread_mutex_unlock(&sleeper_mutex
);
441 switch (context
->test
)
444 assert(context
->execute_pairs
);
445 execute_set(memc
, context
->execute_pairs
, context
->execute_number
);
448 execute_get(memc
, context
->initial_pairs
, context
->initial_number
);
451 execute_mget(memc
, (const char*const*)context
->keys
, context
->key_lengths
,
452 context
->initial_number
);
455 WATCHPOINT_ASSERT(context
->test
);
459 memcached_free(memc
);
461 if (context
->execute_pairs
)
462 pairs_free(context
->execute_pairs
);
466 pthread_mutex_lock(&counter_mutex
);
468 pthread_cond_signal(&count_threshhold
);
469 pthread_mutex_unlock(&counter_mutex
);
474 void flush_all(memcached_st
*memc
)
476 memcached_flush(memc
, 0);
479 pairs_st
*load_create_data(memcached_st
*memc
, unsigned int number_of
,
480 unsigned int *actual_loaded
)
482 memcached_st
*memc_clone
;
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);
489 pairs
= pairs_generate(number_of
, 400);
490 *actual_loaded
= execute_set(memc_clone
, pairs
, number_of
);
492 memcached_free(memc_clone
);