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