13 #include <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 unsigned int thread_counter
;
29 pthread_mutex_t counter_mutex
;
30 pthread_cond_t count_threshhold
;
31 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
;
52 memcached_server_st
*servers
;
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_server_st
*servers
, unsigned int number_of
,
67 unsigned int *actual_loaded
);
68 void flush_all(memcached_server_st
*servers
);
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
);
101 servers
= memcached_servers_parse(opt_servers
);
103 pthread_mutex_init(&counter_mutex
, NULL
);
104 pthread_cond_init(&count_threshhold
, NULL
);
105 pthread_mutex_init(&sleeper_mutex
, NULL
);
106 pthread_cond_init(&sleep_threshhold
, NULL
);
108 scheduler(servers
, &conclusion
);
112 (void)pthread_mutex_destroy(&counter_mutex
);
113 (void)pthread_cond_destroy(&count_threshhold
);
114 (void)pthread_mutex_destroy(&sleeper_mutex
);
115 (void)pthread_cond_destroy(&sleep_threshhold
);
116 conclusions_print(&conclusion
);
117 memcached_server_list_free(servers
);
122 void scheduler(memcached_server_st
*servers
, conclusions_st
*conclusion
)
125 unsigned int actual_loaded
;
127 struct timeval start_time
, end_time
;
128 pthread_t mainthread
; /* Thread descriptor */
129 pthread_attr_t attr
; /* Thread attributes */
130 pairs_st
*pairs
= NULL
;
132 pthread_attr_init(&attr
);
133 pthread_attr_setdetachstate(&attr
,
134 PTHREAD_CREATE_DETACHED
);
138 if (opt_createial_load
)
139 pairs
= load_create_data(servers
, opt_createial_load
, &actual_loaded
);
141 pthread_mutex_lock(&counter_mutex
);
144 pthread_mutex_lock(&sleeper_mutex
);
146 pthread_mutex_unlock(&sleeper_mutex
);
148 for (x
= 0; x
< opt_concurrency
; x
++)
150 thread_context_st
*context
;
151 context
= (thread_context_st
*)malloc(sizeof(thread_context_st
));
153 context
->servers
= servers
;
154 context
->test
= opt_test
;
156 context
->initial_pairs
= pairs
;
157 context
->initial_number
= actual_loaded
;
159 if (opt_test
== SET_TEST
)
161 context
->execute_pairs
= pairs_generate(opt_execute_number
);
162 context
->execute_number
= opt_execute_number
;
165 /* now you create the thread */
166 if (pthread_create(&mainthread
, &attr
, run_task
,
167 (void *)context
) != 0)
169 fprintf(stderr
,"Could not create thread\n");
175 pthread_mutex_unlock(&counter_mutex
);
176 pthread_attr_destroy(&attr
);
178 pthread_mutex_lock(&sleeper_mutex
);
180 pthread_mutex_unlock(&sleeper_mutex
);
181 pthread_cond_broadcast(&sleep_threshhold
);
183 gettimeofday(&start_time
, NULL
);
185 We loop until we know that all children have cleaned up.
187 pthread_mutex_lock(&counter_mutex
);
188 while (thread_counter
)
190 struct timespec abstime
;
192 memset(&abstime
, 0, sizeof(struct timespec
));
195 pthread_cond_timedwait(&count_threshhold
, &counter_mutex
, &abstime
);
197 pthread_mutex_unlock(&counter_mutex
);
199 gettimeofday(&end_time
, NULL
);
201 conclusion
->load_time
= timedif(end_time
, start_time
);
202 conclusion
->read_time
= timedif(end_time
, start_time
);
206 void options_parse(int argc
, char *argv
[])
208 memcached_programs_help_st help_options
[]=
213 static struct option long_options
[]=
215 {"concurrency", required_argument
, NULL
, OPT_SLAP_CONCURRENCY
},
216 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
217 {"execute-number", required_argument
, NULL
, OPT_SLAP_EXECUTE_NUMBER
},
218 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
219 {"flush", no_argument
, &opt_flush
, OPT_FLUSH
},
220 {"help", no_argument
, NULL
, OPT_HELP
},
221 {"initial-load", required_argument
, NULL
, OPT_SLAP_INITIAL_LOAD
}, /* Number to load initially */
222 {"non-blocking", no_argument
, &opt_non_blocking_io
, OPT_SLAP_NON_BLOCK
},
223 {"servers", required_argument
, NULL
, OPT_SERVERS
},
224 {"tcp-nodelay", no_argument
, &opt_tcp_nodelay
, OPT_SLAP_TCP_NODELAY
},
225 {"test", required_argument
, NULL
, OPT_SLAP_TEST
},
226 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
227 {"version", no_argument
, NULL
, OPT_VERSION
},
236 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
237 if (option_rv
== -1) break;
242 case OPT_VERBOSE
: /* --verbose or -v */
243 opt_verbose
= OPT_VERBOSE
;
245 case OPT_DEBUG
: /* --debug or -d */
246 opt_verbose
= OPT_DEBUG
;
248 case OPT_VERSION
: /* --version or -V */
249 version_command(PROGRAM_NAME
);
251 case OPT_HELP
: /* --help or -h */
252 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
254 case OPT_SERVERS
: /* --servers or -s */
255 opt_servers
= strdup(optarg
);
258 if (!strcmp(optarg
, "get"))
260 else if (!strcmp(optarg
, "set"))
264 fprintf(stderr
, "Your test, %s, is not a known test\n", optarg
);
268 case OPT_SLAP_CONCURRENCY
:
269 opt_concurrency
= strtol(optarg
, (char **)NULL
, 10);
270 case OPT_SLAP_EXECUTE_NUMBER
:
271 opt_execute_number
= strtol(optarg
, (char **)NULL
, 10);
273 case OPT_SLAP_INITIAL_LOAD
:
274 opt_createial_load
= strtol(optarg
, (char **)NULL
, 10);
277 /* getopt_long already printed an error message. */
284 if (opt_test
== GET_TEST
&& opt_createial_load
== 0)
285 opt_createial_load
= DEFAULT_INITIAL_LOAD
;
287 if (opt_execute_number
== 0)
288 opt_execute_number
= DEFAULT_EXECUTE_NUMBER
;
290 if (opt_concurrency
== 0)
291 opt_concurrency
= DEFAULT_CONCURRENCY
;
294 void conclusions_print(conclusions_st
*conclusion
)
296 printf("\tThreads connecting to servers %u\n", opt_concurrency
);
298 printf("\tLoaded %u rows\n", conclusion
->rows_loaded
);
299 printf("\tRead %u rows\n", conclusion
->rows_read
);
301 if (opt_test
== SET_TEST
)
302 printf("\tTook %ld.%03ld seconds to load data\n", conclusion
->load_time
/ 1000,
303 conclusion
->load_time
% 1000);
305 printf("\tTook %ld.%03ld seconds to read data\n", conclusion
->read_time
/ 1000,
306 conclusion
->read_time
% 1000);
309 void *run_task(void *p
)
311 thread_context_st
*context
= (thread_context_st
*)p
;
313 unsigned int value
= 1;
315 memc
= memcached_create(NULL
);
316 if (opt_non_blocking_io
)
317 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &value
);
319 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &value
);
321 memcached_server_push(memc
, context
->servers
);
323 pthread_mutex_lock(&sleeper_mutex
);
324 while (master_wakeup
)
326 pthread_cond_wait(&sleep_threshhold
, &sleeper_mutex
);
328 pthread_mutex_unlock(&sleeper_mutex
);
331 switch (context
->test
)
334 execute_set(memc
, context
->execute_pairs
, context
->execute_number
);
337 execute_get(memc
, context
->initial_pairs
, context
->initial_number
);
341 pthread_mutex_lock(&counter_mutex
);
343 pthread_cond_signal(&count_threshhold
);
344 pthread_mutex_unlock(&counter_mutex
);
345 memcached_free(memc
);
347 if (context
->execute_pairs
)
348 pairs_free(context
->execute_pairs
);
354 void flush_all(memcached_server_st
*servers
)
358 memc
= memcached_create(NULL
);
360 memcached_server_push(memc
, servers
);
362 memcached_flush(memc
, 0);
364 memcached_free(memc
);
367 pairs_st
*load_create_data(memcached_server_st
*servers
, unsigned int number_of
,
368 unsigned int *actual_loaded
)
373 memc
= memcached_create(NULL
);
374 /* We always used non-blocking IO for load since it is faster */
375 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
376 memcached_server_push(memc
, servers
);
378 pairs
= pairs_generate(number_of
);
379 *actual_loaded
= execute_set(memc
, pairs
, number_of
);
381 memcached_free(memc
);