blob: 5a5188f1378d9010ed8f8576cfd3c2f56dc0168c [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;
47 case TD_VERIFYING:
48 c = 'V';
49 break;
50 case TD_FSYNCING:
51 c = 'F';
52 break;
53 case TD_CREATED:
54 c = 'C';
55 break;
56 case TD_INITIALIZED:
57 c = 'I';
58 break;
59 case TD_NOT_CREATED:
60 c = 'P';
61 break;
62 default:
63 log_err("state %d\n", td->runstate);
Jens Axboe263e5292006-10-18 15:37:01 +020064 }
65
66 run_str[td->thread_number - 1] = c;
67}
68
69/*
70 * Convert seconds to a printable string.
71 */
Gurudas Pai9c02f6e2008-06-13 08:40:11 +020072static void eta_to_str(char *str, unsigned long eta_sec)
Jens Axboe263e5292006-10-18 15:37:01 +020073{
74 unsigned int d, h, m, s;
Jens Axboe165faf12007-02-07 11:30:37 +010075 int disp_hour = 0;
Jens Axboe263e5292006-10-18 15:37:01 +020076
Jens Axboe263e5292006-10-18 15:37:01 +020077 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 Axboe165faf12007-02-07 11:30:37 +010085 if (d) {
86 disp_hour = 1;
Jens Axboe1e97cce2006-12-05 11:44:16 +010087 str += sprintf(str, "%02ud:", d);
Jens Axboe263e5292006-10-18 15:37:01 +020088 }
Jens Axboe165faf12007-02-07 11:30:37 +010089
90 if (h || disp_hour)
Jens Axboe1e97cce2006-12-05 11:44:16 +010091 str += sprintf(str, "%02uh:", h);
Jens Axboe263e5292006-10-18 15:37:01 +020092
Jens Axboe1e97cce2006-12-05 11:44:16 +010093 str += sprintf(str, "%02um:", m);
94 str += sprintf(str, "%02us", s);
Jens Axboe263e5292006-10-18 15:37:01 +020095}
96
97/*
98 * Best effort calculation of the estimated pending runtime of a job.
99 */
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200100static int thread_eta(struct thread_data *td)
Jens Axboe263e5292006-10-18 15:37:01 +0200101{
102 unsigned long long bytes_total, bytes_done;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100103 unsigned long eta_sec = 0;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200104 unsigned long elapsed;
105
106 elapsed = (mtime_since_now(&td->epoch) + 999) / 1000;
Jens Axboe263e5292006-10-18 15:37:01 +0200107
108 bytes_total = td->total_io_size;
109
Jens Axboe74939e32006-10-19 20:37:12 +0200110 /*
111 * if writing, bytes_total will be twice the size. If mixing,
112 * assume a 50/50 split and thus bytes_total will be 50% larger.
113 */
Jens Axboe0dd83772007-09-04 12:38:28 +0200114 if (td->o.do_verify && td->o.verify && td_write(td)) {
Jens Axboe74939e32006-10-19 20:37:12 +0200115 if (td_rw(td))
116 bytes_total = bytes_total * 3 / 2;
117 else
118 bytes_total <<= 1;
119 }
120
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100121 if (td->o.zone_size && td->o.zone_skip)
122 bytes_total /= (td->o.zone_skip / td->o.zone_size);
Jens Axboe263e5292006-10-18 15:37:01 +0200123
Shaozhi Shawn Ye9c5a3852008-09-10 09:09:37 +0200124 if (td->o.fill_device && td->o.size == -1ULL)
125 return 0;
126
Jens Axboe263e5292006-10-18 15:37:01 +0200127 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
Jens Axboecf4464c2007-04-17 20:14:42 +0200128 double perc, perc_t;
Jens Axboe263e5292006-10-18 15:37:01 +0200129
130 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
131 perc = (double) bytes_done / (double) bytes_total;
132 if (perc > 1.0)
133 perc = 1.0;
134
Jens Axboecf4464c2007-04-17 20:14:42 +0200135 if (td->o.time_based) {
136 perc_t = (double) elapsed / (double) td->o.timeout;
137 if (perc_t < perc)
138 perc = perc_t;
139 }
140
Jens Axboe1e97cce2006-12-05 11:44:16 +0100141 eta_sec = (unsigned long) (elapsed * (1.0 / perc)) - elapsed;
Jens Axboe263e5292006-10-18 15:37:01 +0200142
Jens Axboed3eeeab2008-04-06 12:19:05 +0200143 if (td->o.timeout &&
144 eta_sec > (td->o.timeout + done_secs - elapsed))
145 eta_sec = td->o.timeout + done_secs - elapsed;
Jens Axboe263e5292006-10-18 15:37:01 +0200146 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200147 || td->runstate == TD_INITIALIZED
148 || td->runstate == TD_RAMP) {
Jens Axboe263e5292006-10-18 15:37:01 +0200149 int t_eta = 0, r_eta = 0;
150
151 /*
152 * We can only guess - assume it'll run the full timeout
153 * if given, otherwise assume it'll run at the specified rate.
154 */
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200155 if (td->o.timeout) {
Jens Axboed3eeeab2008-04-06 12:19:05 +0200156 t_eta = td->o.timeout + td->o.start_delay;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200157
158 if (in_ramp_time(td)) {
159 unsigned long ramp_left;
160
161 ramp_left = mtime_since_now(&td->start);
162 ramp_left = (ramp_left + 999) / 1000;
163 if (ramp_left <= t_eta)
164 t_eta -= ramp_left;
165 }
166 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100167 if (td->o.rate) {
168 r_eta = (bytes_total / 1024) / td->o.rate;
Jens Axboed3eeeab2008-04-06 12:19:05 +0200169 r_eta += td->o.start_delay;
Jens Axboe263e5292006-10-18 15:37:01 +0200170 }
171
172 if (r_eta && t_eta)
173 eta_sec = min(r_eta, t_eta);
174 else if (r_eta)
175 eta_sec = r_eta;
176 else if (t_eta)
177 eta_sec = t_eta;
178 else
179 eta_sec = 0;
180 } else {
181 /*
182 * thread is already done or waiting for fsync
183 */
184 eta_sec = 0;
185 }
186
187 return eta_sec;
188}
189
Jens Axboe46fda8d2007-02-11 00:49:30 +0100190static void calc_rate(unsigned long mtime, unsigned long long *io_bytes,
191 unsigned long long *prev_io_bytes, unsigned int *rate)
192{
193 rate[0] = (io_bytes[0] - prev_io_bytes[0]) / mtime;
194 rate[1] = (io_bytes[1] - prev_io_bytes[1]) / mtime;
195 prev_io_bytes[0] = io_bytes[0];
196 prev_io_bytes[1] = io_bytes[1];
197}
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100198
Jens Axboe263e5292006-10-18 15:37:01 +0200199/*
200 * Print status of the jobs we know about. This includes rate estimates,
201 * ETA, thread state, etc.
202 */
203void print_thread_status(void)
204{
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200205 unsigned long elapsed = (mtime_since_genesis() + 999) / 1000;
206 int i, nr_ramp, nr_running, nr_pending, t_rate, m_rate;
Jens Axboee6e41602007-03-27 20:20:36 +0200207 int t_iops, m_iops, files_open;
Jens Axboe34572e22006-10-20 09:33:18 +0200208 struct thread_data *td;
Jens Axboe2eaa41c2007-02-11 00:51:30 +0100209 char eta_str[128];
Jens Axboe263e5292006-10-18 15:37:01 +0200210 double perc = 0.0;
Jens Axboe6043c572006-11-03 11:37:47 +0100211 unsigned long long io_bytes[2];
Jens Axboed3eeeab2008-04-06 12:19:05 +0200212 unsigned long rate_time, disp_time, bw_avg_time, *eta_secs, eta_sec;
Jens Axboe46fda8d2007-02-11 00:49:30 +0100213 struct timeval now;
214
215 static unsigned long long rate_io_bytes[2];
216 static unsigned long long disp_io_bytes[2];
217 static struct timeval rate_prev_time, disp_prev_time;
218 static unsigned int rate[2];
Aaron Carroll43819602007-10-26 11:10:28 +0200219 static int linelen_last;
Jens Axboecac58fd2008-06-06 01:17:03 +0200220 static int eta_good;
Jens Axboe6043c572006-11-03 11:37:47 +0100221
Aaron Carrolle592a062007-09-14 09:49:41 +0200222 if (temp_stall_ts || terse_output || eta_print == FIO_ETA_NEVER)
223 return;
224
225 if (!isatty(STDOUT_FILENO) && (eta_print != FIO_ETA_ALWAYS))
Jens Axboe263e5292006-10-18 15:37:01 +0200226 return;
227
Jens Axboe46fda8d2007-02-11 00:49:30 +0100228 if (!rate_io_bytes[0] && !rate_io_bytes[1])
229 fill_start_time(&rate_prev_time);
230 if (!disp_io_bytes[0] && !disp_io_bytes[1])
231 fill_start_time(&disp_prev_time);
Jens Axboe6043c572006-11-03 11:37:47 +0100232
Jens Axboed3eeeab2008-04-06 12:19:05 +0200233 eta_secs = malloc(thread_number * sizeof(unsigned long));
234 memset(eta_secs, 0, thread_number * sizeof(unsigned long));
Jens Axboe263e5292006-10-18 15:37:01 +0200235
Jens Axboe6043c572006-11-03 11:37:47 +0100236 io_bytes[0] = io_bytes[1] = 0;
Jens Axboeac71feb2007-03-16 10:24:07 +0100237 nr_pending = nr_running = t_rate = m_rate = t_iops = m_iops = 0;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200238 nr_ramp = 0;
Jens Axboebb3884d2007-01-17 17:23:11 +1100239 bw_avg_time = ULONG_MAX;
Jens Axboee6e41602007-03-27 20:20:36 +0200240 files_open = 0;
Jens Axboe34572e22006-10-20 09:33:18 +0200241 for_each_td(td, i) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100242 if (td->o.bw_avg_time < bw_avg_time)
243 bw_avg_time = td->o.bw_avg_time;
Jens Axboe46fda8d2007-02-11 00:49:30 +0100244 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING
245 || td->runstate == TD_FSYNCING) {
Jens Axboe263e5292006-10-18 15:37:01 +0200246 nr_running++;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100247 t_rate += td->o.rate;
248 m_rate += td->o.ratemin;
Jens Axboeac71feb2007-03-16 10:24:07 +0100249 t_iops += td->o.rate_iops;
250 m_iops += td->o.rate_iops_min;
Jens Axboee6e41602007-03-27 20:20:36 +0200251 files_open += td->nr_open_files;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200252 } else if (td->runstate == TD_RAMP) {
253 nr_running++;
254 nr_ramp++;
Jens Axboe263e5292006-10-18 15:37:01 +0200255 } else if (td->runstate < TD_RUNNING)
256 nr_pending++;
257
258 if (elapsed >= 3)
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200259 eta_secs[i] = thread_eta(td);
Jens Axboe263e5292006-10-18 15:37:01 +0200260 else
261 eta_secs[i] = INT_MAX;
262
263 check_str_update(td);
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200264
265 if (td->runstate > TD_RAMP) {
266 io_bytes[0] += td->io_bytes[0];
267 io_bytes[1] += td->io_bytes[1];
268 }
Jens Axboe263e5292006-10-18 15:37:01 +0200269 }
270
271 if (exitall_on_terminate)
272 eta_sec = INT_MAX;
273 else
274 eta_sec = 0;
275
Jens Axboe34572e22006-10-20 09:33:18 +0200276 for_each_td(td, i) {
Shaozhi Shawn Ye9c5a3852008-09-10 09:09:37 +0200277 if (exitall_on_terminate) {
278 if (eta_secs[i] < eta_sec)
279 eta_sec = eta_secs[i];
280 } else {
281 if (eta_secs[i] > eta_sec)
282 eta_sec = eta_secs[i];
283 }
Jens Axboe263e5292006-10-18 15:37:01 +0200284 }
285
Jens Axboeeecf2722006-10-20 14:00:36 +0200286 free(eta_secs);
287
Jens Axboe263e5292006-10-18 15:37:01 +0200288 if (eta_sec != INT_MAX && elapsed) {
289 perc = (double) elapsed / (double) (elapsed + eta_sec);
290 eta_to_str(eta_str, eta_sec);
291 }
292
Jens Axboe46fda8d2007-02-11 00:49:30 +0100293 fio_gettime(&now, NULL);
294 rate_time = mtime_since(&rate_prev_time, &now);
295
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200296 if (write_bw_log && rate_time > bw_avg_time && !in_ramp_time(td)) {
Jens Axboe46fda8d2007-02-11 00:49:30 +0100297 calc_rate(rate_time, io_bytes, rate_io_bytes, rate);
298 memcpy(&rate_prev_time, &now, sizeof(now));
Jens Axboe306ddc92009-05-18 13:08:12 +0200299 add_agg_sample(rate[DDIR_READ], DDIR_READ, 0);
300 add_agg_sample(rate[DDIR_WRITE], DDIR_WRITE, 0);
Jens Axboe6043c572006-11-03 11:37:47 +0100301 }
302
Jens Axboe46fda8d2007-02-11 00:49:30 +0100303 disp_time = mtime_since(&disp_prev_time, &now);
304 if (disp_time < 1000)
305 return;
306
307 calc_rate(disp_time, io_bytes, disp_io_bytes, rate);
308 memcpy(&disp_prev_time, &now, sizeof(now));
309
Jens Axboe263e5292006-10-18 15:37:01 +0200310 if (!nr_running && !nr_pending)
311 return;
312
Jens Axboee6e41602007-03-27 20:20:36 +0200313 printf("Jobs: %d (f=%d)", nr_running, files_open);
Jens Axboe263e5292006-10-18 15:37:01 +0200314 if (m_rate || t_rate)
Jens Axboe2cb8d962007-01-13 15:15:47 +0100315 printf(", CR=%d/%d KiB/s", t_rate, m_rate);
Jens Axboeac71feb2007-03-16 10:24:07 +0100316 else if (m_iops || t_iops)
317 printf(", CR=%d/%d IOPS", t_iops, m_iops);
Jens Axboe263e5292006-10-18 15:37:01 +0200318 if (eta_sec != INT_MAX && nr_running) {
Jens Axboe0721d112008-06-05 09:03:05 +0200319 char perc_str[32];
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100320 int ll;
321
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200322 if ((!eta_sec && !eta_good) || nr_ramp == nr_running)
Jens Axboecac58fd2008-06-06 01:17:03 +0200323 strcpy(perc_str, "-.-% done");
324 else {
325 eta_good = 1;
326 perc *= 100.0;
Jens Axboe0721d112008-06-05 09:03:05 +0200327 sprintf(perc_str, "%3.1f%% done", perc);
Jens Axboecac58fd2008-06-06 01:17:03 +0200328 }
Jens Axboe0721d112008-06-05 09:03:05 +0200329
Jens Axboe0721d112008-06-05 09:03:05 +0200330 ll = printf(": [%s] [%s] [%6u/%6u kb/s] [eta %s]",
331 run_str, perc_str, rate[0], rate[1], eta_str);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100332 if (ll >= 0 && ll < linelen_last)
333 printf("%*s", linelen_last - ll, "");
334 linelen_last = ll;
Jens Axboe263e5292006-10-18 15:37:01 +0200335 }
336 printf("\r");
337 fflush(stdout);
Jens Axboe263e5292006-10-18 15:37:01 +0200338}
339
340void print_status_init(int thread_number)
341{
342 run_str[thread_number] = 'P';
343}