+static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
+{
+ enum fio_ddir odir = ddir ^ 1;
+ struct timeval t;
+ long usec;
+
+ if (td->rate_pending_usleep[ddir] <= 0)
+ return ddir;
+
+ /*
+ * We have too much pending sleep in this direction. See if we
+ * should switch.
+ */
+ if (td_rw(td)) {
+ /*
+ * Other direction does not have too much pending, switch
+ */
+ if (td->rate_pending_usleep[odir] < 100000)
+ return odir;
+
+ /*
+ * Both directions have pending sleep. Sleep the minimum time
+ * and deduct from both.
+ */
+ if (td->rate_pending_usleep[ddir] <=
+ td->rate_pending_usleep[odir]) {
+ usec = td->rate_pending_usleep[ddir];
+ } else {
+ usec = td->rate_pending_usleep[odir];
+ ddir = odir;
+ }
+ } else
+ usec = td->rate_pending_usleep[ddir];
+
+ fio_gettime(&t, NULL);
+ usec_sleep(td, usec);
+ usec = utime_since_now(&t);
+
+ td->rate_pending_usleep[ddir] -= usec;
+
+ odir = ddir ^ 1;
+ if (td_rw(td) && __should_check_rate(td, odir))
+ td->rate_pending_usleep[odir] -= usec;
+
+ return ddir;
+}
+