Add uninitialized_var() to silence bogus compiler warnings
[fio.git] / eta.c
CommitLineData
263e529f
JA
1/*
2 * Status and ETA code
3 */
4#include <unistd.h>
5#include <fcntl.h>
6#include <string.h>
7
8#include "fio.h"
263e529f
JA
9
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) {
5ec10eaa
JA
20 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);
263e529f
JA
61 }
62
63 run_str[td->thread_number - 1] = c;
64}
65
66/*
67 * Convert seconds to a printable string.
68 */
9c02f6ef 69static void eta_to_str(char *str, unsigned long eta_sec)
263e529f
JA
70{
71 unsigned int d, h, m, s;
165faf16 72 int disp_hour = 0;
263e529f 73
263e529f
JA
74 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
165faf16
JA
82 if (d) {
83 disp_hour = 1;
1e97cce9 84 str += sprintf(str, "%02ud:", d);
263e529f 85 }
165faf16
JA
86
87 if (h || disp_hour)
1e97cce9 88 str += sprintf(str, "%02uh:", h);
263e529f 89
1e97cce9
JA
90 str += sprintf(str, "%02um:", m);
91 str += sprintf(str, "%02us", s);
263e529f
JA
92}
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;
1e97cce9 100 unsigned long eta_sec = 0;
263e529f
JA
101
102 bytes_total = td->total_io_size;
103
74939e38
JA
104 /*
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 */
0dd8377f 108 if (td->o.do_verify && td->o.verify && td_write(td)) {
74939e38
JA
109 if (td_rw(td))
110 bytes_total = bytes_total * 3 / 2;
111 else
112 bytes_total <<= 1;
113 }
114
2dc1bbeb
JA
115 if (td->o.zone_size && td->o.zone_skip)
116 bytes_total /= (td->o.zone_skip / td->o.zone_size);
263e529f
JA
117
118 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
cf4464ca 119 double perc, perc_t;
263e529f
JA
120
121 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
122 perc = (double) bytes_done / (double) bytes_total;
123 if (perc > 1.0)
124 perc = 1.0;
125
cf4464ca
JA
126 if (td->o.time_based) {
127 perc_t = (double) elapsed / (double) td->o.timeout;
128 if (perc_t < perc)
129 perc = perc_t;
130 }
131
1e97cce9 132 eta_sec = (unsigned long) (elapsed * (1.0 / perc)) - elapsed;
263e529f 133
d3eeeabc
JA
134 if (td->o.timeout &&
135 eta_sec > (td->o.timeout + done_secs - elapsed))
136 eta_sec = td->o.timeout + done_secs - elapsed;
263e529f
JA
137 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
138 || td->runstate == TD_INITIALIZED) {
139 int t_eta = 0, r_eta = 0;
140
141 /*
142 * We can only guess - assume it'll run the full timeout
143 * if given, otherwise assume it'll run at the specified rate.
144 */
2dc1bbeb 145 if (td->o.timeout)
d3eeeabc 146 t_eta = td->o.timeout + td->o.start_delay;
2dc1bbeb
JA
147 if (td->o.rate) {
148 r_eta = (bytes_total / 1024) / td->o.rate;
d3eeeabc 149 r_eta += td->o.start_delay;
263e529f
JA
150 }
151
152 if (r_eta && t_eta)
153 eta_sec = min(r_eta, t_eta);
154 else if (r_eta)
155 eta_sec = r_eta;
156 else if (t_eta)
157 eta_sec = t_eta;
158 else
159 eta_sec = 0;
160 } else {
161 /*
162 * thread is already done or waiting for fsync
163 */
164 eta_sec = 0;
165 }
166
167 return eta_sec;
168}
169
46fda8d0
JA
170static void calc_rate(unsigned long mtime, unsigned long long *io_bytes,
171 unsigned long long *prev_io_bytes, unsigned int *rate)
172{
173 rate[0] = (io_bytes[0] - prev_io_bytes[0]) / mtime;
174 rate[1] = (io_bytes[1] - prev_io_bytes[1]) / mtime;
175 prev_io_bytes[0] = io_bytes[0];
176 prev_io_bytes[1] = io_bytes[1];
177}
5ec10eaa 178
263e529f
JA
179/*
180 * Print status of the jobs we know about. This includes rate estimates,
181 * ETA, thread state, etc.
182 */
183void print_thread_status(void)
184{
185 unsigned long elapsed = mtime_since_genesis() / 1000;
d3eeeabc 186 int i, nr_running, nr_pending, t_rate, m_rate;
e6e41602 187 int t_iops, m_iops, files_open;
34572e28 188 struct thread_data *td;
2eaa41ce 189 char eta_str[128];
263e529f 190 double perc = 0.0;
6043c579 191 unsigned long long io_bytes[2];
d3eeeabc 192 unsigned long rate_time, disp_time, bw_avg_time, *eta_secs, eta_sec;
46fda8d0
JA
193 struct timeval now;
194
195 static unsigned long long rate_io_bytes[2];
196 static unsigned long long disp_io_bytes[2];
197 static struct timeval rate_prev_time, disp_prev_time;
198 static unsigned int rate[2];
43819609 199 static int linelen_last;
cac58fd5 200 static int eta_good;
6043c579 201
e592a06b
AC
202 if (temp_stall_ts || terse_output || eta_print == FIO_ETA_NEVER)
203 return;
204
205 if (!isatty(STDOUT_FILENO) && (eta_print != FIO_ETA_ALWAYS))
263e529f
JA
206 return;
207
46fda8d0
JA
208 if (!rate_io_bytes[0] && !rate_io_bytes[1])
209 fill_start_time(&rate_prev_time);
210 if (!disp_io_bytes[0] && !disp_io_bytes[1])
211 fill_start_time(&disp_prev_time);
6043c579 212
d3eeeabc
JA
213 eta_secs = malloc(thread_number * sizeof(unsigned long));
214 memset(eta_secs, 0, thread_number * sizeof(unsigned long));
263e529f 215
6043c579 216 io_bytes[0] = io_bytes[1] = 0;
ac71feb1 217 nr_pending = nr_running = t_rate = m_rate = t_iops = m_iops = 0;
bb3884d8 218 bw_avg_time = ULONG_MAX;
e6e41602 219 files_open = 0;
34572e28 220 for_each_td(td, i) {
2dc1bbeb
JA
221 if (td->o.bw_avg_time < bw_avg_time)
222 bw_avg_time = td->o.bw_avg_time;
46fda8d0
JA
223 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING
224 || td->runstate == TD_FSYNCING) {
263e529f 225 nr_running++;
2dc1bbeb
JA
226 t_rate += td->o.rate;
227 m_rate += td->o.ratemin;
ac71feb1
JA
228 t_iops += td->o.rate_iops;
229 m_iops += td->o.rate_iops_min;
e6e41602 230 files_open += td->nr_open_files;
263e529f
JA
231 } else if (td->runstate < TD_RUNNING)
232 nr_pending++;
233
234 if (elapsed >= 3)
235 eta_secs[i] = thread_eta(td, elapsed);
236 else
237 eta_secs[i] = INT_MAX;
238
239 check_str_update(td);
6043c579
JA
240 io_bytes[0] += td->io_bytes[0];
241 io_bytes[1] += td->io_bytes[1];
263e529f
JA
242 }
243
244 if (exitall_on_terminate)
245 eta_sec = INT_MAX;
246 else
247 eta_sec = 0;
248
34572e28 249 for_each_td(td, i) {
d3eeeabc
JA
250 if (eta_secs[i] != INT_MAX)
251 eta_sec += eta_secs[i];
263e529f
JA
252 }
253
eecf272f
JA
254 free(eta_secs);
255
263e529f
JA
256 if (eta_sec != INT_MAX && elapsed) {
257 perc = (double) elapsed / (double) (elapsed + eta_sec);
258 eta_to_str(eta_str, eta_sec);
259 }
260
46fda8d0
JA
261 fio_gettime(&now, NULL);
262 rate_time = mtime_since(&rate_prev_time, &now);
263
5ec10eaa 264 if (write_bw_log && rate_time > bw_avg_time) {
46fda8d0
JA
265 calc_rate(rate_time, io_bytes, rate_io_bytes, rate);
266 memcpy(&rate_prev_time, &now, sizeof(now));
267 add_agg_sample(rate[DDIR_READ], DDIR_READ);
268 add_agg_sample(rate[DDIR_WRITE], DDIR_WRITE);
6043c579
JA
269 }
270
46fda8d0
JA
271 disp_time = mtime_since(&disp_prev_time, &now);
272 if (disp_time < 1000)
273 return;
274
275 calc_rate(disp_time, io_bytes, disp_io_bytes, rate);
276 memcpy(&disp_prev_time, &now, sizeof(now));
277
263e529f
JA
278 if (!nr_running && !nr_pending)
279 return;
280
e6e41602 281 printf("Jobs: %d (f=%d)", nr_running, files_open);
263e529f 282 if (m_rate || t_rate)
2cb8d960 283 printf(", CR=%d/%d KiB/s", t_rate, m_rate);
ac71feb1
JA
284 else if (m_iops || t_iops)
285 printf(", CR=%d/%d IOPS", t_iops, m_iops);
263e529f 286 if (eta_sec != INT_MAX && nr_running) {
0721d11e 287 char perc_str[32];
5ec10eaa
JA
288 int ll;
289
cac58fd5
JA
290 if (!eta_sec && !eta_good)
291 strcpy(perc_str, "-.-% done");
292 else {
293 eta_good = 1;
294 perc *= 100.0;
0721d11e 295 sprintf(perc_str, "%3.1f%% done", perc);
cac58fd5 296 }
0721d11e 297
0721d11e
JA
298 ll = printf(": [%s] [%s] [%6u/%6u kb/s] [eta %s]",
299 run_str, perc_str, rate[0], rate[1], eta_str);
5ec10eaa
JA
300 if (ll >= 0 && ll < linelen_last)
301 printf("%*s", linelen_last - ll, "");
302 linelen_last = ll;
263e529f
JA
303 }
304 printf("\r");
305 fflush(stdout);
263e529f
JA
306}
307
308void print_status_init(int thread_number)
309{
310 run_str[thread_number] = 'P';
311}