blob: 17f9f6fcd187b69feda17140ba62928d7a8f4914 [file] [log] [blame]
Jens Axboe3c39a372006-06-06 20:56:12 +02001#include <time.h>
2#include <sys/time.h>
3
4#include "fio.h"
5
Jens Axboe263e5292006-10-18 15:37:01 +02006static struct timeval genesis;
Jens Axboefd841462007-01-13 12:20:30 +01007static unsigned long ns_granularity;
Jens Axboe263e5292006-10-18 15:37:01 +02008
Jens Axboe3c39a372006-06-06 20:56:12 +02009/*
10 * busy looping version for the last few usec
11 */
Jens Axboe6dd6f2c2008-12-10 13:24:12 +010012void usec_spin(unsigned int usec)
Jens Axboe3c39a372006-06-06 20:56:12 +020013{
14 struct timeval start;
15
Jens Axboe02bcaa82006-11-24 10:42:00 +010016 fio_gettime(&start, NULL);
Jens Axboe3c39a372006-06-06 20:56:12 +020017 while (utime_since_now(&start) < usec)
18 nop;
19}
20
21void usec_sleep(struct thread_data *td, unsigned long usec)
22{
Jens Axboefd841462007-01-13 12:20:30 +010023 struct timespec req;
24 struct timeval tv;
Jens Axboe3c39a372006-06-06 20:56:12 +020025
26 do {
Jens Axboefd841462007-01-13 12:20:30 +010027 unsigned long ts = usec;
28
29 if (usec < ns_granularity) {
Jens Axboe6dd6f2c2008-12-10 13:24:12 +010030 usec_spin(usec);
Jens Axboe3c39a372006-06-06 20:56:12 +020031 break;
32 }
33
Jens Axboefd841462007-01-13 12:20:30 +010034 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)
Jens Axboe3c39a372006-06-06 20:56:12 +020046 break;
47
Jens Axboefd841462007-01-13 12:20:30 +010048 ts = utime_since_now(&tv);
49 if (ts >= usec)
Jens Axboe3c39a372006-06-06 20:56:12 +020050 break;
51
Jens Axboefd841462007-01-13 12:20:30 +010052 usec -= ts;
Jens Axboe3c39a372006-06-06 20:56:12 +020053 } while (!td->terminate);
54}
55
Jens Axboeaa60bc52013-01-04 13:24:52 +010056uint64_t mtime_since_genesis(void)
Jens Axboe263e5292006-10-18 15:37:01 +020057{
58 return mtime_since_now(&genesis);
59}
60
Jens Axboeb29ee5b2008-09-11 10:17:26 +020061int in_ramp_time(struct thread_data *td)
62{
63 return td->o.ramp_time && !td->ramp_time_over;
64}
65
Jens Axboe721938a2008-09-10 09:46:16 +020066int 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;
Jens Axboeb29ee5b2008-09-11 10:17:26 +020076 reset_all_stats(td);
77 td_set_runstate(td, TD_RAMP);
Jens Axboe721938a2008-09-10 09:46:16 +020078 return 1;
79 }
80
81 return 0;
82}
83
Bruce Cran03e20d62011-01-02 20:14:54 +010084void fio_time_init(void)
Jens Axboe263e5292006-10-18 15:37:01 +020085{
Jens Axboefd841462007-01-13 12:20:30 +010086 int i;
87
Jens Axboec223da82010-03-24 13:23:53 +010088 fio_clock_init();
89
Jens Axboefd841462007-01-13 12:20:30 +010090 /*
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 }
Jens Axboe263e5292006-10-18 15:37:01 +0200108}
Jens Axboe6043c572006-11-03 11:37:47 +0100109
Jens Axboea2f77c92007-02-22 11:53:00 +0100110void set_genesis_time(void)
111{
112 fio_gettime(&genesis, NULL);
113}
114
Jens Axboe6043c572006-11-03 11:37:47 +0100115void fill_start_time(struct timeval *t)
116{
117 memcpy(t, &genesis, sizeof(genesis));
118}