7 #ifdef CONFIG_VALGRIND_DEV
8 #include <valgrind/drd.h>
10 #define DRD_IGNORE_VAR(x) do { } while (0)
16 static char __run_str[REAL_MAX_JOBS + 1];
17 static char run_str[__THREAD_RUNSTR_SZ(REAL_MAX_JOBS) + 1];
19 static void update_condensed_str(char *rstr, char *run_str_condensed)
25 *run_str_condensed++ = *rstr++;
26 while (*(rstr - 1) == *rstr) {
30 run_str_condensed += sprintf(run_str_condensed, "(%u),", nr);
34 *run_str_condensed = '\0';
38 * Sets the status of the 'td' in the printed status map.
40 static void check_str_update(struct thread_data *td)
42 char c = __run_str[td->thread_number - 1];
44 switch (td->runstate) {
62 if (td->o.rwmix[DDIR_READ] == 100)
64 else if (td->o.rwmix[DDIR_WRITE] == 100)
69 if (td->o.rwmix[DDIR_READ] == 100)
71 else if (td->o.rwmix[DDIR_WRITE] == 100)
76 } else if (td_read(td)) {
81 } else if (td_write(td)) {
116 log_err("state %d\n", td->runstate);
119 __run_str[td->thread_number - 1] = c;
120 update_condensed_str(__run_str, run_str);
124 * Convert seconds to a printable string.
126 void eta_to_str(char *str, unsigned long eta_sec)
128 unsigned int d, h, m, s;
146 str += sprintf(str, "%02ud:", d);
150 str += sprintf(str, "%02uh:", h);
152 str += sprintf(str, "%02um:", m);
153 sprintf(str, "%02us", s);
157 * Best effort calculation of the estimated pending runtime of a job.
159 static unsigned long thread_eta(struct thread_data *td)
161 unsigned long long bytes_total, bytes_done;
162 unsigned long eta_sec = 0;
163 unsigned long elapsed;
166 elapsed = (mtime_since_now(&td->epoch) + 999) / 1000;
167 timeout = td->o.timeout / 1000000UL;
169 bytes_total = td->total_io_size;
171 if (td->flags & TD_F_NO_PROGRESS)
174 if (td->o.fill_device && td->o.size == -1ULL) {
175 if (!td->fill_device_size || td->fill_device_size == -1ULL)
178 bytes_total = td->fill_device_size;
182 * If io_size is set, bytes_total is an exact value that does not need
185 if (td->o.zone_size && td->o.zone_skip && bytes_total &&
186 !fio_option_is_set(&td->o, io_size)) {
187 unsigned int nr_zones;
191 * Calculate the upper bound of the number of zones that will
192 * be processed, including skipped bytes between zones. If this
193 * is larger than total_io_size (e.g. when --io_size or --size
194 * specify a small value), use the lower bound to avoid
195 * adjustments to a negative value that would result in a very
196 * large bytes_total and an incorrect eta.
198 zone_bytes = td->o.zone_size + td->o.zone_skip;
199 nr_zones = (bytes_total + zone_bytes - 1) / zone_bytes;
200 if (bytes_total < nr_zones * td->o.zone_skip)
201 nr_zones = bytes_total / zone_bytes;
202 bytes_total -= nr_zones * td->o.zone_skip;
206 * if writing and verifying afterwards, bytes_total will be twice the
207 * size. In a mixed workload, verify phase will be the size of the
208 * first stage writes.
210 if (td->o.do_verify && td->o.verify && td_write(td)) {
212 unsigned int perc = 50;
214 if (td->o.rwmix[DDIR_WRITE])
215 perc = td->o.rwmix[DDIR_WRITE];
217 bytes_total += (bytes_total * perc) / 100;
223 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
226 bytes_done = ddir_rw_sum(td->io_bytes);
229 perc = (double) bytes_done / (double) bytes_total;
236 if (td->o.time_based) {
238 perc_t = (double) elapsed / (double) timeout;
243 * Will never hit, we can't have time_based
244 * without a timeout set.
253 eta_sec = (unsigned long) (elapsed * (1.0 / perc)) - elapsed;
257 eta_sec > (timeout + done_secs - elapsed))
258 eta_sec = timeout + done_secs - elapsed;
259 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
260 || td->runstate == TD_INITIALIZED
261 || td->runstate == TD_SETTING_UP
262 || td->runstate == TD_RAMP
263 || td->runstate == TD_PRE_READING) {
264 int64_t t_eta = 0, r_eta = 0;
265 unsigned long long rate_bytes;
268 * We can only guess - assume it'll run the full timeout
269 * if given, otherwise assume it'll run at the specified rate.
272 uint64_t __timeout = td->o.timeout;
273 uint64_t start_delay = td->o.start_delay;
274 uint64_t ramp_time = td->o.ramp_time;
276 t_eta = __timeout + start_delay;
277 if (!td->ramp_time_over) {
282 if ((td->runstate == TD_RAMP) && in_ramp_time(td)) {
283 unsigned long ramp_left;
285 ramp_left = mtime_since_now(&td->epoch);
286 ramp_left = (ramp_left + 999) / 1000;
287 if (ramp_left <= t_eta)
293 rate_bytes = td->o.rate[DDIR_READ];
295 rate_bytes += td->o.rate[DDIR_WRITE];
297 rate_bytes += td->o.rate[DDIR_TRIM];
300 r_eta = bytes_total / rate_bytes;
301 r_eta += (td->o.start_delay / 1000000ULL);
305 eta_sec = min(r_eta, t_eta);
314 * thread is already done or waiting for fsync
322 static void calc_rate(int unified_rw_rep, unsigned long mtime,
323 unsigned long long *io_bytes,
324 unsigned long long *prev_io_bytes, uint64_t *rate)
328 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
329 unsigned long long diff, this_rate;
331 diff = io_bytes[i] - prev_io_bytes[i];
333 this_rate = ((1000 * diff) / mtime) / 1024; /* KiB/s */
337 if (unified_rw_rep == UNIFIED_MIXED) {
339 rate[0] += this_rate;
343 prev_io_bytes[i] = io_bytes[i];
347 static void calc_iops(int unified_rw_rep, unsigned long mtime,
348 unsigned long long *io_iops,
349 unsigned long long *prev_io_iops, unsigned int *iops)
353 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
354 unsigned long long diff, this_iops;
356 diff = io_iops[i] - prev_io_iops[i];
358 this_iops = (diff * 1000) / mtime;
362 if (unified_rw_rep == UNIFIED_MIXED) {
364 iops[0] += this_iops;
368 prev_io_iops[i] = io_iops[i];
373 * Allow a little slack - if we're within 95% of the time, allow ETA.
375 bool eta_time_within_slack(unsigned int time)
377 return time > ((eta_interval_msec * 95) / 100);
381 * These are the conditions under which we might be able to skip the eta
384 static bool skip_eta()
386 if (!(output_format & FIO_OUTPUT_NORMAL) && f_out == stdout)
388 if (temp_stall_ts || eta_print == FIO_ETA_NEVER)
390 if (!isatty(STDOUT_FILENO) && eta_print != FIO_ETA_ALWAYS)
397 * Print status of the jobs we know about. This includes rate estimates,
398 * ETA, thread state, etc.
400 static bool calc_thread_status(struct jobs_eta *je, int force)
404 uint64_t rate_time, disp_time, bw_avg_time, *eta_secs;
405 unsigned long long io_bytes[DDIR_RWDIR_CNT] = {};
406 unsigned long long io_iops[DDIR_RWDIR_CNT] = {};
409 static unsigned long long rate_io_bytes[DDIR_RWDIR_CNT];
410 static unsigned long long disp_io_bytes[DDIR_RWDIR_CNT];
411 static unsigned long long disp_io_iops[DDIR_RWDIR_CNT];
412 static struct timespec rate_prev_time, disp_prev_time;
416 if (!force && skip_eta()) {
423 if (!ddir_rw_sum(rate_io_bytes))
424 fill_start_time(&rate_prev_time);
425 if (!ddir_rw_sum(disp_io_bytes))
426 fill_start_time(&disp_prev_time);
428 eta_secs = calloc(thread_number, sizeof(uint64_t));
430 je->elapsed_sec = (mtime_since_genesis() + 999) / 1000;
432 bw_avg_time = ULONG_MAX;
435 unified_rw_rep += td->o.unified_rw_rep;
436 if (is_power_of_2(td->o.kb_base))
438 je->unit_base = td->o.unit_base;
439 je->sig_figs = td->o.sig_figs;
440 if (td->o.bw_avg_time < bw_avg_time)
441 bw_avg_time = td->o.bw_avg_time;
442 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING
443 || td->runstate == TD_FSYNCING
444 || td->runstate == TD_PRE_READING
445 || td->runstate == TD_FINISHING) {
448 je->t_rate[0] += td->o.rate[DDIR_READ];
449 je->t_iops[0] += td->o.rate_iops[DDIR_READ];
450 je->m_rate[0] += td->o.ratemin[DDIR_READ];
451 je->m_iops[0] += td->o.rate_iops_min[DDIR_READ];
454 je->t_rate[1] += td->o.rate[DDIR_WRITE];
455 je->t_iops[1] += td->o.rate_iops[DDIR_WRITE];
456 je->m_rate[1] += td->o.ratemin[DDIR_WRITE];
457 je->m_iops[1] += td->o.rate_iops_min[DDIR_WRITE];
460 je->t_rate[2] += td->o.rate[DDIR_TRIM];
461 je->t_iops[2] += td->o.rate_iops[DDIR_TRIM];
462 je->m_rate[2] += td->o.ratemin[DDIR_TRIM];
463 je->m_iops[2] += td->o.rate_iops_min[DDIR_TRIM];
466 je->files_open += td->nr_open_files;
467 } else if (td->runstate == TD_RAMP) {
470 } else if (td->runstate == TD_SETTING_UP)
472 else if (td->runstate < TD_RUNNING)
475 if (je->elapsed_sec >= 3)
476 eta_secs[__td_index] = thread_eta(td);
478 eta_secs[__td_index] = INT_MAX;
480 check_str_update(td);
482 if (td->runstate > TD_SETTING_UP) {
485 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
486 if (unified_rw_rep) {
487 io_bytes[0] += td->io_bytes[ddir];
488 io_iops[0] += td->io_blocks[ddir];
490 io_bytes[ddir] += td->io_bytes[ddir];
491 io_iops[ddir] += td->io_blocks[ddir];
497 if (exitall_on_terminate) {
498 je->eta_sec = INT_MAX;
499 for_each_td_index() {
500 if (eta_secs[__td_index] < je->eta_sec)
501 je->eta_sec = eta_secs[__td_index];
504 unsigned long eta_stone = 0;
508 if ((td->runstate == TD_NOT_CREATED) && td->o.stonewall)
509 eta_stone += eta_secs[__td_index];
511 if (eta_secs[__td_index] > je->eta_sec)
512 je->eta_sec = eta_secs[__td_index];
515 je->eta_sec += eta_stone;
520 fio_gettime(&now, NULL);
521 rate_time = mtime_since(&rate_prev_time, &now);
523 any_td_in_ramp = false;
525 any_td_in_ramp |= in_ramp_time(td);
527 if (write_bw_log && rate_time > bw_avg_time && !any_td_in_ramp) {
528 calc_rate(unified_rw_rep, rate_time, io_bytes, rate_io_bytes,
530 memcpy(&rate_prev_time, &now, sizeof(now));
532 for_each_rw_ddir(ddir) {
533 add_agg_sample(sample_val(je->rate[ddir]), ddir, 0);
537 disp_time = mtime_since(&disp_prev_time, &now);
539 if (!force && !eta_time_within_slack(disp_time))
542 calc_rate(unified_rw_rep, disp_time, io_bytes, disp_io_bytes, je->rate);
543 calc_iops(unified_rw_rep, disp_time, io_iops, disp_io_iops, je->iops);
545 memcpy(&disp_prev_time, &now, sizeof(now));
547 if (!force && !je->nr_running && !je->nr_pending)
550 je->nr_threads = thread_number;
551 update_condensed_str(__run_str, run_str);
552 memcpy(je->run_str, run_str, strlen(run_str));
556 static int gen_eta_str(struct jobs_eta *je, char *p, size_t left,
557 char **rate_str, char **iops_str)
559 static const char c[DDIR_RWDIR_CNT] = {'r', 'w', 't'};
560 bool has[DDIR_RWDIR_CNT];
561 bool has_any = false;
565 for_each_rw_ddir(ddir) {
566 has[ddir] = (je->rate[ddir] || je->iops[ddir]);
567 has_any |= has[ddir];
572 l += snprintf(p + l, left - l, "[");
574 for_each_rw_ddir(ddir) {
576 l += snprintf(p + l, left - l, "%s%c=%s",
577 sep, c[ddir], rate_str[ddir]);
581 l += snprintf(p + l, left - l, "][");
583 for_each_rw_ddir(ddir) {
585 l += snprintf(p + l, left - l, "%s%c=%s",
586 sep, c[ddir], iops_str[ddir]);
590 l += snprintf(p + l, left - l, " IOPS]");
595 void display_thread_status(struct jobs_eta *je)
597 static struct timespec disp_eta_new_line;
598 static int eta_new_line_init, eta_new_line_pending;
599 static int linelen_last;
601 char output[__THREAD_RUNSTR_SZ(REAL_MAX_JOBS) + 512], *p = output;
605 if (je->eta_sec != INT_MAX && je->elapsed_sec) {
606 perc = (double) je->elapsed_sec / (double) (je->elapsed_sec + je->eta_sec);
607 eta_to_str(eta_str, je->eta_sec);
610 if (eta_new_line_pending) {
611 eta_new_line_pending = 0;
613 p += sprintf(p, "\n");
616 p += sprintf(p, "Jobs: %d (f=%d)", je->nr_running, je->files_open);
618 /* rate limits, if any */
619 if (je->m_rate[0] || je->m_rate[1] || je->m_rate[2] ||
620 je->t_rate[0] || je->t_rate[1] || je->t_rate[2]) {
623 mr = num2str(je->m_rate[0] + je->m_rate[1] + je->m_rate[2],
624 je->sig_figs, 1, je->is_pow2, N2S_BYTEPERSEC);
625 tr = num2str(je->t_rate[0] + je->t_rate[1] + je->t_rate[2],
626 je->sig_figs, 1, je->is_pow2, N2S_BYTEPERSEC);
628 p += sprintf(p, ", %s-%s", mr, tr);
631 } else if (je->m_iops[0] || je->m_iops[1] || je->m_iops[2] ||
632 je->t_iops[0] || je->t_iops[1] || je->t_iops[2]) {
633 p += sprintf(p, ", %d-%d IOPS",
634 je->m_iops[0] + je->m_iops[1] + je->m_iops[2],
635 je->t_iops[0] + je->t_iops[1] + je->t_iops[2]);
638 /* current run string, % done, bandwidth, iops, eta */
639 if (je->eta_sec != INT_MAX && je->nr_running) {
641 char *iops_str[DDIR_RWDIR_CNT];
642 char *rate_str[DDIR_RWDIR_CNT];
648 if ((!je->eta_sec && !eta_good) || je->nr_ramp == je->nr_running ||
650 strcpy(perc_str, "-.-%");
654 if (je->nr_setting_up && je->nr_running)
655 mult *= (1.0 - (double) je->nr_setting_up / (double) je->nr_running);
659 sprintf(perc_str, "%3.1f%%", perc);
662 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
663 rate_str[ddir] = num2str(je->rate[ddir], 4,
664 1024, je->is_pow2, je->unit_base);
665 iops_str[ddir] = num2str(je->iops[ddir], 4, 1, 0, N2S_NONE);
668 left = sizeof(output) - (p - output) - 1;
669 l = snprintf(p, left, ": [%s][%s]", je->run_str, perc_str);
670 l += gen_eta_str(je, p + l, left - l, rate_str, iops_str);
671 l += snprintf(p + l, left - l, "[eta %s]", eta_str);
673 /* If truncation occurred adjust l so p is on the null */
677 linelen = p - output;
678 if (l >= 0 && linelen < linelen_last)
679 p += sprintf(p, "%*s", linelen_last - linelen, "");
680 linelen_last = linelen;
682 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
683 free(rate_str[ddir]);
684 free(iops_str[ddir]);
689 printf("%s", output);
691 if (!eta_new_line_init) {
692 fio_gettime(&disp_eta_new_line, NULL);
693 eta_new_line_init = 1;
694 } else if (eta_new_line && mtime_since_now(&disp_eta_new_line) > eta_new_line) {
695 fio_gettime(&disp_eta_new_line, NULL);
696 eta_new_line_pending = 1;
702 struct jobs_eta *get_jobs_eta(bool force, size_t *size)
709 *size = sizeof(*je) + THREAD_RUNSTR_SZ + 8;
710 je = calloc(1, *size);
714 if (!calc_thread_status(je, force)) {
719 *size = sizeof(*je) + strlen((char *) je->run_str) + 1;
723 void print_thread_status(void)
728 je = get_jobs_eta(false, &size);
730 display_thread_status(je);
735 void print_status_init(int thr_number)
737 struct jobs_eta_packed jep;
739 compiletime_assert(sizeof(struct jobs_eta) == sizeof(jep), "jobs_eta");
741 DRD_IGNORE_VAR(__run_str);
742 __run_str[thr_number] = 'P';
743 update_condensed_str(__run_str, run_str);