rand: fix truncated rand_seed on Windows
[fio.git] / time.c
1 #include <time.h>
2 #include <sys/time.h>
3
4 #include "fio.h"
5
6 static struct timespec genesis;
7 static unsigned long ns_granularity;
8
9 void timespec_add_msec(struct timespec *ts, unsigned int msec)
10 {
11         uint64_t adj_nsec = 1000000ULL * msec;
12
13         ts->tv_nsec += adj_nsec;
14         if (adj_nsec >= 1000000000) {
15                 uint64_t adj_sec = adj_nsec / 1000000000;
16
17                 ts->tv_nsec -= adj_sec * 1000000000;
18                 ts->tv_sec += adj_sec;
19         }
20         if (ts->tv_nsec >= 1000000000){
21                 ts->tv_nsec -= 1000000000;
22                 ts->tv_sec++;
23         }
24 }
25
26 /*
27  * busy looping version for the last few usec
28  */
29 uint64_t usec_spin(unsigned int usec)
30 {
31         struct timespec start;
32         uint64_t t;
33
34         fio_gettime(&start, NULL);
35         while ((t = utime_since_now(&start)) < usec)
36                 nop;
37
38         return t;
39 }
40
41 uint64_t usec_sleep(struct thread_data *td, unsigned long usec)
42 {
43         struct timespec req;
44         struct timespec tv;
45         uint64_t t = 0;
46
47         do {
48                 unsigned long ts = usec;
49
50                 if (usec < ns_granularity) {
51                         t += usec_spin(usec);
52                         break;
53                 }
54
55                 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)
67                         break;
68
69                 ts = utime_since_now(&tv);
70                 t += ts;
71                 if (ts >= usec)
72                         break;
73
74                 usec -= ts;
75         } while (!td->terminate);
76
77         return t;
78 }
79
80 uint64_t time_since_genesis(void)
81 {
82         return time_since_now(&genesis);
83 }
84
85 uint64_t mtime_since_genesis(void)
86 {
87         return mtime_since_now(&genesis);
88 }
89
90 uint64_t utime_since_genesis(void)
91 {
92         return utime_since_now(&genesis);
93 }
94
95 bool in_ramp_time(struct thread_data *td)
96 {
97         return td->o.ramp_time && !td->ramp_time_over;
98 }
99
100 static bool 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 false;
106
107         reset_all_stats(parent);
108         parent->ramp_time_over = true;
109         td_set_runstate(parent, TD_RAMP);
110         return true;
111 }
112
113 bool ramp_time_over(struct thread_data *td)
114 {
115         if (!td->o.ramp_time || td->ramp_time_over)
116                 return true;
117
118         if (utime_since_now(&td->epoch) >= td->o.ramp_time) {
119                 td->ramp_time_over = true;
120                 reset_all_stats(td);
121                 reset_io_stats(td);
122                 td_set_runstate(td, TD_RAMP);
123
124                 /*
125                  * If we have a parent, the parent isn't doing IO. Hence
126                  * the parent never enters do_io(), which will switch us
127                  * from RAMP -> RUNNING. Do this manually here.
128                  */
129                 if (parent_update_ramp(td))
130                         td_set_runstate(td, TD_RUNNING);
131
132                 return true;
133         }
134
135         return false;
136 }
137
138 void fio_time_init(void)
139 {
140         int i;
141
142         fio_clock_init();
143
144         /*
145          * Check the granularity of the nanosleep function
146          */
147         for (i = 0; i < 10; i++) {
148                 struct timespec tv, ts;
149                 unsigned long elapsed;
150
151                 fio_gettime(&tv, NULL);
152                 ts.tv_sec = 0;
153                 ts.tv_nsec = 1000;
154
155                 nanosleep(&ts, NULL);
156                 elapsed = utime_since_now(&tv);
157
158                 if (elapsed > ns_granularity)
159                         ns_granularity = elapsed;
160         }
161 }
162
163 void set_genesis_time(void)
164 {
165         fio_gettime(&genesis, NULL);
166 }
167
168 void set_epoch_time(struct thread_data *td, int log_unix_epoch)
169 {
170         fio_gettime(&td->epoch, NULL);
171         if (log_unix_epoch) {
172                 struct timeval tv;
173                 gettimeofday(&tv, NULL);
174                 td->unix_epoch = (unsigned long long)(tv.tv_sec) * 1000 +
175                                  (unsigned long long)(tv.tv_usec) / 1000;
176         }
177 }
178
179 void fill_start_time(struct timespec *t)
180 {
181         memcpy(t, &genesis, sizeof(genesis));
182 }