[PATCH] Don't segfault on missing argument data
[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;
73 static int always_d, always_h;
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
85 if (d || always_d) {
86 always_d = 1;
87 str += sprintf(str, "%02dd:", d);
88 }
89 if (h || always_h) {
90 always_h = 1;
91 str += sprintf(str, "%02dh:", h);
92 }
93
94 str += sprintf(str, "%02dm:", m);
95 str += sprintf(str, "%02ds", s);
96}
97
98/*
99 * Best effort calculation of the estimated pending runtime of a job.
100 */
101static int thread_eta(struct thread_data *td, unsigned long elapsed)
102{
103 unsigned long long bytes_total, bytes_done;
104 unsigned int eta_sec = 0;
105
106 bytes_total = td->total_io_size;
107
74939e38
JA
108 /*
109 * if writing, bytes_total will be twice the size. If mixing,
110 * assume a 50/50 split and thus bytes_total will be 50% larger.
111 */
112 if (td->verify) {
113 if (td_rw(td))
114 bytes_total = bytes_total * 3 / 2;
115 else
116 bytes_total <<= 1;
117 }
118
263e529f
JA
119 if (td->zone_size && td->zone_skip)
120 bytes_total /= (td->zone_skip / td->zone_size);
121
122 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
123 double perc;
124
125 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
126 perc = (double) bytes_done / (double) bytes_total;
127 if (perc > 1.0)
128 perc = 1.0;
129
130 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
131
132 if (td->timeout && eta_sec > (td->timeout - elapsed))
133 eta_sec = td->timeout - elapsed;
134 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
135 || td->runstate == TD_INITIALIZED) {
136 int t_eta = 0, r_eta = 0;
137
138 /*
139 * We can only guess - assume it'll run the full timeout
140 * if given, otherwise assume it'll run at the specified rate.
141 */
142 if (td->timeout)
143 t_eta = td->timeout + td->start_delay - elapsed;
144 if (td->rate) {
145 r_eta = (bytes_total / 1024) / td->rate;
146 r_eta += td->start_delay - elapsed;
147 }
148
149 if (r_eta && t_eta)
150 eta_sec = min(r_eta, t_eta);
151 else if (r_eta)
152 eta_sec = r_eta;
153 else if (t_eta)
154 eta_sec = t_eta;
155 else
156 eta_sec = 0;
157 } else {
158 /*
159 * thread is already done or waiting for fsync
160 */
161 eta_sec = 0;
162 }
163
164 return eta_sec;
165}
166
167/*
168 * Print status of the jobs we know about. This includes rate estimates,
169 * ETA, thread state, etc.
170 */
171void print_thread_status(void)
172{
173 unsigned long elapsed = mtime_since_genesis() / 1000;
174 int i, nr_running, nr_pending, t_rate, m_rate, *eta_secs, eta_sec;
34572e28 175 struct thread_data *td;
263e529f
JA
176 char eta_str[32];
177 double perc = 0.0;
178
6043c579
JA
179 static unsigned long long prev_io_bytes[2];
180 static struct timeval prev_time;
181 static unsigned int r_rate, w_rate;
182 unsigned long long io_bytes[2];
183 unsigned long mtime;
184
263e529f
JA
185 if (temp_stall_ts || terse_output)
186 return;
187
6043c579
JA
188 if (!prev_io_bytes[0] && !prev_io_bytes[1])
189 fill_start_time(&prev_time);
190
263e529f
JA
191 eta_secs = malloc(thread_number * sizeof(int));
192 memset(eta_secs, 0, thread_number * sizeof(int));
193
6043c579 194 io_bytes[0] = io_bytes[1] = 0;
263e529f 195 nr_pending = nr_running = t_rate = m_rate = 0;
34572e28 196 for_each_td(td, i) {
263e529f
JA
197 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING||
198 td->runstate == TD_FSYNCING) {
199 nr_running++;
200 t_rate += td->rate;
201 m_rate += td->ratemin;
202 } else if (td->runstate < TD_RUNNING)
203 nr_pending++;
204
205 if (elapsed >= 3)
206 eta_secs[i] = thread_eta(td, elapsed);
207 else
208 eta_secs[i] = INT_MAX;
209
210 check_str_update(td);
6043c579
JA
211 io_bytes[0] += td->io_bytes[0];
212 io_bytes[1] += td->io_bytes[1];
263e529f
JA
213 }
214
215 if (exitall_on_terminate)
216 eta_sec = INT_MAX;
217 else
218 eta_sec = 0;
219
34572e28 220 for_each_td(td, i) {
263e529f
JA
221 if (exitall_on_terminate) {
222 if (eta_secs[i] < eta_sec)
223 eta_sec = eta_secs[i];
224 } else {
225 if (eta_secs[i] > eta_sec)
226 eta_sec = eta_secs[i];
227 }
228 }
229
eecf272f
JA
230 free(eta_secs);
231
263e529f
JA
232 if (eta_sec != INT_MAX && elapsed) {
233 perc = (double) elapsed / (double) (elapsed + eta_sec);
234 eta_to_str(eta_str, eta_sec);
235 }
236
6043c579
JA
237 mtime = mtime_since_now(&prev_time);
238 if (mtime > 1000) {
239 r_rate = (io_bytes[0] - prev_io_bytes[0]) / mtime;
240 w_rate = (io_bytes[1] - prev_io_bytes[1]) / mtime;
241 gettimeofday(&prev_time, NULL);
242 memcpy(prev_io_bytes, io_bytes, sizeof(io_bytes));
243 }
244
263e529f
JA
245 if (!nr_running && !nr_pending)
246 return;
247
248 printf("Threads running: %d", nr_running);
249 if (m_rate || t_rate)
250 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
251 if (eta_sec != INT_MAX && nr_running) {
252 perc *= 100.0;
6043c579 253 printf(": [%s] [%3.2f%% done] [%6u/%6u kb/s] [eta %s]", run_str, perc, r_rate, w_rate, eta_str);
263e529f
JA
254 }
255 printf("\r");
256 fflush(stdout);
263e529f
JA
257}
258
259void print_status_init(int thread_number)
260{
261 run_str[thread_number] = 'P';
262}