t/nvmept_trim: increase transfer size for some tests
[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 /*
42  * busy loop for a fixed amount of cycles
43  */
44 void cycles_spin(unsigned int n)
45 {
46         unsigned long i;
47
48         for (i=0; i < n; i++)
49                 nop;
50 }
51
52 uint64_t usec_sleep(struct thread_data *td, unsigned long usec)
53 {
54         struct timespec req;
55         struct timespec tv;
56         uint64_t t = 0;
57
58         do {
59                 unsigned long ts = usec;
60
61                 if (usec < ns_granularity) {
62                         t += usec_spin(usec);
63                         break;
64                 }
65
66                 ts = usec - ns_granularity;
67
68                 if (ts >= 1000000) {
69                         req.tv_sec = ts / 1000000;
70                         ts -= 1000000 * req.tv_sec;
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;
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)
85                         break;
86
87                 ts = utime_since_now(&tv);
88                 t += ts;
89                 if (ts >= usec)
90                         break;
91
92                 usec -= ts;
93         } while (!td->terminate);
94
95         return t;
96 }
97
98 uint64_t time_since_genesis(void)
99 {
100         return time_since_now(&genesis);
101 }
102
103 uint64_t mtime_since_genesis(void)
104 {
105         return mtime_since_now(&genesis);
106 }
107
108 uint64_t utime_since_genesis(void)
109 {
110         return utime_since_now(&genesis);
111 }
112
113 bool in_ramp_time(struct thread_data *td)
114 {
115         return td->o.ramp_time && !td->ramp_time_over;
116 }
117
118 static bool parent_update_ramp(struct thread_data *td)
119 {
120         struct thread_data *parent = td->parent;
121
122         if (!parent || parent->ramp_time_over)
123                 return false;
124
125         reset_all_stats(parent);
126         parent->ramp_time_over = true;
127         td_set_runstate(parent, TD_RAMP);
128         return true;
129 }
130
131 bool ramp_time_over(struct thread_data *td)
132 {
133         if (!td->o.ramp_time || td->ramp_time_over)
134                 return true;
135
136         if (utime_since_now(&td->epoch) >= td->o.ramp_time) {
137                 td->ramp_time_over = true;
138                 reset_all_stats(td);
139                 reset_io_stats(td);
140                 td_set_runstate(td, TD_RAMP);
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
150                 return true;
151         }
152
153         return false;
154 }
155
156 void fio_time_init(void)
157 {
158         int i;
159
160         fio_clock_init();
161
162         /*
163          * Check the granularity of the nanosleep function
164          */
165         for (i = 0; i < 10; i++) {
166                 struct timespec tv, ts;
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         }
179 }
180
181 void set_genesis_time(void)
182 {
183         fio_gettime(&genesis, NULL);
184 }
185
186 void set_epoch_time(struct thread_data *td, clockid_t log_alternate_epoch_clock_id, clockid_t job_start_clock_id)
187 {
188         struct timespec ts;
189         fio_gettime(&td->epoch, NULL);
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;
202         }
203 }
204
205 void fill_start_time(struct timespec *t)
206 {
207         memcpy(t, &genesis, sizeof(genesis));
208 }