5a9571d8b0c626662efaba592c4b95d01cb75d8e
[nit.git] / lib / realtime.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # This file is free software, which comes along with NIT. This software is
6 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 # is kept unaltered, and a notification of the changes is added.
10 # You are allowed to redistribute it and sell it, alone or is a part of
11 # another product.
12
13 # Services to keep time of the wall clock time
14 module realtime is ldflags "-lrt"
15
16 in "C header" `{
17 #ifdef _POSIX_C_SOURCE
18 #undef _POSIX_C_SOURCE
19 #endif
20 #define _POSIX_C_SOURCE 199309L
21 #include <time.h>
22 `}
23
24 in "C" `{
25
26 #ifdef __MACH__
27 /* OS X does not have clock_gettime, mascarade it and use clock_get_time
28 * cf http://stackoverflow.com/questions/11680461/monotonic-clock-on-osx
29 */
30 #include <mach/clock.h>
31 #include <mach/mach.h>
32 #define CLOCK_REALTIME CALENDAR_CLOCK
33 #define CLOCK_MONOTONIC SYSTEM_CLOCK
34 void clock_gettime(clock_t clock_name, struct timespec *ts) {
35 clock_serv_t cclock;
36 mach_timespec_t mts;
37 host_get_clock_service(mach_host_self(), clock_name, &cclock);
38 clock_get_time(cclock, &mts);
39 mach_port_deallocate(mach_task_self(), cclock);
40 ts->tv_sec = mts.tv_sec;
41 ts->tv_nsec = mts.tv_nsec;
42 }
43 #endif
44 `}
45
46 # Elapsed time representation.
47 extern class Timespec `{struct timespec*`}
48
49 # Init a new Timespec from `s` seconds and `ns` nanoseconds.
50 new ( s, ns : Int ) `{
51 struct timespec* tv = malloc( sizeof(struct timespec) );
52 tv->tv_sec = s; tv->tv_nsec = ns;
53 return tv;
54 `}
55
56 # Init a new Timespec from now.
57 new monotonic_now `{
58 struct timespec* tv = malloc( sizeof(struct timespec) );
59 clock_gettime( CLOCK_MONOTONIC, tv );
60 return tv;
61 `}
62
63 # Init a new Timespec copied from another.
64 new copy_of( other : Timespec ) `{
65 struct timespec* tv = malloc( sizeof(struct timespec) );
66 tv->tv_sec = other->tv_sec;
67 tv->tv_nsec = other->tv_nsec;
68 return tv;
69 `}
70
71 # Update `self` clock.
72 fun update `{
73 clock_gettime(CLOCK_MONOTONIC, self);
74 `}
75
76 # Subtract `other` from `self`
77 fun -(other: Timespec): Timespec
78 do
79 var s = sec - other.sec
80 var ns = nanosec - other.nanosec
81 if ns < 0 then
82 s -= 1
83 ns += 1000000000
84 end
85 return new Timespec(s, ns)
86 end
87
88 # Number of whole seconds of elapsed time.
89 fun sec : Int `{
90 return self->tv_sec;
91 `}
92
93 # Rest of the elapsed time (a fraction of a second).
94 #
95 # Number of nanoseconds.
96 fun nanosec : Int `{
97 return self->tv_nsec;
98 `}
99
100 # Elapsed time in microseconds, with both whole seconds and the rest
101 #
102 # May cause an `Int` overflow, use only with a low number of seconds.
103 fun microsec: Int `{
104 return self->tv_sec*1000000 + self->tv_nsec/1000;
105 `}
106
107 # Elapsed time in milliseconds, with both whole seconds and the rest
108 #
109 # May cause an `Int` overflow, use only with a low number of seconds.
110 fun millisec: Int `{
111 return self->tv_sec*1000 + self->tv_nsec/1000000;
112 `}
113
114 # Number of seconds as a `Float`
115 #
116 # Incurs a loss of precision, but the result is pretty to print.
117 fun to_f: Float do return sec.to_f + nanosec.to_f / 1000000000.0
118
119 redef fun to_s do return "{to_f}s"
120 end
121
122 # Keeps track of real time
123 class Clock
124 super FinalizableOnce
125
126 # TODO use less mallocs
127
128 # Time at creation
129 protected var time_at_beginning = new Timespec.monotonic_now
130
131 # Time at last time a lapse method was called
132 protected var time_at_last_lapse = new Timespec.monotonic_now
133
134 # Smallest time frame reported by clock
135 fun resolution: Timespec `{
136 struct timespec* tv = malloc( sizeof(struct timespec) );
137 #ifdef __MACH__
138 clock_serv_t cclock;
139 int nsecs;
140 mach_msg_type_number_t count;
141 host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
142 clock_get_attributes(cclock, CLOCK_GET_TIME_RES, (clock_attr_t)&nsecs, &count);
143 mach_port_deallocate(mach_task_self(), cclock);
144 tv->tv_sec = 0;
145 tv->tv_nsec = nsecs;
146 #else
147 clock_getres( CLOCK_MONOTONIC, tv );
148 #endif
149 return tv;
150 `}
151
152 # Seconds since the creation of this instance
153 fun total: Float
154 do
155 var now = new Timespec.monotonic_now
156 var diff = now - time_at_beginning
157 var r = diff.to_f
158 diff.free
159 now.free
160 return r
161 end
162
163 # Seconds since the last call to `lapse`
164 fun lapse: Float
165 do
166 var nt = new Timespec.monotonic_now
167 var dt = nt - time_at_last_lapse
168 var r = dt.to_f
169 dt.free
170 time_at_last_lapse.free
171 time_at_last_lapse = nt
172 return r
173 end
174
175 # Seconds since the last call to `lapse`, without resetting the lapse counter
176 fun peek_lapse: Float
177 do
178 var nt = new Timespec.monotonic_now
179 var dt = nt - time_at_last_lapse
180 var r = dt.to_f
181 nt.free
182 dt.free
183 return r
184 end
185
186 redef fun finalize_once
187 do
188 time_at_beginning.free
189 time_at_last_lapse.free
190 end
191 end