Move file service type defines to file.h
[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         /*
114          * if writing, bytes_total will be twice the size. If mixing,
115          * assume a 50/50 split and thus bytes_total will be 50% larger.
116          */
117         if (td->o.do_verify && td->o.verify && td_write(td)) {
118                 if (td_rw(td))
119                         bytes_total = bytes_total * 3 / 2;
120                 else
121                         bytes_total <<= 1;
122         }
123
124         if (td->o.zone_size && td->o.zone_skip)
125                 bytes_total /= (td->o.zone_skip / td->o.zone_size);
126
127         if (td->o.fill_device && td->o.size  == -1ULL)
128                 return 0;
129
130         if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
131                 double perc, perc_t;
132
133                 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
134                 perc = (double) bytes_done / (double) bytes_total;
135                 if (perc > 1.0)
136                         perc = 1.0;
137
138                 if (td->o.time_based) {
139                         perc_t = (double) elapsed / (double) td->o.timeout;
140                         if (perc_t < perc)
141                                 perc = perc_t;
142                 }
143
144                 eta_sec = (unsigned long) (elapsed * (1.0 / perc)) - elapsed;
145
146                 if (td->o.timeout &&
147                     eta_sec > (td->o.timeout + done_secs - elapsed))
148                         eta_sec = td->o.timeout + done_secs - elapsed;
149         } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
150                         || td->runstate == TD_INITIALIZED
151                         || td->runstate == TD_RAMP
152                         || td->runstate == TD_PRE_READING) {
153                 int t_eta = 0, r_eta = 0;
154
155                 /*
156                  * We can only guess - assume it'll run the full timeout
157                  * if given, otherwise assume it'll run at the specified rate.
158                  */
159                 if (td->o.timeout) {
160                         t_eta = td->o.timeout + td->o.start_delay;
161
162                         if (in_ramp_time(td)) {
163                                 unsigned long ramp_left;
164
165                                 ramp_left = mtime_since_now(&td->start);
166                                 ramp_left = (ramp_left + 999) / 1000;
167                                 if (ramp_left <= t_eta)
168                                         t_eta -= ramp_left;
169                         }
170                 }
171                 if (td->o.rate) {
172                         r_eta = (bytes_total / 1024) / td->o.rate;
173                         r_eta += td->o.start_delay;
174                 }
175
176                 if (r_eta && t_eta)
177                         eta_sec = min(r_eta, t_eta);
178                 else if (r_eta)
179                         eta_sec = r_eta;
180                 else if (t_eta)
181                         eta_sec = t_eta;
182                 else
183                         eta_sec = 0;
184         } else {
185                 /*
186                  * thread is already done or waiting for fsync
187                  */
188                 eta_sec = 0;
189         }
190
191         return eta_sec;
192 }
193
194 static void calc_rate(unsigned long mtime, unsigned long long *io_bytes,
195                       unsigned long long *prev_io_bytes, unsigned int *rate)
196 {
197         rate[0] = (io_bytes[0] - prev_io_bytes[0]) / mtime;
198         rate[1] = (io_bytes[1] - prev_io_bytes[1]) / mtime;
199         prev_io_bytes[0] = io_bytes[0];
200         prev_io_bytes[1] = io_bytes[1];
201 }
202
203 /*
204  * Print status of the jobs we know about. This includes rate estimates,
205  * ETA, thread state, etc.
206  */
207 void print_thread_status(void)
208 {
209         unsigned long elapsed = (mtime_since_genesis() + 999) / 1000;
210         int i, nr_ramp, nr_running, nr_pending, t_rate, m_rate;
211         int t_iops, m_iops, files_open;
212         struct thread_data *td;
213         char eta_str[128];
214         double perc = 0.0;
215         unsigned long long io_bytes[2];
216         unsigned long rate_time, disp_time, bw_avg_time, *eta_secs, eta_sec;
217         struct timeval now;
218
219         static unsigned long long rate_io_bytes[2];
220         static unsigned long long disp_io_bytes[2];
221         static struct timeval rate_prev_time, disp_prev_time;
222         static unsigned int rate[2];
223         static int linelen_last;
224         static int eta_good;
225
226         if (temp_stall_ts || terse_output || eta_print == FIO_ETA_NEVER)
227                 return;
228
229         if (!isatty(STDOUT_FILENO) && (eta_print != FIO_ETA_ALWAYS))
230                 return;
231
232         if (!rate_io_bytes[0] && !rate_io_bytes[1])
233                 fill_start_time(&rate_prev_time);
234         if (!disp_io_bytes[0] && !disp_io_bytes[1])
235                 fill_start_time(&disp_prev_time);
236
237         eta_secs = malloc(thread_number * sizeof(unsigned long));
238         memset(eta_secs, 0, thread_number * sizeof(unsigned long));
239
240         io_bytes[0] = io_bytes[1] = 0;
241         nr_pending = nr_running = t_rate = m_rate = t_iops = m_iops = 0;
242         nr_ramp = 0;
243         bw_avg_time = ULONG_MAX;
244         files_open = 0;
245         for_each_td(td, i) {
246                 if (td->o.bw_avg_time < bw_avg_time)
247                         bw_avg_time = td->o.bw_avg_time;
248                 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING
249                     || td->runstate == TD_FSYNCING
250                     || td->runstate == TD_PRE_READING) {
251                         nr_running++;
252                         t_rate += td->o.rate;
253                         m_rate += td->o.ratemin;
254                         t_iops += td->o.rate_iops;
255                         m_iops += td->o.rate_iops_min;
256                         files_open += td->nr_open_files;
257                 } else if (td->runstate == TD_RAMP) {
258                         nr_running++;
259                         nr_ramp++;
260                 } else if (td->runstate < TD_RUNNING)
261                         nr_pending++;
262
263                 if (elapsed >= 3)
264                         eta_secs[i] = thread_eta(td);
265                 else
266                         eta_secs[i] = INT_MAX;
267
268                 check_str_update(td);
269
270                 if (td->runstate > TD_RAMP) {
271                         io_bytes[0] += td->io_bytes[0];
272                         io_bytes[1] += td->io_bytes[1];
273                 }
274         }
275
276         if (exitall_on_terminate)
277                 eta_sec = INT_MAX;
278         else
279                 eta_sec = 0;
280
281         for_each_td(td, i) {
282                 if (exitall_on_terminate) {
283                         if (eta_secs[i] < eta_sec)
284                                 eta_sec = eta_secs[i];
285                 } else {
286                         if (eta_secs[i] > eta_sec)
287                                 eta_sec = eta_secs[i];
288                 }
289         }
290
291         free(eta_secs);
292
293         if (eta_sec != INT_MAX && elapsed) {
294                 perc = (double) elapsed / (double) (elapsed + eta_sec);
295                 eta_to_str(eta_str, eta_sec);
296         }
297
298         fio_gettime(&now, NULL);
299         rate_time = mtime_since(&rate_prev_time, &now);
300
301         if (write_bw_log && rate_time > bw_avg_time && !in_ramp_time(td)) {
302                 calc_rate(rate_time, io_bytes, rate_io_bytes, rate);
303                 memcpy(&rate_prev_time, &now, sizeof(now));
304                 add_agg_sample(rate[DDIR_READ], DDIR_READ, 0);
305                 add_agg_sample(rate[DDIR_WRITE], DDIR_WRITE, 0);
306         }
307
308         disp_time = mtime_since(&disp_prev_time, &now);
309         if (disp_time < 1000)
310                 return;
311
312         calc_rate(disp_time, io_bytes, disp_io_bytes, rate);
313         memcpy(&disp_prev_time, &now, sizeof(now));
314
315         if (!nr_running && !nr_pending)
316                 return;
317
318         printf("Jobs: %d (f=%d)", nr_running, files_open);
319         if (m_rate || t_rate)
320                 printf(", CR=%d/%d KiB/s", t_rate, m_rate);
321         else if (m_iops || t_iops)
322                 printf(", CR=%d/%d IOPS", t_iops, m_iops);
323         if (eta_sec != INT_MAX && nr_running) {
324                 char perc_str[32];
325                 int ll;
326
327                 if ((!eta_sec && !eta_good) || nr_ramp == nr_running)
328                         strcpy(perc_str, "-.-% done");
329                 else {
330                         eta_good = 1;
331                         perc *= 100.0;
332                         sprintf(perc_str, "%3.1f%% done", perc);
333                 }
334
335                 ll = printf(": [%s] [%s] [%6u/%6u kb/s] [eta %s]",
336                                  run_str, perc_str, rate[0], rate[1], eta_str);
337                 if (ll >= 0 && ll < linelen_last)
338                         printf("%*s", linelen_last - ll, "");
339                 linelen_last = ll;
340         }
341         printf("\r");
342         fflush(stdout);
343 }
344
345 void print_status_init(int thread_number)
346 {
347         run_str[thread_number] = 'P';
348 }