Fix warning.
[awesomized/libmemcached] / src / memslap.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <sys/mman.h>
8 #include <fcntl.h>
9 #include <sys/time.h>
10 #include <getopt.h>
11 #include <pthread.h>
12
13 #include <memcached.h>
14
15 #include "client_options.h"
16 #include "utilities.h"
17 #include "generator.h"
18 #include "execute.h"
19
20 #define DEFAULT_INITIAL_LOAD 10000
21 #define DEFAULT_EXECUTE_NUMBER 10000
22 #define DEFAULT_CONCURRENCY 1
23
24 #define PROGRAM_NAME "memslap"
25 #define PROGRAM_DESCRIPTION "Generates a load against a memcached custer of servers."
26
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;
34
35 void *run_task(void *p);
36
37 /* Types */
38 typedef struct conclusions_st conclusions_st;
39 typedef struct thread_context_st thread_context_st;
40 typedef enum {
41 SET_TEST,
42 GET_TEST,
43 } test_type;
44
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;
51 test_type test;
52 memcached_server_st *servers;
53 };
54
55 struct conclusions_st {
56 long int load_time;
57 long int read_time;
58 unsigned int rows_loaded;
59 unsigned int rows_read;
60 };
61
62 /* Prototypes */
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);
69
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;
80
81 int main(int argc, char *argv[])
82 {
83 conclusions_st conclusion;
84 memcached_server_st *servers;
85
86 memset(&conclusion, 0, sizeof(conclusions_st));
87
88 srandom(time(NULL));
89 options_parse(argc, argv);
90
91 if (!opt_servers)
92 {
93 char *temp;
94
95 if ((temp= getenv("MEMCACHED_SERVERS")))
96 opt_servers= strdup(temp);
97 else
98 exit(1);
99 }
100
101 servers= memcached_servers_parse(opt_servers);
102
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);
107
108 scheduler(servers, &conclusion);
109
110 free(opt_servers);
111
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);
118
119 return 0;
120 }
121
122 void scheduler(memcached_server_st *servers, conclusions_st *conclusion)
123 {
124 unsigned int x;
125 unsigned int actual_loaded;
126
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;
131
132 pthread_attr_init(&attr);
133 pthread_attr_setdetachstate(&attr,
134 PTHREAD_CREATE_DETACHED);
135
136 if (opt_flush)
137 flush_all(servers);
138 if (opt_createial_load)
139 pairs= load_create_data(servers, opt_createial_load, &actual_loaded);
140
141 pthread_mutex_lock(&counter_mutex);
142 thread_counter= 0;
143
144 pthread_mutex_lock(&sleeper_mutex);
145 master_wakeup= 1;
146 pthread_mutex_unlock(&sleeper_mutex);
147
148 for (x= 0; x < opt_concurrency; x++)
149 {
150 thread_context_st *context;
151 context= (thread_context_st *)malloc(sizeof(thread_context_st));
152
153 context->servers= servers;
154 context->test= opt_test;
155
156 context->initial_pairs= pairs;
157 context->initial_number= actual_loaded;
158
159 if (opt_test == SET_TEST)
160 {
161 context->execute_pairs= pairs_generate(opt_execute_number);
162 context->execute_number= opt_execute_number;
163 }
164
165 /* now you create the thread */
166 if (pthread_create(&mainthread, &attr, run_task,
167 (void *)context) != 0)
168 {
169 fprintf(stderr,"Could not create thread\n");
170 exit(1);
171 }
172 thread_counter++;
173 }
174
175 pthread_mutex_unlock(&counter_mutex);
176 pthread_attr_destroy(&attr);
177
178 pthread_mutex_lock(&sleeper_mutex);
179 master_wakeup= 0;
180 pthread_mutex_unlock(&sleeper_mutex);
181 pthread_cond_broadcast(&sleep_threshhold);
182
183 gettimeofday(&start_time, NULL);
184 /*
185 We loop until we know that all children have cleaned up.
186 */
187 pthread_mutex_lock(&counter_mutex);
188 while (thread_counter)
189 {
190 struct timespec abstime;
191
192 memset(&abstime, 0, sizeof(struct timespec));
193 abstime.tv_sec= 1;
194
195 pthread_cond_timedwait(&count_threshhold, &counter_mutex, &abstime);
196 }
197 pthread_mutex_unlock(&counter_mutex);
198
199 gettimeofday(&end_time, NULL);
200
201 conclusion->load_time= timedif(end_time, start_time);
202 conclusion->read_time= timedif(end_time, start_time);
203 pairs_free(pairs);
204 }
205
206 void options_parse(int argc, char *argv[])
207 {
208 memcached_programs_help_st help_options[]=
209 {
210 {0},
211 };
212
213 static struct option long_options[]=
214 {
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},
228 {0, 0, 0, 0},
229 };
230
231 int option_index= 0;
232 int option_rv;
233
234 while (1)
235 {
236 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
237 if (option_rv == -1) break;
238 switch (option_rv)
239 {
240 case 0:
241 break;
242 case OPT_VERBOSE: /* --verbose or -v */
243 opt_verbose = OPT_VERBOSE;
244 break;
245 case OPT_DEBUG: /* --debug or -d */
246 opt_verbose = OPT_DEBUG;
247 break;
248 case OPT_VERSION: /* --version or -V */
249 version_command(PROGRAM_NAME);
250 break;
251 case OPT_HELP: /* --help or -h */
252 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
253 break;
254 case OPT_SERVERS: /* --servers or -s */
255 opt_servers= strdup(optarg);
256 break;
257 case OPT_SLAP_TEST:
258 if (!strcmp(optarg, "get"))
259 opt_test= GET_TEST ;
260 else if (!strcmp(optarg, "set"))
261 opt_test= SET_TEST;
262 else
263 {
264 fprintf(stderr, "Your test, %s, is not a known test\n", optarg);
265 exit(1);
266 }
267 break;
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);
272 break;
273 case OPT_SLAP_INITIAL_LOAD:
274 opt_createial_load= strtol(optarg, (char **)NULL, 10);
275 break;
276 case '?':
277 /* getopt_long already printed an error message. */
278 exit(1);
279 default:
280 abort();
281 }
282 }
283
284 if (opt_test == GET_TEST && opt_createial_load == 0)
285 opt_createial_load= DEFAULT_INITIAL_LOAD;
286
287 if (opt_execute_number == 0)
288 opt_execute_number= DEFAULT_EXECUTE_NUMBER;
289
290 if (opt_concurrency == 0)
291 opt_concurrency= DEFAULT_CONCURRENCY;
292 }
293
294 void conclusions_print(conclusions_st *conclusion)
295 {
296 printf("\tThreads connecting to servers %u\n", opt_concurrency);
297 #ifdef NOT_FINISHED
298 printf("\tLoaded %u rows\n", conclusion->rows_loaded);
299 printf("\tRead %u rows\n", conclusion->rows_read);
300 #endif
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);
304 else
305 printf("\tTook %ld.%03ld seconds to read data\n", conclusion->read_time / 1000,
306 conclusion->read_time % 1000);
307 }
308
309 void *run_task(void *p)
310 {
311 thread_context_st *context= (thread_context_st *)p;
312 memcached_st *memc;
313 unsigned int value= 1;
314
315 memc= memcached_create(NULL);
316 if (opt_non_blocking_io)
317 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &value);
318 if (opt_tcp_nodelay)
319 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &value);
320
321 memcached_server_push(memc, context->servers);
322
323 pthread_mutex_lock(&sleeper_mutex);
324 while (master_wakeup)
325 {
326 pthread_cond_wait(&sleep_threshhold, &sleeper_mutex);
327 }
328 pthread_mutex_unlock(&sleeper_mutex);
329
330 /* Do Stuff */
331 switch (context->test)
332 {
333 case SET_TEST:
334 execute_set(memc, context->execute_pairs, context->execute_number);
335 break;
336 case GET_TEST:
337 execute_get(memc, context->initial_pairs, context->initial_number);
338 break;
339 }
340
341 pthread_mutex_lock(&counter_mutex);
342 thread_counter--;
343 pthread_cond_signal(&count_threshhold);
344 pthread_mutex_unlock(&counter_mutex);
345 memcached_free(memc);
346
347 if (context->execute_pairs)
348 pairs_free(context->execute_pairs);
349 free(context);
350
351 return NULL;
352 }
353
354 void flush_all(memcached_server_st *servers)
355 {
356 memcached_st *memc;
357
358 memc= memcached_create(NULL);
359
360 memcached_server_push(memc, servers);
361
362 memcached_flush(memc, 0);
363
364 memcached_free(memc);
365 }
366
367 pairs_st *load_create_data(memcached_server_st *servers, unsigned int number_of,
368 unsigned int *actual_loaded)
369 {
370 memcached_st *memc;
371 pairs_st *pairs;
372
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);
377
378 pairs= pairs_generate(number_of);
379 *actual_loaded= execute_set(memc, pairs, number_of);
380
381 memcached_free(memc);
382
383 return pairs;
384 }