7e2f8af10876b6542b3307dc0539830ac91e7dc7
[fio.git] / profiles / act.c
1 #include "../fio.h"
2 #include "../profile.h"
3 #include "../parse.h"
4
5 /*
6  * 1x loads
7  */
8 #define R_LOAD          2000
9 #define W_LOAD          1000
10
11 #define SAMPLE_SEC      3600            /* 1h checks */
12
13 struct act_pass_criteria {
14         unsigned int max_usec;
15         unsigned int max_perm;
16 };
17 #define ACT_MAX_CRIT    3
18
19 static struct act_pass_criteria act_pass[ACT_MAX_CRIT] = {
20         {
21                 .max_usec =     1000,
22                 .max_perm =     50,
23         },
24         {
25                 .max_usec =     8000,
26                 .max_perm =     10,
27         },
28         {
29                 .max_usec =     64000,
30                 .max_perm =     1,
31         },
32 };
33
34 struct act_slice {
35         uint64_t lat_buckets[ACT_MAX_CRIT];
36         uint64_t total_ios;
37 };
38
39 struct act_run_data {
40         struct fio_mutex *mutex;
41         unsigned int pending;
42
43         struct act_slice *slices;
44         unsigned int nr_slices;
45 };
46 static struct act_run_data *act_run_data;
47
48 struct act_prof_data {
49         struct timeval sample_tv;
50         struct act_slice *slices;
51         unsigned int cur_slice;
52         unsigned int nr_slices;
53 };
54
55 static char *device_names;
56 static unsigned int load;
57 static unsigned int prep;
58 static unsigned int threads_per_queue;
59 static unsigned int num_read_blocks;
60 static unsigned int write_size;
61 static unsigned long long test_duration;
62
63 #define ACT_MAX_OPTS    128
64 static const char *act_opts[ACT_MAX_OPTS] = {
65         "direct=1",
66         "ioengine=sync",
67         "random_generator=lfsr",
68         "group_reporting=1",
69         "thread",
70         NULL,
71 };
72 static unsigned int opt_idx = 5;
73 static unsigned int org_idx;
74
75 static int act_add_opt(const char *format, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
76
77 static struct fio_option options[] = {
78         {
79                 .name   = "device-names",
80                 .lname  = "device-names",
81                 .type   = FIO_OPT_STR_STORE,
82                 .roff1  = &device_names,
83                 .help   = "Devices to use",
84                 .category = FIO_OPT_C_PROFILE,
85                 .group  = FIO_OPT_G_ACT,
86         },
87         {
88                 .name   = "load",
89                 .lname  = "Load multiplier",
90                 .type   = FIO_OPT_INT,
91                 .roff1  = &load,
92                 .help   = "ACT load multipler (default 1x)",
93                 .def    = "1",
94                 .category = FIO_OPT_C_PROFILE,
95                 .group  = FIO_OPT_G_ACT,
96         },
97         {
98                 .name   = "test-duration",
99                 .lname  = "Test duration",
100                 .type   = FIO_OPT_STR_VAL_TIME,
101                 .roff1  = &test_duration,
102                 .help   = "How long the entire test takes to run",
103                 .def    = "24h",
104                 .category = FIO_OPT_C_PROFILE,
105                 .group  = FIO_OPT_G_ACT,
106         },
107         {
108                 .name   = "threads-per-queue",
109                 .lname  = "Number of read IO threads per device",
110                 .type   = FIO_OPT_INT,
111                 .roff1  = &threads_per_queue,
112                 .help   = "Number of read IO threads per device",
113                 .def    = "8",
114                 .category = FIO_OPT_C_PROFILE,
115                 .group  = FIO_OPT_G_ACT,
116         },
117         {
118                 .name   = "read-req-num-512-blocks",
119                 .lname  = "Number of 512b blocks to read",
120                 .type   = FIO_OPT_INT,
121                 .roff1  = &num_read_blocks,
122                 .help   = "Number of 512b blocks to read at the time",
123                 .def    = "3",
124                 .category = FIO_OPT_C_PROFILE,
125                 .group  = FIO_OPT_G_ACT,
126         },
127         {
128                 .name   = "large-block-op-kbytes",
129                 .lname  = "Size of large block ops (writes)",
130                 .type   = FIO_OPT_INT,
131                 .roff1  = &write_size,
132                 .help   = "Size of large block ops (writes)",
133                 .def    = "128k",
134                 .category = FIO_OPT_C_PROFILE,
135                 .group  = FIO_OPT_G_ACT,
136         },
137         {
138                 .name   = "prep",
139                 .lname  = "Run ACT prep phase",
140                 .type   = FIO_OPT_STR_SET,
141                 .roff1  = &prep,
142                 .help   = "Set to run ACT prep phase",
143                 .category = FIO_OPT_C_PROFILE,
144                 .group  = FIO_OPT_G_ACT,
145         },
146         {
147                 .name   = NULL,
148         },
149 };
150
151 static int act_add_opt(const char *str, ...)
152 {
153         char buffer[512];
154         va_list args;
155         size_t len;
156
157         if (opt_idx == ACT_MAX_OPTS) {
158                 log_err("act: ACT_MAX_OPTS is too small\n");
159                 return 1;
160         }
161
162         va_start(args, str);
163         len = vsnprintf(buffer, sizeof(buffer), str, args);
164         va_end(args);
165
166         if (len)
167                 act_opts[opt_idx++] = strdup(buffer);
168
169         return 0;
170 }
171
172 static int act_add_rw(const char *dev, int reads)
173 {
174         if (act_add_opt("name=act-%s-%s", reads ? "read" : "write", dev))
175                 return 1;
176         if (act_add_opt("filename=%s", dev))
177                 return 1;
178         if (act_add_opt("rw=%s", reads ? "randread" : "randwrite"))
179                 return 1;
180         if (reads) {
181                 int rload = load * R_LOAD / threads_per_queue;
182
183                 if (act_add_opt("numjobs=%u", threads_per_queue))
184                         return 1;
185                 if (act_add_opt("rate_iops=%u", rload))
186                         return 1;
187                 if (act_add_opt("bs=%u", num_read_blocks * 512))
188                         return 1;
189         } else {
190                 const int rsize = write_size / (num_read_blocks * 512);
191                 int wload = (load * W_LOAD + rsize - 1) / rsize;
192
193                 if (act_add_opt("rate_iops=%u", wload))
194                         return 1;
195                 if (act_add_opt("bs=%u", write_size))
196                         return 1;
197         }
198
199         return 0;
200 }
201
202 static int act_add_dev_prep(const char *dev)
203 {
204         /* Add sequential zero phase */
205         if (act_add_opt("name=act-prep-zeroes-%s", dev))
206                 return 1;
207         if (act_add_opt("filename=%s", dev))
208                 return 1;
209         if (act_add_opt("bs=1M"))
210                 return 1;
211         if (act_add_opt("zero_buffers"))
212                 return 1;
213         if (act_add_opt("rw=write"))
214                 return 1;
215
216         /* Randomly overwrite device */
217         if (act_add_opt("name=act-prep-salt-%s", dev))
218                 return 1;
219         if (act_add_opt("stonewall"))
220                 return 1;
221         if (act_add_opt("filename=%s", dev))
222                 return 1;
223         if (act_add_opt("bs=4k"))
224                 return 1;
225         if (act_add_opt("ioengine=libaio"))
226                 return 1;
227         if (act_add_opt("iodepth=64"))
228                 return 1;
229         if (act_add_opt("rw=randwrite"))
230                 return 1;
231
232         return 0;
233 }
234
235 static int act_add_dev(const char *dev)
236 {
237         if (prep)
238                 return act_add_dev_prep(dev);
239
240         if (act_add_opt("runtime=%llus", test_duration))
241                 return 1;
242         if (act_add_opt("time_based=1"))
243                 return 1;
244
245         if (act_add_rw(dev, 1))
246                 return 1;
247         if (act_add_rw(dev, 0))
248                 return 1;
249
250         return 0;
251 }
252
253 /*
254  * Fill our private options into the command line
255  */
256 static int act_prep_cmdline(void)
257 {
258         if (!device_names) {
259                 log_err("act: you need to set IO target(s) with the "
260                         "device-names option.\n");
261                 return 1;
262         }
263
264         org_idx = opt_idx;
265
266         do {
267                 char *dev;
268
269                 dev = strsep(&device_names, ",");
270                 if (!dev)
271                         break;
272
273                 if (act_add_dev(dev)) {
274                         log_err("act: failed adding device to the mix\n");
275                         break;
276                 }
277         } while (1);
278
279         return 0;
280 }
281
282 static int act_io_u_lat(struct thread_data *td, uint64_t usec)
283 {
284         struct act_prof_data *apd = td->prof_data;
285         struct act_slice *slice;
286         int i, ret = 0;
287         double perm;
288
289         if (prep)
290                 return 0;
291
292         /*
293          * Really should not happen, but lets not let jitter at the end
294          * ruin our day.
295          */
296         if (apd->cur_slice >= apd->nr_slices)
297                 return 0;
298
299         slice = &apd->slices[apd->cur_slice];
300         slice->total_ios++;
301
302         for (i = ACT_MAX_CRIT - 1; i >= 0; i--) {
303                 if (usec > act_pass[i].max_usec) {
304                         slice->lat_buckets[i]++;
305                         break;
306                 }
307         }
308
309         if (time_since_now(&apd->sample_tv) < SAMPLE_SEC)
310                 return 0;
311
312         /* SAMPLE_SEC has passed, check criteria for pass */
313         for (i = 0; i < ACT_MAX_CRIT; i++) {
314                 perm = (1000.0 * slice->lat_buckets[i]) / slice->total_ios;
315                 if (perm < act_pass[i].max_perm)
316                         continue;
317
318                 log_err("act: %f%% exceeds pass criteria of %f%%\n", perm / 10.0, (double) act_pass[i].max_perm / 10.0);
319                 ret = 1;
320                 break;
321         }
322
323         fio_gettime(&apd->sample_tv, NULL);
324         apd->cur_slice++;
325         return ret;
326 }
327
328 static void get_act_ref(void)
329 {
330         fio_mutex_down(act_run_data->mutex);
331         act_run_data->pending++;
332         fio_mutex_up(act_run_data->mutex);
333 }
334
335 static int show_slice(struct act_slice *slice, unsigned int slice_num)
336 {
337         unsigned int i, failed = 0;
338
339         log_info("   %2u", slice_num);
340
341         for (i = 0; i < ACT_MAX_CRIT; i++) {
342                 double perc = 0.0;
343
344                 if (slice->total_ios)
345                         perc = 100.0 * (double) slice->lat_buckets[i] / (double) slice->total_ios;
346                 if ((perc * 10.0) >= act_pass[i].max_perm)
347                         failed++;
348                 log_info("\t%2.2f", perc);
349         }
350         for (i = 0; i < ACT_MAX_CRIT; i++) {
351                 double perc = 0.0;
352
353                 if (slice->total_ios)
354                         perc = 100.0 * (double) slice->lat_buckets[i] / (double) slice->total_ios;
355                 log_info("\t%2.2f", perc);
356         }
357         log_info("\n");
358
359         return failed;
360 }
361
362 static void act_show_all_stats(void)
363 {
364         unsigned int i, fails = 0;
365
366         log_info("        trans                   device\n");
367         log_info("        %%>(ms)                  %%>(ms)\n");
368         log_info(" slice");
369
370         for (i = 0; i < ACT_MAX_CRIT; i++)
371                 log_info("\t %2u", act_pass[i].max_usec / 1000);
372         for (i = 0; i < ACT_MAX_CRIT; i++)
373                 log_info("\t %2u", act_pass[i].max_usec / 1000);
374
375         log_info("\n");
376         log_info(" -----  -----   -----  ------   -----   -----  ------\n");
377
378         for (i = 0; i < act_run_data->nr_slices; i++)
379                 fails += show_slice(&act_run_data->slices[i], i + 1);
380
381         log_info("\nact: test complete, device(s): %s\n", fails ? "FAILED" : "PASSED");
382 }
383
384 static void put_act_ref(struct thread_data *td)
385 {
386         struct act_prof_data *apd = td->prof_data;
387         unsigned int i, slice;
388
389         fio_mutex_down(act_run_data->mutex);
390
391         if (!act_run_data->slices) {
392                 act_run_data->slices = calloc(apd->nr_slices, sizeof(struct act_slice));
393                 act_run_data->nr_slices = apd->nr_slices;
394         }
395
396         for (slice = 0; slice < apd->nr_slices; slice++) {
397                 struct act_slice *dst = &act_run_data->slices[slice];
398                 struct act_slice *src = &apd->slices[slice];
399
400                 dst->total_ios += src->total_ios;
401
402                 for (i = 0; i < ACT_MAX_CRIT; i++)
403                         dst->lat_buckets[i] += src->lat_buckets[i];
404         }
405
406         if (!--act_run_data->pending)
407                 act_show_all_stats();
408
409         fio_mutex_up(act_run_data->mutex);
410 }
411
412 static int act_td_init(struct thread_data *td)
413 {
414         struct act_prof_data *apd;
415         unsigned int nr_slices;
416
417         get_act_ref();
418
419         apd = calloc(1, sizeof(*apd));
420         nr_slices = (test_duration + SAMPLE_SEC - 1) / SAMPLE_SEC;
421         apd->slices = calloc(nr_slices, sizeof(struct act_slice));
422         apd->nr_slices = nr_slices;
423         fio_gettime(&apd->sample_tv, NULL);
424         td->prof_data = apd;
425         return 0;
426 }
427
428 static void act_td_exit(struct thread_data *td)
429 {
430         struct act_prof_data *apd = td->prof_data;
431
432         put_act_ref(td);
433         free(apd->slices);
434         free(apd);
435         td->prof_data = NULL;
436 }
437
438 static struct prof_io_ops act_io_ops = {
439         .td_init        = act_td_init,
440         .td_exit        = act_td_exit,
441         .io_u_lat       = act_io_u_lat,
442 };
443
444 static struct profile_ops act_profile = {
445         .name           = "act",
446         .desc           = "ACT Aerospike like benchmark",
447         .options        = options,
448         .prep_cmd       = act_prep_cmdline,
449         .cmdline        = act_opts,
450         .io_ops         = &act_io_ops,
451 };
452
453 static void fio_init act_register(void)
454 {
455         act_run_data = calloc(1, sizeof(*act_run_data));
456         act_run_data->mutex = fio_mutex_init(FIO_MUTEX_UNLOCKED);
457
458         if (register_profile(&act_profile))
459                 log_err("fio: failed to register profile 'act'\n");
460 }
461
462 static void fio_exit act_unregister(void)
463 {
464         while (org_idx && org_idx < opt_idx)
465                 free((void *) act_opts[++org_idx]);
466
467         unregister_profile(&act_profile);
468         fio_mutex_remove(act_run_data->mutex);
469         free(act_run_data->slices);
470         free(act_run_data);
471         act_run_data = NULL;
472 }