client: initial support for multiple connections
[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 9
fca70358 10static char run_str[REAL_MAX_JOBS + 1];
263e529f
JA
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;
b29ee5b3
JA
26 case TD_RAMP:
27 c = '/';
28 break;
5ec10eaa
JA
29 case TD_RUNNING:
30 if (td_rw(td)) {
e3b3f81e
JA
31 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 }
5ec10eaa
JA
46 } 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;
b0f65863
JA
58 case TD_PRE_READING:
59 c = 'p';
60 break;
5ec10eaa
JA
61 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);
263e529f
JA
78 }
79
80 run_str[td->thread_number - 1] = c;
81}
82
83/*
84 * Convert seconds to a printable string.
85 */
9c02f6ef 86static void eta_to_str(char *str, unsigned long eta_sec)
263e529f
JA
87{
88 unsigned int d, h, m, s;
165faf16 89 int disp_hour = 0;
263e529f 90
263e529f
JA
91 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
165faf16
JA
99 if (d) {
100 disp_hour = 1;
1e97cce9 101 str += sprintf(str, "%02ud:", d);
263e529f 102 }
165faf16
JA
103
104 if (h || disp_hour)
1e97cce9 105 str += sprintf(str, "%02uh:", h);
263e529f 106
1e97cce9
JA
107 str += sprintf(str, "%02um:", m);
108 str += sprintf(str, "%02us", s);
263e529f
JA
109}
110
111/*
112 * Best effort calculation of the estimated pending runtime of a job.
113 */
b29ee5b3 114static int thread_eta(struct thread_data *td)
263e529f
JA
115{
116 unsigned long long bytes_total, bytes_done;
1e97cce9 117 unsigned long eta_sec = 0;
b29ee5b3
JA
118 unsigned long elapsed;
119
120 elapsed = (mtime_since_now(&td->epoch) + 999) / 1000;
263e529f
JA
121
122 bytes_total = td->total_io_size;
123
2e3bd4c2
JA
124 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
74939e38
JA
131 /*
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 */
0dd8377f 135 if (td->o.do_verify && td->o.verify && td_write(td)) {
74939e38
JA
136 if (td_rw(td))
137 bytes_total = bytes_total * 3 / 2;
138 else
139 bytes_total <<= 1;
140 }
141
2dc1bbeb
JA
142 if (td->o.zone_size && td->o.zone_skip)
143 bytes_total /= (td->o.zone_skip / td->o.zone_size);
263e529f
JA
144
145 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
cf4464ca 146 double perc, perc_t;
263e529f
JA
147
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
cf4464ca
JA
153 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
1e97cce9 159 eta_sec = (unsigned long) (elapsed * (1.0 / perc)) - elapsed;
263e529f 160
d3eeeabc
JA
161 if (td->o.timeout &&
162 eta_sec > (td->o.timeout + done_secs - elapsed))
163 eta_sec = td->o.timeout + done_secs - elapsed;
263e529f 164 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
b29ee5b3 165 || td->runstate == TD_INITIALIZED
b0f65863
JA
166 || td->runstate == TD_RAMP
167 || td->runstate == TD_PRE_READING) {
263e529f
JA
168 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 */
b29ee5b3 174 if (td->o.timeout) {
cda99fa0
SSP
175 t_eta = td->o.timeout + td->o.start_delay +
176 td->o.ramp_time;
b29ee5b3
JA
177
178 if (in_ramp_time(td)) {
179 unsigned long ramp_left;
180
cda99fa0 181 ramp_left = mtime_since_now(&td->epoch);
b29ee5b3
JA
182 ramp_left = (ramp_left + 999) / 1000;
183 if (ramp_left <= t_eta)
184 t_eta -= ramp_left;
185 }
186 }
581e7141 187 if (td->o.rate[0] || td->o.rate[1]) {
0b9d69ec
JA
188 r_eta = (bytes_total / 1024) /
189 (td->o.rate[0] + td->o.rate[1]);
d3eeeabc 190 r_eta += td->o.start_delay;
263e529f
JA
191 }
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
46fda8d0
JA
211static 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}
5ec10eaa 219
adb02ba8
JA
220static 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
263e529f
JA
229/*
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{
b29ee5b3
JA
235 unsigned long elapsed = (mtime_since_genesis() + 999) / 1000;
236 int i, nr_ramp, nr_running, nr_pending, t_rate, m_rate;
e6e41602 237 int t_iops, m_iops, files_open;
34572e28 238 struct thread_data *td;
2eaa41ce 239 char eta_str[128];
263e529f 240 double perc = 0.0;
adb02ba8 241 unsigned long long io_bytes[2], io_iops[2];
d3eeeabc 242 unsigned long rate_time, disp_time, bw_avg_time, *eta_secs, eta_sec;
46fda8d0
JA
243 struct timeval now;
244
245 static unsigned long long rate_io_bytes[2];
246 static unsigned long long disp_io_bytes[2];
adb02ba8 247 static unsigned long long disp_io_iops[2];
46fda8d0 248 static struct timeval rate_prev_time, disp_prev_time;
adb02ba8 249 static unsigned int rate[2], iops[2];
43819609 250 static int linelen_last;
cac58fd5 251 static int eta_good;
37db14fe 252 char output[512], *p = output;
d6978a32 253 int i2p = 0;
6043c579 254
e592a06b
AC
255 if (temp_stall_ts || terse_output || eta_print == FIO_ETA_NEVER)
256 return;
257
258 if (!isatty(STDOUT_FILENO) && (eta_print != FIO_ETA_ALWAYS))
263e529f
JA
259 return;
260
46fda8d0
JA
261 if (!rate_io_bytes[0] && !rate_io_bytes[1])
262 fill_start_time(&rate_prev_time);
263 if (!disp_io_bytes[0] && !disp_io_bytes[1])
264 fill_start_time(&disp_prev_time);
6043c579 265
d3eeeabc
JA
266 eta_secs = malloc(thread_number * sizeof(unsigned long));
267 memset(eta_secs, 0, thread_number * sizeof(unsigned long));
263e529f 268
6043c579 269 io_bytes[0] = io_bytes[1] = 0;
adb02ba8 270 io_iops[0] = io_iops[1] = 0;
ac71feb1 271 nr_pending = nr_running = t_rate = m_rate = t_iops = m_iops = 0;
b29ee5b3 272 nr_ramp = 0;
bb3884d8 273 bw_avg_time = ULONG_MAX;
e6e41602 274 files_open = 0;
34572e28 275 for_each_td(td, i) {
2dc1bbeb
JA
276 if (td->o.bw_avg_time < bw_avg_time)
277 bw_avg_time = td->o.bw_avg_time;
46fda8d0 278 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING
b0f65863
JA
279 || td->runstate == TD_FSYNCING
280 || td->runstate == TD_PRE_READING) {
263e529f 281 nr_running++;
581e7141
JA
282 t_rate += td->o.rate[0] + td->o.rate[1];
283 m_rate += td->o.ratemin[0] + td->o.ratemin[1];
284 t_iops += td->o.rate_iops[0] + td->o.rate_iops[1];
0b9d69ec
JA
285 m_iops += td->o.rate_iops_min[0] +
286 td->o.rate_iops_min[1];
e6e41602 287 files_open += td->nr_open_files;
b29ee5b3
JA
288 } else if (td->runstate == TD_RAMP) {
289 nr_running++;
290 nr_ramp++;
263e529f
JA
291 } else if (td->runstate < TD_RUNNING)
292 nr_pending++;
293
294 if (elapsed >= 3)
b29ee5b3 295 eta_secs[i] = thread_eta(td);
263e529f
JA
296 else
297 eta_secs[i] = INT_MAX;
298
299 check_str_update(td);
b29ee5b3
JA
300
301 if (td->runstate > TD_RAMP) {
302 io_bytes[0] += td->io_bytes[0];
303 io_bytes[1] += td->io_bytes[1];
adb02ba8
JA
304 io_iops[0] += td->io_blocks[0];
305 io_iops[1] += td->io_blocks[1];
b29ee5b3 306 }
263e529f
JA
307 }
308
309 if (exitall_on_terminate)
310 eta_sec = INT_MAX;
311 else
312 eta_sec = 0;
313
34572e28 314 for_each_td(td, i) {
d6978a32
JA
315 if (!i2p && is_power_of_2(td->o.kb_base))
316 i2p = 1;
9c5a3854
SSY
317 if (exitall_on_terminate) {
318 if (eta_secs[i] < eta_sec)
319 eta_sec = eta_secs[i];
320 } else {
321 if (eta_secs[i] > eta_sec)
322 eta_sec = eta_secs[i];
323 }
263e529f
JA
324 }
325
eecf272f
JA
326 free(eta_secs);
327
263e529f
JA
328 if (eta_sec != INT_MAX && elapsed) {
329 perc = (double) elapsed / (double) (elapsed + eta_sec);
330 eta_to_str(eta_str, eta_sec);
331 }
332
46fda8d0
JA
333 fio_gettime(&now, NULL);
334 rate_time = mtime_since(&rate_prev_time, &now);
335
b29ee5b3 336 if (write_bw_log && rate_time > bw_avg_time && !in_ramp_time(td)) {
46fda8d0
JA
337 calc_rate(rate_time, io_bytes, rate_io_bytes, rate);
338 memcpy(&rate_prev_time, &now, sizeof(now));
306ddc97
JA
339 add_agg_sample(rate[DDIR_READ], DDIR_READ, 0);
340 add_agg_sample(rate[DDIR_WRITE], DDIR_WRITE, 0);
6043c579
JA
341 }
342
46fda8d0 343 disp_time = mtime_since(&disp_prev_time, &now);
a5b01f1b
JA
344
345 /*
346 * Allow a little slack, the target is to print it every 1000 msecs
347 */
348 if (disp_time < 900)
46fda8d0
JA
349 return;
350
351 calc_rate(disp_time, io_bytes, disp_io_bytes, rate);
adb02ba8
JA
352 calc_iops(disp_time, io_iops, disp_io_iops, iops);
353
46fda8d0
JA
354 memcpy(&disp_prev_time, &now, sizeof(now));
355
263e529f
JA
356 if (!nr_running && !nr_pending)
357 return;
358
37db14fe 359 p += sprintf(p, "Jobs: %d (f=%d)", nr_running, files_open);
581e7141
JA
360 if (m_rate || t_rate) {
361 char *tr, *mr;
362
d6978a32
JA
363 mr = num2str(m_rate, 4, 0, i2p);
364 tr = num2str(t_rate, 4, 0, i2p);
37db14fe 365 p += sprintf(p, ", CR=%s/%s KB/s", tr, mr);
581e7141
JA
366 free(tr);
367 free(mr);
368 } else if (m_iops || t_iops)
37db14fe 369 p += sprintf(p, ", CR=%d/%d IOPS", t_iops, m_iops);
263e529f 370 if (eta_sec != INT_MAX && nr_running) {
0721d11e 371 char perc_str[32];
adb02ba8 372 char *iops_str[2];
d1bd7213 373 char *rate_str[2];
adb02ba8 374 int l;
5ec10eaa 375
b29ee5b3 376 if ((!eta_sec && !eta_good) || nr_ramp == nr_running)
cac58fd5
JA
377 strcpy(perc_str, "-.-% done");
378 else {
379 eta_good = 1;
380 perc *= 100.0;
0721d11e 381 sprintf(perc_str, "%3.1f%% done", perc);
cac58fd5 382 }
0721d11e 383
d6978a32
JA
384 rate_str[0] = num2str(rate[0], 5, 10, i2p);
385 rate_str[1] = num2str(rate[1], 5, 10, i2p);
d1bd7213 386
adb02ba8
JA
387 iops_str[0] = num2str(iops[0], 4, 1, 0);
388 iops_str[1] = num2str(iops[1], 4, 1, 0);
389
37db14fe 390 l = sprintf(p, ": [%s] [%s] [%s/%s /s] [%s/%s iops] [eta %s]",
0b9d69ec 391 run_str, perc_str, rate_str[0], rate_str[1],
adb02ba8 392 iops_str[0], iops_str[1], eta_str);
37db14fe 393 p += l;
adb02ba8 394 if (l >= 0 && l < linelen_last)
37db14fe 395 p += sprintf(p, "%*s", linelen_last - l, "");
adb02ba8 396 linelen_last = l;
d1bd7213
JA
397
398 free(rate_str[0]);
399 free(rate_str[1]);
adb02ba8
JA
400 free(iops_str[0]);
401 free(iops_str[1]);
263e529f 402 }
37db14fe
JA
403 p += sprintf(p, "\r");
404
405 if (!is_backend) {
406 printf("%s", output);
407 fflush(stdout);
408 } else
409 fio_server_text_output(output, p - output);
263e529f
JA
410}
411
2b13e716 412void print_status_init(int thr_number)
263e529f 413{
2b13e716 414 run_str[thr_number] = 'P';
263e529f 415}