13 #include <libmemcached/memcached.h>
15 #include "client_options.h"
16 #include "utilities.h"
17 #include "generator.h"
20 #define DEFAULT_INITIAL_LOAD 10000
21 #define DEFAULT_EXECUTE_NUMBER 10000
22 #define DEFAULT_CONCURRENCY 1
24 #define PROGRAM_NAME "memslap"
25 #define PROGRAM_DESCRIPTION "Generates a load against a memcached custer of servers."
27 /* Global Thread counter */
28 volatile unsigned int thread_counter
;
29 pthread_mutex_t counter_mutex
;
30 pthread_cond_t count_threshhold
;
31 volatile unsigned int master_wakeup
;
32 pthread_mutex_t sleeper_mutex
;
33 pthread_cond_t sleep_threshhold
;
35 void *run_task(void *p
);
38 typedef struct conclusions_st conclusions_st
;
39 typedef struct thread_context_st thread_context_st
;
45 struct thread_context_st
{
46 unsigned int key_count
;
47 pairs_st
*initial_pairs
;
48 unsigned int initial_number
;
49 pairs_st
*execute_pairs
;
50 unsigned int execute_number
;
55 struct conclusions_st
{
58 unsigned int rows_loaded
;
59 unsigned int rows_read
;
63 void options_parse(int argc
, char *argv
[]);
64 void conclusions_print(conclusions_st
*conclusion
);
65 void scheduler(memcached_server_st
*servers
, conclusions_st
*conclusion
);
66 pairs_st
*load_create_data(memcached_st
*memc
, unsigned int number_of
,
67 unsigned int *actual_loaded
);
68 void flush_all(memcached_st
*memc
);
70 static int opt_binary
= 0;
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 static int opt_udp_io
= 0;
81 test_type opt_test
= SET_TEST
;
83 int main(int argc
, char *argv
[])
85 conclusions_st conclusion
;
86 memcached_server_st
*servers
;
88 memset(&conclusion
, 0, sizeof(conclusions_st
));
91 options_parse(argc
, argv
);
97 if ((temp
= getenv("MEMCACHED_SERVERS")))
98 opt_servers
= strdup(temp
);
101 fprintf(stderr
, "No Servers provided\n");
106 servers
= memcached_servers_parse(opt_servers
);
108 pthread_mutex_init(&counter_mutex
, NULL
);
109 pthread_cond_init(&count_threshhold
, NULL
);
110 pthread_mutex_init(&sleeper_mutex
, NULL
);
111 pthread_cond_init(&sleep_threshhold
, NULL
);
113 scheduler(servers
, &conclusion
);
117 (void)pthread_mutex_destroy(&counter_mutex
);
118 (void)pthread_cond_destroy(&count_threshhold
);
119 (void)pthread_mutex_destroy(&sleeper_mutex
);
120 (void)pthread_cond_destroy(&sleep_threshhold
);
121 conclusions_print(&conclusion
);
122 memcached_server_list_free(servers
);
127 void scheduler(memcached_server_st
*servers
, conclusions_st
*conclusion
)
130 unsigned int actual_loaded
= 0; /* Fix warning */
133 struct timeval start_time
, end_time
;
134 pthread_t mainthread
; /* Thread descriptor */
135 pthread_attr_t attr
; /* Thread attributes */
136 pairs_st
*pairs
= NULL
;
138 pthread_attr_init(&attr
);
139 pthread_attr_setdetachstate(&attr
,
140 PTHREAD_CREATE_DETACHED
);
142 memc
= memcached_create(NULL
);
144 /* We need to set udp behavior before adding servers to the client */
147 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, opt_udp_io
);
149 for(x
= 0; x
< servers
[0].count
; x
++ )
150 servers
[x
].type
= MEMCACHED_CONNECTION_UDP
;
152 memcached_server_push(memc
, servers
);
154 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, opt_binary
);
158 if (opt_createial_load
)
159 pairs
= load_create_data(memc
, opt_createial_load
, &actual_loaded
);
161 /* We set this after we have loaded */
163 if (opt_non_blocking_io
)
164 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
166 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
170 pthread_mutex_lock(&counter_mutex
);
173 pthread_mutex_lock(&sleeper_mutex
);
175 pthread_mutex_unlock(&sleeper_mutex
);
177 for (x
= 0; x
< opt_concurrency
; x
++)
179 thread_context_st
*context
;
180 context
= (thread_context_st
*)malloc(sizeof(thread_context_st
));
181 memset(context
, 0, sizeof(thread_context_st
));
183 context
->memc
= memcached_clone(NULL
, memc
);
184 context
->test
= opt_test
;
186 context
->initial_pairs
= pairs
;
187 context
->initial_number
= actual_loaded
;
189 if (opt_test
== SET_TEST
)
191 context
->execute_pairs
= pairs_generate(opt_execute_number
, 400);
192 context
->execute_number
= opt_execute_number
;
195 /* now you create the thread */
196 if (pthread_create(&mainthread
, &attr
, run_task
,
197 (void *)context
) != 0)
199 fprintf(stderr
,"Could not create thread\n");
205 pthread_mutex_unlock(&counter_mutex
);
206 pthread_attr_destroy(&attr
);
208 pthread_mutex_lock(&sleeper_mutex
);
210 pthread_mutex_unlock(&sleeper_mutex
);
211 pthread_cond_broadcast(&sleep_threshhold
);
213 gettimeofday(&start_time
, NULL
);
215 We loop until we know that all children have cleaned up.
217 pthread_mutex_lock(&counter_mutex
);
218 while (thread_counter
)
219 pthread_cond_wait(&count_threshhold
, &counter_mutex
);
220 pthread_mutex_unlock(&counter_mutex
);
222 gettimeofday(&end_time
, NULL
);
224 conclusion
->load_time
= timedif(end_time
, start_time
);
225 conclusion
->read_time
= timedif(end_time
, start_time
);
227 memcached_free(memc
);
230 void options_parse(int argc
, char *argv
[])
232 memcached_programs_help_st help_options
[]=
237 static struct option long_options
[]=
239 {"concurrency", required_argument
, NULL
, OPT_SLAP_CONCURRENCY
},
240 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
241 {"execute-number", required_argument
, NULL
, OPT_SLAP_EXECUTE_NUMBER
},
242 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
243 {"flush", no_argument
, &opt_flush
, OPT_FLUSH
},
244 {"help", no_argument
, NULL
, OPT_HELP
},
245 {"initial-load", required_argument
, NULL
, OPT_SLAP_INITIAL_LOAD
}, /* Number to load initially */
246 {"non-blocking", no_argument
, &opt_non_blocking_io
, OPT_SLAP_NON_BLOCK
},
247 {"servers", required_argument
, NULL
, OPT_SERVERS
},
248 {"tcp-nodelay", no_argument
, &opt_tcp_nodelay
, OPT_SLAP_TCP_NODELAY
},
249 {"test", required_argument
, NULL
, OPT_SLAP_TEST
},
250 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
251 {"version", no_argument
, NULL
, OPT_VERSION
},
252 {"binary", no_argument
, NULL
, OPT_BINARY
},
253 {"udp", no_argument
, NULL
, OPT_UDP
},
262 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
263 if (option_rv
== -1) break;
269 if (opt_test
== GET_TEST
)
271 fprintf(stderr
, "You can not run a get test in UDP mode. UDP mode "
272 "does not currently support get ops.\n");
280 case OPT_VERBOSE
: /* --verbose or -v */
281 opt_verbose
= OPT_VERBOSE
;
283 case OPT_DEBUG
: /* --debug or -d */
284 opt_verbose
= OPT_DEBUG
;
286 case OPT_VERSION
: /* --version or -V */
287 version_command(PROGRAM_NAME
);
289 case OPT_HELP
: /* --help or -h */
290 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
292 case OPT_SERVERS
: /* --servers or -s */
293 opt_servers
= strdup(optarg
);
296 if (!strcmp(optarg
, "get"))
300 fprintf(stderr
, "You can not run a get test in UDP mode. UDP mode "
301 "does not currently support get ops.\n");
306 else if (!strcmp(optarg
, "set"))
310 fprintf(stderr
, "Your test, %s, is not a known test\n", optarg
);
314 case OPT_SLAP_CONCURRENCY
:
315 opt_concurrency
= strtol(optarg
, (char **)NULL
, 10);
317 case OPT_SLAP_EXECUTE_NUMBER
:
318 opt_execute_number
= strtol(optarg
, (char **)NULL
, 10);
320 case OPT_SLAP_INITIAL_LOAD
:
321 opt_createial_load
= strtol(optarg
, (char **)NULL
, 10);
324 /* getopt_long already printed an error message. */
331 if (opt_test
== GET_TEST
&& opt_createial_load
== 0)
332 opt_createial_load
= DEFAULT_INITIAL_LOAD
;
334 if (opt_execute_number
== 0)
335 opt_execute_number
= DEFAULT_EXECUTE_NUMBER
;
337 if (opt_concurrency
== 0)
338 opt_concurrency
= DEFAULT_CONCURRENCY
;
341 void conclusions_print(conclusions_st
*conclusion
)
343 printf("\tThreads connecting to servers %u\n", opt_concurrency
);
345 printf("\tLoaded %u rows\n", conclusion
->rows_loaded
);
346 printf("\tRead %u rows\n", conclusion
->rows_read
);
348 if (opt_test
== SET_TEST
)
349 printf("\tTook %ld.%03ld seconds to load data\n", conclusion
->load_time
/ 1000,
350 conclusion
->load_time
% 1000);
352 printf("\tTook %ld.%03ld seconds to read data\n", conclusion
->read_time
/ 1000,
353 conclusion
->read_time
% 1000);
356 void *run_task(void *p
)
358 thread_context_st
*context
= (thread_context_st
*)p
;
363 pthread_mutex_lock(&sleeper_mutex
);
364 while (master_wakeup
)
366 pthread_cond_wait(&sleep_threshhold
, &sleeper_mutex
);
368 pthread_mutex_unlock(&sleeper_mutex
);
371 switch (context
->test
)
374 execute_set(memc
, context
->execute_pairs
, context
->execute_number
);
377 execute_get(memc
, context
->initial_pairs
, context
->initial_number
);
381 memcached_free(memc
);
383 if (context
->execute_pairs
)
384 pairs_free(context
->execute_pairs
);
388 pthread_mutex_lock(&counter_mutex
);
390 pthread_cond_signal(&count_threshhold
);
391 pthread_mutex_unlock(&counter_mutex
);
396 void flush_all(memcached_st
*memc
)
398 memcached_flush(memc
, 0);
401 pairs_st
*load_create_data(memcached_st
*memc
, unsigned int number_of
,
402 unsigned int *actual_loaded
)
407 clone
= memcached_clone(NULL
, memc
);
408 /* We always used non-blocking IO for load since it is faster */
409 memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
411 pairs
= pairs_generate(number_of
, 400);
412 *actual_loaded
= execute_set(clone
, pairs
, number_of
);
414 memcached_free(clone
);