Add t/gen-rand to test random generator
[fio.git] / eta.c
1 /*
2  * Status and ETA code
3  */
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <string.h>
7
8 #include "fio.h"
9 #include "lib/pow2.h"
10
11 static char __run_str[REAL_MAX_JOBS + 1];
12 static char run_str[__THREAD_RUNSTR_SZ(REAL_MAX_JOBS)];
13
14 static void update_condensed_str(char *rstr, char *run_str_condensed)
15 {
16         if (*rstr) {
17                 while (*rstr) {
18                         int nr = 1;
19
20                         *run_str_condensed++ = *rstr++;
21                         while (*(rstr - 1) == *rstr) {
22                                 rstr++;
23                                 nr++;
24                         }
25                         run_str_condensed += sprintf(run_str_condensed, "(%u),", nr);
26                 }
27                 run_str_condensed--;
28         }
29         *run_str_condensed = '\0';
30 }
31
32 /*
33  * Sets the status of the 'td' in the printed status map.
34  */
35 static void check_str_update(struct thread_data *td)
36 {
37         char c = __run_str[td->thread_number - 1];
38
39         switch (td->runstate) {
40         case TD_REAPED:
41                 if (td->error)
42                         c = 'X';
43                 else if (td->sig)
44                         c = 'K';
45                 else
46                         c = '_';
47                 break;
48         case TD_EXITED:
49                 c = 'E';
50                 break;
51         case TD_RAMP:
52                 c = '/';
53                 break;
54         case TD_RUNNING:
55                 if (td_rw(td)) {
56                         if (td_random(td)) {
57                                 if (td->o.rwmix[DDIR_READ] == 100)
58                                         c = 'r';
59                                 else if (td->o.rwmix[DDIR_WRITE] == 100)
60                                         c = 'w';
61                                 else
62                                         c = 'm';
63                         } else {
64                                 if (td->o.rwmix[DDIR_READ] == 100)
65                                         c = 'R';
66                                 else if (td->o.rwmix[DDIR_WRITE] == 100)
67                                         c = 'W';
68                                 else
69                                         c = 'M';
70                         }
71                 } else if (td_read(td)) {
72                         if (td_random(td))
73                                 c = 'r';
74                         else
75                                 c = 'R';
76                 } else if (td_write(td)) {
77                         if (td_random(td))
78                                 c = 'w';
79                         else
80                                 c = 'W';
81                 } else {
82                         if (td_random(td))
83                                 c = 'd';
84                         else
85                                 c = 'D';
86                 }
87                 break;
88         case TD_PRE_READING:
89                 c = 'p';
90                 break;
91         case TD_VERIFYING:
92                 c = 'V';
93                 break;
94         case TD_FSYNCING:
95                 c = 'F';
96                 break;
97         case TD_FINISHING:
98                 c = 'f';
99                 break;
100         case TD_CREATED:
101                 c = 'C';
102                 break;
103         case TD_INITIALIZED:
104         case TD_SETTING_UP:
105                 c = 'I';
106                 break;
107         case TD_NOT_CREATED:
108                 c = 'P';
109                 break;
110         default:
111                 log_err("state %d\n", td->runstate);
112         }
113
114         __run_str[td->thread_number - 1] = c;
115         update_condensed_str(__run_str, run_str);
116 }
117
118 /*
119  * Convert seconds to a printable string.
120  */
121 void eta_to_str(char *str, unsigned long eta_sec)
122 {
123         unsigned int d, h, m, s;
124         int disp_hour = 0;
125
126         if (eta_sec == -1) {
127                 sprintf(str, "--");
128                 return;
129         }
130
131         s = eta_sec % 60;
132         eta_sec /= 60;
133         m = eta_sec % 60;
134         eta_sec /= 60;
135         h = eta_sec % 24;
136         eta_sec /= 24;
137         d = eta_sec;
138
139         if (d) {
140                 disp_hour = 1;
141                 str += sprintf(str, "%02ud:", d);
142         }
143
144         if (h || disp_hour)
145                 str += sprintf(str, "%02uh:", h);
146
147         str += sprintf(str, "%02um:", m);
148         str += sprintf(str, "%02us", s);
149 }
150
151 /*
152  * Best effort calculation of the estimated pending runtime of a job.
153  */
154 static unsigned long thread_eta(struct thread_data *td)
155 {
156         unsigned long long bytes_total, bytes_done;
157         unsigned long eta_sec = 0;
158         unsigned long elapsed;
159         uint64_t timeout;
160
161         elapsed = (mtime_since_now(&td->epoch) + 999) / 1000;
162         timeout = td->o.timeout / 1000000UL;
163
164         bytes_total = td->total_io_size;
165
166         if (td->flags & TD_F_NO_PROGRESS)
167                 return -1;
168
169         if (td->o.fill_device && td->o.size  == -1ULL) {
170                 if (!td->fill_device_size || td->fill_device_size == -1ULL)
171                         return 0;
172
173                 bytes_total = td->fill_device_size;
174         }
175
176         if (td->o.zone_size && td->o.zone_skip && bytes_total) {
177                 unsigned int nr_zones;
178                 uint64_t zone_bytes;
179
180                 zone_bytes = bytes_total + td->o.zone_size + td->o.zone_skip;
181                 nr_zones = (zone_bytes - 1) / (td->o.zone_size + td->o.zone_skip);
182                 bytes_total -= nr_zones * td->o.zone_skip;
183         }
184
185         /*
186          * if writing and verifying afterwards, bytes_total will be twice the
187          * size. In a mixed workload, verify phase will be the size of the
188          * first stage writes.
189          */
190         if (td->o.do_verify && td->o.verify && td_write(td)) {
191                 if (td_rw(td)) {
192                         unsigned int perc = 50;
193
194                         if (td->o.rwmix[DDIR_WRITE])
195                                 perc = td->o.rwmix[DDIR_WRITE];
196
197                         bytes_total += (bytes_total * perc) / 100;
198                 } else
199                         bytes_total <<= 1;
200         }
201
202         if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
203                 double perc, perc_t;
204
205                 bytes_done = ddir_rw_sum(td->io_bytes);
206
207                 if (bytes_total) {
208                         perc = (double) bytes_done / (double) bytes_total;
209                         if (perc > 1.0)
210                                 perc = 1.0;
211                 } else
212                         perc = 0.0;
213
214                 if (td->o.time_based) {
215                         if (timeout) {
216                                 perc_t = (double) elapsed / (double) timeout;
217                                 if (perc_t < perc)
218                                         perc = perc_t;
219                         } else {
220                                 /*
221                                  * Will never hit, we can't have time_based
222                                  * without a timeout set.
223                                  */
224                                 perc = 0.0;
225                         }
226                 }
227
228                 eta_sec = (unsigned long) (elapsed * (1.0 / perc)) - elapsed;
229
230                 if (td->o.timeout &&
231                     eta_sec > (timeout + done_secs - elapsed))
232                         eta_sec = timeout + done_secs - elapsed;
233         } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
234                         || td->runstate == TD_INITIALIZED
235                         || td->runstate == TD_SETTING_UP
236                         || td->runstate == TD_RAMP
237                         || td->runstate == TD_PRE_READING) {
238                 int t_eta = 0, r_eta = 0;
239                 unsigned long long rate_bytes;
240
241                 /*
242                  * We can only guess - assume it'll run the full timeout
243                  * if given, otherwise assume it'll run at the specified rate.
244                  */
245                 if (td->o.timeout) {
246                         uint64_t __timeout = td->o.timeout;
247                         uint64_t start_delay = td->o.start_delay;
248                         uint64_t ramp_time = td->o.ramp_time;
249
250                         t_eta = __timeout + start_delay + ramp_time;
251                         t_eta /= 1000000ULL;
252
253                         if ((td->runstate == TD_RAMP) && in_ramp_time(td)) {
254                                 unsigned long ramp_left;
255
256                                 ramp_left = mtime_since_now(&td->epoch);
257                                 ramp_left = (ramp_left + 999) / 1000;
258                                 if (ramp_left <= t_eta)
259                                         t_eta -= ramp_left;
260                         }
261                 }
262                 rate_bytes = ddir_rw_sum(td->o.rate);
263                 if (rate_bytes) {
264                         r_eta = (bytes_total / 1024) / rate_bytes;
265                         r_eta += (td->o.start_delay / 1000000ULL);
266                 }
267
268                 if (r_eta && t_eta)
269                         eta_sec = min(r_eta, t_eta);
270                 else if (r_eta)
271                         eta_sec = r_eta;
272                 else if (t_eta)
273                         eta_sec = t_eta;
274                 else
275                         eta_sec = 0;
276         } else {
277                 /*
278                  * thread is already done or waiting for fsync
279                  */
280                 eta_sec = 0;
281         }
282
283         return eta_sec;
284 }
285
286 static void calc_rate(int unified_rw_rep, unsigned long mtime,
287                       unsigned long long *io_bytes,
288                       unsigned long long *prev_io_bytes, unsigned int *rate)
289 {
290         int i;
291
292         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
293                 unsigned long long diff, this_rate;
294
295                 diff = io_bytes[i] - prev_io_bytes[i];
296                 if (mtime)
297                         this_rate = ((1000 * diff) / mtime) / 1024;
298                 else
299                         this_rate = 0;
300
301                 if (unified_rw_rep) {
302                         rate[i] = 0;
303                         rate[0] += this_rate;
304                 } else
305                         rate[i] = this_rate;
306
307                 prev_io_bytes[i] = io_bytes[i];
308         }
309 }
310
311 static void calc_iops(int unified_rw_rep, unsigned long mtime,
312                       unsigned long long *io_iops,
313                       unsigned long long *prev_io_iops, unsigned int *iops)
314 {
315         int i;
316
317         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
318                 unsigned long long diff, this_iops;
319
320                 diff = io_iops[i] - prev_io_iops[i];
321                 if (mtime)
322                         this_iops = (diff * 1000) / mtime;
323                 else
324                         this_iops = 0;
325
326                 if (unified_rw_rep) {
327                         iops[i] = 0;
328                         iops[0] += this_iops;
329                 } else
330                         iops[i] = this_iops;
331
332                 prev_io_iops[i] = io_iops[i];
333         }
334 }
335
336 /*
337  * Print status of the jobs we know about. This includes rate estimates,
338  * ETA, thread state, etc.
339  */
340 int calc_thread_status(struct jobs_eta *je, int force)
341 {
342         struct thread_data *td;
343         int i, unified_rw_rep;
344         unsigned long rate_time, disp_time, bw_avg_time, *eta_secs;
345         unsigned long long io_bytes[DDIR_RWDIR_CNT];
346         unsigned long long io_iops[DDIR_RWDIR_CNT];
347         struct timeval now;
348
349         static unsigned long long rate_io_bytes[DDIR_RWDIR_CNT];
350         static unsigned long long disp_io_bytes[DDIR_RWDIR_CNT];
351         static unsigned long long disp_io_iops[DDIR_RWDIR_CNT];
352         static struct timeval rate_prev_time, disp_prev_time;
353
354         if (!force) {
355                 if (!(output_format & FIO_OUTPUT_NORMAL) &&
356                     f_out == stdout)
357                         return 0;
358                 if (temp_stall_ts || eta_print == FIO_ETA_NEVER)
359                         return 0;
360
361                 if (!isatty(STDOUT_FILENO) && (eta_print != FIO_ETA_ALWAYS))
362                         return 0;
363         }
364
365         if (!ddir_rw_sum(rate_io_bytes))
366                 fill_start_time(&rate_prev_time);
367         if (!ddir_rw_sum(disp_io_bytes))
368                 fill_start_time(&disp_prev_time);
369
370         eta_secs = malloc(thread_number * sizeof(unsigned long));
371         memset(eta_secs, 0, thread_number * sizeof(unsigned long));
372
373         je->elapsed_sec = (mtime_since_genesis() + 999) / 1000;
374
375         io_bytes[DDIR_READ] = io_bytes[DDIR_WRITE] = io_bytes[DDIR_TRIM] = 0;
376         io_iops[DDIR_READ] = io_iops[DDIR_WRITE] = io_iops[DDIR_TRIM] = 0;
377         bw_avg_time = ULONG_MAX;
378         unified_rw_rep = 0;
379         for_each_td(td, i) {
380                 unified_rw_rep += td->o.unified_rw_rep;
381                 if (is_power_of_2(td->o.kb_base))
382                         je->is_pow2 = 1;
383                 je->unit_base = td->o.unit_base;
384                 if (td->o.bw_avg_time < bw_avg_time)
385                         bw_avg_time = td->o.bw_avg_time;
386                 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING
387                     || td->runstate == TD_FSYNCING
388                     || td->runstate == TD_PRE_READING
389                     || td->runstate == TD_FINISHING) {
390                         je->nr_running++;
391                         if (td_read(td)) {
392                                 je->t_rate[0] += td->o.rate[DDIR_READ];
393                                 je->t_iops[0] += td->o.rate_iops[DDIR_READ];
394                                 je->m_rate[0] += td->o.ratemin[DDIR_READ];
395                                 je->m_iops[0] += td->o.rate_iops_min[DDIR_READ];
396                         }
397                         if (td_write(td)) {
398                                 je->t_rate[1] += td->o.rate[DDIR_WRITE];
399                                 je->t_iops[1] += td->o.rate_iops[DDIR_WRITE];
400                                 je->m_rate[1] += td->o.ratemin[DDIR_WRITE];
401                                 je->m_iops[1] += td->o.rate_iops_min[DDIR_WRITE];
402                         }
403                         if (td_trim(td)) {
404                                 je->t_rate[2] += td->o.rate[DDIR_TRIM];
405                                 je->t_iops[2] += td->o.rate_iops[DDIR_TRIM];
406                                 je->m_rate[2] += td->o.ratemin[DDIR_TRIM];
407                                 je->m_iops[2] += td->o.rate_iops_min[DDIR_TRIM];
408                         }
409
410                         je->files_open += td->nr_open_files;
411                 } else if (td->runstate == TD_RAMP) {
412                         je->nr_running++;
413                         je->nr_ramp++;
414                 } else if (td->runstate == TD_SETTING_UP)
415                         je->nr_setting_up++;
416                 else if (td->runstate < TD_RUNNING)
417                         je->nr_pending++;
418
419                 if (je->elapsed_sec >= 3)
420                         eta_secs[i] = thread_eta(td);
421                 else
422                         eta_secs[i] = INT_MAX;
423
424                 check_str_update(td);
425
426                 if (td->runstate > TD_SETTING_UP) {
427                         int ddir;
428
429                         for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
430                                 if (unified_rw_rep) {
431                                         io_bytes[0] += td->io_bytes[ddir];
432                                         io_iops[0] += td->io_blocks[ddir];
433                                 } else {
434                                         io_bytes[ddir] += td->io_bytes[ddir];
435                                         io_iops[ddir] += td->io_blocks[ddir];
436                                 }
437                         }
438                 }
439         }
440
441         if (exitall_on_terminate) {
442                 je->eta_sec = INT_MAX;
443                 for_each_td(td, i) {
444                         if (eta_secs[i] < je->eta_sec)
445                                 je->eta_sec = eta_secs[i];
446                 }
447         } else {
448                 unsigned long eta_stone = 0;
449
450                 je->eta_sec = 0;
451                 for_each_td(td, i) {
452                         if ((td->runstate == TD_NOT_CREATED) && td->o.stonewall)
453                                 eta_stone += eta_secs[i];
454                         else {
455                                 if (eta_secs[i] > je->eta_sec)
456                                         je->eta_sec = eta_secs[i];
457                         }
458                 }
459                 je->eta_sec += eta_stone;
460         }
461
462         free(eta_secs);
463
464         fio_gettime(&now, NULL);
465         rate_time = mtime_since(&rate_prev_time, &now);
466
467         if (write_bw_log && rate_time > bw_avg_time && !in_ramp_time(td)) {
468                 calc_rate(unified_rw_rep, rate_time, io_bytes, rate_io_bytes,
469                                 je->rate);
470                 memcpy(&rate_prev_time, &now, sizeof(now));
471                 add_agg_sample(je->rate[DDIR_READ], DDIR_READ, 0);
472                 add_agg_sample(je->rate[DDIR_WRITE], DDIR_WRITE, 0);
473                 add_agg_sample(je->rate[DDIR_TRIM], DDIR_TRIM, 0);
474         }
475
476         disp_time = mtime_since(&disp_prev_time, &now);
477
478         /*
479          * Allow a little slack, the target is to print it every 1000 msecs
480          */
481         if (!force && disp_time < 900)
482                 return 0;
483
484         calc_rate(unified_rw_rep, disp_time, io_bytes, disp_io_bytes, je->rate);
485         calc_iops(unified_rw_rep, disp_time, io_iops, disp_io_iops, je->iops);
486
487         memcpy(&disp_prev_time, &now, sizeof(now));
488
489         if (!force && !je->nr_running && !je->nr_pending)
490                 return 0;
491
492         je->nr_threads = thread_number;
493         update_condensed_str(__run_str, run_str);
494         memcpy(je->run_str, run_str, strlen(run_str));
495         return 1;
496 }
497
498 void display_thread_status(struct jobs_eta *je)
499 {
500         static struct timeval disp_eta_new_line;
501         static int eta_new_line_init, eta_new_line_pending;
502         static int linelen_last;
503         static int eta_good;
504         char output[REAL_MAX_JOBS + 512], *p = output;
505         char eta_str[128];
506         double perc = 0.0;
507
508         if (je->eta_sec != INT_MAX && je->elapsed_sec) {
509                 perc = (double) je->elapsed_sec / (double) (je->elapsed_sec + je->eta_sec);
510                 eta_to_str(eta_str, je->eta_sec);
511         }
512
513         if (eta_new_line_pending) {
514                 eta_new_line_pending = 0;
515                 p += sprintf(p, "\n");
516         }
517
518         p += sprintf(p, "Jobs: %d (f=%d)", je->nr_running, je->files_open);
519         if (je->m_rate[0] || je->m_rate[1] || je->t_rate[0] || je->t_rate[1]) {
520                 char *tr, *mr;
521
522                 mr = num2str(je->m_rate[0] + je->m_rate[1], 4, 0, je->is_pow2, 8);
523                 tr = num2str(je->t_rate[0] + je->t_rate[1], 4, 0, je->is_pow2, 8);
524                 p += sprintf(p, ", CR=%s/%s KB/s", tr, mr);
525                 free(tr);
526                 free(mr);
527         } else if (je->m_iops[0] || je->m_iops[1] || je->t_iops[0] || je->t_iops[1]) {
528                 p += sprintf(p, ", CR=%d/%d IOPS",
529                                         je->t_iops[0] + je->t_iops[1],
530                                         je->m_iops[0] + je->m_iops[1]);
531         }
532         if (je->eta_sec != INT_MAX && je->nr_running) {
533                 char perc_str[32];
534                 char *iops_str[DDIR_RWDIR_CNT];
535                 char *rate_str[DDIR_RWDIR_CNT];
536                 size_t left;
537                 int l;
538                 int ddir;
539
540                 if ((!je->eta_sec && !eta_good) || je->nr_ramp == je->nr_running ||
541                     je->eta_sec == -1)
542                         strcpy(perc_str, "-.-% done");
543                 else {
544                         double mult = 100.0;
545
546                         if (je->nr_setting_up && je->nr_running)
547                                 mult *= (1.0 - (double) je->nr_setting_up / (double) je->nr_running);
548
549                         eta_good = 1;
550                         perc *= mult;
551                         sprintf(perc_str, "%3.1f%% done", perc);
552                 }
553
554                 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
555                         rate_str[ddir] = num2str(je->rate[ddir], 5,
556                                                 1024, je->is_pow2, je->unit_base);
557                         iops_str[ddir] = num2str(je->iops[ddir], 4, 1, 0, 0);
558                 }
559
560                 left = sizeof(output) - (p - output) - 1;
561
562                 l = snprintf(p, left, ": [%s] [%s] [%s/%s/%s /s] [%s/%s/%s iops] [eta %s]",
563                                 je->run_str, perc_str, rate_str[DDIR_READ],
564                                 rate_str[DDIR_WRITE], rate_str[DDIR_TRIM],
565                                 iops_str[DDIR_READ], iops_str[DDIR_WRITE],
566                                 iops_str[DDIR_TRIM], eta_str);
567                 p += l;
568                 if (l >= 0 && l < linelen_last)
569                         p += sprintf(p, "%*s", linelen_last - l, "");
570                 linelen_last = l;
571
572                 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
573                         free(rate_str[ddir]);
574                         free(iops_str[ddir]);
575                 }
576         }
577         p += sprintf(p, "\r");
578
579         printf("%s", output);
580
581         if (!eta_new_line_init) {
582                 fio_gettime(&disp_eta_new_line, NULL);
583                 eta_new_line_init = 1;
584         } else if (eta_new_line && mtime_since_now(&disp_eta_new_line) > eta_new_line) {
585                 fio_gettime(&disp_eta_new_line, NULL);
586                 eta_new_line_pending = 1;
587         }
588
589         fflush(stdout);
590 }
591
592 struct jobs_eta *get_jobs_eta(bool force, size_t *size)
593 {
594         struct jobs_eta *je;
595
596         if (!thread_number)
597                 return NULL;
598
599         *size = sizeof(*je) + THREAD_RUNSTR_SZ + 8;
600         je = malloc(*size);
601         if (!je)
602                 return NULL;
603         memset(je, 0, *size);
604
605         if (!calc_thread_status(je, force)) {
606                 free(je);
607                 return NULL;
608         }
609
610         *size = sizeof(*je) + strlen((char *) je->run_str) + 1;
611         return je;
612 }
613
614 void print_thread_status(void)
615 {
616         struct jobs_eta *je;
617         size_t size;
618
619         je = get_jobs_eta(false, &size);
620         if (je)
621                 display_thread_status(je);
622
623         free(je);
624 }
625
626 void print_status_init(int thr_number)
627 {
628         __run_str[thr_number] = 'P';
629         update_condensed_str(__run_str, run_str);
630 }