Removed old asserts in client apps.
[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_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 {
99 fprintf(stderr, "No Servers provided\n");
100 exit(1);
101 }
102 }
103
104 servers= memcached_servers_parse(opt_servers);
105
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);
110
111 scheduler(servers, &conclusion);
112
113 free(opt_servers);
114
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);
121
122 return 0;
123 }
124
125 void scheduler(memcached_server_st *servers, conclusions_st *conclusion)
126 {
127 unsigned int x;
128 unsigned int actual_loaded= 0; /* Fix warning */
129 memcached_st *memc;
130
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;
135
136 pthread_attr_init(&attr);
137 pthread_attr_setdetachstate(&attr,
138 PTHREAD_CREATE_DETACHED);
139
140 memc= memcached_create(NULL);
141 memcached_server_push(memc, servers);
142
143 if (opt_flush)
144 flush_all(memc);
145 if (opt_createial_load)
146 pairs= load_create_data(memc, opt_createial_load, &actual_loaded);
147
148 /* We set this after we have loaded */
149 {
150 if (opt_non_blocking_io)
151 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
152 if (opt_tcp_nodelay)
153 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 1);
154 }
155
156
157 pthread_mutex_lock(&counter_mutex);
158 thread_counter= 0;
159
160 pthread_mutex_lock(&sleeper_mutex);
161 master_wakeup= 1;
162 pthread_mutex_unlock(&sleeper_mutex);
163
164 for (x= 0; x < opt_concurrency; x++)
165 {
166 thread_context_st *context;
167 context= (thread_context_st *)malloc(sizeof(thread_context_st));
168 memset(context, 0, sizeof(thread_context_st));
169
170 context->memc= memcached_clone(NULL, memc);
171 context->test= opt_test;
172
173 context->initial_pairs= pairs;
174 context->initial_number= actual_loaded;
175
176 if (opt_test == SET_TEST)
177 {
178 context->execute_pairs= pairs_generate(opt_execute_number, 400);
179 context->execute_number= opt_execute_number;
180 }
181
182 /* now you create the thread */
183 if (pthread_create(&mainthread, &attr, run_task,
184 (void *)context) != 0)
185 {
186 fprintf(stderr,"Could not create thread\n");
187 exit(1);
188 }
189 thread_counter++;
190 }
191
192 pthread_mutex_unlock(&counter_mutex);
193 pthread_attr_destroy(&attr);
194
195 pthread_mutex_lock(&sleeper_mutex);
196 master_wakeup= 0;
197 pthread_mutex_unlock(&sleeper_mutex);
198 pthread_cond_broadcast(&sleep_threshhold);
199
200 gettimeofday(&start_time, NULL);
201 /*
202 We loop until we know that all children have cleaned up.
203 */
204 pthread_mutex_lock(&counter_mutex);
205 while (thread_counter)
206 {
207 struct timespec abstime;
208
209 memset(&abstime, 0, sizeof(struct timespec));
210 abstime.tv_sec= 1;
211
212 pthread_cond_timedwait(&count_threshhold, &counter_mutex, &abstime);
213 }
214 pthread_mutex_unlock(&counter_mutex);
215
216 gettimeofday(&end_time, NULL);
217
218 conclusion->load_time= timedif(end_time, start_time);
219 conclusion->read_time= timedif(end_time, start_time);
220 pairs_free(pairs);
221 memcached_free(memc);
222 }
223
224 void options_parse(int argc, char *argv[])
225 {
226 memcached_programs_help_st help_options[]=
227 {
228 {0},
229 };
230
231 static struct option long_options[]=
232 {
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},
246 {0, 0, 0, 0},
247 };
248
249 int option_index= 0;
250 int option_rv;
251
252 while (1)
253 {
254 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
255 if (option_rv == -1) break;
256 switch (option_rv)
257 {
258 case 0:
259 break;
260 case OPT_VERBOSE: /* --verbose or -v */
261 opt_verbose = OPT_VERBOSE;
262 break;
263 case OPT_DEBUG: /* --debug or -d */
264 opt_verbose = OPT_DEBUG;
265 break;
266 case OPT_VERSION: /* --version or -V */
267 version_command(PROGRAM_NAME);
268 break;
269 case OPT_HELP: /* --help or -h */
270 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
271 break;
272 case OPT_SERVERS: /* --servers or -s */
273 opt_servers= strdup(optarg);
274 break;
275 case OPT_SLAP_TEST:
276 if (!strcmp(optarg, "get"))
277 opt_test= GET_TEST ;
278 else if (!strcmp(optarg, "set"))
279 opt_test= SET_TEST;
280 else
281 {
282 fprintf(stderr, "Your test, %s, is not a known test\n", optarg);
283 exit(1);
284 }
285 break;
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);
290 break;
291 case OPT_SLAP_INITIAL_LOAD:
292 opt_createial_load= strtol(optarg, (char **)NULL, 10);
293 break;
294 case '?':
295 /* getopt_long already printed an error message. */
296 exit(1);
297 default:
298 abort();
299 }
300 }
301
302 if (opt_test == GET_TEST && opt_createial_load == 0)
303 opt_createial_load= DEFAULT_INITIAL_LOAD;
304
305 if (opt_execute_number == 0)
306 opt_execute_number= DEFAULT_EXECUTE_NUMBER;
307
308 if (opt_concurrency == 0)
309 opt_concurrency= DEFAULT_CONCURRENCY;
310 }
311
312 void conclusions_print(conclusions_st *conclusion)
313 {
314 printf("\tThreads connecting to servers %u\n", opt_concurrency);
315 #ifdef NOT_FINISHED
316 printf("\tLoaded %u rows\n", conclusion->rows_loaded);
317 printf("\tRead %u rows\n", conclusion->rows_read);
318 #endif
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);
322 else
323 printf("\tTook %ld.%03ld seconds to read data\n", conclusion->read_time / 1000,
324 conclusion->read_time % 1000);
325 }
326
327 void *run_task(void *p)
328 {
329 thread_context_st *context= (thread_context_st *)p;
330 memcached_st *memc;
331
332 memc= context->memc;
333
334 pthread_mutex_lock(&sleeper_mutex);
335 while (master_wakeup)
336 {
337 pthread_cond_wait(&sleep_threshhold, &sleeper_mutex);
338 }
339 pthread_mutex_unlock(&sleeper_mutex);
340
341 /* Do Stuff */
342 switch (context->test)
343 {
344 case SET_TEST:
345 execute_set(memc, context->execute_pairs, context->execute_number);
346 break;
347 case GET_TEST:
348 execute_get(memc, context->initial_pairs, context->initial_number);
349 break;
350 }
351
352 memcached_free(memc);
353
354 if (context->execute_pairs)
355 pairs_free(context->execute_pairs);
356
357 free(context);
358
359 pthread_mutex_lock(&counter_mutex);
360 thread_counter--;
361 pthread_cond_signal(&count_threshhold);
362 pthread_mutex_unlock(&counter_mutex);
363
364 return NULL;
365 }
366
367 void flush_all(memcached_st *memc)
368 {
369 memcached_flush(memc, 0);
370 }
371
372 pairs_st *load_create_data(memcached_st *memc, unsigned int number_of,
373 unsigned int *actual_loaded)
374 {
375 memcached_st *clone;
376 pairs_st *pairs;
377
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);
381
382 pairs= pairs_generate(number_of, 400);
383 *actual_loaded= execute_set(clone, pairs, number_of);
384
385 memcached_free(clone);
386
387 return pairs;
388 }