Add support for preadv2/pwritev2
[fio.git] / time.c
CommitLineData
3c39a379
JA
1#include <time.h>
2#include <sys/time.h>
3
4#include "fio.h"
5
263e529f 6static struct timeval genesis;
fd841467 7static unsigned long ns_granularity;
263e529f 8
3c39a379
JA
9/*
10 * busy looping version for the last few usec
11 */
9cc80b6d 12uint64_t usec_spin(unsigned int usec)
3c39a379
JA
13{
14 struct timeval start;
9cc80b6d 15 uint64_t t;
3c39a379 16
02bcaa8c 17 fio_gettime(&start, NULL);
9cc80b6d 18 while ((t = utime_since_now(&start)) < usec)
3c39a379 19 nop;
9cc80b6d
JA
20
21 return t;
3c39a379
JA
22}
23
9cc80b6d 24uint64_t usec_sleep(struct thread_data *td, unsigned long usec)
3c39a379 25{
fd841467
JA
26 struct timespec req;
27 struct timeval tv;
9cc80b6d 28 uint64_t t = 0;
3c39a379
JA
29
30 do {
fd841467
JA
31 unsigned long ts = usec;
32
33 if (usec < ns_granularity) {
9cc80b6d 34 t += usec_spin(usec);
3c39a379
JA
35 break;
36 }
37
fd841467 38 ts = usec - ns_granularity;
3c39a379 39
fd841467
JA
40 if (ts >= 1000000) {
41 req.tv_sec = ts / 1000000;
42 ts -= 1000000 * req.tv_sec;
43 } else
44 req.tv_sec = 0;
45
46 req.tv_nsec = ts * 1000;
47 fio_gettime(&tv, NULL);
48
49 if (nanosleep(&req, NULL) < 0)
3c39a379
JA
50 break;
51
fd841467 52 ts = utime_since_now(&tv);
9cc80b6d 53 t += ts;
fd841467
JA
54 if (ts >= usec)
55 break;
3c39a379 56
fd841467 57 usec -= ts;
3c39a379 58 } while (!td->terminate);
9cc80b6d
JA
59
60 return t;
3c39a379
JA
61}
62
8b22c785
JA
63uint64_t time_since_genesis(void)
64{
65 return time_since_now(&genesis);
66}
67
aa60bc58 68uint64_t mtime_since_genesis(void)
263e529f
JA
69{
70 return mtime_since_now(&genesis);
71}
72
0de5b26f
JA
73uint64_t utime_since_genesis(void)
74{
75 return utime_since_now(&genesis);
76}
77
b29ee5b3
JA
78int in_ramp_time(struct thread_data *td)
79{
80 return td->o.ramp_time && !td->ramp_time_over;
81}
82
ada8f235
JA
83static void parent_update_ramp(struct thread_data *td)
84{
85 struct thread_data *parent = td->parent;
86
87 if (!parent || parent->ramp_time_over)
88 return;
89
90 reset_all_stats(parent);
91 parent->ramp_time_over = 1;
92 td_set_runstate(parent, TD_RAMP);
93}
94
721938ae
JA
95int ramp_time_over(struct thread_data *td)
96{
97 struct timeval tv;
98
99 if (!td->o.ramp_time || td->ramp_time_over)
100 return 1;
101
102 fio_gettime(&tv, NULL);
0de5b26f 103 if (utime_since(&td->epoch, &tv) >= td->o.ramp_time) {
721938ae 104 td->ramp_time_over = 1;
b29ee5b3
JA
105 reset_all_stats(td);
106 td_set_runstate(td, TD_RAMP);
ada8f235 107 parent_update_ramp(td);
721938ae
JA
108 return 1;
109 }
110
111 return 0;
112}
113
03e20d68 114void fio_time_init(void)
263e529f 115{
fd841467
JA
116 int i;
117
c223da83
JA
118 fio_clock_init();
119
fd841467
JA
120 /*
121 * Check the granularity of the nanosleep function
122 */
123 for (i = 0; i < 10; i++) {
124 struct timeval tv;
125 struct timespec ts;
126 unsigned long elapsed;
127
128 fio_gettime(&tv, NULL);
129 ts.tv_sec = 0;
130 ts.tv_nsec = 1000;
131
132 nanosleep(&ts, NULL);
133 elapsed = utime_since_now(&tv);
134
135 if (elapsed > ns_granularity)
136 ns_granularity = elapsed;
137 }
263e529f 138}
6043c579 139
a2f77c9f
JA
140void set_genesis_time(void)
141{
142 fio_gettime(&genesis, NULL);
143}
144
6043c579
JA
145void fill_start_time(struct timeval *t)
146{
147 memcpy(t, &genesis, sizeof(genesis));
148}