GUASI API change
[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) {
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)) {
413dd459 28 if (td_random(td))
263e529f 29 c = 'm';
263e529f 30 else
413dd459
JA
31 c = 'M';
32 } else if (td_read(td)) {
33 if (td_random(td))
263e529f 34 c = 'r';
263e529f 35 else
413dd459
JA
36 c = 'R';
37 } else {
38 if (td_random(td))
263e529f 39 c = 'w';
413dd459
JA
40 else
41 c = 'W';
263e529f
JA
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);
61 }
62
63 run_str[td->thread_number - 1] = c;
64}
65
66/*
67 * Convert seconds to a printable string.
68 */
69static void eta_to_str(char *str, int eta_sec)
70{
71 unsigned int d, h, m, s;
165faf16 72 int disp_hour = 0;
263e529f
JA
73
74 d = h = m = s = 0;
75
76 s = eta_sec % 60;
77 eta_sec /= 60;
78 m = eta_sec % 60;
79 eta_sec /= 60;
80 h = eta_sec % 24;
81 eta_sec /= 24;
82 d = eta_sec;
83
165faf16
JA
84 if (d) {
85 disp_hour = 1;
1e97cce9 86 str += sprintf(str, "%02ud:", d);
263e529f 87 }
165faf16
JA
88
89 if (h || disp_hour)
1e97cce9 90 str += sprintf(str, "%02uh:", h);
263e529f 91
1e97cce9
JA
92 str += sprintf(str, "%02um:", m);
93 str += sprintf(str, "%02us", s);
263e529f
JA
94}
95
96/*
97 * Best effort calculation of the estimated pending runtime of a job.
98 */
99static int thread_eta(struct thread_data *td, unsigned long elapsed)
100{
101 unsigned long long bytes_total, bytes_done;
1e97cce9 102 unsigned long eta_sec = 0;
263e529f
JA
103
104 bytes_total = td->total_io_size;
105
74939e38
JA
106 /*
107 * if writing, bytes_total will be twice the size. If mixing,
108 * assume a 50/50 split and thus bytes_total will be 50% larger.
109 */
2dc1bbeb 110 if (td->o.verify) {
74939e38
JA
111 if (td_rw(td))
112 bytes_total = bytes_total * 3 / 2;
113 else
114 bytes_total <<= 1;
115 }
116
2dc1bbeb
JA
117 if (td->o.zone_size && td->o.zone_skip)
118 bytes_total /= (td->o.zone_skip / td->o.zone_size);
263e529f
JA
119
120 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
121 double perc;
122
123 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
124 perc = (double) bytes_done / (double) bytes_total;
125 if (perc > 1.0)
126 perc = 1.0;
127
1e97cce9 128 eta_sec = (unsigned long) (elapsed * (1.0 / perc)) - elapsed;
263e529f 129
2dc1bbeb
JA
130 if (td->o.timeout && eta_sec > (td->o.timeout - elapsed))
131 eta_sec = td->o.timeout - elapsed;
263e529f
JA
132 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
133 || td->runstate == TD_INITIALIZED) {
134 int t_eta = 0, r_eta = 0;
135
136 /*
137 * We can only guess - assume it'll run the full timeout
138 * if given, otherwise assume it'll run at the specified rate.
139 */
2dc1bbeb
JA
140 if (td->o.timeout)
141 t_eta = td->o.timeout + td->o.start_delay - elapsed;
142 if (td->o.rate) {
143 r_eta = (bytes_total / 1024) / td->o.rate;
144 r_eta += td->o.start_delay - elapsed;
263e529f
JA
145 }
146
147 if (r_eta && t_eta)
148 eta_sec = min(r_eta, t_eta);
149 else if (r_eta)
150 eta_sec = r_eta;
151 else if (t_eta)
152 eta_sec = t_eta;
153 else
154 eta_sec = 0;
155 } else {
156 /*
157 * thread is already done or waiting for fsync
158 */
159 eta_sec = 0;
160 }
161
162 return eta_sec;
163}
164
46fda8d0
JA
165static void calc_rate(unsigned long mtime, unsigned long long *io_bytes,
166 unsigned long long *prev_io_bytes, unsigned int *rate)
167{
168 rate[0] = (io_bytes[0] - prev_io_bytes[0]) / mtime;
169 rate[1] = (io_bytes[1] - prev_io_bytes[1]) / mtime;
170 prev_io_bytes[0] = io_bytes[0];
171 prev_io_bytes[1] = io_bytes[1];
172}
173
263e529f
JA
174/*
175 * Print status of the jobs we know about. This includes rate estimates,
176 * ETA, thread state, etc.
177 */
178void print_thread_status(void)
179{
180 unsigned long elapsed = mtime_since_genesis() / 1000;
181 int i, nr_running, nr_pending, t_rate, m_rate, *eta_secs, eta_sec;
e6e41602 182 int t_iops, m_iops, files_open;
34572e28 183 struct thread_data *td;
2eaa41ce 184 char eta_str[128];
263e529f 185 double perc = 0.0;
6043c579 186 unsigned long long io_bytes[2];
46fda8d0
JA
187 unsigned long rate_time, disp_time, bw_avg_time;
188 struct timeval now;
189
190 static unsigned long long rate_io_bytes[2];
191 static unsigned long long disp_io_bytes[2];
192 static struct timeval rate_prev_time, disp_prev_time;
193 static unsigned int rate[2];
6043c579 194
263e529f
JA
195 if (temp_stall_ts || terse_output)
196 return;
197
46fda8d0
JA
198 if (!rate_io_bytes[0] && !rate_io_bytes[1])
199 fill_start_time(&rate_prev_time);
200 if (!disp_io_bytes[0] && !disp_io_bytes[1])
201 fill_start_time(&disp_prev_time);
6043c579 202
263e529f
JA
203 eta_secs = malloc(thread_number * sizeof(int));
204 memset(eta_secs, 0, thread_number * sizeof(int));
205
6043c579 206 io_bytes[0] = io_bytes[1] = 0;
ac71feb1 207 nr_pending = nr_running = t_rate = m_rate = t_iops = m_iops = 0;
bb3884d8 208 bw_avg_time = ULONG_MAX;
e6e41602 209 files_open = 0;
34572e28 210 for_each_td(td, i) {
2dc1bbeb
JA
211 if (td->o.bw_avg_time < bw_avg_time)
212 bw_avg_time = td->o.bw_avg_time;
46fda8d0
JA
213 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING
214 || td->runstate == TD_FSYNCING) {
263e529f 215 nr_running++;
2dc1bbeb
JA
216 t_rate += td->o.rate;
217 m_rate += td->o.ratemin;
ac71feb1
JA
218 t_iops += td->o.rate_iops;
219 m_iops += td->o.rate_iops_min;
e6e41602 220 files_open += td->nr_open_files;
263e529f
JA
221 } else if (td->runstate < TD_RUNNING)
222 nr_pending++;
223
224 if (elapsed >= 3)
225 eta_secs[i] = thread_eta(td, elapsed);
226 else
227 eta_secs[i] = INT_MAX;
228
229 check_str_update(td);
6043c579
JA
230 io_bytes[0] += td->io_bytes[0];
231 io_bytes[1] += td->io_bytes[1];
263e529f
JA
232 }
233
234 if (exitall_on_terminate)
235 eta_sec = INT_MAX;
236 else
237 eta_sec = 0;
238
34572e28 239 for_each_td(td, i) {
263e529f
JA
240 if (exitall_on_terminate) {
241 if (eta_secs[i] < eta_sec)
242 eta_sec = eta_secs[i];
243 } else {
244 if (eta_secs[i] > eta_sec)
245 eta_sec = eta_secs[i];
246 }
247 }
248
eecf272f
JA
249 free(eta_secs);
250
263e529f
JA
251 if (eta_sec != INT_MAX && elapsed) {
252 perc = (double) elapsed / (double) (elapsed + eta_sec);
253 eta_to_str(eta_str, eta_sec);
254 }
255
46fda8d0
JA
256 fio_gettime(&now, NULL);
257 rate_time = mtime_since(&rate_prev_time, &now);
258
259 if (write_bw_log && rate_time> bw_avg_time) {
260 calc_rate(rate_time, io_bytes, rate_io_bytes, rate);
261 memcpy(&rate_prev_time, &now, sizeof(now));
262 add_agg_sample(rate[DDIR_READ], DDIR_READ);
263 add_agg_sample(rate[DDIR_WRITE], DDIR_WRITE);
6043c579
JA
264 }
265
46fda8d0
JA
266 disp_time = mtime_since(&disp_prev_time, &now);
267 if (disp_time < 1000)
268 return;
269
270 calc_rate(disp_time, io_bytes, disp_io_bytes, rate);
271 memcpy(&disp_prev_time, &now, sizeof(now));
272
263e529f
JA
273 if (!nr_running && !nr_pending)
274 return;
275
e6e41602 276 printf("Jobs: %d (f=%d)", nr_running, files_open);
263e529f 277 if (m_rate || t_rate)
2cb8d960 278 printf(", CR=%d/%d KiB/s", t_rate, m_rate);
ac71feb1
JA
279 else if (m_iops || t_iops)
280 printf(", CR=%d/%d IOPS", t_iops, m_iops);
263e529f
JA
281 if (eta_sec != INT_MAX && nr_running) {
282 perc *= 100.0;
46fda8d0 283 printf(": [%s] [%3.1f%% done] [%6u/%6u kb/s] [eta %s]", run_str, perc, rate[0], rate[1], eta_str);
263e529f
JA
284 }
285 printf("\r");
286 fflush(stdout);
263e529f
JA
287}
288
289void print_status_init(int thread_number)
290{
291 run_str[thread_number] = 'P';
292}