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 str += 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;
181 if (td->o.zone_size && td->o.zone_skip && bytes_total) {
182 unsigned int nr_zones;
185 zone_bytes = bytes_total + td->o.zone_size + td->o.zone_skip;
186 nr_zones = (zone_bytes - 1) / (td->o.zone_size + td->o.zone_skip);
187 bytes_total -= nr_zones * td->o.zone_skip;
191 * if writing and verifying afterwards, bytes_total will be twice the
192 * size. In a mixed workload, verify phase will be the size of the
193 * first stage writes.
195 if (td->o.do_verify && td->o.verify && td_write(td)) {
197 unsigned int perc = 50;
199 if (td->o.rwmix[DDIR_WRITE])
200 perc = td->o.rwmix[DDIR_WRITE];
202 bytes_total += (bytes_total * perc) / 100;
207 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
210 bytes_done = ddir_rw_sum(td->io_bytes);
213 perc = (double) bytes_done / (double) bytes_total;
219 if (td->o.time_based) {
221 perc_t = (double) elapsed / (double) timeout;
226 * Will never hit, we can't have time_based
227 * without a timeout set.
236 eta_sec = (unsigned long) (elapsed * (1.0 / perc)) - elapsed;
240 eta_sec > (timeout + done_secs - elapsed))
241 eta_sec = timeout + done_secs - elapsed;
242 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
243 || td->runstate == TD_INITIALIZED
244 || td->runstate == TD_SETTING_UP
245 || td->runstate == TD_RAMP
246 || td->runstate == TD_PRE_READING) {
247 int64_t t_eta = 0, r_eta = 0;
248 unsigned long long rate_bytes;
251 * We can only guess - assume it'll run the full timeout
252 * if given, otherwise assume it'll run at the specified rate.
255 uint64_t __timeout = td->o.timeout;
256 uint64_t start_delay = td->o.start_delay;
257 uint64_t ramp_time = td->o.ramp_time;
259 t_eta = __timeout + start_delay;
260 if (!td->ramp_time_over) {
265 if ((td->runstate == TD_RAMP) && in_ramp_time(td)) {
266 unsigned long ramp_left;
268 ramp_left = mtime_since_now(&td->epoch);
269 ramp_left = (ramp_left + 999) / 1000;
270 if (ramp_left <= t_eta)
276 rate_bytes = td->o.rate[DDIR_READ];
278 rate_bytes += td->o.rate[DDIR_WRITE];
280 rate_bytes += td->o.rate[DDIR_TRIM];
283 r_eta = bytes_total / rate_bytes;
284 r_eta += (td->o.start_delay / 1000000ULL);
288 eta_sec = min(r_eta, t_eta);
297 * thread is already done or waiting for fsync
305 static void calc_rate(int unified_rw_rep, unsigned long mtime,
306 unsigned long long *io_bytes,
307 unsigned long long *prev_io_bytes, uint64_t *rate)
311 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
312 unsigned long long diff, this_rate;
314 diff = io_bytes[i] - prev_io_bytes[i];
316 this_rate = ((1000 * diff) / mtime) / 1024; /* KiB/s */
320 if (unified_rw_rep) {
322 rate[0] += this_rate;
326 prev_io_bytes[i] = io_bytes[i];
330 static void calc_iops(int unified_rw_rep, unsigned long mtime,
331 unsigned long long *io_iops,
332 unsigned long long *prev_io_iops, unsigned int *iops)
336 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
337 unsigned long long diff, this_iops;
339 diff = io_iops[i] - prev_io_iops[i];
341 this_iops = (diff * 1000) / mtime;
345 if (unified_rw_rep) {
347 iops[0] += this_iops;
351 prev_io_iops[i] = io_iops[i];
356 * Allow a little slack - if we're within 95% of the time, allow ETA.
358 bool eta_time_within_slack(unsigned int time)
360 return time > ((eta_interval_msec * 95) / 100);
364 * Print status of the jobs we know about. This includes rate estimates,
365 * ETA, thread state, etc.
367 bool calc_thread_status(struct jobs_eta *je, int force)
369 struct thread_data *td;
370 int i, unified_rw_rep;
371 uint64_t rate_time, disp_time, bw_avg_time, *eta_secs;
372 unsigned long long io_bytes[DDIR_RWDIR_CNT];
373 unsigned long long io_iops[DDIR_RWDIR_CNT];
376 static unsigned long long rate_io_bytes[DDIR_RWDIR_CNT];
377 static unsigned long long disp_io_bytes[DDIR_RWDIR_CNT];
378 static unsigned long long disp_io_iops[DDIR_RWDIR_CNT];
379 static struct timespec rate_prev_time, disp_prev_time;
382 if (!(output_format & FIO_OUTPUT_NORMAL) &&
385 if (temp_stall_ts || eta_print == FIO_ETA_NEVER)
388 if (!isatty(STDOUT_FILENO) && (eta_print != FIO_ETA_ALWAYS))
392 if (!ddir_rw_sum(rate_io_bytes))
393 fill_start_time(&rate_prev_time);
394 if (!ddir_rw_sum(disp_io_bytes))
395 fill_start_time(&disp_prev_time);
397 eta_secs = malloc(thread_number * sizeof(uint64_t));
398 memset(eta_secs, 0, thread_number * sizeof(uint64_t));
400 je->elapsed_sec = (mtime_since_genesis() + 999) / 1000;
402 io_bytes[DDIR_READ] = io_bytes[DDIR_WRITE] = io_bytes[DDIR_TRIM] = 0;
403 io_iops[DDIR_READ] = io_iops[DDIR_WRITE] = io_iops[DDIR_TRIM] = 0;
404 bw_avg_time = ULONG_MAX;
407 unified_rw_rep += td->o.unified_rw_rep;
408 if (is_power_of_2(td->o.kb_base))
410 je->unit_base = td->o.unit_base;
411 if (td->o.bw_avg_time < bw_avg_time)
412 bw_avg_time = td->o.bw_avg_time;
413 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING
414 || td->runstate == TD_FSYNCING
415 || td->runstate == TD_PRE_READING
416 || td->runstate == TD_FINISHING) {
419 je->t_rate[0] += td->o.rate[DDIR_READ];
420 je->t_iops[0] += td->o.rate_iops[DDIR_READ];
421 je->m_rate[0] += td->o.ratemin[DDIR_READ];
422 je->m_iops[0] += td->o.rate_iops_min[DDIR_READ];
425 je->t_rate[1] += td->o.rate[DDIR_WRITE];
426 je->t_iops[1] += td->o.rate_iops[DDIR_WRITE];
427 je->m_rate[1] += td->o.ratemin[DDIR_WRITE];
428 je->m_iops[1] += td->o.rate_iops_min[DDIR_WRITE];
431 je->t_rate[2] += td->o.rate[DDIR_TRIM];
432 je->t_iops[2] += td->o.rate_iops[DDIR_TRIM];
433 je->m_rate[2] += td->o.ratemin[DDIR_TRIM];
434 je->m_iops[2] += td->o.rate_iops_min[DDIR_TRIM];
437 je->files_open += td->nr_open_files;
438 } else if (td->runstate == TD_RAMP) {
441 } else if (td->runstate == TD_SETTING_UP)
443 else if (td->runstate < TD_RUNNING)
446 if (je->elapsed_sec >= 3)
447 eta_secs[i] = thread_eta(td);
449 eta_secs[i] = INT_MAX;
451 check_str_update(td);
453 if (td->runstate > TD_SETTING_UP) {
456 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
457 if (unified_rw_rep) {
458 io_bytes[0] += td->io_bytes[ddir];
459 io_iops[0] += td->io_blocks[ddir];
461 io_bytes[ddir] += td->io_bytes[ddir];
462 io_iops[ddir] += td->io_blocks[ddir];
468 if (exitall_on_terminate) {
469 je->eta_sec = INT_MAX;
471 if (eta_secs[i] < je->eta_sec)
472 je->eta_sec = eta_secs[i];
475 unsigned long eta_stone = 0;
479 if ((td->runstate == TD_NOT_CREATED) && td->o.stonewall)
480 eta_stone += eta_secs[i];
482 if (eta_secs[i] > je->eta_sec)
483 je->eta_sec = eta_secs[i];
486 je->eta_sec += eta_stone;
491 fio_gettime(&now, NULL);
492 rate_time = mtime_since(&rate_prev_time, &now);
494 if (write_bw_log && rate_time > bw_avg_time && !in_ramp_time(td)) {
495 calc_rate(unified_rw_rep, rate_time, io_bytes, rate_io_bytes,
497 memcpy(&rate_prev_time, &now, sizeof(now));
498 add_agg_sample(sample_val(je->rate[DDIR_READ]), DDIR_READ, 0);
499 add_agg_sample(sample_val(je->rate[DDIR_WRITE]), DDIR_WRITE, 0);
500 add_agg_sample(sample_val(je->rate[DDIR_TRIM]), DDIR_TRIM, 0);
503 disp_time = mtime_since(&disp_prev_time, &now);
505 if (!force && !eta_time_within_slack(disp_time))
508 calc_rate(unified_rw_rep, disp_time, io_bytes, disp_io_bytes, je->rate);
509 calc_iops(unified_rw_rep, disp_time, io_iops, disp_io_iops, je->iops);
511 memcpy(&disp_prev_time, &now, sizeof(now));
513 if (!force && !je->nr_running && !je->nr_pending)
516 je->nr_threads = thread_number;
517 update_condensed_str(__run_str, run_str);
518 memcpy(je->run_str, run_str, strlen(run_str));
522 void display_thread_status(struct jobs_eta *je)
524 static struct timespec disp_eta_new_line;
525 static int eta_new_line_init, eta_new_line_pending;
526 static int linelen_last;
528 char output[__THREAD_RUNSTR_SZ(REAL_MAX_JOBS) + 512], *p = output;
532 if (je->eta_sec != INT_MAX && je->elapsed_sec) {
533 perc = (double) je->elapsed_sec / (double) (je->elapsed_sec + je->eta_sec);
534 eta_to_str(eta_str, je->eta_sec);
537 if (eta_new_line_pending) {
538 eta_new_line_pending = 0;
540 p += sprintf(p, "\n");
543 p += sprintf(p, "Jobs: %d (f=%d)", je->nr_running, je->files_open);
545 /* rate limits, if any */
546 if (je->m_rate[0] || je->m_rate[1] || je->m_rate[2] ||
547 je->t_rate[0] || je->t_rate[1] || je->t_rate[2]) {
550 mr = num2str(je->m_rate[0] + je->m_rate[1] + je->m_rate[2],
551 je->sig_figs, 0, je->is_pow2, N2S_BYTEPERSEC);
552 tr = num2str(je->t_rate[0] + je->t_rate[1] + je->t_rate[2],
553 je->sig_figs, 0, je->is_pow2, N2S_BYTEPERSEC);
555 p += sprintf(p, ", %s-%s", mr, tr);
558 } else if (je->m_iops[0] || je->m_iops[1] || je->m_iops[2] ||
559 je->t_iops[0] || je->t_iops[1] || je->t_iops[2]) {
560 p += sprintf(p, ", %d-%d IOPS",
561 je->m_iops[0] + je->m_iops[1] + je->m_iops[2],
562 je->t_iops[0] + je->t_iops[1] + je->t_iops[2]);
565 /* current run string, % done, bandwidth, iops, eta */
566 if (je->eta_sec != INT_MAX && je->nr_running) {
568 char *iops_str[DDIR_RWDIR_CNT];
569 char *rate_str[DDIR_RWDIR_CNT];
575 if ((!je->eta_sec && !eta_good) || je->nr_ramp == je->nr_running ||
577 strcpy(perc_str, "-.-%");
581 if (je->nr_setting_up && je->nr_running)
582 mult *= (1.0 - (double) je->nr_setting_up / (double) je->nr_running);
586 sprintf(perc_str, "%3.1f%%", perc);
589 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
590 rate_str[ddir] = num2str(je->rate[ddir], 4,
591 1024, je->is_pow2, je->unit_base);
592 iops_str[ddir] = num2str(je->iops[ddir], 4, 1, 0, N2S_NONE);
595 left = sizeof(output) - (p - output) - 1;
597 if (je->rate[DDIR_TRIM] || je->iops[DDIR_TRIM])
598 l = snprintf(p, left,
599 ": [%s][%s][r=%s,w=%s,t=%s][r=%s,w=%s,t=%s IOPS][eta %s]",
600 je->run_str, perc_str, rate_str[DDIR_READ],
601 rate_str[DDIR_WRITE], rate_str[DDIR_TRIM],
602 iops_str[DDIR_READ], iops_str[DDIR_WRITE],
603 iops_str[DDIR_TRIM], eta_str);
605 l = snprintf(p, left,
606 ": [%s][%s][r=%s,w=%s][r=%s,w=%s IOPS][eta %s]",
607 je->run_str, perc_str,
608 rate_str[DDIR_READ], rate_str[DDIR_WRITE],
609 iops_str[DDIR_READ], iops_str[DDIR_WRITE],
611 /* If truncation occurred adjust l so p is on the null */
615 linelen = p - output;
616 if (l >= 0 && linelen < linelen_last)
617 p += sprintf(p, "%*s", linelen_last - linelen, "");
618 linelen_last = linelen;
620 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
621 free(rate_str[ddir]);
622 free(iops_str[ddir]);
625 p += sprintf(p, "\r");
627 printf("%s", output);
629 if (!eta_new_line_init) {
630 fio_gettime(&disp_eta_new_line, NULL);
631 eta_new_line_init = 1;
632 } else if (eta_new_line && mtime_since_now(&disp_eta_new_line) > eta_new_line) {
633 fio_gettime(&disp_eta_new_line, NULL);
634 eta_new_line_pending = 1;
640 struct jobs_eta *get_jobs_eta(bool force, size_t *size)
647 *size = sizeof(*je) + THREAD_RUNSTR_SZ + 8;
651 memset(je, 0, *size);
653 if (!calc_thread_status(je, force)) {
658 *size = sizeof(*je) + strlen((char *) je->run_str) + 1;
662 void print_thread_status(void)
667 je = get_jobs_eta(false, &size);
669 display_thread_status(je);
674 void print_status_init(int thr_number)
676 DRD_IGNORE_VAR(__run_str);
677 __run_str[thr_number] = 'P';
678 update_condensed_str(__run_str, run_str);