+/*
+ * Cheesy number->string conversion, complete with carry rounding error.
+ */
+static inline char *num2str(unsigned long num, int maxlen, int base, int pow2)
+{
+ char postfix[] = { ' ', 'K', 'M', 'G', 'P', 'E' };
+ unsigned int thousand;
+ char *buf;
+ int i;
+
+ if (pow2)
+ thousand = 1024;
+ else
+ thousand = 1000;
+
+ buf = malloc(128);
+
+ for (i = 0; base > 1; i++)
+ base /= thousand;
+
+ do {
+ int len, carry = 0;
+
+ len = sprintf(buf, "%'lu", num);
+ if (len <= maxlen) {
+ if (i >= 1) {
+ buf[len] = postfix[i];
+ buf[len + 1] = '\0';
+ }
+ return buf;
+ }
+
+ if ((num % thousand) >= (thousand / 2))
+ carry = 1;
+
+ num /= thousand;
+ num += carry;
+ i++;
+ } while (i <= 5);
+
+ return buf;
+}
+
+static inline int __should_check_rate(struct thread_data *td,
+ enum fio_ddir ddir)
+{
+ struct thread_options *o = &td->o;
+
+ /*
+ * If some rate setting was given, we need to check it
+ */
+ if (o->rate[ddir] || o->ratemin[ddir] || o->rate_iops[ddir] ||
+ o->rate_iops_min[ddir])
+ return 1;
+
+ return 0;
+}
+
+static inline int should_check_rate(struct thread_data *td,
+ unsigned long *bytes_done)
+{
+ int ret = 0;
+
+ if (bytes_done[0])
+ ret |= __should_check_rate(td, 0);
+ if (bytes_done[1])
+ ret |= __should_check_rate(td, 1);
+
+ return ret;
+}
+
+static inline int is_power_of_2(unsigned int val)
+{
+ return (val != 0 && ((val & (val - 1)) == 0));
+}
+
+/*
+ * We currently only need to do locking if we have verifier threads
+ * accessing our internal structures too
+ */
+static inline void td_io_u_lock(struct thread_data *td)
+{
+ if (td->o.verify_async)
+ pthread_mutex_lock(&td->io_u_lock);
+}
+
+static inline void td_io_u_unlock(struct thread_data *td)
+{
+ if (td->o.verify_async)
+ pthread_mutex_unlock(&td->io_u_lock);
+}
+
+static inline void td_io_u_free_notify(struct thread_data *td)
+{
+ if (td->o.verify_async)
+ pthread_cond_signal(&td->free_cond);
+}
+