Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 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 | #include "os.h" |
| 10 | |
| 11 | static char run_str[MAX_JOBS + 1]; |
| 12 | |
| 13 | /* |
| 14 | * Sets the status of the 'td' in the printed status map. |
| 15 | */ |
| 16 | static void check_str_update(struct thread_data *td) |
| 17 | { |
| 18 | char c = run_str[td->thread_number - 1]; |
| 19 | |
| 20 | switch (td->runstate) { |
| 21 | case TD_REAPED: |
| 22 | c = '_'; |
| 23 | break; |
| 24 | case TD_EXITED: |
| 25 | c = 'E'; |
| 26 | break; |
| 27 | case TD_RUNNING: |
| 28 | if (td_rw(td)) { |
| 29 | if (td->sequential) |
| 30 | c = 'M'; |
| 31 | else |
| 32 | c = 'm'; |
| 33 | } else if (td_read(td)) { |
| 34 | if (td->sequential) |
| 35 | c = 'R'; |
| 36 | else |
| 37 | c = 'r'; |
| 38 | } else { |
| 39 | if (td->sequential) |
| 40 | c = 'W'; |
| 41 | else |
| 42 | c = 'w'; |
| 43 | } |
| 44 | break; |
| 45 | case TD_VERIFYING: |
| 46 | c = 'V'; |
| 47 | break; |
| 48 | case TD_FSYNCING: |
| 49 | c = 'F'; |
| 50 | break; |
| 51 | case TD_CREATED: |
| 52 | c = 'C'; |
| 53 | break; |
| 54 | case TD_INITIALIZED: |
| 55 | c = 'I'; |
| 56 | break; |
| 57 | case TD_NOT_CREATED: |
| 58 | c = 'P'; |
| 59 | break; |
| 60 | default: |
| 61 | log_err("state %d\n", td->runstate); |
| 62 | } |
| 63 | |
| 64 | run_str[td->thread_number - 1] = c; |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Convert seconds to a printable string. |
| 69 | */ |
| 70 | static void eta_to_str(char *str, int eta_sec) |
| 71 | { |
| 72 | unsigned int d, h, m, s; |
Jens Axboe | 165faf1 | 2007-02-07 11:30:37 +0100 | [diff] [blame] | 73 | int disp_hour = 0; |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 74 | |
| 75 | d = h = m = s = 0; |
| 76 | |
| 77 | s = eta_sec % 60; |
| 78 | eta_sec /= 60; |
| 79 | m = eta_sec % 60; |
| 80 | eta_sec /= 60; |
| 81 | h = eta_sec % 24; |
| 82 | eta_sec /= 24; |
| 83 | d = eta_sec; |
| 84 | |
Jens Axboe | 165faf1 | 2007-02-07 11:30:37 +0100 | [diff] [blame] | 85 | if (d) { |
| 86 | disp_hour = 1; |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 87 | str += sprintf(str, "%02ud:", d); |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 88 | } |
Jens Axboe | 165faf1 | 2007-02-07 11:30:37 +0100 | [diff] [blame] | 89 | |
| 90 | if (h || disp_hour) |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 91 | str += sprintf(str, "%02uh:", h); |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 92 | |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 93 | str += sprintf(str, "%02um:", m); |
| 94 | str += sprintf(str, "%02us", s); |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Best effort calculation of the estimated pending runtime of a job. |
| 99 | */ |
| 100 | static int thread_eta(struct thread_data *td, unsigned long elapsed) |
| 101 | { |
| 102 | unsigned long long bytes_total, bytes_done; |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 103 | unsigned long eta_sec = 0; |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 104 | |
| 105 | bytes_total = td->total_io_size; |
| 106 | |
Jens Axboe | 74939e3 | 2006-10-19 20:37:12 +0200 | [diff] [blame] | 107 | /* |
| 108 | * if writing, bytes_total will be twice the size. If mixing, |
| 109 | * assume a 50/50 split and thus bytes_total will be 50% larger. |
| 110 | */ |
| 111 | if (td->verify) { |
| 112 | if (td_rw(td)) |
| 113 | bytes_total = bytes_total * 3 / 2; |
| 114 | else |
| 115 | bytes_total <<= 1; |
| 116 | } |
| 117 | |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 118 | if (td->zone_size && td->zone_skip) |
| 119 | bytes_total /= (td->zone_skip / td->zone_size); |
| 120 | |
| 121 | if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) { |
| 122 | double perc; |
| 123 | |
| 124 | bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE]; |
| 125 | perc = (double) bytes_done / (double) bytes_total; |
| 126 | if (perc > 1.0) |
| 127 | perc = 1.0; |
| 128 | |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 129 | eta_sec = (unsigned long) (elapsed * (1.0 / perc)) - elapsed; |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 130 | |
| 131 | if (td->timeout && eta_sec > (td->timeout - elapsed)) |
| 132 | eta_sec = td->timeout - elapsed; |
| 133 | } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED |
| 134 | || td->runstate == TD_INITIALIZED) { |
| 135 | int t_eta = 0, r_eta = 0; |
| 136 | |
| 137 | /* |
| 138 | * We can only guess - assume it'll run the full timeout |
| 139 | * if given, otherwise assume it'll run at the specified rate. |
| 140 | */ |
| 141 | if (td->timeout) |
| 142 | t_eta = td->timeout + td->start_delay - elapsed; |
| 143 | if (td->rate) { |
| 144 | r_eta = (bytes_total / 1024) / td->rate; |
| 145 | r_eta += td->start_delay - elapsed; |
| 146 | } |
| 147 | |
| 148 | if (r_eta && t_eta) |
| 149 | eta_sec = min(r_eta, t_eta); |
| 150 | else if (r_eta) |
| 151 | eta_sec = r_eta; |
| 152 | else if (t_eta) |
| 153 | eta_sec = t_eta; |
| 154 | else |
| 155 | eta_sec = 0; |
| 156 | } else { |
| 157 | /* |
| 158 | * thread is already done or waiting for fsync |
| 159 | */ |
| 160 | eta_sec = 0; |
| 161 | } |
| 162 | |
| 163 | return eta_sec; |
| 164 | } |
| 165 | |
Jens Axboe | 46fda8d | 2007-02-11 00:49:30 +0100 | [diff] [blame] | 166 | static void calc_rate(unsigned long mtime, unsigned long long *io_bytes, |
| 167 | unsigned long long *prev_io_bytes, unsigned int *rate) |
| 168 | { |
| 169 | rate[0] = (io_bytes[0] - prev_io_bytes[0]) / mtime; |
| 170 | rate[1] = (io_bytes[1] - prev_io_bytes[1]) / mtime; |
| 171 | prev_io_bytes[0] = io_bytes[0]; |
| 172 | prev_io_bytes[1] = io_bytes[1]; |
| 173 | } |
| 174 | |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 175 | /* |
| 176 | * Print status of the jobs we know about. This includes rate estimates, |
| 177 | * ETA, thread state, etc. |
| 178 | */ |
| 179 | void print_thread_status(void) |
| 180 | { |
| 181 | unsigned long elapsed = mtime_since_genesis() / 1000; |
| 182 | int i, nr_running, nr_pending, t_rate, m_rate, *eta_secs, eta_sec; |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 183 | struct thread_data *td; |
Jens Axboe | 2eaa41c | 2007-02-11 00:51:30 +0100 | [diff] [blame^] | 184 | char eta_str[128]; |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 185 | double perc = 0.0; |
Jens Axboe | 6043c57 | 2006-11-03 11:37:47 +0100 | [diff] [blame] | 186 | unsigned long long io_bytes[2]; |
Jens Axboe | 46fda8d | 2007-02-11 00:49:30 +0100 | [diff] [blame] | 187 | unsigned long rate_time, disp_time, bw_avg_time; |
| 188 | struct timeval now; |
| 189 | |
| 190 | static unsigned long long rate_io_bytes[2]; |
| 191 | static unsigned long long disp_io_bytes[2]; |
| 192 | static struct timeval rate_prev_time, disp_prev_time; |
| 193 | static unsigned int rate[2]; |
Jens Axboe | 6043c57 | 2006-11-03 11:37:47 +0100 | [diff] [blame] | 194 | |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 195 | if (temp_stall_ts || terse_output) |
| 196 | return; |
| 197 | |
Jens Axboe | 46fda8d | 2007-02-11 00:49:30 +0100 | [diff] [blame] | 198 | if (!rate_io_bytes[0] && !rate_io_bytes[1]) |
| 199 | fill_start_time(&rate_prev_time); |
| 200 | if (!disp_io_bytes[0] && !disp_io_bytes[1]) |
| 201 | fill_start_time(&disp_prev_time); |
Jens Axboe | 6043c57 | 2006-11-03 11:37:47 +0100 | [diff] [blame] | 202 | |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 203 | eta_secs = malloc(thread_number * sizeof(int)); |
| 204 | memset(eta_secs, 0, thread_number * sizeof(int)); |
| 205 | |
Jens Axboe | 6043c57 | 2006-11-03 11:37:47 +0100 | [diff] [blame] | 206 | io_bytes[0] = io_bytes[1] = 0; |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 207 | nr_pending = nr_running = t_rate = m_rate = 0; |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 208 | bw_avg_time = ULONG_MAX; |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 209 | for_each_td(td, i) { |
Jens Axboe | bb3884d | 2007-01-17 17:23:11 +1100 | [diff] [blame] | 210 | if (td->bw_avg_time < bw_avg_time) |
| 211 | bw_avg_time = td->bw_avg_time; |
Jens Axboe | 46fda8d | 2007-02-11 00:49:30 +0100 | [diff] [blame] | 212 | if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING |
| 213 | || td->runstate == TD_FSYNCING) { |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 214 | nr_running++; |
| 215 | t_rate += td->rate; |
| 216 | m_rate += td->ratemin; |
| 217 | } else if (td->runstate < TD_RUNNING) |
| 218 | nr_pending++; |
| 219 | |
| 220 | if (elapsed >= 3) |
| 221 | eta_secs[i] = thread_eta(td, elapsed); |
| 222 | else |
| 223 | eta_secs[i] = INT_MAX; |
| 224 | |
| 225 | check_str_update(td); |
Jens Axboe | 6043c57 | 2006-11-03 11:37:47 +0100 | [diff] [blame] | 226 | io_bytes[0] += td->io_bytes[0]; |
| 227 | io_bytes[1] += td->io_bytes[1]; |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | if (exitall_on_terminate) |
| 231 | eta_sec = INT_MAX; |
| 232 | else |
| 233 | eta_sec = 0; |
| 234 | |
Jens Axboe | 34572e2 | 2006-10-20 09:33:18 +0200 | [diff] [blame] | 235 | for_each_td(td, i) { |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 236 | if (exitall_on_terminate) { |
| 237 | if (eta_secs[i] < eta_sec) |
| 238 | eta_sec = eta_secs[i]; |
| 239 | } else { |
| 240 | if (eta_secs[i] > eta_sec) |
| 241 | eta_sec = eta_secs[i]; |
| 242 | } |
| 243 | } |
| 244 | |
Jens Axboe | eecf272 | 2006-10-20 14:00:36 +0200 | [diff] [blame] | 245 | free(eta_secs); |
| 246 | |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 247 | if (eta_sec != INT_MAX && elapsed) { |
| 248 | perc = (double) elapsed / (double) (elapsed + eta_sec); |
| 249 | eta_to_str(eta_str, eta_sec); |
| 250 | } |
| 251 | |
Jens Axboe | 46fda8d | 2007-02-11 00:49:30 +0100 | [diff] [blame] | 252 | fio_gettime(&now, NULL); |
| 253 | rate_time = mtime_since(&rate_prev_time, &now); |
| 254 | |
| 255 | if (write_bw_log && rate_time> bw_avg_time) { |
| 256 | calc_rate(rate_time, io_bytes, rate_io_bytes, rate); |
| 257 | memcpy(&rate_prev_time, &now, sizeof(now)); |
| 258 | add_agg_sample(rate[DDIR_READ], DDIR_READ); |
| 259 | add_agg_sample(rate[DDIR_WRITE], DDIR_WRITE); |
Jens Axboe | 6043c57 | 2006-11-03 11:37:47 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Jens Axboe | 46fda8d | 2007-02-11 00:49:30 +0100 | [diff] [blame] | 262 | disp_time = mtime_since(&disp_prev_time, &now); |
| 263 | if (disp_time < 1000) |
| 264 | return; |
| 265 | |
| 266 | calc_rate(disp_time, io_bytes, disp_io_bytes, rate); |
| 267 | memcpy(&disp_prev_time, &now, sizeof(now)); |
| 268 | |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 269 | if (!nr_running && !nr_pending) |
| 270 | return; |
| 271 | |
Jens Axboe | d56cbab | 2007-01-11 19:24:55 +0100 | [diff] [blame] | 272 | printf("Threads: %d", nr_running); |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 273 | if (m_rate || t_rate) |
Jens Axboe | 2cb8d96 | 2007-01-13 15:15:47 +0100 | [diff] [blame] | 274 | printf(", CR=%d/%d KiB/s", t_rate, m_rate); |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 275 | if (eta_sec != INT_MAX && nr_running) { |
| 276 | perc *= 100.0; |
Jens Axboe | 46fda8d | 2007-02-11 00:49:30 +0100 | [diff] [blame] | 277 | printf(": [%s] [%3.1f%% done] [%6u/%6u kb/s] [eta %s]", run_str, perc, rate[0], rate[1], eta_str); |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 278 | } |
| 279 | printf("\r"); |
| 280 | fflush(stdout); |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void print_status_init(int thread_number) |
| 284 | { |
| 285 | run_str[thread_number] = 'P'; |
| 286 | } |