Update OS support line
[fio.git] / eta.c
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
10 static char run_str[MAX_JOBS + 1];
11
12 /*
13  * Sets the status of the 'td' in the printed status map.
14  */
15 static 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_RAMP:
27                 c = '/';
28                 break;
29         case TD_RUNNING:
30                 if (td_rw(td)) {
31                         if (td_random(td))
32                                 c = 'm';
33                         else
34                                 c = 'M';
35                 } else if (td_read(td)) {
36                         if (td_random(td))
37                                 c = 'r';
38                         else
39                                 c = 'R';
40                 } else {
41                         if (td_random(td))
42                                 c = 'w';
43                         else
44                                 c = 'W';
45                 }
46                 break;
47         case TD_PRE_READING:
48                 c = 'p';
49                 break;
50         case TD_VERIFYING:
51                 c = 'V';
52                 break;
53         case TD_FSYNCING:
54                 c = 'F';
55                 break;
56         case TD_CREATED:
57                 c = 'C';
58                 break;
59         case TD_INITIALIZED:
60                 c = 'I';
61                 break;
62         case TD_NOT_CREATED:
63                 c = 'P';
64                 break;
65         default:
66                 log_err("state %d\n", td->runstate);
67         }
68
69         run_str[td->thread_number - 1] = c;
70 }
71
72 /*
73  * Convert seconds to a printable string.
74  */
75 static void eta_to_str(char *str, unsigned long eta_sec)
76 {
77         unsigned int d, h, m, s;
78         int disp_hour = 0;
79
80         s = eta_sec % 60;
81         eta_sec /= 60;
82         m = eta_sec % 60;
83         eta_sec /= 60;
84         h = eta_sec % 24;
85         eta_sec /= 24;
86         d = eta_sec;
87
88         if (d) {
89                 disp_hour = 1;
90                 str += sprintf(str, "%02ud:", d);
91         }
92
93         if (h || disp_hour)
94                 str += sprintf(str, "%02uh:", h);
95
96         str += sprintf(str, "%02um:", m);
97         str += sprintf(str, "%02us", s);
98 }
99
100 /*
101  * Best effort calculation of the estimated pending runtime of a job.
102  */
103 static int thread_eta(struct thread_data *td)
104 {
105         unsigned long long bytes_total, bytes_done;
106         unsigned long eta_sec = 0;
107         unsigned long elapsed;
108
109         elapsed = (mtime_since_now(&td->epoch) + 999) / 1000;
110
111         bytes_total = td->total_io_size;
112
113         if (td->o.fill_device && td->o.size  == -1ULL) {
114                 if (!td->fill_device_size || td->fill_device_size == -1ULL)
115                         return 0;
116
117                 bytes_total = td->fill_device_size;
118         }
119
120         /*
121          * if writing, bytes_total will be twice the size. If mixing,
122          * assume a 50/50 split and thus bytes_total will be 50% larger.
123          */
124         if (td->o.do_verify && td->o.verify && td_write(td)) {
125                 if (td_rw(td))
126                         bytes_total = bytes_total * 3 / 2;
127                 else
128                         bytes_total <<= 1;
129         }
130
131         if (td->o.zone_size && td->o.zone_skip)
132                 bytes_total /= (td->o.zone_skip / td->o.zone_size);
133
134         if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
135                 double perc, perc_t;
136
137                 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
138                 perc = (double) bytes_done / (double) bytes_total;
139                 if (perc > 1.0)
140                         perc = 1.0;
141
142                 if (td->o.time_based) {
143                         perc_t = (double) elapsed / (double) td->o.timeout;
144                         if (perc_t < perc)
145                                 perc = perc_t;
146                 }
147
148                 eta_sec = (unsigned long) (elapsed * (1.0 / perc)) - elapsed;
149
150                 if (td->o.timeout &&
151                     eta_sec > (td->o.timeout + done_secs - elapsed))
152                         eta_sec = td->o.timeout + done_secs - elapsed;
153         } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
154                         || td->runstate == TD_INITIALIZED
155                         || td->runstate == TD_RAMP
156                         || td->runstate == TD_PRE_READING) {
157                 int t_eta = 0, r_eta = 0;
158
159                 /*
160                  * We can only guess - assume it'll run the full timeout
161                  * if given, otherwise assume it'll run at the specified rate.
162                  */
163                 if (td->o.timeout) {
164                         t_eta = td->o.timeout + td->o.start_delay;
165
166                         if (in_ramp_time(td)) {
167                                 unsigned long ramp_left;
168
169                                 ramp_left = mtime_since_now(&td->start);
170                                 ramp_left = (ramp_left + 999) / 1000;
171                                 if (ramp_left <= t_eta)
172                                         t_eta -= ramp_left;
173                         }
174                 }
175                 if (td->o.rate[0] || td->o.rate[1]) {
176                         r_eta = (bytes_total / 1024) /
177                                         (td->o.rate[0] + td->o.rate[1]);
178                         r_eta += td->o.start_delay;
179                 }
180
181                 if (r_eta && t_eta)
182                         eta_sec = min(r_eta, t_eta);
183                 else if (r_eta)
184                         eta_sec = r_eta;
185                 else if (t_eta)
186                         eta_sec = t_eta;
187                 else
188                         eta_sec = 0;
189         } else {
190                 /*
191                  * thread is already done or waiting for fsync
192                  */
193                 eta_sec = 0;
194         }
195
196         return eta_sec;
197 }
198
199 static void calc_rate(unsigned long mtime, unsigned long long *io_bytes,
200                       unsigned long long *prev_io_bytes, unsigned int *rate)
201 {
202         rate[0] = (io_bytes[0] - prev_io_bytes[0]) / mtime;
203         rate[1] = (io_bytes[1] - prev_io_bytes[1]) / mtime;
204         prev_io_bytes[0] = io_bytes[0];
205         prev_io_bytes[1] = io_bytes[1];
206 }
207
208 static void calc_iops(unsigned long mtime, unsigned long long *io_iops,
209                       unsigned long long *prev_io_iops, unsigned int *iops)
210 {
211         iops[0] = ((io_iops[0] - prev_io_iops[0]) * 1000) / mtime;
212         iops[1] = ((io_iops[1] - prev_io_iops[1]) * 1000) / mtime;
213         prev_io_iops[0] = io_iops[0];
214         prev_io_iops[1] = io_iops[1];
215 }
216
217 /*
218  * Print status of the jobs we know about. This includes rate estimates,
219  * ETA, thread state, etc.
220  */
221 void print_thread_status(void)
222 {
223         unsigned long elapsed = (mtime_since_genesis() + 999) / 1000;
224         int i, nr_ramp, nr_running, nr_pending, t_rate, m_rate;
225         int t_iops, m_iops, files_open;
226         struct thread_data *td;
227         char eta_str[128];
228         double perc = 0.0;
229         unsigned long long io_bytes[2], io_iops[2];
230         unsigned long rate_time, disp_time, bw_avg_time, *eta_secs, eta_sec;
231         struct timeval now;
232
233         static unsigned long long rate_io_bytes[2];
234         static unsigned long long disp_io_bytes[2];
235         static unsigned long long disp_io_iops[2];
236         static struct timeval rate_prev_time, disp_prev_time;
237         static unsigned int rate[2], iops[2];
238         static int linelen_last;
239         static int eta_good;
240         int i2p = 0;
241
242         if (temp_stall_ts || terse_output || eta_print == FIO_ETA_NEVER)
243                 return;
244
245         if (!isatty(STDOUT_FILENO) && (eta_print != FIO_ETA_ALWAYS))
246                 return;
247
248         if (!rate_io_bytes[0] && !rate_io_bytes[1])
249                 fill_start_time(&rate_prev_time);
250         if (!disp_io_bytes[0] && !disp_io_bytes[1])
251                 fill_start_time(&disp_prev_time);
252
253         eta_secs = malloc(thread_number * sizeof(unsigned long));
254         memset(eta_secs, 0, thread_number * sizeof(unsigned long));
255
256         io_bytes[0] = io_bytes[1] = 0;
257         io_iops[0] = io_iops[1] = 0;
258         nr_pending = nr_running = t_rate = m_rate = t_iops = m_iops = 0;
259         nr_ramp = 0;
260         bw_avg_time = ULONG_MAX;
261         files_open = 0;
262         for_each_td(td, i) {
263                 if (td->o.bw_avg_time < bw_avg_time)
264                         bw_avg_time = td->o.bw_avg_time;
265                 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING
266                     || td->runstate == TD_FSYNCING
267                     || td->runstate == TD_PRE_READING) {
268                         nr_running++;
269                         t_rate += td->o.rate[0] + td->o.rate[1];
270                         m_rate += td->o.ratemin[0] + td->o.ratemin[1];
271                         t_iops += td->o.rate_iops[0] + td->o.rate_iops[1];
272                         m_iops += td->o.rate_iops_min[0] +
273                                         td->o.rate_iops_min[1];
274                         files_open += td->nr_open_files;
275                 } else if (td->runstate == TD_RAMP) {
276                         nr_running++;
277                         nr_ramp++;
278                 } else if (td->runstate < TD_RUNNING)
279                         nr_pending++;
280
281                 if (elapsed >= 3)
282                         eta_secs[i] = thread_eta(td);
283                 else
284                         eta_secs[i] = INT_MAX;
285
286                 check_str_update(td);
287
288                 if (td->runstate > TD_RAMP) {
289                         io_bytes[0] += td->io_bytes[0];
290                         io_bytes[1] += td->io_bytes[1];
291                         io_iops[0] += td->io_blocks[0];
292                         io_iops[1] += td->io_blocks[1];
293                 }
294         }
295
296         if (exitall_on_terminate)
297                 eta_sec = INT_MAX;
298         else
299                 eta_sec = 0;
300
301         for_each_td(td, i) {
302                 if (!i2p && is_power_of_2(td->o.kb_base))
303                         i2p = 1;
304                 if (exitall_on_terminate) {
305                         if (eta_secs[i] < eta_sec)
306                                 eta_sec = eta_secs[i];
307                 } else {
308                         if (eta_secs[i] > eta_sec)
309                                 eta_sec = eta_secs[i];
310                 }
311         }
312
313         free(eta_secs);
314
315         if (eta_sec != INT_MAX && elapsed) {
316                 perc = (double) elapsed / (double) (elapsed + eta_sec);
317                 eta_to_str(eta_str, eta_sec);
318         }
319
320         fio_gettime(&now, NULL);
321         rate_time = mtime_since(&rate_prev_time, &now);
322
323         if (write_bw_log && rate_time > bw_avg_time && !in_ramp_time(td)) {
324                 calc_rate(rate_time, io_bytes, rate_io_bytes, rate);
325                 memcpy(&rate_prev_time, &now, sizeof(now));
326                 add_agg_sample(rate[DDIR_READ], DDIR_READ, 0);
327                 add_agg_sample(rate[DDIR_WRITE], DDIR_WRITE, 0);
328         }
329
330         disp_time = mtime_since(&disp_prev_time, &now);
331         if (disp_time < 1000)
332                 return;
333
334         calc_rate(disp_time, io_bytes, disp_io_bytes, rate);
335         calc_iops(disp_time, io_iops, disp_io_iops, iops);
336
337         memcpy(&disp_prev_time, &now, sizeof(now));
338
339         if (!nr_running && !nr_pending)
340                 return;
341
342         printf("Jobs: %d (f=%d)", nr_running, files_open);
343         if (m_rate || t_rate) {
344                 char *tr, *mr;
345
346                 mr = num2str(m_rate, 4, 0, i2p);
347                 tr = num2str(t_rate, 4, 0, i2p);
348                 printf(", CR=%s/%s KB/s", tr, mr);
349                 free(tr);
350                 free(mr);
351         } else if (m_iops || t_iops)
352                 printf(", CR=%d/%d IOPS", t_iops, m_iops);
353         if (eta_sec != INT_MAX && nr_running) {
354                 char perc_str[32];
355                 char *iops_str[2];
356                 char *rate_str[2];
357                 int l;
358
359                 if ((!eta_sec && !eta_good) || nr_ramp == nr_running)
360                         strcpy(perc_str, "-.-% done");
361                 else {
362                         eta_good = 1;
363                         perc *= 100.0;
364                         sprintf(perc_str, "%3.1f%% done", perc);
365                 }
366
367                 rate_str[0] = num2str(rate[0], 5, 10, i2p);
368                 rate_str[1] = num2str(rate[1], 5, 10, i2p);
369
370                 iops_str[0] = num2str(iops[0], 4, 1, 0);
371                 iops_str[1] = num2str(iops[1], 4, 1, 0);
372
373                 l = printf(": [%s] [%s] [%s/%s /s] [%s/%s iops] [eta %s]",
374                                  run_str, perc_str, rate_str[0], rate_str[1],
375                                  iops_str[0], iops_str[1], eta_str);
376                 if (l >= 0 && l < linelen_last)
377                         printf("%*s", linelen_last - l, "");
378                 linelen_last = l;
379
380                 free(rate_str[0]);
381                 free(rate_str[1]);
382                 free(iops_str[0]);
383                 free(iops_str[1]);
384         }
385         printf("\r");
386         fflush(stdout);
387 }
388
389 void print_status_init(int thread_number)
390 {
391         run_str[thread_number] = 'P';
392 }