t/io_uring: only calculate per-file depth if we have files
[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
0d8cc753
CL
41/*
42 * busy loop for a fixed amount of cycles
43 */
44void cycles_spin(unsigned int n)
45{
46 unsigned long i;
47
48 for (i=0; i < n; i++)
49 nop;
50}
51
9cc80b6d 52uint64_t usec_sleep(struct thread_data *td, unsigned long usec)
3c39a379 53{
fd841467 54 struct timespec req;
8b6a404c 55 struct timespec tv;
9cc80b6d 56 uint64_t t = 0;
3c39a379
JA
57
58 do {
fd841467
JA
59 unsigned long ts = usec;
60
61 if (usec < ns_granularity) {
9cc80b6d 62 t += usec_spin(usec);
3c39a379
JA
63 break;
64 }
65
fd841467 66 ts = usec - ns_granularity;
3c39a379 67
fd841467
JA
68 if (ts >= 1000000) {
69 req.tv_sec = ts / 1000000;
70 ts -= 1000000 * req.tv_sec;
7eff05d7
JA
71 /*
72 * Limit sleep to ~1 second at most, otherwise we
73 * don't notice then someone signaled the job to
74 * exit manually.
75 */
76 if (req.tv_sec > 1)
77 req.tv_sec = 1;
fd841467
JA
78 } else
79 req.tv_sec = 0;
80
81 req.tv_nsec = ts * 1000;
82 fio_gettime(&tv, NULL);
83
84 if (nanosleep(&req, NULL) < 0)
3c39a379
JA
85 break;
86
fd841467 87 ts = utime_since_now(&tv);
9cc80b6d 88 t += ts;
fd841467
JA
89 if (ts >= usec)
90 break;
3c39a379 91
fd841467 92 usec -= ts;
3c39a379 93 } while (!td->terminate);
9cc80b6d
JA
94
95 return t;
3c39a379
JA
96}
97
8b22c785
JA
98uint64_t time_since_genesis(void)
99{
100 return time_since_now(&genesis);
101}
102
aa60bc58 103uint64_t mtime_since_genesis(void)
263e529f
JA
104{
105 return mtime_since_now(&genesis);
106}
107
0de5b26f
JA
108uint64_t utime_since_genesis(void)
109{
110 return utime_since_now(&genesis);
111}
112
356014ff 113bool in_ramp_time(struct thread_data *td)
b29ee5b3
JA
114{
115 return td->o.ramp_time && !td->ramp_time_over;
116}
117
3eeb1620 118static bool parent_update_ramp(struct thread_data *td)
ada8f235
JA
119{
120 struct thread_data *parent = td->parent;
121
122 if (!parent || parent->ramp_time_over)
3eeb1620 123 return false;
ada8f235
JA
124
125 reset_all_stats(parent);
49f3f914 126 parent->ramp_time_over = true;
ada8f235 127 td_set_runstate(parent, TD_RAMP);
3eeb1620 128 return true;
ada8f235
JA
129}
130
356014ff 131bool ramp_time_over(struct thread_data *td)
721938ae 132{
721938ae 133 if (!td->o.ramp_time || td->ramp_time_over)
356014ff 134 return true;
721938ae 135
8b6a404c 136 if (utime_since_now(&td->epoch) >= td->o.ramp_time) {
49f3f914 137 td->ramp_time_over = true;
b29ee5b3 138 reset_all_stats(td);
8eb142dd 139 reset_io_stats(td);
b29ee5b3 140 td_set_runstate(td, TD_RAMP);
3eeb1620
JA
141
142 /*
143 * If we have a parent, the parent isn't doing IO. Hence
144 * the parent never enters do_io(), which will switch us
145 * from RAMP -> RUNNING. Do this manually here.
146 */
147 if (parent_update_ramp(td))
148 td_set_runstate(td, TD_RUNNING);
149
356014ff 150 return true;
721938ae
JA
151 }
152
356014ff 153 return false;
721938ae
JA
154}
155
03e20d68 156void fio_time_init(void)
263e529f 157{
fd841467
JA
158 int i;
159
c223da83
JA
160 fio_clock_init();
161
fd841467
JA
162 /*
163 * Check the granularity of the nanosleep function
164 */
165 for (i = 0; i < 10; i++) {
8b6a404c 166 struct timespec tv, ts;
fd841467
JA
167 unsigned long elapsed;
168
169 fio_gettime(&tv, NULL);
170 ts.tv_sec = 0;
171 ts.tv_nsec = 1000;
172
173 nanosleep(&ts, NULL);
174 elapsed = utime_since_now(&tv);
175
176 if (elapsed > ns_granularity)
177 ns_granularity = elapsed;
178 }
263e529f 179}
6043c579 180
a2f77c9f
JA
181void set_genesis_time(void)
182{
183 fio_gettime(&genesis, NULL);
184}
185
12d325ca 186void set_epoch_time(struct thread_data *td, clockid_t log_alternate_epoch_clock_id, clockid_t job_start_clock_id)
3aea75b1 187{
12d325ca 188 struct timespec ts;
3aea75b1 189 fio_gettime(&td->epoch, NULL);
12d325ca 190 clock_gettime(log_alternate_epoch_clock_id, &ts);
191 td->alternate_epoch = (unsigned long long)(ts.tv_sec) * 1000 +
192 (unsigned long long)(ts.tv_nsec) / 1000000;
193 if (job_start_clock_id == log_alternate_epoch_clock_id)
194 {
195 td->job_start = td->alternate_epoch;
196 }
197 else
198 {
199 clock_gettime(job_start_clock_id, &ts);
200 td->job_start = (unsigned long long)(ts.tv_sec) * 1000 +
201 (unsigned long long)(ts.tv_nsec) / 1000000;
3aea75b1
KC
202 }
203}
204
8b6a404c 205void fill_start_time(struct timespec *t)
6043c579
JA
206{
207 memcpy(t, &genesis, sizeof(genesis));
208}