blob: 47e9e339514dd4780bab3a7f049c79e0a7ff6101 [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;
26 case TD_RUNNING:
27 if (td_rw(td)) {
28 if (td_random(td))
29 c = 'm';
30 else
31 c = 'M';
32 } else if (td_read(td)) {
33 if (td_random(td))
34 c = 'r';
35 else
36 c = 'R';
37 } else {
38 if (td_random(td))
39 c = 'w';
40 else
41 c = 'W';
42 }
43 break;
44 case TD_VERIFYING:
45 c = 'V';
46 break;
47 case TD_FSYNCING:
48 c = 'F';
49 break;
50 case TD_CREATED:
51 c = 'C';
52 break;
53 case TD_INITIALIZED:
54 c = 'I';
55 break;
56 case TD_NOT_CREATED:
57 c = 'P';
58 break;
59 default:
60 log_err("state %d\n", td->runstate);
Jens Axboe263e5292006-10-18 15:37:01 +020061 }
62
63 run_str[td->thread_number - 1] = c;
64}
65
66/*
67 * Convert seconds to a printable string.
68 */
Gurudas Pai9c02f6e2008-06-13 08:40:11 +020069static void eta_to_str(char *str, unsigned long eta_sec)
Jens Axboe263e5292006-10-18 15:37:01 +020070{
71 unsigned int d, h, m, s;
Jens Axboe165faf12007-02-07 11:30:37 +010072 int disp_hour = 0;
Jens Axboe263e5292006-10-18 15:37:01 +020073
Jens Axboe263e5292006-10-18 15:37:01 +020074 s = eta_sec % 60;
75 eta_sec /= 60;
76 m = eta_sec % 60;
77 eta_sec /= 60;
78 h = eta_sec % 24;
79 eta_sec /= 24;
80 d = eta_sec;
81
Jens Axboe165faf12007-02-07 11:30:37 +010082 if (d) {
83 disp_hour = 1;
Jens Axboe1e97cce2006-12-05 11:44:16 +010084 str += sprintf(str, "%02ud:", d);
Jens Axboe263e5292006-10-18 15:37:01 +020085 }
Jens Axboe165faf12007-02-07 11:30:37 +010086
87 if (h || disp_hour)
Jens Axboe1e97cce2006-12-05 11:44:16 +010088 str += sprintf(str, "%02uh:", h);
Jens Axboe263e5292006-10-18 15:37:01 +020089
Jens Axboe1e97cce2006-12-05 11:44:16 +010090 str += sprintf(str, "%02um:", m);
91 str += sprintf(str, "%02us", s);
Jens Axboe263e5292006-10-18 15:37:01 +020092}
93
94/*
95 * Best effort calculation of the estimated pending runtime of a job.
96 */
97static int thread_eta(struct thread_data *td, unsigned long elapsed)
98{
99 unsigned long long bytes_total, bytes_done;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100100 unsigned long eta_sec = 0;
Jens Axboe263e5292006-10-18 15:37:01 +0200101
102 bytes_total = td->total_io_size;
103
Jens Axboe74939e32006-10-19 20:37:12 +0200104 /*
105 * if writing, bytes_total will be twice the size. If mixing,
106 * assume a 50/50 split and thus bytes_total will be 50% larger.
107 */
Jens Axboe0dd83772007-09-04 12:38:28 +0200108 if (td->o.do_verify && td->o.verify && td_write(td)) {
Jens Axboe74939e32006-10-19 20:37:12 +0200109 if (td_rw(td))
110 bytes_total = bytes_total * 3 / 2;
111 else
112 bytes_total <<= 1;
113 }
114
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100115 if (td->o.zone_size && td->o.zone_skip)
116 bytes_total /= (td->o.zone_skip / td->o.zone_size);
Jens Axboe263e5292006-10-18 15:37:01 +0200117
Shaozhi Shawn Ye9c5a3852008-09-10 09:09:37 +0200118 if (td->o.fill_device && td->o.size == -1ULL)
119 return 0;
120
Jens Axboe263e5292006-10-18 15:37:01 +0200121 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
Jens Axboecf4464c2007-04-17 20:14:42 +0200122 double perc, perc_t;
Jens Axboe263e5292006-10-18 15:37:01 +0200123
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 Axboecf4464c2007-04-17 20:14:42 +0200129 if (td->o.time_based) {
130 perc_t = (double) elapsed / (double) td->o.timeout;
131 if (perc_t < perc)
132 perc = perc_t;
133 }
134
Jens Axboe1e97cce2006-12-05 11:44:16 +0100135 eta_sec = (unsigned long) (elapsed * (1.0 / perc)) - elapsed;
Jens Axboe263e5292006-10-18 15:37:01 +0200136
Jens Axboed3eeeab2008-04-06 12:19:05 +0200137 if (td->o.timeout &&
138 eta_sec > (td->o.timeout + done_secs - elapsed))
139 eta_sec = td->o.timeout + done_secs - elapsed;
Jens Axboe263e5292006-10-18 15:37:01 +0200140 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
141 || td->runstate == TD_INITIALIZED) {
142 int t_eta = 0, r_eta = 0;
143
144 /*
145 * We can only guess - assume it'll run the full timeout
146 * if given, otherwise assume it'll run at the specified rate.
147 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100148 if (td->o.timeout)
Jens Axboed3eeeab2008-04-06 12:19:05 +0200149 t_eta = td->o.timeout + td->o.start_delay;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100150 if (td->o.rate) {
151 r_eta = (bytes_total / 1024) / td->o.rate;
Jens Axboed3eeeab2008-04-06 12:19:05 +0200152 r_eta += td->o.start_delay;
Jens Axboe263e5292006-10-18 15:37:01 +0200153 }
154
155 if (r_eta && t_eta)
156 eta_sec = min(r_eta, t_eta);
157 else if (r_eta)
158 eta_sec = r_eta;
159 else if (t_eta)
160 eta_sec = t_eta;
161 else
162 eta_sec = 0;
163 } else {
164 /*
165 * thread is already done or waiting for fsync
166 */
167 eta_sec = 0;
168 }
169
170 return eta_sec;
171}
172
Jens Axboe46fda8d2007-02-11 00:49:30 +0100173static void calc_rate(unsigned long mtime, unsigned long long *io_bytes,
174 unsigned long long *prev_io_bytes, unsigned int *rate)
175{
176 rate[0] = (io_bytes[0] - prev_io_bytes[0]) / mtime;
177 rate[1] = (io_bytes[1] - prev_io_bytes[1]) / mtime;
178 prev_io_bytes[0] = io_bytes[0];
179 prev_io_bytes[1] = io_bytes[1];
180}
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100181
Jens Axboe263e5292006-10-18 15:37:01 +0200182/*
183 * Print status of the jobs we know about. This includes rate estimates,
184 * ETA, thread state, etc.
185 */
186void print_thread_status(void)
187{
188 unsigned long elapsed = mtime_since_genesis() / 1000;
Jens Axboed3eeeab2008-04-06 12:19:05 +0200189 int i, nr_running, nr_pending, t_rate, m_rate;
Jens Axboee6e41602007-03-27 20:20:36 +0200190 int t_iops, m_iops, files_open;
Jens Axboe34572e22006-10-20 09:33:18 +0200191 struct thread_data *td;
Jens Axboe2eaa41c2007-02-11 00:51:30 +0100192 char eta_str[128];
Jens Axboe263e5292006-10-18 15:37:01 +0200193 double perc = 0.0;
Jens Axboe6043c572006-11-03 11:37:47 +0100194 unsigned long long io_bytes[2];
Jens Axboed3eeeab2008-04-06 12:19:05 +0200195 unsigned long rate_time, disp_time, bw_avg_time, *eta_secs, eta_sec;
Jens Axboe46fda8d2007-02-11 00:49:30 +0100196 struct timeval now;
197
198 static unsigned long long rate_io_bytes[2];
199 static unsigned long long disp_io_bytes[2];
200 static struct timeval rate_prev_time, disp_prev_time;
201 static unsigned int rate[2];
Aaron Carroll43819602007-10-26 11:10:28 +0200202 static int linelen_last;
Jens Axboecac58fd2008-06-06 01:17:03 +0200203 static int eta_good;
Jens Axboe6043c572006-11-03 11:37:47 +0100204
Aaron Carrolle592a062007-09-14 09:49:41 +0200205 if (temp_stall_ts || terse_output || eta_print == FIO_ETA_NEVER)
206 return;
207
208 if (!isatty(STDOUT_FILENO) && (eta_print != FIO_ETA_ALWAYS))
Jens Axboe263e5292006-10-18 15:37:01 +0200209 return;
210
Jens Axboe46fda8d2007-02-11 00:49:30 +0100211 if (!rate_io_bytes[0] && !rate_io_bytes[1])
212 fill_start_time(&rate_prev_time);
213 if (!disp_io_bytes[0] && !disp_io_bytes[1])
214 fill_start_time(&disp_prev_time);
Jens Axboe6043c572006-11-03 11:37:47 +0100215
Jens Axboed3eeeab2008-04-06 12:19:05 +0200216 eta_secs = malloc(thread_number * sizeof(unsigned long));
217 memset(eta_secs, 0, thread_number * sizeof(unsigned long));
Jens Axboe263e5292006-10-18 15:37:01 +0200218
Jens Axboe6043c572006-11-03 11:37:47 +0100219 io_bytes[0] = io_bytes[1] = 0;
Jens Axboeac71feb2007-03-16 10:24:07 +0100220 nr_pending = nr_running = t_rate = m_rate = t_iops = m_iops = 0;
Jens Axboebb3884d2007-01-17 17:23:11 +1100221 bw_avg_time = ULONG_MAX;
Jens Axboee6e41602007-03-27 20:20:36 +0200222 files_open = 0;
Jens Axboe34572e22006-10-20 09:33:18 +0200223 for_each_td(td, i) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100224 if (td->o.bw_avg_time < bw_avg_time)
225 bw_avg_time = td->o.bw_avg_time;
Jens Axboe46fda8d2007-02-11 00:49:30 +0100226 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING
227 || td->runstate == TD_FSYNCING) {
Jens Axboe263e5292006-10-18 15:37:01 +0200228 nr_running++;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100229 t_rate += td->o.rate;
230 m_rate += td->o.ratemin;
Jens Axboeac71feb2007-03-16 10:24:07 +0100231 t_iops += td->o.rate_iops;
232 m_iops += td->o.rate_iops_min;
Jens Axboee6e41602007-03-27 20:20:36 +0200233 files_open += td->nr_open_files;
Jens Axboe263e5292006-10-18 15:37:01 +0200234 } else if (td->runstate < TD_RUNNING)
235 nr_pending++;
236
237 if (elapsed >= 3)
238 eta_secs[i] = thread_eta(td, elapsed);
239 else
240 eta_secs[i] = INT_MAX;
241
242 check_str_update(td);
Jens Axboe6043c572006-11-03 11:37:47 +0100243 io_bytes[0] += td->io_bytes[0];
244 io_bytes[1] += td->io_bytes[1];
Jens Axboe263e5292006-10-18 15:37:01 +0200245 }
246
247 if (exitall_on_terminate)
248 eta_sec = INT_MAX;
249 else
250 eta_sec = 0;
251
Jens Axboe34572e22006-10-20 09:33:18 +0200252 for_each_td(td, i) {
Shaozhi Shawn Ye9c5a3852008-09-10 09:09:37 +0200253 if (exitall_on_terminate) {
254 if (eta_secs[i] < eta_sec)
255 eta_sec = eta_secs[i];
256 } else {
257 if (eta_secs[i] > eta_sec)
258 eta_sec = eta_secs[i];
259 }
Jens Axboe263e5292006-10-18 15:37:01 +0200260 }
261
Jens Axboeeecf2722006-10-20 14:00:36 +0200262 free(eta_secs);
263
Jens Axboe263e5292006-10-18 15:37:01 +0200264 if (eta_sec != INT_MAX && elapsed) {
265 perc = (double) elapsed / (double) (elapsed + eta_sec);
266 eta_to_str(eta_str, eta_sec);
267 }
268
Jens Axboe46fda8d2007-02-11 00:49:30 +0100269 fio_gettime(&now, NULL);
270 rate_time = mtime_since(&rate_prev_time, &now);
271
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100272 if (write_bw_log && rate_time > bw_avg_time) {
Jens Axboe46fda8d2007-02-11 00:49:30 +0100273 calc_rate(rate_time, io_bytes, rate_io_bytes, rate);
274 memcpy(&rate_prev_time, &now, sizeof(now));
275 add_agg_sample(rate[DDIR_READ], DDIR_READ);
276 add_agg_sample(rate[DDIR_WRITE], DDIR_WRITE);
Jens Axboe6043c572006-11-03 11:37:47 +0100277 }
278
Jens Axboe46fda8d2007-02-11 00:49:30 +0100279 disp_time = mtime_since(&disp_prev_time, &now);
280 if (disp_time < 1000)
281 return;
282
283 calc_rate(disp_time, io_bytes, disp_io_bytes, rate);
284 memcpy(&disp_prev_time, &now, sizeof(now));
285
Jens Axboe263e5292006-10-18 15:37:01 +0200286 if (!nr_running && !nr_pending)
287 return;
288
Jens Axboee6e41602007-03-27 20:20:36 +0200289 printf("Jobs: %d (f=%d)", nr_running, files_open);
Jens Axboe263e5292006-10-18 15:37:01 +0200290 if (m_rate || t_rate)
Jens Axboe2cb8d962007-01-13 15:15:47 +0100291 printf(", CR=%d/%d KiB/s", t_rate, m_rate);
Jens Axboeac71feb2007-03-16 10:24:07 +0100292 else if (m_iops || t_iops)
293 printf(", CR=%d/%d IOPS", t_iops, m_iops);
Jens Axboe263e5292006-10-18 15:37:01 +0200294 if (eta_sec != INT_MAX && nr_running) {
Jens Axboe0721d112008-06-05 09:03:05 +0200295 char perc_str[32];
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100296 int ll;
297
Jens Axboecac58fd2008-06-06 01:17:03 +0200298 if (!eta_sec && !eta_good)
299 strcpy(perc_str, "-.-% done");
300 else {
301 eta_good = 1;
302 perc *= 100.0;
Jens Axboe0721d112008-06-05 09:03:05 +0200303 sprintf(perc_str, "%3.1f%% done", perc);
Jens Axboecac58fd2008-06-06 01:17:03 +0200304 }
Jens Axboe0721d112008-06-05 09:03:05 +0200305
Jens Axboe0721d112008-06-05 09:03:05 +0200306 ll = printf(": [%s] [%s] [%6u/%6u kb/s] [eta %s]",
307 run_str, perc_str, rate[0], rate[1], eta_str);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100308 if (ll >= 0 && ll < linelen_last)
309 printf("%*s", linelen_last - ll, "");
310 linelen_last = ll;
Jens Axboe263e5292006-10-18 15:37:01 +0200311 }
312 printf("\r");
313 fflush(stdout);
Jens Axboe263e5292006-10-18 15:37:01 +0200314}
315
316void print_status_init(int thread_number)
317{
318 run_str[thread_number] = 'P';
319}