blob: 279ee48492302ebd5a1080e5422cc48ecd6b7a59 [file] [log] [blame]
Jens Axboe3c39a372006-06-06 20:56:12 +02001#include <time.h>
2#include <sys/time.h>
3
4#include "fio.h"
5
Jens Axboe263e5292006-10-18 15:37:01 +02006static struct timeval genesis;
Jens Axboefd841462007-01-13 12:20:30 +01007static unsigned long ns_granularity;
Jens Axboe263e5292006-10-18 15:37:01 +02008
Elliott Hugheseda3a602017-05-19 18:53:02 -07009void timeval_add_msec(struct timeval *tv, unsigned int msec)
10{
11 unsigned long adj_usec = 1000 * msec;
12
13 tv->tv_usec += adj_usec;
14 if (adj_usec >= 1000000) {
15 unsigned long adj_sec = adj_usec / 1000000;
16
17 tv->tv_usec -= adj_sec * 1000000;
18 tv->tv_sec += adj_sec;
19 }
20 if (tv->tv_usec >= 1000000){
21 tv->tv_usec -= 1000000;
22 tv->tv_sec++;
23 }
24}
25
Jens Axboe3c39a372006-06-06 20:56:12 +020026/*
27 * busy looping version for the last few usec
28 */
Jens Axboe7e63b3d2015-01-15 10:39:12 -070029uint64_t usec_spin(unsigned int usec)
Jens Axboe3c39a372006-06-06 20:56:12 +020030{
31 struct timeval start;
Jens Axboe7e63b3d2015-01-15 10:39:12 -070032 uint64_t t;
Jens Axboe3c39a372006-06-06 20:56:12 +020033
Jens Axboe02bcaa82006-11-24 10:42:00 +010034 fio_gettime(&start, NULL);
Jens Axboe7e63b3d2015-01-15 10:39:12 -070035 while ((t = utime_since_now(&start)) < usec)
Jens Axboe3c39a372006-06-06 20:56:12 +020036 nop;
Jens Axboe7e63b3d2015-01-15 10:39:12 -070037
38 return t;
Jens Axboe3c39a372006-06-06 20:56:12 +020039}
40
Jens Axboe7e63b3d2015-01-15 10:39:12 -070041uint64_t usec_sleep(struct thread_data *td, unsigned long usec)
Jens Axboe3c39a372006-06-06 20:56:12 +020042{
Jens Axboefd841462007-01-13 12:20:30 +010043 struct timespec req;
44 struct timeval tv;
Jens Axboe7e63b3d2015-01-15 10:39:12 -070045 uint64_t t = 0;
Jens Axboe3c39a372006-06-06 20:56:12 +020046
47 do {
Jens Axboefd841462007-01-13 12:20:30 +010048 unsigned long ts = usec;
49
50 if (usec < ns_granularity) {
Jens Axboe7e63b3d2015-01-15 10:39:12 -070051 t += usec_spin(usec);
Jens Axboe3c39a372006-06-06 20:56:12 +020052 break;
53 }
54
Jens Axboefd841462007-01-13 12:20:30 +010055 ts = usec - ns_granularity;
56
57 if (ts >= 1000000) {
58 req.tv_sec = ts / 1000000;
59 ts -= 1000000 * req.tv_sec;
60 } else
61 req.tv_sec = 0;
62
63 req.tv_nsec = ts * 1000;
64 fio_gettime(&tv, NULL);
65
66 if (nanosleep(&req, NULL) < 0)
Jens Axboe3c39a372006-06-06 20:56:12 +020067 break;
68
Jens Axboefd841462007-01-13 12:20:30 +010069 ts = utime_since_now(&tv);
Jens Axboe7e63b3d2015-01-15 10:39:12 -070070 t += ts;
Jens Axboefd841462007-01-13 12:20:30 +010071 if (ts >= usec)
Jens Axboe3c39a372006-06-06 20:56:12 +020072 break;
73
Jens Axboefd841462007-01-13 12:20:30 +010074 usec -= ts;
Jens Axboe3c39a372006-06-06 20:56:12 +020075 } while (!td->terminate);
Jens Axboe7e63b3d2015-01-15 10:39:12 -070076
77 return t;
Jens Axboe3c39a372006-06-06 20:56:12 +020078}
79
Jens Axboee0698992014-11-07 10:36:33 -070080uint64_t time_since_genesis(void)
81{
82 return time_since_now(&genesis);
83}
84
Jens Axboeaa60bc52013-01-04 13:24:52 +010085uint64_t mtime_since_genesis(void)
Jens Axboe263e5292006-10-18 15:37:01 +020086{
87 return mtime_since_now(&genesis);
88}
89
Jens Axboe0de5b262014-02-21 15:26:01 -080090uint64_t utime_since_genesis(void)
91{
92 return utime_since_now(&genesis);
93}
94
Elliott Hugheseda3a602017-05-19 18:53:02 -070095bool in_ramp_time(struct thread_data *td)
Jens Axboeb29ee5b2008-09-11 10:17:26 +020096{
97 return td->o.ramp_time && !td->ramp_time_over;
98}
99
Elliott Hugheseda3a602017-05-19 18:53:02 -0700100static void parent_update_ramp(struct thread_data *td)
101{
102 struct thread_data *parent = td->parent;
103
104 if (!parent || parent->ramp_time_over)
105 return;
106
107 reset_all_stats(parent);
108 parent->ramp_time_over = 1;
109 td_set_runstate(parent, TD_RAMP);
110}
111
112bool ramp_time_over(struct thread_data *td)
Jens Axboe721938a2008-09-10 09:46:16 +0200113{
114 struct timeval tv;
115
116 if (!td->o.ramp_time || td->ramp_time_over)
Elliott Hugheseda3a602017-05-19 18:53:02 -0700117 return true;
Jens Axboe721938a2008-09-10 09:46:16 +0200118
119 fio_gettime(&tv, NULL);
Jens Axboe0de5b262014-02-21 15:26:01 -0800120 if (utime_since(&td->epoch, &tv) >= td->o.ramp_time) {
Jens Axboe721938a2008-09-10 09:46:16 +0200121 td->ramp_time_over = 1;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200122 reset_all_stats(td);
123 td_set_runstate(td, TD_RAMP);
Elliott Hugheseda3a602017-05-19 18:53:02 -0700124 parent_update_ramp(td);
125 return true;
Jens Axboe721938a2008-09-10 09:46:16 +0200126 }
127
Elliott Hugheseda3a602017-05-19 18:53:02 -0700128 return false;
Jens Axboe721938a2008-09-10 09:46:16 +0200129}
130
Bruce Cran03e20d62011-01-02 20:14:54 +0100131void fio_time_init(void)
Jens Axboe263e5292006-10-18 15:37:01 +0200132{
Jens Axboefd841462007-01-13 12:20:30 +0100133 int i;
134
Jens Axboec223da82010-03-24 13:23:53 +0100135 fio_clock_init();
136
Jens Axboefd841462007-01-13 12:20:30 +0100137 /*
138 * Check the granularity of the nanosleep function
139 */
140 for (i = 0; i < 10; i++) {
141 struct timeval tv;
142 struct timespec ts;
143 unsigned long elapsed;
144
145 fio_gettime(&tv, NULL);
146 ts.tv_sec = 0;
147 ts.tv_nsec = 1000;
148
149 nanosleep(&ts, NULL);
150 elapsed = utime_since_now(&tv);
151
152 if (elapsed > ns_granularity)
153 ns_granularity = elapsed;
154 }
Jens Axboe263e5292006-10-18 15:37:01 +0200155}
Jens Axboe6043c572006-11-03 11:37:47 +0100156
Jens Axboea2f77c92007-02-22 11:53:00 +0100157void set_genesis_time(void)
158{
159 fio_gettime(&genesis, NULL);
160}
161
Elliott Hugheseda3a602017-05-19 18:53:02 -0700162void set_epoch_time(struct thread_data *td, int log_unix_epoch)
163{
164 fio_gettime(&td->epoch, NULL);
165 if (log_unix_epoch) {
166 struct timeval tv;
167 gettimeofday(&tv, NULL);
168 td->unix_epoch = (unsigned long long)(tv.tv_sec) * 1000 +
169 (unsigned long long)(tv.tv_usec) / 1000;
170 }
171}
172
Jens Axboe6043c572006-11-03 11:37:47 +0100173void fill_start_time(struct timeval *t)
174{
175 memcpy(t, &genesis, sizeof(genesis));
176}