| 1 | /* |
| 2 | * Status and ETA code |
| 3 | */ |
| 4 | #include <unistd.h> |
| 5 | #include <string.h> |
| 6 | #include <stdlib.h> |
| 7 | #ifdef CONFIG_VALGRIND_DEV |
| 8 | #include <valgrind/drd.h> |
| 9 | #else |
| 10 | #define DRD_IGNORE_VAR(x) do { } while (0) |
| 11 | #endif |
| 12 | |
| 13 | #include "fio.h" |
| 14 | #include "lib/pow2.h" |
| 15 | |
| 16 | static char __run_str[REAL_MAX_JOBS + 1]; |
| 17 | static char run_str[__THREAD_RUNSTR_SZ(REAL_MAX_JOBS) + 1]; |
| 18 | |
| 19 | static void update_condensed_str(char *rstr, char *run_str_condensed) |
| 20 | { |
| 21 | if (*rstr) { |
| 22 | while (*rstr) { |
| 23 | int nr = 1; |
| 24 | |
| 25 | *run_str_condensed++ = *rstr++; |
| 26 | while (*(rstr - 1) == *rstr) { |
| 27 | rstr++; |
| 28 | nr++; |
| 29 | } |
| 30 | run_str_condensed += sprintf(run_str_condensed, "(%u),", nr); |
| 31 | } |
| 32 | run_str_condensed--; |
| 33 | } |
| 34 | *run_str_condensed = '\0'; |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * Sets the status of the 'td' in the printed status map. |
| 39 | */ |
| 40 | static void check_str_update(struct thread_data *td) |
| 41 | { |
| 42 | char c = __run_str[td->thread_number - 1]; |
| 43 | |
| 44 | switch (td->runstate) { |
| 45 | case TD_REAPED: |
| 46 | if (td->error) |
| 47 | c = 'X'; |
| 48 | else if (td->sig) |
| 49 | c = 'K'; |
| 50 | else |
| 51 | c = '_'; |
| 52 | break; |
| 53 | case TD_EXITED: |
| 54 | c = 'E'; |
| 55 | break; |
| 56 | case TD_RAMP: |
| 57 | c = '/'; |
| 58 | break; |
| 59 | case TD_RUNNING: |
| 60 | if (td_rw(td)) { |
| 61 | if (td_random(td)) { |
| 62 | if (td->o.rwmix[DDIR_READ] == 100) |
| 63 | c = 'r'; |
| 64 | else if (td->o.rwmix[DDIR_WRITE] == 100) |
| 65 | c = 'w'; |
| 66 | else |
| 67 | c = 'm'; |
| 68 | } else { |
| 69 | if (td->o.rwmix[DDIR_READ] == 100) |
| 70 | c = 'R'; |
| 71 | else if (td->o.rwmix[DDIR_WRITE] == 100) |
| 72 | c = 'W'; |
| 73 | else |
| 74 | c = 'M'; |
| 75 | } |
| 76 | } else if (td_read(td)) { |
| 77 | if (td_random(td)) |
| 78 | c = 'r'; |
| 79 | else |
| 80 | c = 'R'; |
| 81 | } else if (td_write(td)) { |
| 82 | if (td_random(td)) |
| 83 | c = 'w'; |
| 84 | else |
| 85 | c = 'W'; |
| 86 | } else { |
| 87 | if (td_random(td)) |
| 88 | c = 'd'; |
| 89 | else |
| 90 | c = 'D'; |
| 91 | } |
| 92 | break; |
| 93 | case TD_PRE_READING: |
| 94 | c = 'p'; |
| 95 | break; |
| 96 | case TD_VERIFYING: |
| 97 | c = 'V'; |
| 98 | break; |
| 99 | case TD_FSYNCING: |
| 100 | c = 'F'; |
| 101 | break; |
| 102 | case TD_FINISHING: |
| 103 | c = 'f'; |
| 104 | break; |
| 105 | case TD_CREATED: |
| 106 | c = 'C'; |
| 107 | break; |
| 108 | case TD_INITIALIZED: |
| 109 | case TD_SETTING_UP: |
| 110 | c = 'I'; |
| 111 | break; |
| 112 | case TD_NOT_CREATED: |
| 113 | c = 'P'; |
| 114 | break; |
| 115 | default: |
| 116 | log_err("state %d\n", td->runstate); |
| 117 | } |
| 118 | |
| 119 | __run_str[td->thread_number - 1] = c; |
| 120 | update_condensed_str(__run_str, run_str); |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Convert seconds to a printable string. |
| 125 | */ |
| 126 | void eta_to_str(char *str, unsigned long eta_sec) |
| 127 | { |
| 128 | unsigned int d, h, m, s; |
| 129 | int disp_hour = 0; |
| 130 | |
| 131 | if (eta_sec == -1) { |
| 132 | sprintf(str, "--"); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | s = eta_sec % 60; |
| 137 | eta_sec /= 60; |
| 138 | m = eta_sec % 60; |
| 139 | eta_sec /= 60; |
| 140 | h = eta_sec % 24; |
| 141 | eta_sec /= 24; |
| 142 | d = eta_sec; |
| 143 | |
| 144 | if (d) { |
| 145 | disp_hour = 1; |
| 146 | str += sprintf(str, "%02ud:", d); |
| 147 | } |
| 148 | |
| 149 | if (h || disp_hour) |
| 150 | str += sprintf(str, "%02uh:", h); |
| 151 | |
| 152 | str += sprintf(str, "%02um:", m); |
| 153 | sprintf(str, "%02us", s); |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Best effort calculation of the estimated pending runtime of a job. |
| 158 | */ |
| 159 | static unsigned long thread_eta(struct thread_data *td) |
| 160 | { |
| 161 | unsigned long long bytes_total, bytes_done; |
| 162 | unsigned long eta_sec = 0; |
| 163 | unsigned long elapsed; |
| 164 | uint64_t timeout; |
| 165 | |
| 166 | elapsed = (mtime_since_now(&td->epoch) + 999) / 1000; |
| 167 | timeout = td->o.timeout / 1000000UL; |
| 168 | |
| 169 | bytes_total = td->total_io_size; |
| 170 | |
| 171 | if (td->flags & TD_F_NO_PROGRESS) |
| 172 | return -1; |
| 173 | |
| 174 | if (td->o.fill_device && td->o.size == -1ULL) { |
| 175 | if (!td->fill_device_size || td->fill_device_size == -1ULL) |
| 176 | return 0; |
| 177 | |
| 178 | bytes_total = td->fill_device_size; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * If io_size is set, bytes_total is an exact value that does not need |
| 183 | * adjustment. |
| 184 | */ |
| 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; |
| 188 | uint64_t zone_bytes; |
| 189 | |
| 190 | /* |
| 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. |
| 197 | */ |
| 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; |
| 203 | } |
| 204 | |
| 205 | /* |
| 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. |
| 209 | */ |
| 210 | if (td->o.do_verify && td->o.verify && td_write(td)) { |
| 211 | if (td_rw(td)) { |
| 212 | unsigned int perc = 50; |
| 213 | |
| 214 | if (td->o.rwmix[DDIR_WRITE]) |
| 215 | perc = td->o.rwmix[DDIR_WRITE]; |
| 216 | |
| 217 | bytes_total += (bytes_total * perc) / 100; |
| 218 | } else |
| 219 | bytes_total <<= 1; |
| 220 | } |
| 221 | |
| 222 | if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) { |
| 223 | double perc, perc_t; |
| 224 | |
| 225 | bytes_done = ddir_rw_sum(td->io_bytes); |
| 226 | |
| 227 | if (bytes_total) { |
| 228 | perc = (double) bytes_done / (double) bytes_total; |
| 229 | if (perc > 1.0) |
| 230 | perc = 1.0; |
| 231 | } else |
| 232 | perc = 0.0; |
| 233 | |
| 234 | if (td->o.time_based) { |
| 235 | if (timeout) { |
| 236 | perc_t = (double) elapsed / (double) timeout; |
| 237 | if (perc_t < perc) |
| 238 | perc = perc_t; |
| 239 | } else { |
| 240 | /* |
| 241 | * Will never hit, we can't have time_based |
| 242 | * without a timeout set. |
| 243 | */ |
| 244 | perc = 0.0; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if (perc == 0.0) { |
| 249 | eta_sec = timeout; |
| 250 | } else { |
| 251 | eta_sec = (unsigned long) (elapsed * (1.0 / perc)) - elapsed; |
| 252 | } |
| 253 | |
| 254 | if (td->o.timeout && |
| 255 | eta_sec > (timeout + done_secs - elapsed)) |
| 256 | eta_sec = timeout + done_secs - elapsed; |
| 257 | } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED |
| 258 | || td->runstate == TD_INITIALIZED |
| 259 | || td->runstate == TD_SETTING_UP |
| 260 | || td->runstate == TD_RAMP |
| 261 | || td->runstate == TD_PRE_READING) { |
| 262 | int64_t t_eta = 0, r_eta = 0; |
| 263 | unsigned long long rate_bytes; |
| 264 | |
| 265 | /* |
| 266 | * We can only guess - assume it'll run the full timeout |
| 267 | * if given, otherwise assume it'll run at the specified rate. |
| 268 | */ |
| 269 | if (td->o.timeout) { |
| 270 | uint64_t __timeout = td->o.timeout; |
| 271 | uint64_t start_delay = td->o.start_delay; |
| 272 | uint64_t ramp_time = td->o.ramp_time; |
| 273 | |
| 274 | t_eta = __timeout + start_delay; |
| 275 | if (!td->ramp_time_over) { |
| 276 | t_eta += ramp_time; |
| 277 | } |
| 278 | t_eta /= 1000000ULL; |
| 279 | |
| 280 | if ((td->runstate == TD_RAMP) && in_ramp_time(td)) { |
| 281 | unsigned long ramp_left; |
| 282 | |
| 283 | ramp_left = mtime_since_now(&td->epoch); |
| 284 | ramp_left = (ramp_left + 999) / 1000; |
| 285 | if (ramp_left <= t_eta) |
| 286 | t_eta -= ramp_left; |
| 287 | } |
| 288 | } |
| 289 | rate_bytes = 0; |
| 290 | if (td_read(td)) |
| 291 | rate_bytes = td->o.rate[DDIR_READ]; |
| 292 | if (td_write(td)) |
| 293 | rate_bytes += td->o.rate[DDIR_WRITE]; |
| 294 | if (td_trim(td)) |
| 295 | rate_bytes += td->o.rate[DDIR_TRIM]; |
| 296 | |
| 297 | if (rate_bytes) { |
| 298 | r_eta = bytes_total / rate_bytes; |
| 299 | r_eta += (td->o.start_delay / 1000000ULL); |
| 300 | } |
| 301 | |
| 302 | if (r_eta && t_eta) |
| 303 | eta_sec = min(r_eta, t_eta); |
| 304 | else if (r_eta) |
| 305 | eta_sec = r_eta; |
| 306 | else if (t_eta) |
| 307 | eta_sec = t_eta; |
| 308 | else |
| 309 | eta_sec = 0; |
| 310 | } else { |
| 311 | /* |
| 312 | * thread is already done or waiting for fsync |
| 313 | */ |
| 314 | eta_sec = 0; |
| 315 | } |
| 316 | |
| 317 | return eta_sec; |
| 318 | } |
| 319 | |
| 320 | static void calc_rate(int unified_rw_rep, unsigned long mtime, |
| 321 | unsigned long long *io_bytes, |
| 322 | unsigned long long *prev_io_bytes, uint64_t *rate) |
| 323 | { |
| 324 | int i; |
| 325 | |
| 326 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
| 327 | unsigned long long diff, this_rate; |
| 328 | |
| 329 | diff = io_bytes[i] - prev_io_bytes[i]; |
| 330 | if (mtime) |
| 331 | this_rate = ((1000 * diff) / mtime) / 1024; /* KiB/s */ |
| 332 | else |
| 333 | this_rate = 0; |
| 334 | |
| 335 | if (unified_rw_rep == UNIFIED_MIXED) { |
| 336 | rate[i] = 0; |
| 337 | rate[0] += this_rate; |
| 338 | } else |
| 339 | rate[i] = this_rate; |
| 340 | |
| 341 | prev_io_bytes[i] = io_bytes[i]; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | static void calc_iops(int unified_rw_rep, unsigned long mtime, |
| 346 | unsigned long long *io_iops, |
| 347 | unsigned long long *prev_io_iops, unsigned int *iops) |
| 348 | { |
| 349 | int i; |
| 350 | |
| 351 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
| 352 | unsigned long long diff, this_iops; |
| 353 | |
| 354 | diff = io_iops[i] - prev_io_iops[i]; |
| 355 | if (mtime) |
| 356 | this_iops = (diff * 1000) / mtime; |
| 357 | else |
| 358 | this_iops = 0; |
| 359 | |
| 360 | if (unified_rw_rep == UNIFIED_MIXED) { |
| 361 | iops[i] = 0; |
| 362 | iops[0] += this_iops; |
| 363 | } else |
| 364 | iops[i] = this_iops; |
| 365 | |
| 366 | prev_io_iops[i] = io_iops[i]; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * Allow a little slack - if we're within 95% of the time, allow ETA. |
| 372 | */ |
| 373 | bool eta_time_within_slack(unsigned int time) |
| 374 | { |
| 375 | return time > ((eta_interval_msec * 95) / 100); |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * These are the conditions under which we might be able to skip the eta |
| 380 | * calculation. |
| 381 | */ |
| 382 | static bool skip_eta() |
| 383 | { |
| 384 | if (!(output_format & FIO_OUTPUT_NORMAL) && f_out == stdout) |
| 385 | return true; |
| 386 | if (temp_stall_ts || eta_print == FIO_ETA_NEVER) |
| 387 | return true; |
| 388 | if (!isatty(STDOUT_FILENO) && eta_print != FIO_ETA_ALWAYS) |
| 389 | return true; |
| 390 | |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Print status of the jobs we know about. This includes rate estimates, |
| 396 | * ETA, thread state, etc. |
| 397 | */ |
| 398 | bool calc_thread_status(struct jobs_eta *je, int force) |
| 399 | { |
| 400 | int unified_rw_rep; |
| 401 | bool any_td_in_ramp; |
| 402 | uint64_t rate_time, disp_time, bw_avg_time, *eta_secs; |
| 403 | unsigned long long io_bytes[DDIR_RWDIR_CNT] = {}; |
| 404 | unsigned long long io_iops[DDIR_RWDIR_CNT] = {}; |
| 405 | struct timespec now; |
| 406 | |
| 407 | static unsigned long long rate_io_bytes[DDIR_RWDIR_CNT]; |
| 408 | static unsigned long long disp_io_bytes[DDIR_RWDIR_CNT]; |
| 409 | static unsigned long long disp_io_iops[DDIR_RWDIR_CNT]; |
| 410 | static struct timespec rate_prev_time, disp_prev_time; |
| 411 | |
| 412 | bool ret = true; |
| 413 | |
| 414 | if (!force && skip_eta()) { |
| 415 | if (write_bw_log) |
| 416 | ret = false; |
| 417 | else |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | if (!ddir_rw_sum(rate_io_bytes)) |
| 422 | fill_start_time(&rate_prev_time); |
| 423 | if (!ddir_rw_sum(disp_io_bytes)) |
| 424 | fill_start_time(&disp_prev_time); |
| 425 | |
| 426 | eta_secs = calloc(thread_number, sizeof(uint64_t)); |
| 427 | |
| 428 | je->elapsed_sec = (mtime_since_genesis() + 999) / 1000; |
| 429 | |
| 430 | bw_avg_time = ULONG_MAX; |
| 431 | unified_rw_rep = 0; |
| 432 | for_each_td(td) { |
| 433 | unified_rw_rep += td->o.unified_rw_rep; |
| 434 | if (is_power_of_2(td->o.kb_base)) |
| 435 | je->is_pow2 = 1; |
| 436 | je->unit_base = td->o.unit_base; |
| 437 | je->sig_figs = td->o.sig_figs; |
| 438 | if (td->o.bw_avg_time < bw_avg_time) |
| 439 | bw_avg_time = td->o.bw_avg_time; |
| 440 | if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING |
| 441 | || td->runstate == TD_FSYNCING |
| 442 | || td->runstate == TD_PRE_READING |
| 443 | || td->runstate == TD_FINISHING) { |
| 444 | je->nr_running++; |
| 445 | if (td_read(td)) { |
| 446 | je->t_rate[0] += td->o.rate[DDIR_READ]; |
| 447 | je->t_iops[0] += td->o.rate_iops[DDIR_READ]; |
| 448 | je->m_rate[0] += td->o.ratemin[DDIR_READ]; |
| 449 | je->m_iops[0] += td->o.rate_iops_min[DDIR_READ]; |
| 450 | } |
| 451 | if (td_write(td)) { |
| 452 | je->t_rate[1] += td->o.rate[DDIR_WRITE]; |
| 453 | je->t_iops[1] += td->o.rate_iops[DDIR_WRITE]; |
| 454 | je->m_rate[1] += td->o.ratemin[DDIR_WRITE]; |
| 455 | je->m_iops[1] += td->o.rate_iops_min[DDIR_WRITE]; |
| 456 | } |
| 457 | if (td_trim(td)) { |
| 458 | je->t_rate[2] += td->o.rate[DDIR_TRIM]; |
| 459 | je->t_iops[2] += td->o.rate_iops[DDIR_TRIM]; |
| 460 | je->m_rate[2] += td->o.ratemin[DDIR_TRIM]; |
| 461 | je->m_iops[2] += td->o.rate_iops_min[DDIR_TRIM]; |
| 462 | } |
| 463 | |
| 464 | je->files_open += td->nr_open_files; |
| 465 | } else if (td->runstate == TD_RAMP) { |
| 466 | je->nr_running++; |
| 467 | je->nr_ramp++; |
| 468 | } else if (td->runstate == TD_SETTING_UP) |
| 469 | je->nr_setting_up++; |
| 470 | else if (td->runstate < TD_RUNNING) |
| 471 | je->nr_pending++; |
| 472 | |
| 473 | if (je->elapsed_sec >= 3) |
| 474 | eta_secs[__td_index] = thread_eta(td); |
| 475 | else |
| 476 | eta_secs[__td_index] = INT_MAX; |
| 477 | |
| 478 | check_str_update(td); |
| 479 | |
| 480 | if (td->runstate > TD_SETTING_UP) { |
| 481 | int ddir; |
| 482 | |
| 483 | for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) { |
| 484 | if (unified_rw_rep) { |
| 485 | io_bytes[0] += td->io_bytes[ddir]; |
| 486 | io_iops[0] += td->io_blocks[ddir]; |
| 487 | } else { |
| 488 | io_bytes[ddir] += td->io_bytes[ddir]; |
| 489 | io_iops[ddir] += td->io_blocks[ddir]; |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | } end_for_each(); |
| 494 | |
| 495 | if (exitall_on_terminate) { |
| 496 | je->eta_sec = INT_MAX; |
| 497 | for_each_td_index() { |
| 498 | if (eta_secs[__td_index] < je->eta_sec) |
| 499 | je->eta_sec = eta_secs[__td_index]; |
| 500 | } end_for_each(); |
| 501 | } else { |
| 502 | unsigned long eta_stone = 0; |
| 503 | |
| 504 | je->eta_sec = 0; |
| 505 | for_each_td(td) { |
| 506 | if ((td->runstate == TD_NOT_CREATED) && td->o.stonewall) |
| 507 | eta_stone += eta_secs[__td_index]; |
| 508 | else { |
| 509 | if (eta_secs[__td_index] > je->eta_sec) |
| 510 | je->eta_sec = eta_secs[__td_index]; |
| 511 | } |
| 512 | } end_for_each(); |
| 513 | je->eta_sec += eta_stone; |
| 514 | } |
| 515 | |
| 516 | free(eta_secs); |
| 517 | |
| 518 | fio_gettime(&now, NULL); |
| 519 | rate_time = mtime_since(&rate_prev_time, &now); |
| 520 | |
| 521 | any_td_in_ramp = false; |
| 522 | for_each_td(td) { |
| 523 | any_td_in_ramp |= in_ramp_time(td); |
| 524 | } end_for_each(); |
| 525 | if (write_bw_log && rate_time > bw_avg_time && !any_td_in_ramp) { |
| 526 | calc_rate(unified_rw_rep, rate_time, io_bytes, rate_io_bytes, |
| 527 | je->rate); |
| 528 | memcpy(&rate_prev_time, &now, sizeof(now)); |
| 529 | regrow_agg_logs(); |
| 530 | for_each_rw_ddir(ddir) { |
| 531 | add_agg_sample(sample_val(je->rate[ddir]), ddir, 0); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | disp_time = mtime_since(&disp_prev_time, &now); |
| 536 | |
| 537 | if (!force && !eta_time_within_slack(disp_time)) |
| 538 | return false; |
| 539 | |
| 540 | calc_rate(unified_rw_rep, disp_time, io_bytes, disp_io_bytes, je->rate); |
| 541 | calc_iops(unified_rw_rep, disp_time, io_iops, disp_io_iops, je->iops); |
| 542 | |
| 543 | memcpy(&disp_prev_time, &now, sizeof(now)); |
| 544 | |
| 545 | if (!force && !je->nr_running && !je->nr_pending) |
| 546 | return false; |
| 547 | |
| 548 | je->nr_threads = thread_number; |
| 549 | update_condensed_str(__run_str, run_str); |
| 550 | memcpy(je->run_str, run_str, strlen(run_str)); |
| 551 | return ret; |
| 552 | } |
| 553 | |
| 554 | static int gen_eta_str(struct jobs_eta *je, char *p, size_t left, |
| 555 | char **rate_str, char **iops_str) |
| 556 | { |
| 557 | static const char c[DDIR_RWDIR_CNT] = {'r', 'w', 't'}; |
| 558 | bool has[DDIR_RWDIR_CNT]; |
| 559 | bool has_any = false; |
| 560 | const char *sep; |
| 561 | int l = 0; |
| 562 | |
| 563 | for_each_rw_ddir(ddir) { |
| 564 | has[ddir] = (je->rate[ddir] || je->iops[ddir]); |
| 565 | has_any |= has[ddir]; |
| 566 | } |
| 567 | if (!has_any) |
| 568 | return 0; |
| 569 | |
| 570 | l += snprintf(p + l, left - l, "["); |
| 571 | sep = ""; |
| 572 | for_each_rw_ddir(ddir) { |
| 573 | if (has[ddir]) { |
| 574 | l += snprintf(p + l, left - l, "%s%c=%s", |
| 575 | sep, c[ddir], rate_str[ddir]); |
| 576 | sep = ","; |
| 577 | } |
| 578 | } |
| 579 | l += snprintf(p + l, left - l, "]["); |
| 580 | sep = ""; |
| 581 | for_each_rw_ddir(ddir) { |
| 582 | if (has[ddir]) { |
| 583 | l += snprintf(p + l, left - l, "%s%c=%s", |
| 584 | sep, c[ddir], iops_str[ddir]); |
| 585 | sep = ","; |
| 586 | } |
| 587 | } |
| 588 | l += snprintf(p + l, left - l, " IOPS]"); |
| 589 | |
| 590 | return l; |
| 591 | } |
| 592 | |
| 593 | void display_thread_status(struct jobs_eta *je) |
| 594 | { |
| 595 | static struct timespec disp_eta_new_line; |
| 596 | static int eta_new_line_init, eta_new_line_pending; |
| 597 | static int linelen_last; |
| 598 | static int eta_good; |
| 599 | char output[__THREAD_RUNSTR_SZ(REAL_MAX_JOBS) + 512], *p = output; |
| 600 | char eta_str[128]; |
| 601 | double perc = 0.0; |
| 602 | |
| 603 | if (je->eta_sec != INT_MAX && je->elapsed_sec) { |
| 604 | perc = (double) je->elapsed_sec / (double) (je->elapsed_sec + je->eta_sec); |
| 605 | eta_to_str(eta_str, je->eta_sec); |
| 606 | } |
| 607 | |
| 608 | if (eta_new_line_pending) { |
| 609 | eta_new_line_pending = 0; |
| 610 | linelen_last = 0; |
| 611 | p += sprintf(p, "\n"); |
| 612 | } |
| 613 | |
| 614 | p += sprintf(p, "Jobs: %d (f=%d)", je->nr_running, je->files_open); |
| 615 | |
| 616 | /* rate limits, if any */ |
| 617 | if (je->m_rate[0] || je->m_rate[1] || je->m_rate[2] || |
| 618 | je->t_rate[0] || je->t_rate[1] || je->t_rate[2]) { |
| 619 | char *tr, *mr; |
| 620 | |
| 621 | mr = num2str(je->m_rate[0] + je->m_rate[1] + je->m_rate[2], |
| 622 | je->sig_figs, 1, je->is_pow2, N2S_BYTEPERSEC); |
| 623 | tr = num2str(je->t_rate[0] + je->t_rate[1] + je->t_rate[2], |
| 624 | je->sig_figs, 1, je->is_pow2, N2S_BYTEPERSEC); |
| 625 | |
| 626 | p += sprintf(p, ", %s-%s", mr, tr); |
| 627 | free(tr); |
| 628 | free(mr); |
| 629 | } else if (je->m_iops[0] || je->m_iops[1] || je->m_iops[2] || |
| 630 | je->t_iops[0] || je->t_iops[1] || je->t_iops[2]) { |
| 631 | p += sprintf(p, ", %d-%d IOPS", |
| 632 | je->m_iops[0] + je->m_iops[1] + je->m_iops[2], |
| 633 | je->t_iops[0] + je->t_iops[1] + je->t_iops[2]); |
| 634 | } |
| 635 | |
| 636 | /* current run string, % done, bandwidth, iops, eta */ |
| 637 | if (je->eta_sec != INT_MAX && je->nr_running) { |
| 638 | char perc_str[32]; |
| 639 | char *iops_str[DDIR_RWDIR_CNT]; |
| 640 | char *rate_str[DDIR_RWDIR_CNT]; |
| 641 | size_t left; |
| 642 | int l; |
| 643 | int ddir; |
| 644 | int linelen; |
| 645 | |
| 646 | if ((!je->eta_sec && !eta_good) || je->nr_ramp == je->nr_running || |
| 647 | je->eta_sec == -1) |
| 648 | strcpy(perc_str, "-.-%"); |
| 649 | else { |
| 650 | double mult = 100.0; |
| 651 | |
| 652 | if (je->nr_setting_up && je->nr_running) |
| 653 | mult *= (1.0 - (double) je->nr_setting_up / (double) je->nr_running); |
| 654 | |
| 655 | eta_good = 1; |
| 656 | perc *= mult; |
| 657 | sprintf(perc_str, "%3.1f%%", perc); |
| 658 | } |
| 659 | |
| 660 | for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) { |
| 661 | rate_str[ddir] = num2str(je->rate[ddir], 4, |
| 662 | 1024, je->is_pow2, je->unit_base); |
| 663 | iops_str[ddir] = num2str(je->iops[ddir], 4, 1, 0, N2S_NONE); |
| 664 | } |
| 665 | |
| 666 | left = sizeof(output) - (p - output) - 1; |
| 667 | l = snprintf(p, left, ": [%s][%s]", je->run_str, perc_str); |
| 668 | l += gen_eta_str(je, p + l, left - l, rate_str, iops_str); |
| 669 | l += snprintf(p + l, left - l, "[eta %s]", eta_str); |
| 670 | |
| 671 | /* If truncation occurred adjust l so p is on the null */ |
| 672 | if (l >= left) |
| 673 | l = left - 1; |
| 674 | p += l; |
| 675 | linelen = p - output; |
| 676 | if (l >= 0 && linelen < linelen_last) |
| 677 | p += sprintf(p, "%*s", linelen_last - linelen, ""); |
| 678 | linelen_last = linelen; |
| 679 | |
| 680 | for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) { |
| 681 | free(rate_str[ddir]); |
| 682 | free(iops_str[ddir]); |
| 683 | } |
| 684 | } |
| 685 | sprintf(p, "\r"); |
| 686 | |
| 687 | printf("%s", output); |
| 688 | |
| 689 | if (!eta_new_line_init) { |
| 690 | fio_gettime(&disp_eta_new_line, NULL); |
| 691 | eta_new_line_init = 1; |
| 692 | } else if (eta_new_line && mtime_since_now(&disp_eta_new_line) > eta_new_line) { |
| 693 | fio_gettime(&disp_eta_new_line, NULL); |
| 694 | eta_new_line_pending = 1; |
| 695 | } |
| 696 | |
| 697 | fflush(stdout); |
| 698 | } |
| 699 | |
| 700 | struct jobs_eta *get_jobs_eta(bool force, size_t *size) |
| 701 | { |
| 702 | struct jobs_eta *je; |
| 703 | |
| 704 | if (!thread_number) |
| 705 | return NULL; |
| 706 | |
| 707 | *size = sizeof(*je) + THREAD_RUNSTR_SZ + 8; |
| 708 | je = calloc(1, *size); |
| 709 | if (!je) |
| 710 | return NULL; |
| 711 | |
| 712 | if (!calc_thread_status(je, force)) { |
| 713 | free(je); |
| 714 | return NULL; |
| 715 | } |
| 716 | |
| 717 | *size = sizeof(*je) + strlen((char *) je->run_str) + 1; |
| 718 | return je; |
| 719 | } |
| 720 | |
| 721 | void print_thread_status(void) |
| 722 | { |
| 723 | struct jobs_eta *je; |
| 724 | size_t size; |
| 725 | |
| 726 | je = get_jobs_eta(false, &size); |
| 727 | if (je) { |
| 728 | display_thread_status(je); |
| 729 | free(je); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | void print_status_init(int thr_number) |
| 734 | { |
| 735 | struct jobs_eta_packed jep; |
| 736 | |
| 737 | compiletime_assert(sizeof(struct jobs_eta) == sizeof(jep), "jobs_eta"); |
| 738 | |
| 739 | DRD_IGNORE_VAR(__run_str); |
| 740 | __run_str[thr_number] = 'P'; |
| 741 | update_condensed_str(__run_str, run_str); |
| 742 | } |