[PATCH] Misc fixes
[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"
9#include "os.h"
10
11static char run_str[MAX_JOBS + 1];
12
13/*
14 * Sets the status of the 'td' in the printed status map.
15 */
16static void check_str_update(struct thread_data *td)
17{
18 char c = run_str[td->thread_number - 1];
19
20 switch (td->runstate) {
21 case TD_REAPED:
22 c = '_';
23 break;
24 case TD_EXITED:
25 c = 'E';
26 break;
27 case TD_RUNNING:
28 if (td_rw(td)) {
29 if (td->sequential)
30 c = 'M';
31 else
32 c = 'm';
33 } else if (td_read(td)) {
34 if (td->sequential)
35 c = 'R';
36 else
37 c = 'r';
38 } else {
39 if (td->sequential)
40 c = 'W';
41 else
42 c = 'w';
43 }
44 break;
45 case TD_VERIFYING:
46 c = 'V';
47 break;
48 case TD_FSYNCING:
49 c = 'F';
50 break;
51 case TD_CREATED:
52 c = 'C';
53 break;
54 case TD_INITIALIZED:
55 c = 'I';
56 break;
57 case TD_NOT_CREATED:
58 c = 'P';
59 break;
60 default:
61 log_err("state %d\n", td->runstate);
62 }
63
64 run_str[td->thread_number - 1] = c;
65}
66
67/*
68 * Convert seconds to a printable string.
69 */
70static void eta_to_str(char *str, int eta_sec)
71{
72 unsigned int d, h, m, s;
165faf16 73 int disp_hour = 0;
263e529f
JA
74
75 d = h = m = s = 0;
76
77 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
165faf16
JA
85 if (d) {
86 disp_hour = 1;
1e97cce9 87 str += sprintf(str, "%02ud:", d);
263e529f 88 }
165faf16
JA
89
90 if (h || disp_hour)
1e97cce9 91 str += sprintf(str, "%02uh:", h);
263e529f 92
1e97cce9
JA
93 str += sprintf(str, "%02um:", m);
94 str += sprintf(str, "%02us", s);
263e529f
JA
95}
96
97/*
98 * Best effort calculation of the estimated pending runtime of a job.
99 */
100static int thread_eta(struct thread_data *td, unsigned long elapsed)
101{
102 unsigned long long bytes_total, bytes_done;
1e97cce9 103 unsigned long eta_sec = 0;
263e529f
JA
104
105 bytes_total = td->total_io_size;
106
74939e38
JA
107 /*
108 * if writing, bytes_total will be twice the size. If mixing,
109 * assume a 50/50 split and thus bytes_total will be 50% larger.
110 */
111 if (td->verify) {
112 if (td_rw(td))
113 bytes_total = bytes_total * 3 / 2;
114 else
115 bytes_total <<= 1;
116 }
117
263e529f
JA
118 if (td->zone_size && td->zone_skip)
119 bytes_total /= (td->zone_skip / td->zone_size);
120
121 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
122 double perc;
123
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
1e97cce9 129 eta_sec = (unsigned long) (elapsed * (1.0 / perc)) - elapsed;
263e529f
JA
130
131 if (td->timeout && eta_sec > (td->timeout - elapsed))
132 eta_sec = td->timeout - elapsed;
133 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
134 || td->runstate == TD_INITIALIZED) {
135 int t_eta = 0, r_eta = 0;
136
137 /*
138 * We can only guess - assume it'll run the full timeout
139 * if given, otherwise assume it'll run at the specified rate.
140 */
141 if (td->timeout)
142 t_eta = td->timeout + td->start_delay - elapsed;
143 if (td->rate) {
144 r_eta = (bytes_total / 1024) / td->rate;
145 r_eta += td->start_delay - elapsed;
146 }
147
148 if (r_eta && t_eta)
149 eta_sec = min(r_eta, t_eta);
150 else if (r_eta)
151 eta_sec = r_eta;
152 else if (t_eta)
153 eta_sec = t_eta;
154 else
155 eta_sec = 0;
156 } else {
157 /*
158 * thread is already done or waiting for fsync
159 */
160 eta_sec = 0;
161 }
162
163 return eta_sec;
164}
165
166/*
167 * Print status of the jobs we know about. This includes rate estimates,
168 * ETA, thread state, etc.
169 */
170void print_thread_status(void)
171{
172 unsigned long elapsed = mtime_since_genesis() / 1000;
173 int i, nr_running, nr_pending, t_rate, m_rate, *eta_secs, eta_sec;
34572e28 174 struct thread_data *td;
263e529f
JA
175 char eta_str[32];
176 double perc = 0.0;
177
6043c579
JA
178 static unsigned long long prev_io_bytes[2];
179 static struct timeval prev_time;
180 static unsigned int r_rate, w_rate;
181 unsigned long long io_bytes[2];
bb3884d8 182 unsigned long mtime, bw_avg_time;
6043c579 183
263e529f
JA
184 if (temp_stall_ts || terse_output)
185 return;
186
6043c579
JA
187 if (!prev_io_bytes[0] && !prev_io_bytes[1])
188 fill_start_time(&prev_time);
189
263e529f
JA
190 eta_secs = malloc(thread_number * sizeof(int));
191 memset(eta_secs, 0, thread_number * sizeof(int));
192
6043c579 193 io_bytes[0] = io_bytes[1] = 0;
263e529f 194 nr_pending = nr_running = t_rate = m_rate = 0;
bb3884d8 195 bw_avg_time = ULONG_MAX;
34572e28 196 for_each_td(td, i) {
bb3884d8
JA
197 if (td->bw_avg_time < bw_avg_time)
198 bw_avg_time = td->bw_avg_time;
263e529f
JA
199 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING||
200 td->runstate == TD_FSYNCING) {
201 nr_running++;
202 t_rate += td->rate;
203 m_rate += td->ratemin;
204 } else if (td->runstate < TD_RUNNING)
205 nr_pending++;
206
207 if (elapsed >= 3)
208 eta_secs[i] = thread_eta(td, elapsed);
209 else
210 eta_secs[i] = INT_MAX;
211
212 check_str_update(td);
6043c579
JA
213 io_bytes[0] += td->io_bytes[0];
214 io_bytes[1] += td->io_bytes[1];
263e529f
JA
215 }
216
217 if (exitall_on_terminate)
218 eta_sec = INT_MAX;
219 else
220 eta_sec = 0;
221
34572e28 222 for_each_td(td, i) {
263e529f
JA
223 if (exitall_on_terminate) {
224 if (eta_secs[i] < eta_sec)
225 eta_sec = eta_secs[i];
226 } else {
227 if (eta_secs[i] > eta_sec)
228 eta_sec = eta_secs[i];
229 }
230 }
231
eecf272f
JA
232 free(eta_secs);
233
263e529f
JA
234 if (eta_sec != INT_MAX && elapsed) {
235 perc = (double) elapsed / (double) (elapsed + eta_sec);
236 eta_to_str(eta_str, eta_sec);
237 }
238
6043c579 239 mtime = mtime_since_now(&prev_time);
bb3884d8 240 if (mtime > bw_avg_time) {
6043c579
JA
241 r_rate = (io_bytes[0] - prev_io_bytes[0]) / mtime;
242 w_rate = (io_bytes[1] - prev_io_bytes[1]) / mtime;
02bcaa8c 243 fio_gettime(&prev_time, NULL);
ae4020f7
JA
244 if (write_bw_log) {
245 add_agg_sample(r_rate, DDIR_READ);
246 add_agg_sample(w_rate, DDIR_WRITE);
247 }
6043c579
JA
248 memcpy(prev_io_bytes, io_bytes, sizeof(io_bytes));
249 }
250
263e529f
JA
251 if (!nr_running && !nr_pending)
252 return;
253
d56cbab0 254 printf("Threads: %d", nr_running);
263e529f 255 if (m_rate || t_rate)
2cb8d960 256 printf(", CR=%d/%d KiB/s", t_rate, m_rate);
263e529f
JA
257 if (eta_sec != INT_MAX && nr_running) {
258 perc *= 100.0;
d56cbab0 259 printf(": [%s] [%3.1f%% done] [%6u/%6u kb/s] [eta %s]", run_str, perc, r_rate, w_rate, eta_str);
263e529f
JA
260 }
261 printf("\r");
262 fflush(stdout);
263e529f
JA
263}
264
265void print_status_init(int thread_number)
266{
267 run_str[thread_number] = 'P';
268}