blob: d93bf1a16f80ff51f9b025149568f3a199472970 [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
Jens Axboefca70352011-07-06 20:12:54 +020010static char run_str[REAL_MAX_JOBS + 1];
Jens Axboe263e5292006-10-18 15:37:01 +020011
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)) {
Jens Axboee3b3f812011-09-01 09:58:11 -060031 if (td_random(td)) {
32 if (td->o.rwmix[DDIR_READ] == 100)
33 c = 'r';
34 else if (td->o.rwmix[DDIR_WRITE] == 100)
35 c = 'w';
36 else
37 c = 'm';
38 } else {
39 if (td->o.rwmix[DDIR_READ] == 100)
40 c = 'R';
41 else if (td->o.rwmix[DDIR_WRITE] == 100)
42 c = 'W';
43 else
44 c = 'M';
45 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +010046 } else if (td_read(td)) {
47 if (td_random(td))
48 c = 'r';
49 else
50 c = 'R';
51 } else {
52 if (td_random(td))
53 c = 'w';
54 else
55 c = 'W';
56 }
57 break;
Jens Axboeb0f65862009-05-20 11:52:15 +020058 case TD_PRE_READING:
59 c = 'p';
60 break;
Jens Axboe5ec10ea2008-03-06 15:42:00 +010061 case TD_VERIFYING:
62 c = 'V';
63 break;
64 case TD_FSYNCING:
65 c = 'F';
66 break;
67 case TD_CREATED:
68 c = 'C';
69 break;
70 case TD_INITIALIZED:
71 c = 'I';
72 break;
73 case TD_NOT_CREATED:
74 c = 'P';
75 break;
76 default:
77 log_err("state %d\n", td->runstate);
Jens Axboe263e5292006-10-18 15:37:01 +020078 }
79
80 run_str[td->thread_number - 1] = c;
81}
82
83/*
84 * Convert seconds to a printable string.
85 */
Gurudas Pai9c02f6e2008-06-13 08:40:11 +020086static void eta_to_str(char *str, unsigned long eta_sec)
Jens Axboe263e5292006-10-18 15:37:01 +020087{
88 unsigned int d, h, m, s;
Jens Axboe165faf12007-02-07 11:30:37 +010089 int disp_hour = 0;
Jens Axboe263e5292006-10-18 15:37:01 +020090
Jens Axboe263e5292006-10-18 15:37:01 +020091 s = eta_sec % 60;
92 eta_sec /= 60;
93 m = eta_sec % 60;
94 eta_sec /= 60;
95 h = eta_sec % 24;
96 eta_sec /= 24;
97 d = eta_sec;
98
Jens Axboe165faf12007-02-07 11:30:37 +010099 if (d) {
100 disp_hour = 1;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100101 str += sprintf(str, "%02ud:", d);
Jens Axboe263e5292006-10-18 15:37:01 +0200102 }
Jens Axboe165faf12007-02-07 11:30:37 +0100103
104 if (h || disp_hour)
Jens Axboe1e97cce2006-12-05 11:44:16 +0100105 str += sprintf(str, "%02uh:", h);
Jens Axboe263e5292006-10-18 15:37:01 +0200106
Jens Axboe1e97cce2006-12-05 11:44:16 +0100107 str += sprintf(str, "%02um:", m);
108 str += sprintf(str, "%02us", s);
Jens Axboe263e5292006-10-18 15:37:01 +0200109}
110
111/*
112 * Best effort calculation of the estimated pending runtime of a job.
113 */
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200114static int thread_eta(struct thread_data *td)
Jens Axboe263e5292006-10-18 15:37:01 +0200115{
116 unsigned long long bytes_total, bytes_done;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100117 unsigned long eta_sec = 0;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200118 unsigned long elapsed;
119
120 elapsed = (mtime_since_now(&td->epoch) + 999) / 1000;
Jens Axboe263e5292006-10-18 15:37:01 +0200121
122 bytes_total = td->total_io_size;
123
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200124 if (td->o.fill_device && td->o.size == -1ULL) {
125 if (!td->fill_device_size || td->fill_device_size == -1ULL)
126 return 0;
127
128 bytes_total = td->fill_device_size;
129 }
130
Jens Axboe74939e32006-10-19 20:37:12 +0200131 /*
132 * if writing, bytes_total will be twice the size. If mixing,
133 * assume a 50/50 split and thus bytes_total will be 50% larger.
134 */
Jens Axboe0dd83772007-09-04 12:38:28 +0200135 if (td->o.do_verify && td->o.verify && td_write(td)) {
Jens Axboe74939e32006-10-19 20:37:12 +0200136 if (td_rw(td))
137 bytes_total = bytes_total * 3 / 2;
138 else
139 bytes_total <<= 1;
140 }
141
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100142 if (td->o.zone_size && td->o.zone_skip)
143 bytes_total /= (td->o.zone_skip / td->o.zone_size);
Jens Axboe263e5292006-10-18 15:37:01 +0200144
145 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
Jens Axboecf4464c2007-04-17 20:14:42 +0200146 double perc, perc_t;
Jens Axboe263e5292006-10-18 15:37:01 +0200147
148 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
149 perc = (double) bytes_done / (double) bytes_total;
150 if (perc > 1.0)
151 perc = 1.0;
152
Jens Axboecf4464c2007-04-17 20:14:42 +0200153 if (td->o.time_based) {
154 perc_t = (double) elapsed / (double) td->o.timeout;
155 if (perc_t < perc)
156 perc = perc_t;
157 }
158
Jens Axboe1e97cce2006-12-05 11:44:16 +0100159 eta_sec = (unsigned long) (elapsed * (1.0 / perc)) - elapsed;
Jens Axboe263e5292006-10-18 15:37:01 +0200160
Jens Axboed3eeeab2008-04-06 12:19:05 +0200161 if (td->o.timeout &&
162 eta_sec > (td->o.timeout + done_secs - elapsed))
163 eta_sec = td->o.timeout + done_secs - elapsed;
Jens Axboe263e5292006-10-18 15:37:01 +0200164 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200165 || td->runstate == TD_INITIALIZED
Jens Axboeb0f65862009-05-20 11:52:15 +0200166 || td->runstate == TD_RAMP
167 || td->runstate == TD_PRE_READING) {
Jens Axboe263e5292006-10-18 15:37:01 +0200168 int t_eta = 0, r_eta = 0;
169
170 /*
171 * We can only guess - assume it'll run the full timeout
172 * if given, otherwise assume it'll run at the specified rate.
173 */
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200174 if (td->o.timeout) {
Signed-off-by Steven Prattcda99fa2010-12-14 08:31:36 +0100175 t_eta = td->o.timeout + td->o.start_delay +
176 td->o.ramp_time;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200177
178 if (in_ramp_time(td)) {
179 unsigned long ramp_left;
180
Signed-off-by Steven Prattcda99fa2010-12-14 08:31:36 +0100181 ramp_left = mtime_since_now(&td->epoch);
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200182 ramp_left = (ramp_left + 999) / 1000;
183 if (ramp_left <= t_eta)
184 t_eta -= ramp_left;
185 }
186 }
Jens Axboe581e7142009-06-09 12:47:16 +0200187 if (td->o.rate[0] || td->o.rate[1]) {
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200188 r_eta = (bytes_total / 1024) /
189 (td->o.rate[0] + td->o.rate[1]);
Jens Axboed3eeeab2008-04-06 12:19:05 +0200190 r_eta += td->o.start_delay;
Jens Axboe263e5292006-10-18 15:37:01 +0200191 }
192
193 if (r_eta && t_eta)
194 eta_sec = min(r_eta, t_eta);
195 else if (r_eta)
196 eta_sec = r_eta;
197 else if (t_eta)
198 eta_sec = t_eta;
199 else
200 eta_sec = 0;
201 } else {
202 /*
203 * thread is already done or waiting for fsync
204 */
205 eta_sec = 0;
206 }
207
208 return eta_sec;
209}
210
Jens Axboe46fda8d2007-02-11 00:49:30 +0100211static void calc_rate(unsigned long mtime, unsigned long long *io_bytes,
212 unsigned long long *prev_io_bytes, unsigned int *rate)
213{
214 rate[0] = (io_bytes[0] - prev_io_bytes[0]) / mtime;
215 rate[1] = (io_bytes[1] - prev_io_bytes[1]) / mtime;
216 prev_io_bytes[0] = io_bytes[0];
217 prev_io_bytes[1] = io_bytes[1];
218}
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100219
Jens Axboeadb02ba2009-06-03 10:47:44 +0200220static void calc_iops(unsigned long mtime, unsigned long long *io_iops,
221 unsigned long long *prev_io_iops, unsigned int *iops)
222{
223 iops[0] = ((io_iops[0] - prev_io_iops[0]) * 1000) / mtime;
224 iops[1] = ((io_iops[1] - prev_io_iops[1]) * 1000) / mtime;
225 prev_io_iops[0] = io_iops[0];
226 prev_io_iops[1] = io_iops[1];
227}
228
Jens Axboe263e5292006-10-18 15:37:01 +0200229/*
230 * Print status of the jobs we know about. This includes rate estimates,
231 * ETA, thread state, etc.
232 */
233void print_thread_status(void)
234{
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200235 unsigned long elapsed = (mtime_since_genesis() + 999) / 1000;
236 int i, nr_ramp, nr_running, nr_pending, t_rate, m_rate;
Jens Axboee6e41602007-03-27 20:20:36 +0200237 int t_iops, m_iops, files_open;
Jens Axboe34572e22006-10-20 09:33:18 +0200238 struct thread_data *td;
Jens Axboe2eaa41c2007-02-11 00:51:30 +0100239 char eta_str[128];
Jens Axboe263e5292006-10-18 15:37:01 +0200240 double perc = 0.0;
Jens Axboeadb02ba2009-06-03 10:47:44 +0200241 unsigned long long io_bytes[2], io_iops[2];
Jens Axboed3eeeab2008-04-06 12:19:05 +0200242 unsigned long rate_time, disp_time, bw_avg_time, *eta_secs, eta_sec;
Jens Axboe46fda8d2007-02-11 00:49:30 +0100243 struct timeval now;
244
245 static unsigned long long rate_io_bytes[2];
246 static unsigned long long disp_io_bytes[2];
Jens Axboeadb02ba2009-06-03 10:47:44 +0200247 static unsigned long long disp_io_iops[2];
Jens Axboe46fda8d2007-02-11 00:49:30 +0100248 static struct timeval rate_prev_time, disp_prev_time;
Jens Axboeadb02ba2009-06-03 10:47:44 +0200249 static unsigned int rate[2], iops[2];
Aaron Carroll43819602007-10-26 11:10:28 +0200250 static int linelen_last;
Jens Axboecac58fd2008-06-06 01:17:03 +0200251 static int eta_good;
Jens Axboed6978a32009-07-18 21:04:09 +0200252 int i2p = 0;
Jens Axboe6043c572006-11-03 11:37:47 +0100253
Aaron Carrolle592a062007-09-14 09:49:41 +0200254 if (temp_stall_ts || terse_output || eta_print == FIO_ETA_NEVER)
255 return;
256
257 if (!isatty(STDOUT_FILENO) && (eta_print != FIO_ETA_ALWAYS))
Jens Axboe263e5292006-10-18 15:37:01 +0200258 return;
259
Jens Axboe46fda8d2007-02-11 00:49:30 +0100260 if (!rate_io_bytes[0] && !rate_io_bytes[1])
261 fill_start_time(&rate_prev_time);
262 if (!disp_io_bytes[0] && !disp_io_bytes[1])
263 fill_start_time(&disp_prev_time);
Jens Axboe6043c572006-11-03 11:37:47 +0100264
Jens Axboed3eeeab2008-04-06 12:19:05 +0200265 eta_secs = malloc(thread_number * sizeof(unsigned long));
266 memset(eta_secs, 0, thread_number * sizeof(unsigned long));
Jens Axboe263e5292006-10-18 15:37:01 +0200267
Jens Axboe6043c572006-11-03 11:37:47 +0100268 io_bytes[0] = io_bytes[1] = 0;
Jens Axboeadb02ba2009-06-03 10:47:44 +0200269 io_iops[0] = io_iops[1] = 0;
Jens Axboeac71feb2007-03-16 10:24:07 +0100270 nr_pending = nr_running = t_rate = m_rate = t_iops = m_iops = 0;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200271 nr_ramp = 0;
Jens Axboebb3884d2007-01-17 17:23:11 +1100272 bw_avg_time = ULONG_MAX;
Jens Axboee6e41602007-03-27 20:20:36 +0200273 files_open = 0;
Jens Axboe34572e22006-10-20 09:33:18 +0200274 for_each_td(td, i) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100275 if (td->o.bw_avg_time < bw_avg_time)
276 bw_avg_time = td->o.bw_avg_time;
Jens Axboe46fda8d2007-02-11 00:49:30 +0100277 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING
Jens Axboeb0f65862009-05-20 11:52:15 +0200278 || td->runstate == TD_FSYNCING
279 || td->runstate == TD_PRE_READING) {
Jens Axboe263e5292006-10-18 15:37:01 +0200280 nr_running++;
Jens Axboe581e7142009-06-09 12:47:16 +0200281 t_rate += td->o.rate[0] + td->o.rate[1];
282 m_rate += td->o.ratemin[0] + td->o.ratemin[1];
283 t_iops += td->o.rate_iops[0] + td->o.rate_iops[1];
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200284 m_iops += td->o.rate_iops_min[0] +
285 td->o.rate_iops_min[1];
Jens Axboee6e41602007-03-27 20:20:36 +0200286 files_open += td->nr_open_files;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200287 } else if (td->runstate == TD_RAMP) {
288 nr_running++;
289 nr_ramp++;
Jens Axboe263e5292006-10-18 15:37:01 +0200290 } else if (td->runstate < TD_RUNNING)
291 nr_pending++;
292
293 if (elapsed >= 3)
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200294 eta_secs[i] = thread_eta(td);
Jens Axboe263e5292006-10-18 15:37:01 +0200295 else
296 eta_secs[i] = INT_MAX;
297
298 check_str_update(td);
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200299
300 if (td->runstate > TD_RAMP) {
301 io_bytes[0] += td->io_bytes[0];
302 io_bytes[1] += td->io_bytes[1];
Jens Axboeadb02ba2009-06-03 10:47:44 +0200303 io_iops[0] += td->io_blocks[0];
304 io_iops[1] += td->io_blocks[1];
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200305 }
Jens Axboe263e5292006-10-18 15:37:01 +0200306 }
307
308 if (exitall_on_terminate)
309 eta_sec = INT_MAX;
310 else
311 eta_sec = 0;
312
Jens Axboe34572e22006-10-20 09:33:18 +0200313 for_each_td(td, i) {
Jens Axboed6978a32009-07-18 21:04:09 +0200314 if (!i2p && is_power_of_2(td->o.kb_base))
315 i2p = 1;
Shaozhi Shawn Ye9c5a3852008-09-10 09:09:37 +0200316 if (exitall_on_terminate) {
317 if (eta_secs[i] < eta_sec)
318 eta_sec = eta_secs[i];
319 } else {
320 if (eta_secs[i] > eta_sec)
321 eta_sec = eta_secs[i];
322 }
Jens Axboe263e5292006-10-18 15:37:01 +0200323 }
324
Jens Axboeeecf2722006-10-20 14:00:36 +0200325 free(eta_secs);
326
Jens Axboe263e5292006-10-18 15:37:01 +0200327 if (eta_sec != INT_MAX && elapsed) {
328 perc = (double) elapsed / (double) (elapsed + eta_sec);
329 eta_to_str(eta_str, eta_sec);
330 }
331
Jens Axboe46fda8d2007-02-11 00:49:30 +0100332 fio_gettime(&now, NULL);
333 rate_time = mtime_since(&rate_prev_time, &now);
334
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200335 if (write_bw_log && rate_time > bw_avg_time && !in_ramp_time(td)) {
Jens Axboe46fda8d2007-02-11 00:49:30 +0100336 calc_rate(rate_time, io_bytes, rate_io_bytes, rate);
337 memcpy(&rate_prev_time, &now, sizeof(now));
Jens Axboe306ddc92009-05-18 13:08:12 +0200338 add_agg_sample(rate[DDIR_READ], DDIR_READ, 0);
339 add_agg_sample(rate[DDIR_WRITE], DDIR_WRITE, 0);
Jens Axboe6043c572006-11-03 11:37:47 +0100340 }
341
Jens Axboe46fda8d2007-02-11 00:49:30 +0100342 disp_time = mtime_since(&disp_prev_time, &now);
Jens Axboea5b01f12010-11-11 09:19:49 +0100343
344 /*
345 * Allow a little slack, the target is to print it every 1000 msecs
346 */
347 if (disp_time < 900)
Jens Axboe46fda8d2007-02-11 00:49:30 +0100348 return;
349
350 calc_rate(disp_time, io_bytes, disp_io_bytes, rate);
Jens Axboeadb02ba2009-06-03 10:47:44 +0200351 calc_iops(disp_time, io_iops, disp_io_iops, iops);
352
Jens Axboe46fda8d2007-02-11 00:49:30 +0100353 memcpy(&disp_prev_time, &now, sizeof(now));
354
Jens Axboe263e5292006-10-18 15:37:01 +0200355 if (!nr_running && !nr_pending)
356 return;
357
Jens Axboee6e41602007-03-27 20:20:36 +0200358 printf("Jobs: %d (f=%d)", nr_running, files_open);
Jens Axboe581e7142009-06-09 12:47:16 +0200359 if (m_rate || t_rate) {
360 char *tr, *mr;
361
Jens Axboed6978a32009-07-18 21:04:09 +0200362 mr = num2str(m_rate, 4, 0, i2p);
363 tr = num2str(t_rate, 4, 0, i2p);
Jens Axboeb22989b2009-07-17 22:29:23 +0200364 printf(", CR=%s/%s KB/s", tr, mr);
Jens Axboe581e7142009-06-09 12:47:16 +0200365 free(tr);
366 free(mr);
367 } else if (m_iops || t_iops)
Jens Axboeac71feb2007-03-16 10:24:07 +0100368 printf(", CR=%d/%d IOPS", t_iops, m_iops);
Jens Axboe263e5292006-10-18 15:37:01 +0200369 if (eta_sec != INT_MAX && nr_running) {
Jens Axboe0721d112008-06-05 09:03:05 +0200370 char perc_str[32];
Jens Axboeadb02ba2009-06-03 10:47:44 +0200371 char *iops_str[2];
Jens Axboed1bd7212009-06-03 10:53:45 +0200372 char *rate_str[2];
Jens Axboeadb02ba2009-06-03 10:47:44 +0200373 int l;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100374
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200375 if ((!eta_sec && !eta_good) || nr_ramp == nr_running)
Jens Axboecac58fd2008-06-06 01:17:03 +0200376 strcpy(perc_str, "-.-% done");
377 else {
378 eta_good = 1;
379 perc *= 100.0;
Jens Axboe0721d112008-06-05 09:03:05 +0200380 sprintf(perc_str, "%3.1f%% done", perc);
Jens Axboecac58fd2008-06-06 01:17:03 +0200381 }
Jens Axboe0721d112008-06-05 09:03:05 +0200382
Jens Axboed6978a32009-07-18 21:04:09 +0200383 rate_str[0] = num2str(rate[0], 5, 10, i2p);
384 rate_str[1] = num2str(rate[1], 5, 10, i2p);
Jens Axboed1bd7212009-06-03 10:53:45 +0200385
Jens Axboeadb02ba2009-06-03 10:47:44 +0200386 iops_str[0] = num2str(iops[0], 4, 1, 0);
387 iops_str[1] = num2str(iops[1], 4, 1, 0);
388
Jens Axboed1bd7212009-06-03 10:53:45 +0200389 l = printf(": [%s] [%s] [%s/%s /s] [%s/%s iops] [eta %s]",
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200390 run_str, perc_str, rate_str[0], rate_str[1],
Jens Axboeadb02ba2009-06-03 10:47:44 +0200391 iops_str[0], iops_str[1], eta_str);
392 if (l >= 0 && l < linelen_last)
393 printf("%*s", linelen_last - l, "");
394 linelen_last = l;
Jens Axboed1bd7212009-06-03 10:53:45 +0200395
396 free(rate_str[0]);
397 free(rate_str[1]);
Jens Axboeadb02ba2009-06-03 10:47:44 +0200398 free(iops_str[0]);
399 free(iops_str[1]);
Jens Axboe263e5292006-10-18 15:37:01 +0200400 }
401 printf("\r");
402 fflush(stdout);
Jens Axboe263e5292006-10-18 15:37:01 +0200403}
404
Jens Axboe2b13e712011-01-19 14:04:16 -0700405void print_status_init(int thr_number)
Jens Axboe263e5292006-10-18 15:37:01 +0200406{
Jens Axboe2b13e712011-01-19 14:04:16 -0700407 run_str[thr_number] = 'P';
Jens Axboe263e5292006-10-18 15:37:01 +0200408}