Patch for memslap by Furuhashi Sadayuk
[awesomized/libmemcached] / clients / 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 <libmemcached/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 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;
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_st *memc;
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_st *memc, unsigned int number_of,
67 unsigned int *actual_loaded);
68 void flush_all(memcached_st *memc);
69
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 test_type opt_test= SET_TEST;
81
82 int main(int argc, char *argv[])
83 {
84 conclusions_st conclusion;
85 memcached_server_st *servers;
86
87 memset(&conclusion, 0, sizeof(conclusions_st));
88
89 srandom(time(NULL));
90 options_parse(argc, argv);
91
92 if (!opt_servers)
93 {
94 char *temp;
95
96 if ((temp= getenv("MEMCACHED_SERVERS")))
97 opt_servers= strdup(temp);
98 else
99 {
100 fprintf(stderr, "No Servers provided\n");
101 exit(1);
102 }
103 }
104
105 servers= memcached_servers_parse(opt_servers);
106
107 pthread_mutex_init(&counter_mutex, NULL);
108 pthread_cond_init(&count_threshhold, NULL);
109 pthread_mutex_init(&sleeper_mutex, NULL);
110 pthread_cond_init(&sleep_threshhold, NULL);
111
112 scheduler(servers, &conclusion);
113
114 free(opt_servers);
115
116 (void)pthread_mutex_destroy(&counter_mutex);
117 (void)pthread_cond_destroy(&count_threshhold);
118 (void)pthread_mutex_destroy(&sleeper_mutex);
119 (void)pthread_cond_destroy(&sleep_threshhold);
120 conclusions_print(&conclusion);
121 memcached_server_list_free(servers);
122
123 return 0;
124 }
125
126 void scheduler(memcached_server_st *servers, conclusions_st *conclusion)
127 {
128 unsigned int x;
129 unsigned int actual_loaded= 0; /* Fix warning */
130 memcached_st *memc;
131
132 struct timeval start_time, end_time;
133 pthread_t mainthread; /* Thread descriptor */
134 pthread_attr_t attr; /* Thread attributes */
135 pairs_st *pairs= NULL;
136
137 pthread_attr_init(&attr);
138 pthread_attr_setdetachstate(&attr,
139 PTHREAD_CREATE_DETACHED);
140
141 memc= memcached_create(NULL);
142 memcached_server_push(memc, servers);
143
144 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, opt_binary);
145
146 if (opt_flush)
147 flush_all(memc);
148 if (opt_createial_load)
149 pairs= load_create_data(memc, opt_createial_load, &actual_loaded);
150
151 /* We set this after we have loaded */
152 {
153 if (opt_non_blocking_io)
154 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
155 if (opt_tcp_nodelay)
156 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 1);
157 }
158
159
160 pthread_mutex_lock(&counter_mutex);
161 thread_counter= 0;
162
163 pthread_mutex_lock(&sleeper_mutex);
164 master_wakeup= 1;
165 pthread_mutex_unlock(&sleeper_mutex);
166
167 for (x= 0; x < opt_concurrency; x++)
168 {
169 thread_context_st *context;
170 context= (thread_context_st *)malloc(sizeof(thread_context_st));
171 memset(context, 0, sizeof(thread_context_st));
172
173 context->memc= memcached_clone(NULL, memc);
174 context->test= opt_test;
175
176 context->initial_pairs= pairs;
177 context->initial_number= actual_loaded;
178
179 if (opt_test == SET_TEST)
180 {
181 context->execute_pairs= pairs_generate(opt_execute_number, 400);
182 context->execute_number= opt_execute_number;
183 }
184
185 /* now you create the thread */
186 if (pthread_create(&mainthread, &attr, run_task,
187 (void *)context) != 0)
188 {
189 fprintf(stderr,"Could not create thread\n");
190 exit(1);
191 }
192 thread_counter++;
193 }
194
195 pthread_mutex_unlock(&counter_mutex);
196 pthread_attr_destroy(&attr);
197
198 pthread_mutex_lock(&sleeper_mutex);
199 master_wakeup= 0;
200 pthread_mutex_unlock(&sleeper_mutex);
201 pthread_cond_broadcast(&sleep_threshhold);
202
203 gettimeofday(&start_time, NULL);
204 /*
205 We loop until we know that all children have cleaned up.
206 */
207 pthread_mutex_lock(&counter_mutex);
208 while (thread_counter)
209 {
210 struct timespec abstime;
211
212 memset(&abstime, 0, sizeof(struct timespec));
213 abstime.tv_sec= 10;
214
215 pthread_cond_timedwait(&count_threshhold, &counter_mutex, &abstime);
216 }
217 pthread_mutex_unlock(&counter_mutex);
218
219 gettimeofday(&end_time, NULL);
220
221 conclusion->load_time= timedif(end_time, start_time);
222 conclusion->read_time= timedif(end_time, start_time);
223 pairs_free(pairs);
224 memcached_free(memc);
225 }
226
227 void options_parse(int argc, char *argv[])
228 {
229 memcached_programs_help_st help_options[]=
230 {
231 {0},
232 };
233
234 static struct option long_options[]=
235 {
236 {"concurrency", required_argument, NULL, OPT_SLAP_CONCURRENCY},
237 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
238 {"execute-number", required_argument, NULL, OPT_SLAP_EXECUTE_NUMBER},
239 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
240 {"flush", no_argument, &opt_flush, OPT_FLUSH},
241 {"help", no_argument, NULL, OPT_HELP},
242 {"initial-load", required_argument, NULL, OPT_SLAP_INITIAL_LOAD}, /* Number to load initially */
243 {"non-blocking", no_argument, &opt_non_blocking_io, OPT_SLAP_NON_BLOCK},
244 {"servers", required_argument, NULL, OPT_SERVERS},
245 {"tcp-nodelay", no_argument, &opt_tcp_nodelay, OPT_SLAP_TCP_NODELAY},
246 {"test", required_argument, NULL, OPT_SLAP_TEST},
247 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
248 {"version", no_argument, NULL, OPT_VERSION},
249 {"binary", no_argument, NULL, OPT_BINARY},
250 {0, 0, 0, 0},
251 };
252
253 int option_index= 0;
254 int option_rv;
255
256 while (1)
257 {
258 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
259 if (option_rv == -1) break;
260 switch (option_rv)
261 {
262 case 0:
263 break;
264 case OPT_BINARY:
265 opt_binary = 1;
266 break;
267 case OPT_VERBOSE: /* --verbose or -v */
268 opt_verbose = OPT_VERBOSE;
269 break;
270 case OPT_DEBUG: /* --debug or -d */
271 opt_verbose = OPT_DEBUG;
272 break;
273 case OPT_VERSION: /* --version or -V */
274 version_command(PROGRAM_NAME);
275 break;
276 case OPT_HELP: /* --help or -h */
277 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
278 break;
279 case OPT_SERVERS: /* --servers or -s */
280 opt_servers= strdup(optarg);
281 break;
282 case OPT_SLAP_TEST:
283 if (!strcmp(optarg, "get"))
284 opt_test= GET_TEST ;
285 else if (!strcmp(optarg, "set"))
286 opt_test= SET_TEST;
287 else
288 {
289 fprintf(stderr, "Your test, %s, is not a known test\n", optarg);
290 exit(1);
291 }
292 break;
293 case OPT_SLAP_CONCURRENCY:
294 opt_concurrency= strtol(optarg, (char **)NULL, 10);
295 break;
296 case OPT_SLAP_EXECUTE_NUMBER:
297 opt_execute_number= strtol(optarg, (char **)NULL, 10);
298 break;
299 case OPT_SLAP_INITIAL_LOAD:
300 opt_createial_load= strtol(optarg, (char **)NULL, 10);
301 break;
302 case '?':
303 /* getopt_long already printed an error message. */
304 exit(1);
305 default:
306 abort();
307 }
308 }
309
310 if (opt_test == GET_TEST && opt_createial_load == 0)
311 opt_createial_load= DEFAULT_INITIAL_LOAD;
312
313 if (opt_execute_number == 0)
314 opt_execute_number= DEFAULT_EXECUTE_NUMBER;
315
316 if (opt_concurrency == 0)
317 opt_concurrency= DEFAULT_CONCURRENCY;
318 }
319
320 void conclusions_print(conclusions_st *conclusion)
321 {
322 printf("\tThreads connecting to servers %u\n", opt_concurrency);
323 #ifdef NOT_FINISHED
324 printf("\tLoaded %u rows\n", conclusion->rows_loaded);
325 printf("\tRead %u rows\n", conclusion->rows_read);
326 #endif
327 if (opt_test == SET_TEST)
328 printf("\tTook %ld.%03ld seconds to load data\n", conclusion->load_time / 1000,
329 conclusion->load_time % 1000);
330 else
331 printf("\tTook %ld.%03ld seconds to read data\n", conclusion->read_time / 1000,
332 conclusion->read_time % 1000);
333 }
334
335 void *run_task(void *p)
336 {
337 thread_context_st *context= (thread_context_st *)p;
338 memcached_st *memc;
339
340 memc= context->memc;
341
342 pthread_mutex_lock(&sleeper_mutex);
343 while (master_wakeup)
344 {
345 pthread_cond_wait(&sleep_threshhold, &sleeper_mutex);
346 }
347 pthread_mutex_unlock(&sleeper_mutex);
348
349 /* Do Stuff */
350 switch (context->test)
351 {
352 case SET_TEST:
353 execute_set(memc, context->execute_pairs, context->execute_number);
354 break;
355 case GET_TEST:
356 execute_get(memc, context->initial_pairs, context->initial_number);
357 break;
358 }
359
360 memcached_free(memc);
361
362 if (context->execute_pairs)
363 pairs_free(context->execute_pairs);
364
365 free(context);
366
367 pthread_mutex_lock(&counter_mutex);
368 thread_counter--;
369 pthread_cond_signal(&count_threshhold);
370 pthread_mutex_unlock(&counter_mutex);
371
372 return NULL;
373 }
374
375 void flush_all(memcached_st *memc)
376 {
377 memcached_flush(memc, 0);
378 }
379
380 pairs_st *load_create_data(memcached_st *memc, unsigned int number_of,
381 unsigned int *actual_loaded)
382 {
383 memcached_st *clone;
384 pairs_st *pairs;
385
386 clone= memcached_clone(NULL, memc);
387 /* We always used non-blocking IO for load since it is faster */
388 memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
389
390 pairs= pairs_generate(number_of, 400);
391 *actual_loaded= execute_set(clone, pairs, number_of);
392
393 memcached_free(clone);
394
395 return pairs;
396 }