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