+static void init_thinktime(struct thread_data *td)
+{
+ if (td->o.thinktime_blocks_type == THINKTIME_BLOCKS_TYPE_COMPLETE)
+ td->thinktime_blocks_counter = td->io_blocks;
+ else
+ td->thinktime_blocks_counter = td->io_issues;
+ td->last_thinktime = td->epoch;
+ td->last_thinktime_blocks = 0;
+}
+
+static void handle_thinktime(struct thread_data *td, enum fio_ddir ddir,
+ struct timespec *time)
+{
+ unsigned long long b;
+ uint64_t total;
+ int left;
+ struct timespec now;
+ bool stall = false;
+
+ if (td->o.thinktime_iotime) {
+ fio_gettime(&now, NULL);
+ if (utime_since(&td->last_thinktime, &now)
+ >= td->o.thinktime_iotime + td->o.thinktime) {
+ stall = true;
+ } else if (!fio_option_is_set(&td->o, thinktime_blocks)) {
+ /*
+ * When thinktime_iotime is set and thinktime_blocks is
+ * not set, skip the thinktime_blocks check, since
+ * thinktime_blocks default value 1 does not work
+ * together with thinktime_iotime.
+ */
+ return;
+ }
+
+ }
+
+ b = ddir_rw_sum(td->thinktime_blocks_counter);
+ if (b >= td->last_thinktime_blocks + td->o.thinktime_blocks)
+ stall = true;
+
+ if (!stall)
+ return;
+
+ io_u_quiesce(td);
+
+ total = 0;
+ if (td->o.thinktime_spin)
+ total = usec_spin(td->o.thinktime_spin);
+
+ left = td->o.thinktime - total;
+ if (left)
+ total += usec_sleep(td, left);
+
+ /*
+ * If we're ignoring thinktime for the rate, add the number of bytes
+ * we would have done while sleeping, minus one block to ensure we
+ * start issuing immediately after the sleep.
+ */
+ if (total && td->rate_bps[ddir] && td->o.rate_ign_think) {
+ uint64_t missed = (td->rate_bps[ddir] * total) / 1000000ULL;
+ uint64_t bs = td->o.min_bs[ddir];
+ uint64_t usperop = bs * 1000000ULL / td->rate_bps[ddir];
+ uint64_t over;
+
+ if (usperop <= total)
+ over = bs;
+ else
+ over = (usperop - total) / usperop * -bs;
+
+ td->rate_io_issue_bytes[ddir] += (missed - over);
+ /* adjust for rate_process=poisson */
+ td->last_usec[ddir] += total;
+ }
+
+ if (time && should_check_rate(td))
+ fio_gettime(time, NULL);
+
+ td->last_thinktime_blocks = b;
+ if (td->o.thinktime_iotime)
+ td->last_thinktime = now;
+}
+