blob: b367ce9d7722cf492811b38deb85e83afc56cf35 [file] [log] [blame]
Jens Axboe263e5292006-10-18 15:37:01 +02001/*
2 * Status and ETA code
3 */
4#include <unistd.h>
5#include <fcntl.h>
6#include <string.h>
7
8#include "fio.h"
Jens Axboe263e5292006-10-18 15:37:01 +02009
10static char run_str[MAX_JOBS + 1];
11
12/*
13 * Sets the status of the 'td' in the printed status map.
14 */
15static void check_str_update(struct thread_data *td)
16{
17 char c = run_str[td->thread_number - 1];
18
19 switch (td->runstate) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +010020 case TD_REAPED:
21 c = '_';
22 break;
23 case TD_EXITED:
24 c = 'E';
25 break;
Jens Axboeb29ee5b2008-09-11 10:17:26 +020026 case TD_RAMP:
27 c = '/';
28 break;
Jens Axboe5ec10ea2008-03-06 15:42:00 +010029 case TD_RUNNING:
30 if (td_rw(td)) {
31 if (td_random(td))
32 c = 'm';
33 else
34 c = 'M';
35 } else if (td_read(td)) {
36 if (td_random(td))
37 c = 'r';
38 else
39 c = 'R';
40 } else {
41 if (td_random(td))
42 c = 'w';
43 else
44 c = 'W';
45 }
46 break;
Jens Axboeb0f65862009-05-20 11:52:15 +020047 case TD_PRE_READING:
48 c = 'p';
49 break;
Jens Axboe5ec10ea2008-03-06 15:42:00 +010050 case TD_VERIFYING:
51 c = 'V';
52 break;
53 case TD_FSYNCING:
54 c = 'F';
55 break;
56 case TD_CREATED:
57 c = 'C';
58 break;
59 case TD_INITIALIZED:
60 c = 'I';
61 break;
62 case TD_NOT_CREATED:
63 c = 'P';
64 break;
65 default:
66 log_err("state %d\n", td->runstate);
Jens Axboe263e5292006-10-18 15:37:01 +020067 }
68
69 run_str[td->thread_number - 1] = c;
70}
71
72/*
73 * Convert seconds to a printable string.
74 */
Gurudas Pai9c02f6e2008-06-13 08:40:11 +020075static void eta_to_str(char *str, unsigned long eta_sec)
Jens Axboe263e5292006-10-18 15:37:01 +020076{
77 unsigned int d, h, m, s;
Jens Axboe165faf12007-02-07 11:30:37 +010078 int disp_hour = 0;
Jens Axboe263e5292006-10-18 15:37:01 +020079
Jens Axboe263e5292006-10-18 15:37:01 +020080 s = eta_sec % 60;
81 eta_sec /= 60;
82 m = eta_sec % 60;
83 eta_sec /= 60;
84 h = eta_sec % 24;
85 eta_sec /= 24;
86 d = eta_sec;
87
Jens Axboe165faf12007-02-07 11:30:37 +010088 if (d) {
89 disp_hour = 1;
Jens Axboe1e97cce2006-12-05 11:44:16 +010090 str += sprintf(str, "%02ud:", d);
Jens Axboe263e5292006-10-18 15:37:01 +020091 }
Jens Axboe165faf12007-02-07 11:30:37 +010092
93 if (h || disp_hour)
Jens Axboe1e97cce2006-12-05 11:44:16 +010094 str += sprintf(str, "%02uh:", h);
Jens Axboe263e5292006-10-18 15:37:01 +020095
Jens Axboe1e97cce2006-12-05 11:44:16 +010096 str += sprintf(str, "%02um:", m);
97 str += sprintf(str, "%02us", s);
Jens Axboe263e5292006-10-18 15:37:01 +020098}
99
100/*
101 * Best effort calculation of the estimated pending runtime of a job.
102 */
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200103static int thread_eta(struct thread_data *td)
Jens Axboe263e5292006-10-18 15:37:01 +0200104{
105 unsigned long long bytes_total, bytes_done;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100106 unsigned long eta_sec = 0;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200107 unsigned long elapsed;
108
109 elapsed = (mtime_since_now(&td->epoch) + 999) / 1000;
Jens Axboe263e5292006-10-18 15:37:01 +0200110
111 bytes_total = td->total_io_size;
112
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200113 if (td->o.fill_device && td->o.size == -1ULL) {
114 if (!td->fill_device_size || td->fill_device_size == -1ULL)
115 return 0;
116
117 bytes_total = td->fill_device_size;
118 }
119
Jens Axboe74939e32006-10-19 20:37:12 +0200120 /*
121 * if writing, bytes_total will be twice the size. If mixing,
122 * assume a 50/50 split and thus bytes_total will be 50% larger.
123 */
Jens Axboe0dd83772007-09-04 12:38:28 +0200124 if (td->o.do_verify && td->o.verify && td_write(td)) {
Jens Axboe74939e32006-10-19 20:37:12 +0200125 if (td_rw(td))
126 bytes_total = bytes_total * 3 / 2;
127 else
128 bytes_total <<= 1;
129 }
130
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100131 if (td->o.zone_size && td->o.zone_skip)
132 bytes_total /= (td->o.zone_skip / td->o.zone_size);
Jens Axboe263e5292006-10-18 15:37:01 +0200133
134 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
Jens Axboecf4464c2007-04-17 20:14:42 +0200135 double perc, perc_t;
Jens Axboe263e5292006-10-18 15:37:01 +0200136
137 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
138 perc = (double) bytes_done / (double) bytes_total;
139 if (perc > 1.0)
140 perc = 1.0;
141
Jens Axboecf4464c2007-04-17 20:14:42 +0200142 if (td->o.time_based) {
143 perc_t = (double) elapsed / (double) td->o.timeout;
144 if (perc_t < perc)
145 perc = perc_t;
146 }
147
Jens Axboe1e97cce2006-12-05 11:44:16 +0100148 eta_sec = (unsigned long) (elapsed * (1.0 / perc)) - elapsed;
Jens Axboe263e5292006-10-18 15:37:01 +0200149
Jens Axboed3eeeab2008-04-06 12:19:05 +0200150 if (td->o.timeout &&
151 eta_sec > (td->o.timeout + done_secs - elapsed))
152 eta_sec = td->o.timeout + done_secs - elapsed;
Jens Axboe263e5292006-10-18 15:37:01 +0200153 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200154 || td->runstate == TD_INITIALIZED
Jens Axboeb0f65862009-05-20 11:52:15 +0200155 || td->runstate == TD_RAMP
156 || td->runstate == TD_PRE_READING) {
Jens Axboe263e5292006-10-18 15:37:01 +0200157 int t_eta = 0, r_eta = 0;
158
159 /*
160 * We can only guess - assume it'll run the full timeout
161 * if given, otherwise assume it'll run at the specified rate.
162 */
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200163 if (td->o.timeout) {
Signed-off-by Steven Prattcda99fa2010-12-14 08:31:36 +0100164 t_eta = td->o.timeout + td->o.start_delay +
165 td->o.ramp_time;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200166
167 if (in_ramp_time(td)) {
168 unsigned long ramp_left;
169
Signed-off-by Steven Prattcda99fa2010-12-14 08:31:36 +0100170 ramp_left = mtime_since_now(&td->epoch);
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200171 ramp_left = (ramp_left + 999) / 1000;
172 if (ramp_left <= t_eta)
173 t_eta -= ramp_left;
174 }
175 }
Jens Axboe581e7142009-06-09 12:47:16 +0200176 if (td->o.rate[0] || td->o.rate[1]) {
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200177 r_eta = (bytes_total / 1024) /
178 (td->o.rate[0] + td->o.rate[1]);
Jens Axboed3eeeab2008-04-06 12:19:05 +0200179 r_eta += td->o.start_delay;
Jens Axboe263e5292006-10-18 15:37:01 +0200180 }
181
182 if (r_eta && t_eta)
183 eta_sec = min(r_eta, t_eta);
184 else if (r_eta)
185 eta_sec = r_eta;
186 else if (t_eta)
187 eta_sec = t_eta;
188 else
189 eta_sec = 0;
190 } else {
191 /*
192 * thread is already done or waiting for fsync
193 */
194 eta_sec = 0;
195 }
196
197 return eta_sec;
198}
199
Jens Axboe46fda8d2007-02-11 00:49:30 +0100200static void calc_rate(unsigned long mtime, unsigned long long *io_bytes,
201 unsigned long long *prev_io_bytes, unsigned int *rate)
202{
203 rate[0] = (io_bytes[0] - prev_io_bytes[0]) / mtime;
204 rate[1] = (io_bytes[1] - prev_io_bytes[1]) / mtime;
205 prev_io_bytes[0] = io_bytes[0];
206 prev_io_bytes[1] = io_bytes[1];
207}
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100208
Jens Axboeadb02ba2009-06-03 10:47:44 +0200209static void calc_iops(unsigned long mtime, unsigned long long *io_iops,
210 unsigned long long *prev_io_iops, unsigned int *iops)
211{
212 iops[0] = ((io_iops[0] - prev_io_iops[0]) * 1000) / mtime;
213 iops[1] = ((io_iops[1] - prev_io_iops[1]) * 1000) / mtime;
214 prev_io_iops[0] = io_iops[0];
215 prev_io_iops[1] = io_iops[1];
216}
217
Jens Axboe263e5292006-10-18 15:37:01 +0200218/*
219 * Print status of the jobs we know about. This includes rate estimates,
220 * ETA, thread state, etc.
221 */
222void print_thread_status(void)
223{
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200224 unsigned long elapsed = (mtime_since_genesis() + 999) / 1000;
225 int i, nr_ramp, nr_running, nr_pending, t_rate, m_rate;
Jens Axboee6e41602007-03-27 20:20:36 +0200226 int t_iops, m_iops, files_open;
Jens Axboe34572e22006-10-20 09:33:18 +0200227 struct thread_data *td;
Jens Axboe2eaa41c2007-02-11 00:51:30 +0100228 char eta_str[128];
Jens Axboe263e5292006-10-18 15:37:01 +0200229 double perc = 0.0;
Jens Axboeadb02ba2009-06-03 10:47:44 +0200230 unsigned long long io_bytes[2], io_iops[2];
Jens Axboed3eeeab2008-04-06 12:19:05 +0200231 unsigned long rate_time, disp_time, bw_avg_time, *eta_secs, eta_sec;
Jens Axboe46fda8d2007-02-11 00:49:30 +0100232 struct timeval now;
233
234 static unsigned long long rate_io_bytes[2];
235 static unsigned long long disp_io_bytes[2];
Jens Axboeadb02ba2009-06-03 10:47:44 +0200236 static unsigned long long disp_io_iops[2];
Jens Axboe46fda8d2007-02-11 00:49:30 +0100237 static struct timeval rate_prev_time, disp_prev_time;
Jens Axboeadb02ba2009-06-03 10:47:44 +0200238 static unsigned int rate[2], iops[2];
Aaron Carroll43819602007-10-26 11:10:28 +0200239 static int linelen_last;
Jens Axboecac58fd2008-06-06 01:17:03 +0200240 static int eta_good;
Jens Axboed6978a32009-07-18 21:04:09 +0200241 int i2p = 0;
Jens Axboe6043c572006-11-03 11:37:47 +0100242
Aaron Carrolle592a062007-09-14 09:49:41 +0200243 if (temp_stall_ts || terse_output || eta_print == FIO_ETA_NEVER)
244 return;
245
246 if (!isatty(STDOUT_FILENO) && (eta_print != FIO_ETA_ALWAYS))
Jens Axboe263e5292006-10-18 15:37:01 +0200247 return;
248
Jens Axboe46fda8d2007-02-11 00:49:30 +0100249 if (!rate_io_bytes[0] && !rate_io_bytes[1])
250 fill_start_time(&rate_prev_time);
251 if (!disp_io_bytes[0] && !disp_io_bytes[1])
252 fill_start_time(&disp_prev_time);
Jens Axboe6043c572006-11-03 11:37:47 +0100253
Jens Axboed3eeeab2008-04-06 12:19:05 +0200254 eta_secs = malloc(thread_number * sizeof(unsigned long));
255 memset(eta_secs, 0, thread_number * sizeof(unsigned long));
Jens Axboe263e5292006-10-18 15:37:01 +0200256
Jens Axboe6043c572006-11-03 11:37:47 +0100257 io_bytes[0] = io_bytes[1] = 0;
Jens Axboeadb02ba2009-06-03 10:47:44 +0200258 io_iops[0] = io_iops[1] = 0;
Jens Axboeac71feb2007-03-16 10:24:07 +0100259 nr_pending = nr_running = t_rate = m_rate = t_iops = m_iops = 0;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200260 nr_ramp = 0;
Jens Axboebb3884d2007-01-17 17:23:11 +1100261 bw_avg_time = ULONG_MAX;
Jens Axboee6e41602007-03-27 20:20:36 +0200262 files_open = 0;
Jens Axboe34572e22006-10-20 09:33:18 +0200263 for_each_td(td, i) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100264 if (td->o.bw_avg_time < bw_avg_time)
265 bw_avg_time = td->o.bw_avg_time;
Jens Axboe46fda8d2007-02-11 00:49:30 +0100266 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING
Jens Axboeb0f65862009-05-20 11:52:15 +0200267 || td->runstate == TD_FSYNCING
268 || td->runstate == TD_PRE_READING) {
Jens Axboe263e5292006-10-18 15:37:01 +0200269 nr_running++;
Jens Axboe581e7142009-06-09 12:47:16 +0200270 t_rate += td->o.rate[0] + td->o.rate[1];
271 m_rate += td->o.ratemin[0] + td->o.ratemin[1];
272 t_iops += td->o.rate_iops[0] + td->o.rate_iops[1];
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200273 m_iops += td->o.rate_iops_min[0] +
274 td->o.rate_iops_min[1];
Jens Axboee6e41602007-03-27 20:20:36 +0200275 files_open += td->nr_open_files;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200276 } else if (td->runstate == TD_RAMP) {
277 nr_running++;
278 nr_ramp++;
Jens Axboe263e5292006-10-18 15:37:01 +0200279 } else if (td->runstate < TD_RUNNING)
280 nr_pending++;
281
282 if (elapsed >= 3)
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200283 eta_secs[i] = thread_eta(td);
Jens Axboe263e5292006-10-18 15:37:01 +0200284 else
285 eta_secs[i] = INT_MAX;
286
287 check_str_update(td);
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200288
289 if (td->runstate > TD_RAMP) {
290 io_bytes[0] += td->io_bytes[0];
291 io_bytes[1] += td->io_bytes[1];
Jens Axboeadb02ba2009-06-03 10:47:44 +0200292 io_iops[0] += td->io_blocks[0];
293 io_iops[1] += td->io_blocks[1];
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200294 }
Jens Axboe263e5292006-10-18 15:37:01 +0200295 }
296
297 if (exitall_on_terminate)
298 eta_sec = INT_MAX;
299 else
300 eta_sec = 0;
301
Jens Axboe34572e22006-10-20 09:33:18 +0200302 for_each_td(td, i) {
Jens Axboed6978a32009-07-18 21:04:09 +0200303 if (!i2p && is_power_of_2(td->o.kb_base))
304 i2p = 1;
Shaozhi Shawn Ye9c5a3852008-09-10 09:09:37 +0200305 if (exitall_on_terminate) {
306 if (eta_secs[i] < eta_sec)
307 eta_sec = eta_secs[i];
308 } else {
309 if (eta_secs[i] > eta_sec)
310 eta_sec = eta_secs[i];
311 }
Jens Axboe263e5292006-10-18 15:37:01 +0200312 }
313
Jens Axboeeecf2722006-10-20 14:00:36 +0200314 free(eta_secs);
315
Jens Axboe263e5292006-10-18 15:37:01 +0200316 if (eta_sec != INT_MAX && elapsed) {
317 perc = (double) elapsed / (double) (elapsed + eta_sec);
318 eta_to_str(eta_str, eta_sec);
319 }
320
Jens Axboe46fda8d2007-02-11 00:49:30 +0100321 fio_gettime(&now, NULL);
322 rate_time = mtime_since(&rate_prev_time, &now);
323
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200324 if (write_bw_log && rate_time > bw_avg_time && !in_ramp_time(td)) {
Jens Axboe46fda8d2007-02-11 00:49:30 +0100325 calc_rate(rate_time, io_bytes, rate_io_bytes, rate);
326 memcpy(&rate_prev_time, &now, sizeof(now));
Jens Axboe306ddc92009-05-18 13:08:12 +0200327 add_agg_sample(rate[DDIR_READ], DDIR_READ, 0);
328 add_agg_sample(rate[DDIR_WRITE], DDIR_WRITE, 0);
Jens Axboe6043c572006-11-03 11:37:47 +0100329 }
330
Jens Axboe46fda8d2007-02-11 00:49:30 +0100331 disp_time = mtime_since(&disp_prev_time, &now);
Jens Axboea5b01f12010-11-11 09:19:49 +0100332
333 /*
334 * Allow a little slack, the target is to print it every 1000 msecs
335 */
336 if (disp_time < 900)
Jens Axboe46fda8d2007-02-11 00:49:30 +0100337 return;
338
339 calc_rate(disp_time, io_bytes, disp_io_bytes, rate);
Jens Axboeadb02ba2009-06-03 10:47:44 +0200340 calc_iops(disp_time, io_iops, disp_io_iops, iops);
341
Jens Axboe46fda8d2007-02-11 00:49:30 +0100342 memcpy(&disp_prev_time, &now, sizeof(now));
343
Jens Axboe263e5292006-10-18 15:37:01 +0200344 if (!nr_running && !nr_pending)
345 return;
346
Jens Axboee6e41602007-03-27 20:20:36 +0200347 printf("Jobs: %d (f=%d)", nr_running, files_open);
Jens Axboe581e7142009-06-09 12:47:16 +0200348 if (m_rate || t_rate) {
349 char *tr, *mr;
350
Jens Axboed6978a32009-07-18 21:04:09 +0200351 mr = num2str(m_rate, 4, 0, i2p);
352 tr = num2str(t_rate, 4, 0, i2p);
Jens Axboeb22989b2009-07-17 22:29:23 +0200353 printf(", CR=%s/%s KB/s", tr, mr);
Jens Axboe581e7142009-06-09 12:47:16 +0200354 free(tr);
355 free(mr);
356 } else if (m_iops || t_iops)
Jens Axboeac71feb2007-03-16 10:24:07 +0100357 printf(", CR=%d/%d IOPS", t_iops, m_iops);
Jens Axboe263e5292006-10-18 15:37:01 +0200358 if (eta_sec != INT_MAX && nr_running) {
Jens Axboe0721d112008-06-05 09:03:05 +0200359 char perc_str[32];
Jens Axboeadb02ba2009-06-03 10:47:44 +0200360 char *iops_str[2];
Jens Axboed1bd7212009-06-03 10:53:45 +0200361 char *rate_str[2];
Jens Axboeadb02ba2009-06-03 10:47:44 +0200362 int l;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100363
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200364 if ((!eta_sec && !eta_good) || nr_ramp == nr_running)
Jens Axboecac58fd2008-06-06 01:17:03 +0200365 strcpy(perc_str, "-.-% done");
366 else {
367 eta_good = 1;
368 perc *= 100.0;
Jens Axboe0721d112008-06-05 09:03:05 +0200369 sprintf(perc_str, "%3.1f%% done", perc);
Jens Axboecac58fd2008-06-06 01:17:03 +0200370 }
Jens Axboe0721d112008-06-05 09:03:05 +0200371
Jens Axboed6978a32009-07-18 21:04:09 +0200372 rate_str[0] = num2str(rate[0], 5, 10, i2p);
373 rate_str[1] = num2str(rate[1], 5, 10, i2p);
Jens Axboed1bd7212009-06-03 10:53:45 +0200374
Jens Axboeadb02ba2009-06-03 10:47:44 +0200375 iops_str[0] = num2str(iops[0], 4, 1, 0);
376 iops_str[1] = num2str(iops[1], 4, 1, 0);
377
Jens Axboed1bd7212009-06-03 10:53:45 +0200378 l = printf(": [%s] [%s] [%s/%s /s] [%s/%s iops] [eta %s]",
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200379 run_str, perc_str, rate_str[0], rate_str[1],
Jens Axboeadb02ba2009-06-03 10:47:44 +0200380 iops_str[0], iops_str[1], eta_str);
381 if (l >= 0 && l < linelen_last)
382 printf("%*s", linelen_last - l, "");
383 linelen_last = l;
Jens Axboed1bd7212009-06-03 10:53:45 +0200384
385 free(rate_str[0]);
386 free(rate_str[1]);
Jens Axboeadb02ba2009-06-03 10:47:44 +0200387 free(iops_str[0]);
388 free(iops_str[1]);
Jens Axboe263e5292006-10-18 15:37:01 +0200389 }
390 printf("\r");
391 fflush(stdout);
Jens Axboe263e5292006-10-18 15:37:01 +0200392}
393
Jens Axboe2b13e712011-01-19 14:04:16 -0700394void print_status_init(int thr_number)
Jens Axboe263e5292006-10-18 15:37:01 +0200395{
Jens Axboe2b13e712011-01-19 14:04:16 -0700396 run_str[thr_number] = 'P';
Jens Axboe263e5292006-10-18 15:37:01 +0200397}