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