Use variable[] instead of GCC variable[0] syntax
[fio.git] / time.c
1 #include <time.h>
2 #include <sys/time.h>
3
4 #include "fio.h"
5
6 static struct timeval genesis;
7 static unsigned long ns_granularity;
8
9 /*
10  * busy looping version for the last few usec
11  */
12 void usec_spin(unsigned int usec)
13 {
14         struct timeval start;
15
16         fio_gettime(&start, NULL);
17         while (utime_since_now(&start) < usec)
18                 nop;
19 }
20
21 void usec_sleep(struct thread_data *td, unsigned long usec)
22 {
23         struct timespec req;
24         struct timeval tv;
25
26         do {
27                 unsigned long ts = usec;
28
29                 if (usec < ns_granularity) {
30                         usec_spin(usec);
31                         break;
32                 }
33
34                 ts = usec - ns_granularity;
35
36                 if (ts >= 1000000) {
37                         req.tv_sec = ts / 1000000;
38                         ts -= 1000000 * req.tv_sec;
39                 } else
40                         req.tv_sec = 0;
41
42                 req.tv_nsec = ts * 1000;
43                 fio_gettime(&tv, NULL);
44
45                 if (nanosleep(&req, NULL) < 0)
46                         break;
47
48                 ts = utime_since_now(&tv);
49                 if (ts >= usec)
50                         break;
51
52                 usec -= ts;
53         } while (!td->terminate);
54 }
55
56 uint64_t mtime_since_genesis(void)
57 {
58         return mtime_since_now(&genesis);
59 }
60
61 int in_ramp_time(struct thread_data *td)
62 {
63         return td->o.ramp_time && !td->ramp_time_over;
64 }
65
66 int ramp_time_over(struct thread_data *td)
67 {
68         struct timeval tv;
69
70         if (!td->o.ramp_time || td->ramp_time_over)
71                 return 1;
72
73         fio_gettime(&tv, NULL);
74         if (mtime_since(&td->epoch, &tv) >= td->o.ramp_time * 1000) {
75                 td->ramp_time_over = 1;
76                 reset_all_stats(td);
77                 td_set_runstate(td, TD_RAMP);
78                 return 1;
79         }
80
81         return 0;
82 }
83
84 void fio_time_init(void)
85 {
86         int i;
87
88         fio_clock_init();
89
90         /*
91          * Check the granularity of the nanosleep function
92          */
93         for (i = 0; i < 10; i++) {
94                 struct timeval tv;
95                 struct timespec ts;
96                 unsigned long elapsed;
97
98                 fio_gettime(&tv, NULL);
99                 ts.tv_sec = 0;
100                 ts.tv_nsec = 1000;
101
102                 nanosleep(&ts, NULL);
103                 elapsed = utime_since_now(&tv);
104
105                 if (elapsed > ns_granularity)
106                         ns_granularity = elapsed;
107         }
108 }
109
110 void set_genesis_time(void)
111 {
112         fio_gettime(&genesis, NULL);
113 }
114
115 void fill_start_time(struct timeval *t)
116 {
117         memcpy(t, &genesis, sizeof(genesis));
118 }