12 #include <memcached.h>
14 #include "client_options.h"
15 #include "utilities.h"
16 #include "generator.h"
19 #define DEFAULT_INITIAL_LOAD 10000
20 #define DEFAULT_EXECUTE_NUMBER 10000
21 #define DEFAULT_CONCURRENCY 1
23 #define PROGRAM_NAME "memslap"
24 #define PROGRAM_DESCRIPTION "Generates a load against a memcached custer of servers."
26 /* Global Thread counter */
27 unsigned int thread_counter
;
28 pthread_mutex_t counter_mutex
;
29 pthread_cond_t count_threshhold
;
30 unsigned int master_wakeup
;
31 pthread_mutex_t sleeper_mutex
;
32 pthread_cond_t sleep_threshhold
;
34 void *run_task(void *p
);
37 typedef struct conclusions_st conclusions_st
;
38 typedef struct thread_context_st thread_context_st
;
44 struct thread_context_st
{
45 unsigned int key_count
;
46 pairs_st
*initial_pairs
;
47 unsigned int initial_number
;
48 pairs_st
*execute_pairs
;
49 unsigned int execute_number
;
51 memcached_server_st
*servers
;
54 struct conclusions_st
{
57 unsigned int rows_loaded
;
58 unsigned int rows_read
;
62 void options_parse(int argc
, char *argv
[]);
63 void conclusions_print(conclusions_st
*conclusion
);
64 void scheduler(memcached_server_st
*servers
, conclusions_st
*conclusion
);
65 pairs_st
*load_initial_data(memcached_server_st
*servers
, unsigned int number_of
,
66 unsigned int *actual_loaded
);
68 static int opt_verbose
= 0;
69 static unsigned int opt_execute_number
= 0;
70 static unsigned int opt_initial_load
= 0;
71 static unsigned int opt_concurrency
= 0;
72 static int opt_displayflag
= 0;
73 static char *opt_servers
= NULL
;
74 test_type opt_test
= SET_TEST
;
76 int main(int argc
, char *argv
[])
78 conclusions_st conclusion
;
79 memcached_server_st
*servers
;
81 memset(&conclusion
, 0, sizeof(conclusions_st
));
84 options_parse(argc
, argv
);
89 servers
= parse_opt_servers(opt_servers
);
91 pthread_mutex_init(&counter_mutex
, NULL
);
92 pthread_cond_init(&count_threshhold
, NULL
);
93 pthread_mutex_init(&sleeper_mutex
, NULL
);
94 pthread_cond_init(&sleep_threshhold
, NULL
);
96 scheduler(servers
, &conclusion
);
100 (void)pthread_mutex_init(&counter_mutex
, NULL
);
101 (void)pthread_cond_init(&count_threshhold
, NULL
);
102 (void)pthread_mutex_init(&sleeper_mutex
, NULL
);
103 (void)pthread_cond_init(&sleep_threshhold
, NULL
);
104 conclusions_print(&conclusion
);
105 memcached_server_list_free(servers
);
110 void scheduler(memcached_server_st
*servers
, conclusions_st
*conclusion
)
113 unsigned int actual_loaded
;
115 struct timeval start_time
, end_time
;
116 pthread_t mainthread
; /* Thread descriptor */
117 pthread_attr_t attr
; /* Thread attributes */
118 pairs_st
*pairs
= NULL
;
120 pthread_attr_init(&attr
);
121 pthread_attr_setdetachstate(&attr
,
122 PTHREAD_CREATE_DETACHED
);
124 if (opt_initial_load
)
125 pairs
= load_initial_data(servers
, opt_initial_load
, &actual_loaded
);
127 pthread_mutex_lock(&counter_mutex
);
130 pthread_mutex_lock(&sleeper_mutex
);
132 pthread_mutex_unlock(&sleeper_mutex
);
134 for (x
= 0; x
< opt_concurrency
; x
++)
136 thread_context_st
*context
;
137 context
= (thread_context_st
*)malloc(sizeof(thread_context_st
));
139 context
->servers
= servers
;
140 context
->test
= opt_test
;
142 context
->initial_pairs
= pairs
;
143 context
->initial_number
= actual_loaded
;
145 if (opt_test
== SET_TEST
)
147 context
->execute_pairs
= pairs_generate(opt_execute_number
);
148 context
->execute_number
= opt_execute_number
;
151 /* now you create the thread */
152 if (pthread_create(&mainthread
, &attr
, run_task
,
153 (void *)context
) != 0)
155 fprintf(stderr
,"Could not create thread\n");
161 pthread_mutex_unlock(&counter_mutex
);
162 pthread_attr_destroy(&attr
);
164 pthread_mutex_lock(&sleeper_mutex
);
166 pthread_mutex_unlock(&sleeper_mutex
);
167 pthread_cond_broadcast(&sleep_threshhold
);
169 gettimeofday(&start_time
, NULL
);
171 We loop until we know that all children have cleaned up.
173 pthread_mutex_lock(&counter_mutex
);
174 while (thread_counter
)
176 struct timespec abstime
;
178 memset(&abstime
, 0, sizeof(struct timespec
));
181 pthread_cond_timedwait(&count_threshhold
, &counter_mutex
, &abstime
);
183 pthread_mutex_unlock(&counter_mutex
);
185 gettimeofday(&end_time
, NULL
);
187 conclusion
->load_time
= timedif(end_time
, start_time
);
188 conclusion
->read_time
= timedif(end_time
, start_time
);
192 void options_parse(int argc
, char *argv
[])
194 memcached_programs_help_st help_options
[]=
199 static struct option long_options
[]=
201 {"concurrency", required_argument
, NULL
, OPT_SLAP_CONCURRENCY
},
202 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
203 {"execute-number", required_argument
, NULL
, OPT_SLAP_EXECUTE_NUMBER
},
204 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
205 {"help", no_argument
, NULL
, OPT_HELP
},
206 {"initial-load", required_argument
, NULL
, OPT_SLAP_INITIAL_LOAD
}, /* Number to load initially */
207 {"servers", required_argument
, NULL
, OPT_SERVERS
},
208 {"test", required_argument
, NULL
, OPT_SLAP_TEST
},
209 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
210 {"version", no_argument
, NULL
, OPT_VERSION
},
219 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
220 if (option_rv
== -1) break;
225 case OPT_VERBOSE
: /* --verbose or -v */
226 opt_verbose
= OPT_VERBOSE
;
228 case OPT_DEBUG
: /* --debug or -d */
229 opt_verbose
= OPT_DEBUG
;
231 case OPT_VERSION
: /* --version or -V */
232 version_command(PROGRAM_NAME
);
234 case OPT_HELP
: /* --help or -h */
235 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
237 case OPT_SERVERS
: /* --servers or -s */
238 opt_servers
= strdup(optarg
);
241 if (!strcmp(optarg
, "get"))
243 else if (!strcmp(optarg
, "set"))
247 fprintf(stderr
, "Your test, %s, is not a known test\n", optarg
);
251 case OPT_SLAP_CONCURRENCY
:
252 opt_concurrency
= strtol(optarg
, (char **)NULL
, 10);
253 case OPT_SLAP_EXECUTE_NUMBER
:
254 opt_execute_number
= strtol(optarg
, (char **)NULL
, 10);
256 case OPT_SLAP_INITIAL_LOAD
:
257 opt_initial_load
= strtol(optarg
, (char **)NULL
, 10);
260 /* getopt_long already printed an error message. */
267 if (opt_test
== GET_TEST
&& opt_initial_load
== 0)
268 opt_initial_load
= DEFAULT_INITIAL_LOAD
;
270 if (opt_execute_number
== 0)
271 opt_execute_number
= DEFAULT_EXECUTE_NUMBER
;
273 if (opt_concurrency
== 0)
274 opt_concurrency
= DEFAULT_CONCURRENCY
;
277 void conclusions_print(conclusions_st
*conclusion
)
279 printf("\tThreads connecting to servers %u\n", opt_concurrency
);
281 printf("\tLoaded %u rows\n", conclusion
->rows_loaded
);
282 printf("\tRead %u rows\n", conclusion
->rows_read
);
284 if (opt_test
== SET_TEST
)
285 printf("\tTook %ld.%03ld seconds to load data\n", conclusion
->load_time
/ 1000,
286 conclusion
->load_time
% 1000);
288 printf("\tTook %ld.%03ld seconds to read data\n", conclusion
->read_time
/ 1000,
289 conclusion
->read_time
% 1000);
292 void *run_task(void *p
)
294 thread_context_st
*context
= (thread_context_st
*)p
;
297 memc
= memcached_init(NULL
);
299 memcached_server_push(memc
, context
->servers
);
301 pthread_mutex_lock(&sleeper_mutex
);
302 while (master_wakeup
)
304 pthread_cond_wait(&sleep_threshhold
, &sleeper_mutex
);
306 pthread_mutex_unlock(&sleeper_mutex
);
309 switch (context
->test
)
312 execute_set(memc
, context
->execute_pairs
, context
->execute_number
);
315 execute_get(memc
, context
->initial_pairs
, context
->initial_number
);
319 pthread_mutex_lock(&counter_mutex
);
321 pthread_cond_signal(&count_threshhold
);
322 pthread_mutex_unlock(&counter_mutex
);
323 memcached_deinit(memc
);
330 pairs_st
*load_initial_data(memcached_server_st
*servers
, unsigned int number_of
,
331 unsigned int *actual_loaded
)
336 memc
= memcached_init(NULL
);
337 memcached_server_push(memc
, servers
);
339 pairs
= pairs_generate(number_of
);
340 *actual_loaded
= execute_set(memc
, pairs
, number_of
);
342 memcached_deinit(memc
);