filesetup: create zbd_info before jumping to done label
[fio.git] / time.c
CommitLineData
3c39a379
JA
1#include <time.h>
2#include <sys/time.h>
3
4#include "fio.h"
5
8b6a404c 6static struct timespec genesis;
fd841467 7static unsigned long ns_granularity;
263e529f 8
8b6a404c 9void timespec_add_msec(struct timespec *ts, unsigned int msec)
a47591e4 10{
90e2cd1e 11 uint64_t adj_nsec = 1000000ULL * msec;
d19251c4 12
8b6a404c
VF
13 ts->tv_nsec += adj_nsec;
14 if (adj_nsec >= 1000000000) {
90e2cd1e 15 uint64_t adj_sec = adj_nsec / 1000000000;
d19251c4 16
90e2cd1e 17 ts->tv_nsec -= adj_sec * 1000000000;
8b6a404c 18 ts->tv_sec += adj_sec;
d19251c4 19 }
90e2cd1e
SW
20 if (ts->tv_nsec >= 1000000000){
21 ts->tv_nsec -= 1000000000;
8b6a404c 22 ts->tv_sec++;
a47591e4
JA
23 }
24}
25
3c39a379
JA
26/*
27 * busy looping version for the last few usec
28 */
9cc80b6d 29uint64_t usec_spin(unsigned int usec)
3c39a379 30{
8b6a404c 31 struct timespec start;
9cc80b6d 32 uint64_t t;
3c39a379 33
02bcaa8c 34 fio_gettime(&start, NULL);
9cc80b6d 35 while ((t = utime_since_now(&start)) < usec)
3c39a379 36 nop;
9cc80b6d
JA
37
38 return t;
3c39a379
JA
39}
40
9cc80b6d 41uint64_t usec_sleep(struct thread_data *td, unsigned long usec)
3c39a379 42{
fd841467 43 struct timespec req;
8b6a404c 44 struct timespec tv;
9cc80b6d 45 uint64_t t = 0;
3c39a379
JA
46
47 do {
fd841467
JA
48 unsigned long ts = usec;
49
50 if (usec < ns_granularity) {
9cc80b6d 51 t += usec_spin(usec);
3c39a379
JA
52 break;
53 }
54
fd841467 55 ts = usec - ns_granularity;
3c39a379 56
fd841467
JA
57 if (ts >= 1000000) {
58 req.tv_sec = ts / 1000000;
59 ts -= 1000000 * req.tv_sec;
7eff05d7
JA
60 /*
61 * Limit sleep to ~1 second at most, otherwise we
62 * don't notice then someone signaled the job to
63 * exit manually.
64 */
65 if (req.tv_sec > 1)
66 req.tv_sec = 1;
fd841467
JA
67 } else
68 req.tv_sec = 0;
69
70 req.tv_nsec = ts * 1000;
71 fio_gettime(&tv, NULL);
72
73 if (nanosleep(&req, NULL) < 0)
3c39a379
JA
74 break;
75
fd841467 76 ts = utime_since_now(&tv);
9cc80b6d 77 t += ts;
fd841467
JA
78 if (ts >= usec)
79 break;
3c39a379 80
fd841467 81 usec -= ts;
3c39a379 82 } while (!td->terminate);
9cc80b6d
JA
83
84 return t;
3c39a379
JA
85}
86
8b22c785
JA
87uint64_t time_since_genesis(void)
88{
89 return time_since_now(&genesis);
90}
91
aa60bc58 92uint64_t mtime_since_genesis(void)
263e529f
JA
93{
94 return mtime_since_now(&genesis);
95}
96
0de5b26f
JA
97uint64_t utime_since_genesis(void)
98{
99 return utime_since_now(&genesis);
100}
101
356014ff 102bool in_ramp_time(struct thread_data *td)
b29ee5b3
JA
103{
104 return td->o.ramp_time && !td->ramp_time_over;
105}
106
3eeb1620 107static bool parent_update_ramp(struct thread_data *td)
ada8f235
JA
108{
109 struct thread_data *parent = td->parent;
110
111 if (!parent || parent->ramp_time_over)
3eeb1620 112 return false;
ada8f235
JA
113
114 reset_all_stats(parent);
49f3f914 115 parent->ramp_time_over = true;
ada8f235 116 td_set_runstate(parent, TD_RAMP);
3eeb1620 117 return true;
ada8f235
JA
118}
119
356014ff 120bool ramp_time_over(struct thread_data *td)
721938ae 121{
721938ae 122 if (!td->o.ramp_time || td->ramp_time_over)
356014ff 123 return true;
721938ae 124
8b6a404c 125 if (utime_since_now(&td->epoch) >= td->o.ramp_time) {
49f3f914 126 td->ramp_time_over = true;
b29ee5b3 127 reset_all_stats(td);
8eb142dd 128 reset_io_stats(td);
b29ee5b3 129 td_set_runstate(td, TD_RAMP);
3eeb1620
JA
130
131 /*
132 * If we have a parent, the parent isn't doing IO. Hence
133 * the parent never enters do_io(), which will switch us
134 * from RAMP -> RUNNING. Do this manually here.
135 */
136 if (parent_update_ramp(td))
137 td_set_runstate(td, TD_RUNNING);
138
356014ff 139 return true;
721938ae
JA
140 }
141
356014ff 142 return false;
721938ae
JA
143}
144
03e20d68 145void fio_time_init(void)
263e529f 146{
fd841467
JA
147 int i;
148
c223da83
JA
149 fio_clock_init();
150
fd841467
JA
151 /*
152 * Check the granularity of the nanosleep function
153 */
154 for (i = 0; i < 10; i++) {
8b6a404c 155 struct timespec tv, ts;
fd841467
JA
156 unsigned long elapsed;
157
158 fio_gettime(&tv, NULL);
159 ts.tv_sec = 0;
160 ts.tv_nsec = 1000;
161
162 nanosleep(&ts, NULL);
163 elapsed = utime_since_now(&tv);
164
165 if (elapsed > ns_granularity)
166 ns_granularity = elapsed;
167 }
263e529f 168}
6043c579 169
a2f77c9f
JA
170void set_genesis_time(void)
171{
172 fio_gettime(&genesis, NULL);
173}
174
3aea75b1
KC
175void set_epoch_time(struct thread_data *td, int log_unix_epoch)
176{
177 fio_gettime(&td->epoch, NULL);
178 if (log_unix_epoch) {
179 struct timeval tv;
180 gettimeofday(&tv, NULL);
181 td->unix_epoch = (unsigned long long)(tv.tv_sec) * 1000 +
182 (unsigned long long)(tv.tv_usec) / 1000;
183 }
184}
185
8b6a404c 186void fill_start_time(struct timespec *t)
6043c579
JA
187{
188 memcpy(t, &genesis, sizeof(genesis));
189}