+static int thread_eta(struct thread_data *td, unsigned long elapsed)
+{
+ unsigned long long bytes_total, bytes_done;
+ unsigned int eta_sec = 0;
+
+ bytes_total = td->total_io_size;
+
+ /*
+ * if writing, bytes_total will be twice the size. If mixing,
+ * assume a 50/50 split and thus bytes_total will be 50% larger.
+ */
+ if (td->verify) {
+ if (td_rw(td))
+ bytes_total = bytes_total * 3 / 2;
+ else
+ bytes_total <<= 1;
+ }
+ if (td->zone_size && td->zone_skip)
+ bytes_total /= (td->zone_skip / td->zone_size);
+
+ if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
+ double perc;
+
+ bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
+ perc = (double) bytes_done / (double) bytes_total;
+ if (perc > 1.0)
+ perc = 1.0;
+
+ eta_sec = (elapsed * (1.0 / perc)) - elapsed;
+
+ if (td->timeout && eta_sec > (td->timeout - elapsed))
+ eta_sec = td->timeout - elapsed;
+ } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED) {
+ int t_eta = 0, r_eta = 0;
+
+ /*
+ * We can only guess - assume it'll run the full timeout
+ * if given, otherwise assume it'll run at the specified rate.
+ */
+ if (td->timeout)
+ t_eta = td->timeout + td->start_delay - elapsed;
+ if (td->rate) {
+ r_eta = (bytes_total / 1024) / td->rate;
+ r_eta += td->start_delay - elapsed;
+ }
+
+ if (r_eta && t_eta)
+ eta_sec = min(r_eta, t_eta);
+ else if (r_eta)
+ eta_sec = r_eta;
+ else if (t_eta)
+ eta_sec = t_eta;
+ else
+ eta_sec = INT_MAX;
+ } else {
+ /*
+ * thread is already done
+ */
+ eta_sec = 0;
+ }
+
+ return eta_sec;
+}
+