6 #ifdef CONFIG_VALGRIND_DEV
7 #include <valgrind/drd.h>
9 #define DRD_IGNORE_VAR(x) do { } while (0)
15 static char __run_str[REAL_MAX_JOBS + 1];
16 static char run_str[__THREAD_RUNSTR_SZ(REAL_MAX_JOBS) + 1];
18 static void update_condensed_str(char *rstr, char *run_str_condensed)
24 *run_str_condensed++ = *rstr++;
25 while (*(rstr - 1) == *rstr) {
29 run_str_condensed += sprintf(run_str_condensed, "(%u),", nr);
33 *run_str_condensed = '\0';
37 * Sets the status of the 'td' in the printed status map.
39 static void check_str_update(struct thread_data *td)
41 char c = __run_str[td->thread_number - 1];
43 switch (td->runstate) {
61 if (td->o.rwmix[DDIR_READ] == 100)
63 else if (td->o.rwmix[DDIR_WRITE] == 100)
68 if (td->o.rwmix[DDIR_READ] == 100)
70 else if (td->o.rwmix[DDIR_WRITE] == 100)
75 } else if (td_read(td)) {
80 } else if (td_write(td)) {
115 log_err("state %d\n", td->runstate);
118 __run_str[td->thread_number - 1] = c;
119 update_condensed_str(__run_str, run_str);
123 * Convert seconds to a printable string.
125 void eta_to_str(char *str, unsigned long eta_sec)
127 unsigned int d, h, m, s;
145 str += sprintf(str, "%02ud:", d);
149 str += sprintf(str, "%02uh:", h);
151 str += sprintf(str, "%02um:", m);
152 sprintf(str, "%02us", s);
156 * Best effort calculation of the estimated pending runtime of a job.
158 static unsigned long thread_eta(struct thread_data *td)
160 unsigned long long bytes_total, bytes_done;
161 unsigned long eta_sec = 0;
162 unsigned long elapsed;
165 elapsed = (mtime_since_now(&td->epoch) + 999) / 1000;
166 timeout = td->o.timeout / 1000000UL;
168 bytes_total = td->total_io_size;
170 if (td->flags & TD_F_NO_PROGRESS)
173 if (td->o.fill_device && td->o.size == -1ULL) {
174 if (!td->fill_device_size || td->fill_device_size == -1ULL)
177 bytes_total = td->fill_device_size;
181 * If io_size is set, bytes_total is an exact value that does not need
184 if (td->o.zone_size && td->o.zone_skip && bytes_total &&
185 !fio_option_is_set(&td->o, io_size)) {
186 unsigned int nr_zones;
190 * Calculate the upper bound of the number of zones that will
191 * be processed, including skipped bytes between zones. If this
192 * is larger than total_io_size (e.g. when --io_size or --size
193 * specify a small value), use the lower bound to avoid
194 * adjustments to a negative value that would result in a very
195 * large bytes_total and an incorrect eta.
197 zone_bytes = td->o.zone_size + td->o.zone_skip;
198 nr_zones = (bytes_total + zone_bytes - 1) / zone_bytes;
199 if (bytes_total < nr_zones * td->o.zone_skip)
200 nr_zones = bytes_total / zone_bytes;
201 bytes_total -= nr_zones * td->o.zone_skip;
205 * if writing and verifying afterwards, bytes_total will be twice the
206 * size. In a mixed workload, verify phase will be the size of the
207 * first stage writes.
209 if (td->o.do_verify && td->o.verify && td_write(td)) {
211 unsigned int perc = 50;
213 if (td->o.rwmix[DDIR_WRITE])
214 perc = td->o.rwmix[DDIR_WRITE];
216 bytes_total += (bytes_total * perc) / 100;
221 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
224 bytes_done = ddir_rw_sum(td->io_bytes);
227 perc = (double) bytes_done / (double) bytes_total;
233 if (td->o.time_based) {
235 perc_t = (double) elapsed / (double) timeout;
240 * Will never hit, we can't have time_based
241 * without a timeout set.
250 eta_sec = (unsigned long) (elapsed * (1.0 / perc)) - elapsed;
254 eta_sec > (timeout + done_secs - elapsed))
255 eta_sec = timeout + done_secs - elapsed;
256 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
257 || td->runstate == TD_INITIALIZED
258 || td->runstate == TD_SETTING_UP
259 || td->runstate == TD_RAMP
260 || td->runstate == TD_PRE_READING) {
261 int64_t t_eta = 0, r_eta = 0;
262 unsigned long long rate_bytes;
265 * We can only guess - assume it'll run the full timeout
266 * if given, otherwise assume it'll run at the specified rate.
269 uint64_t __timeout = td->o.timeout;
270 uint64_t start_delay = td->o.start_delay;
271 uint64_t ramp_time = td->o.ramp_time;
273 t_eta = __timeout + start_delay;
274 if (!td->ramp_time_over) {
279 if ((td->runstate == TD_RAMP) && in_ramp_time(td)) {
280 unsigned long ramp_left;
282 ramp_left = mtime_since_now(&td->epoch);
283 ramp_left = (ramp_left + 999) / 1000;
284 if (ramp_left <= t_eta)
290 rate_bytes = td->o.rate[DDIR_READ];
292 rate_bytes += td->o.rate[DDIR_WRITE];
294 rate_bytes += td->o.rate[DDIR_TRIM];
297 r_eta = bytes_total / rate_bytes;
298 r_eta += (td->o.start_delay / 1000000ULL);
302 eta_sec = min(r_eta, t_eta);
311 * thread is already done or waiting for fsync
319 static void calc_rate(int unified_rw_rep, unsigned long mtime,
320 unsigned long long *io_bytes,
321 unsigned long long *prev_io_bytes, uint64_t *rate)
325 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
326 unsigned long long diff, this_rate;
328 diff = io_bytes[i] - prev_io_bytes[i];
330 this_rate = ((1000 * diff) / mtime) / 1024; /* KiB/s */
334 if (unified_rw_rep) {
336 rate[0] += this_rate;
340 prev_io_bytes[i] = io_bytes[i];
344 static void calc_iops(int unified_rw_rep, unsigned long mtime,
345 unsigned long long *io_iops,
346 unsigned long long *prev_io_iops, unsigned int *iops)
350 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
351 unsigned long long diff, this_iops;
353 diff = io_iops[i] - prev_io_iops[i];
355 this_iops = (diff * 1000) / mtime;
359 if (unified_rw_rep) {
361 iops[0] += this_iops;
365 prev_io_iops[i] = io_iops[i];
370 * Allow a little slack - if we're within 95% of the time, allow ETA.
372 bool eta_time_within_slack(unsigned int time)
374 return time > ((eta_interval_msec * 95) / 100);
378 * Print status of the jobs we know about. This includes rate estimates,
379 * ETA, thread state, etc.
381 bool calc_thread_status(struct jobs_eta *je, int force)
383 struct thread_data *td;
384 int i, unified_rw_rep;
385 uint64_t rate_time, disp_time, bw_avg_time, *eta_secs;
386 unsigned long long io_bytes[DDIR_RWDIR_CNT] = {};
387 unsigned long long io_iops[DDIR_RWDIR_CNT] = {};
390 static unsigned long long rate_io_bytes[DDIR_RWDIR_CNT];
391 static unsigned long long disp_io_bytes[DDIR_RWDIR_CNT];
392 static unsigned long long disp_io_iops[DDIR_RWDIR_CNT];
393 static struct timespec rate_prev_time, disp_prev_time;
396 if (!(output_format & FIO_OUTPUT_NORMAL) &&
399 if (temp_stall_ts || eta_print == FIO_ETA_NEVER)
402 if (!isatty(STDOUT_FILENO) && (eta_print != FIO_ETA_ALWAYS))
406 if (!ddir_rw_sum(rate_io_bytes))
407 fill_start_time(&rate_prev_time);
408 if (!ddir_rw_sum(disp_io_bytes))
409 fill_start_time(&disp_prev_time);
411 eta_secs = malloc(thread_number * sizeof(uint64_t));
412 memset(eta_secs, 0, thread_number * sizeof(uint64_t));
414 je->elapsed_sec = (mtime_since_genesis() + 999) / 1000;
416 bw_avg_time = ULONG_MAX;
419 unified_rw_rep += td->o.unified_rw_rep;
420 if (is_power_of_2(td->o.kb_base))
422 je->unit_base = td->o.unit_base;
423 if (td->o.bw_avg_time < bw_avg_time)
424 bw_avg_time = td->o.bw_avg_time;
425 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING
426 || td->runstate == TD_FSYNCING
427 || td->runstate == TD_PRE_READING
428 || td->runstate == TD_FINISHING) {
431 je->t_rate[0] += td->o.rate[DDIR_READ];
432 je->t_iops[0] += td->o.rate_iops[DDIR_READ];
433 je->m_rate[0] += td->o.ratemin[DDIR_READ];
434 je->m_iops[0] += td->o.rate_iops_min[DDIR_READ];
437 je->t_rate[1] += td->o.rate[DDIR_WRITE];
438 je->t_iops[1] += td->o.rate_iops[DDIR_WRITE];
439 je->m_rate[1] += td->o.ratemin[DDIR_WRITE];
440 je->m_iops[1] += td->o.rate_iops_min[DDIR_WRITE];
443 je->t_rate[2] += td->o.rate[DDIR_TRIM];
444 je->t_iops[2] += td->o.rate_iops[DDIR_TRIM];
445 je->m_rate[2] += td->o.ratemin[DDIR_TRIM];
446 je->m_iops[2] += td->o.rate_iops_min[DDIR_TRIM];
449 je->files_open += td->nr_open_files;
450 } else if (td->runstate == TD_RAMP) {
453 } else if (td->runstate == TD_SETTING_UP)
455 else if (td->runstate < TD_RUNNING)
458 if (je->elapsed_sec >= 3)
459 eta_secs[i] = thread_eta(td);
461 eta_secs[i] = INT_MAX;
463 check_str_update(td);
465 if (td->runstate > TD_SETTING_UP) {
468 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
469 if (unified_rw_rep) {
470 io_bytes[0] += td->io_bytes[ddir];
471 io_iops[0] += td->io_blocks[ddir];
473 io_bytes[ddir] += td->io_bytes[ddir];
474 io_iops[ddir] += td->io_blocks[ddir];
480 if (exitall_on_terminate) {
481 je->eta_sec = INT_MAX;
483 if (eta_secs[i] < je->eta_sec)
484 je->eta_sec = eta_secs[i];
487 unsigned long eta_stone = 0;
491 if ((td->runstate == TD_NOT_CREATED) && td->o.stonewall)
492 eta_stone += eta_secs[i];
494 if (eta_secs[i] > je->eta_sec)
495 je->eta_sec = eta_secs[i];
498 je->eta_sec += eta_stone;
503 fio_gettime(&now, NULL);
504 rate_time = mtime_since(&rate_prev_time, &now);
506 if (write_bw_log && rate_time > bw_avg_time && !in_ramp_time(td)) {
507 calc_rate(unified_rw_rep, rate_time, io_bytes, rate_io_bytes,
509 memcpy(&rate_prev_time, &now, sizeof(now));
510 for_each_rw_ddir(ddir) {
511 add_agg_sample(sample_val(je->rate[ddir]), ddir, 0, 0);
515 disp_time = mtime_since(&disp_prev_time, &now);
517 if (!force && !eta_time_within_slack(disp_time))
520 calc_rate(unified_rw_rep, disp_time, io_bytes, disp_io_bytes, je->rate);
521 calc_iops(unified_rw_rep, disp_time, io_iops, disp_io_iops, je->iops);
523 memcpy(&disp_prev_time, &now, sizeof(now));
525 if (!force && !je->nr_running && !je->nr_pending)
528 je->nr_threads = thread_number;
529 update_condensed_str(__run_str, run_str);
530 memcpy(je->run_str, run_str, strlen(run_str));
534 static int gen_eta_str(struct jobs_eta *je, char *p, size_t left,
535 char **rate_str, char **iops_str)
537 bool has_r = je->rate[DDIR_READ] || je->iops[DDIR_READ];
538 bool has_w = je->rate[DDIR_WRITE] || je->iops[DDIR_WRITE];
539 bool has_t = je->rate[DDIR_TRIM] || je->iops[DDIR_TRIM];
542 if (!has_r && !has_w && !has_t)
546 l += snprintf(p + l, left - l, "[r=%s", rate_str[DDIR_READ]);
548 l += snprintf(p + l, left - l, "]");
552 l += snprintf(p + l, left - l, ",");
554 l += snprintf(p + l, left - l, "[");
555 l += snprintf(p + l, left - l, "w=%s", rate_str[DDIR_WRITE]);
557 l += snprintf(p + l, left - l, "]");
561 l += snprintf(p + l, left - l, ",");
562 else if (!has_r && !has_w)
563 l += snprintf(p + l, left - l, "[");
564 l += snprintf(p + l, left - l, "t=%s]", rate_str[DDIR_TRIM]);
567 l += snprintf(p + l, left - l, "[r=%s", iops_str[DDIR_READ]);
569 l += snprintf(p + l, left - l, " IOPS]");
573 l += snprintf(p + l, left - l, ",");
575 l += snprintf(p + l, left - l, "[");
576 l += snprintf(p + l, left - l, "w=%s", iops_str[DDIR_WRITE]);
578 l += snprintf(p + l, left - l, " IOPS]");
582 l += snprintf(p + l, left - l, ",");
583 else if (!has_r && !has_w)
584 l += snprintf(p + l, left - l, "[");
585 l += snprintf(p + l, left - l, "t=%s IOPS]", iops_str[DDIR_TRIM]);
591 void display_thread_status(struct jobs_eta *je)
593 static struct timespec disp_eta_new_line;
594 static int eta_new_line_init, eta_new_line_pending;
595 static int linelen_last;
597 char output[__THREAD_RUNSTR_SZ(REAL_MAX_JOBS) + 512], *p = output;
601 if (je->eta_sec != INT_MAX && je->elapsed_sec) {
602 perc = (double) je->elapsed_sec / (double) (je->elapsed_sec + je->eta_sec);
603 eta_to_str(eta_str, je->eta_sec);
606 if (eta_new_line_pending) {
607 eta_new_line_pending = 0;
609 p += sprintf(p, "\n");
612 p += sprintf(p, "Jobs: %d (f=%d)", je->nr_running, je->files_open);
614 /* rate limits, if any */
615 if (je->m_rate[0] || je->m_rate[1] || je->m_rate[2] ||
616 je->t_rate[0] || je->t_rate[1] || je->t_rate[2]) {
619 mr = num2str(je->m_rate[0] + je->m_rate[1] + je->m_rate[2],
620 je->sig_figs, 0, je->is_pow2, N2S_BYTEPERSEC);
621 tr = num2str(je->t_rate[0] + je->t_rate[1] + je->t_rate[2],
622 je->sig_figs, 0, je->is_pow2, N2S_BYTEPERSEC);
624 p += sprintf(p, ", %s-%s", mr, tr);
627 } else if (je->m_iops[0] || je->m_iops[1] || je->m_iops[2] ||
628 je->t_iops[0] || je->t_iops[1] || je->t_iops[2]) {
629 p += sprintf(p, ", %d-%d IOPS",
630 je->m_iops[0] + je->m_iops[1] + je->m_iops[2],
631 je->t_iops[0] + je->t_iops[1] + je->t_iops[2]);
634 /* current run string, % done, bandwidth, iops, eta */
635 if (je->eta_sec != INT_MAX && je->nr_running) {
637 char *iops_str[DDIR_RWDIR_CNT];
638 char *rate_str[DDIR_RWDIR_CNT];
644 if ((!je->eta_sec && !eta_good) || je->nr_ramp == je->nr_running ||
646 strcpy(perc_str, "-.-%");
650 if (je->nr_setting_up && je->nr_running)
651 mult *= (1.0 - (double) je->nr_setting_up / (double) je->nr_running);
655 sprintf(perc_str, "%3.1f%%", perc);
658 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
659 rate_str[ddir] = num2str(je->rate[ddir], 4,
660 1024, je->is_pow2, je->unit_base);
661 iops_str[ddir] = num2str(je->iops[ddir], 4, 1, 0, N2S_NONE);
664 left = sizeof(output) - (p - output) - 1;
665 l = snprintf(p, left, ": [%s][%s]", je->run_str, perc_str);
666 l += gen_eta_str(je, p + l, left - l, rate_str, iops_str);
667 l += snprintf(p + l, left - l, "[eta %s]", eta_str);
669 /* If truncation occurred adjust l so p is on the null */
673 linelen = p - output;
674 if (l >= 0 && linelen < linelen_last)
675 p += sprintf(p, "%*s", linelen_last - linelen, "");
676 linelen_last = linelen;
678 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
679 free(rate_str[ddir]);
680 free(iops_str[ddir]);
685 printf("%s", output);
687 if (!eta_new_line_init) {
688 fio_gettime(&disp_eta_new_line, NULL);
689 eta_new_line_init = 1;
690 } else if (eta_new_line && mtime_since_now(&disp_eta_new_line) > eta_new_line) {
691 fio_gettime(&disp_eta_new_line, NULL);
692 eta_new_line_pending = 1;
698 struct jobs_eta *get_jobs_eta(bool force, size_t *size)
705 *size = sizeof(*je) + THREAD_RUNSTR_SZ + 8;
709 memset(je, 0, *size);
711 if (!calc_thread_status(je, force)) {
716 *size = sizeof(*je) + strlen((char *) je->run_str) + 1;
720 void print_thread_status(void)
725 je = get_jobs_eta(false, &size);
727 display_thread_status(je);
732 void print_status_init(int thr_number)
734 struct jobs_eta_packed jep;
736 compiletime_assert(sizeof(struct jobs_eta) == sizeof(jep), "jobs_eta");
738 DRD_IGNORE_VAR(__run_str);
739 __run_str[thr_number] = 'P';
740 update_condensed_str(__run_str, run_str);