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