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_verbose
= 0;
71 static int opt_flush
= 0;
72 static int opt_non_blocking_io
= 0;
73 static int opt_tcp_nodelay
= 0;
74 static unsigned int opt_execute_number
= 0;
75 static unsigned int opt_createial_load
= 0;
76 static unsigned int opt_concurrency
= 0;
77 static int opt_displayflag
= 0;
78 static char *opt_servers
= NULL
;
79 test_type opt_test
= SET_TEST
;
81 int main(int argc
, char *argv
[])
83 conclusions_st conclusion
;
84 memcached_server_st
*servers
;
86 memset(&conclusion
, 0, sizeof(conclusions_st
));
89 options_parse(argc
, argv
);
95 if ((temp
= getenv("MEMCACHED_SERVERS")))
96 opt_servers
= strdup(temp
);
99 fprintf(stderr
, "No Servers provided\n");
104 servers
= memcached_servers_parse(opt_servers
);
106 pthread_mutex_init(&counter_mutex
, NULL
);
107 pthread_cond_init(&count_threshhold
, NULL
);
108 pthread_mutex_init(&sleeper_mutex
, NULL
);
109 pthread_cond_init(&sleep_threshhold
, NULL
);
111 scheduler(servers
, &conclusion
);
115 (void)pthread_mutex_destroy(&counter_mutex
);
116 (void)pthread_cond_destroy(&count_threshhold
);
117 (void)pthread_mutex_destroy(&sleeper_mutex
);
118 (void)pthread_cond_destroy(&sleep_threshhold
);
119 conclusions_print(&conclusion
);
120 memcached_server_list_free(servers
);
125 void scheduler(memcached_server_st
*servers
, conclusions_st
*conclusion
)
128 unsigned int actual_loaded
= 0; /* Fix warning */
131 struct timeval start_time
, end_time
;
132 pthread_t mainthread
; /* Thread descriptor */
133 pthread_attr_t attr
; /* Thread attributes */
134 pairs_st
*pairs
= NULL
;
136 pthread_attr_init(&attr
);
137 pthread_attr_setdetachstate(&attr
,
138 PTHREAD_CREATE_DETACHED
);
140 memc
= memcached_create(NULL
);
141 memcached_server_push(memc
, servers
);
145 if (opt_createial_load
)
146 pairs
= load_create_data(memc
, opt_createial_load
, &actual_loaded
);
148 /* We set this after we have loaded */
150 if (opt_non_blocking_io
)
151 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
153 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
157 pthread_mutex_lock(&counter_mutex
);
160 pthread_mutex_lock(&sleeper_mutex
);
162 pthread_mutex_unlock(&sleeper_mutex
);
164 for (x
= 0; x
< opt_concurrency
; x
++)
166 thread_context_st
*context
;
167 context
= (thread_context_st
*)malloc(sizeof(thread_context_st
));
168 memset(context
, 0, sizeof(thread_context_st
));
170 context
->memc
= memcached_clone(NULL
, memc
);
171 context
->test
= opt_test
;
173 context
->initial_pairs
= pairs
;
174 context
->initial_number
= actual_loaded
;
176 if (opt_test
== SET_TEST
)
178 context
->execute_pairs
= pairs_generate(opt_execute_number
, 400);
179 context
->execute_number
= opt_execute_number
;
182 /* now you create the thread */
183 if (pthread_create(&mainthread
, &attr
, run_task
,
184 (void *)context
) != 0)
186 fprintf(stderr
,"Could not create thread\n");
192 pthread_mutex_unlock(&counter_mutex
);
193 pthread_attr_destroy(&attr
);
195 pthread_mutex_lock(&sleeper_mutex
);
197 pthread_mutex_unlock(&sleeper_mutex
);
198 pthread_cond_broadcast(&sleep_threshhold
);
200 gettimeofday(&start_time
, NULL
);
202 We loop until we know that all children have cleaned up.
204 pthread_mutex_lock(&counter_mutex
);
205 while (thread_counter
)
207 struct timespec abstime
;
209 memset(&abstime
, 0, sizeof(struct timespec
));
212 pthread_cond_timedwait(&count_threshhold
, &counter_mutex
, &abstime
);
214 pthread_mutex_unlock(&counter_mutex
);
216 gettimeofday(&end_time
, NULL
);
218 conclusion
->load_time
= timedif(end_time
, start_time
);
219 conclusion
->read_time
= timedif(end_time
, start_time
);
221 memcached_free(memc
);
224 void options_parse(int argc
, char *argv
[])
226 memcached_programs_help_st help_options
[]=
231 static struct option long_options
[]=
233 {"concurrency", required_argument
, NULL
, OPT_SLAP_CONCURRENCY
},
234 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
235 {"execute-number", required_argument
, NULL
, OPT_SLAP_EXECUTE_NUMBER
},
236 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
237 {"flush", no_argument
, &opt_flush
, OPT_FLUSH
},
238 {"help", no_argument
, NULL
, OPT_HELP
},
239 {"initial-load", required_argument
, NULL
, OPT_SLAP_INITIAL_LOAD
}, /* Number to load initially */
240 {"non-blocking", no_argument
, &opt_non_blocking_io
, OPT_SLAP_NON_BLOCK
},
241 {"servers", required_argument
, NULL
, OPT_SERVERS
},
242 {"tcp-nodelay", no_argument
, &opt_tcp_nodelay
, OPT_SLAP_TCP_NODELAY
},
243 {"test", required_argument
, NULL
, OPT_SLAP_TEST
},
244 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
245 {"version", no_argument
, NULL
, OPT_VERSION
},
254 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
255 if (option_rv
== -1) break;
260 case OPT_VERBOSE
: /* --verbose or -v */
261 opt_verbose
= OPT_VERBOSE
;
263 case OPT_DEBUG
: /* --debug or -d */
264 opt_verbose
= OPT_DEBUG
;
266 case OPT_VERSION
: /* --version or -V */
267 version_command(PROGRAM_NAME
);
269 case OPT_HELP
: /* --help or -h */
270 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
272 case OPT_SERVERS
: /* --servers or -s */
273 opt_servers
= strdup(optarg
);
276 if (!strcmp(optarg
, "get"))
278 else if (!strcmp(optarg
, "set"))
282 fprintf(stderr
, "Your test, %s, is not a known test\n", optarg
);
286 case OPT_SLAP_CONCURRENCY
:
287 opt_concurrency
= strtol(optarg
, (char **)NULL
, 10);
288 case OPT_SLAP_EXECUTE_NUMBER
:
289 opt_execute_number
= strtol(optarg
, (char **)NULL
, 10);
291 case OPT_SLAP_INITIAL_LOAD
:
292 opt_createial_load
= strtol(optarg
, (char **)NULL
, 10);
295 /* getopt_long already printed an error message. */
302 if (opt_test
== GET_TEST
&& opt_createial_load
== 0)
303 opt_createial_load
= DEFAULT_INITIAL_LOAD
;
305 if (opt_execute_number
== 0)
306 opt_execute_number
= DEFAULT_EXECUTE_NUMBER
;
308 if (opt_concurrency
== 0)
309 opt_concurrency
= DEFAULT_CONCURRENCY
;
312 void conclusions_print(conclusions_st
*conclusion
)
314 printf("\tThreads connecting to servers %u\n", opt_concurrency
);
316 printf("\tLoaded %u rows\n", conclusion
->rows_loaded
);
317 printf("\tRead %u rows\n", conclusion
->rows_read
);
319 if (opt_test
== SET_TEST
)
320 printf("\tTook %ld.%03ld seconds to load data\n", conclusion
->load_time
/ 1000,
321 conclusion
->load_time
% 1000);
323 printf("\tTook %ld.%03ld seconds to read data\n", conclusion
->read_time
/ 1000,
324 conclusion
->read_time
% 1000);
327 void *run_task(void *p
)
329 thread_context_st
*context
= (thread_context_st
*)p
;
334 pthread_mutex_lock(&sleeper_mutex
);
335 while (master_wakeup
)
337 pthread_cond_wait(&sleep_threshhold
, &sleeper_mutex
);
339 pthread_mutex_unlock(&sleeper_mutex
);
342 switch (context
->test
)
345 execute_set(memc
, context
->execute_pairs
, context
->execute_number
);
348 execute_get(memc
, context
->initial_pairs
, context
->initial_number
);
352 memcached_free(memc
);
354 if (context
->execute_pairs
)
355 pairs_free(context
->execute_pairs
);
359 pthread_mutex_lock(&counter_mutex
);
361 pthread_cond_signal(&count_threshhold
);
362 pthread_mutex_unlock(&counter_mutex
);
367 void flush_all(memcached_st
*memc
)
369 memcached_flush(memc
, 0);
372 pairs_st
*load_create_data(memcached_st
*memc
, unsigned int number_of
,
373 unsigned int *actual_loaded
)
378 clone
= memcached_clone(NULL
, memc
);
379 /* We always used non-blocking IO for load since it is faster */
380 memcached_behavior_set(clone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
382 pairs
= pairs_generate(number_of
, 400);
383 *actual_loaded
= execute_set(clone
, pairs
, number_of
);
385 memcached_free(clone
);