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