-static unsigned long utime_since(struct timeval *s, struct timeval *e)
-{
- double sec, usec;
-
- sec = e->tv_sec - s->tv_sec;
- usec = e->tv_usec - s->tv_usec;
- if (sec > 0 && usec < 0) {
- sec--;
- usec += 1000000;
- }
-
- sec *= (double) 1000000;
-
- return sec + usec;
-}
-
-static unsigned long utime_since_now(struct timeval *s)
-{
- struct timeval t;
-
- gettimeofday(&t, NULL);
- return utime_since(s, &t);
-}
-
-static unsigned long mtime_since(struct timeval *s, struct timeval *e)
-{
- double sec, usec;
-
- sec = e->tv_sec - s->tv_sec;
- usec = e->tv_usec - s->tv_usec;
- if (sec > 0 && usec < 0) {
- sec--;
- usec += 1000000;
- }
-
- sec *= (double) 1000;
- usec /= (double) 1000;
-
- return sec + usec;
-}
-
-static unsigned long mtime_since_now(struct timeval *s)
-{
- struct timeval t;
-
- gettimeofday(&t, NULL);
- return mtime_since(s, &t);
-}
-
-static inline unsigned long msec_now(struct timeval *s)
-{
- return s->tv_sec * 1000 + s->tv_usec / 1000;
-}
-
-static unsigned long time_since_now(struct timeval *s)
-{
- return mtime_since_now(s) / 1000;
-}
-
-static int random_map_free(struct thread_data *td, unsigned long long block)
-{
- unsigned int idx = RAND_MAP_IDX(td, block);
- unsigned int bit = RAND_MAP_BIT(td, block);
-
- return (td->file_map[idx] & (1UL << bit)) == 0;
-}
-
-static int get_next_free_block(struct thread_data *td, unsigned long long *b)
-{
- int i;
-
- *b = 0;
- i = 0;
- while ((*b) * td->min_bs < td->io_size) {
- if (td->file_map[i] != -1UL) {
- *b += ffz(td->file_map[i]);
- return 0;
- }
-
- *b += BLOCKS_PER_MAP;
- i++;
- }
-
- return 1;
-}
-
-static void mark_random_map(struct thread_data *td, struct io_u *io_u)
-{
- unsigned long block = io_u->offset / td->min_bs;
- unsigned int blocks = 0;
-
- while (blocks < (io_u->buflen / td->min_bs)) {
- unsigned int idx, bit;
-
- if (!random_map_free(td, block))
- break;
-
- idx = RAND_MAP_IDX(td, block);
- bit = RAND_MAP_BIT(td, block);
-
- assert(idx < td->num_maps);
-
- td->file_map[idx] |= (1UL << bit);
- block++;
- blocks++;
- }
-
- if ((blocks * td->min_bs) < io_u->buflen)
- io_u->buflen = blocks * td->min_bs;
-}
-
-static inline void add_stat_sample(struct io_stat *is, unsigned long val)
-{
- if (val > is->max_val)
- is->max_val = val;
- if (val < is->min_val)
- is->min_val = val;
-
- is->val += val;
- is->val_sq += val * val;
- is->samples++;
-}
-
-static void add_log_sample(struct thread_data *td, struct io_log *iolog,
- unsigned long val, int ddir)
-{
- if (iolog->nr_samples == iolog->max_samples) {
- int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
-
- iolog->log = realloc(iolog->log, new_size);
- iolog->max_samples <<= 1;
- }
-
- iolog->log[iolog->nr_samples].val = val;
- iolog->log[iolog->nr_samples].time = mtime_since_now(&td->epoch);
- iolog->log[iolog->nr_samples].ddir = ddir;
- iolog->nr_samples++;
-}
-
-static void add_clat_sample(struct thread_data *td, int ddir,unsigned long msec)
-{
- add_stat_sample(&td->clat_stat[ddir], msec);
-
- if (td->clat_log)
- add_log_sample(td, td->clat_log, msec, ddir);
-}
-
-static void add_slat_sample(struct thread_data *td, int ddir,unsigned long msec)
-{
- add_stat_sample(&td->slat_stat[ddir], msec);
-
- if (td->slat_log)
- add_log_sample(td, td->slat_log, msec, ddir);
-}
-
-static void add_bw_sample(struct thread_data *td, int ddir)
-{
- unsigned long spent = mtime_since_now(&td->stat_sample_time[ddir]);
- unsigned long rate;
-
- if (spent < td->bw_avg_time)
- return;
-
- rate = (td->this_io_bytes[ddir] - td->stat_io_bytes[ddir]) / spent;
- add_stat_sample(&td->bw_stat[ddir], rate);
-
- if (td->bw_log)
- add_log_sample(td, td->bw_log, rate, ddir);
-
- gettimeofday(&td->stat_sample_time[ddir], NULL);
- td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
-}
-
-static int get_next_offset(struct thread_data *td, unsigned long long *offset)
-{
- unsigned long long b, rb;
- long r;
-
- if (!td->sequential) {
- unsigned long max_blocks = td->io_size / td->min_bs;
- int loops = 50;
-
- do {
- lrand48_r(&td->random_state, &r);
- b = ((max_blocks - 1) * r / (RAND_MAX+1.0));
- rb = b + (td->file_offset / td->min_bs);
- loops--;
- } while (!random_map_free(td, rb) && loops);
-
- if (!loops) {
- if (get_next_free_block(td, &b))
- return 1;
- }
- } else
- b = td->last_pos / td->min_bs;
-
- *offset = (b * td->min_bs) + td->file_offset;
- if (*offset > td->real_file_size)
- return 1;
-
- return 0;
-}
-
-static unsigned int get_next_buflen(struct thread_data *td)
-{
- unsigned int buflen;
- long r;
-
- if (td->min_bs == td->max_bs)
- buflen = td->min_bs;
- else {
- lrand48_r(&td->bsrange_state, &r);
- buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
- buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
- }
-
- if (buflen > td->io_size - td->this_io_bytes[td->ddir])
- buflen = td->io_size - td->this_io_bytes[td->ddir];
-
- return buflen;
-}
-