1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Alexis Laferrière <alexis.laf@xymus.net>
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
13 # Services to keep time of the wall clock time
14 module realtime
is ldflags
"-lrt"
17 #ifdef _POSIX_C_SOURCE
18 #undef _POSIX_C_SOURCE
20 #define _POSIX_C_SOURCE 199309L
26 #if defined(__MACH__) && !defined(CLOCK_REALTIME)
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
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) {
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;
46 # Elapsed time representation.
47 extern class Timespec `{struct timespec*`}
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
;
56 # Init a new Timespec from now.
58 struct timespec
* tv
= malloc
( sizeof
(struct timespec
) );
59 clock_gettime
( CLOCK_MONOTONIC, tv
);
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
;
71 # Update `self` clock.
73 clock_gettime
(CLOCK_MONOTONIC, self);
76 # Subtract `other
` from `self`
77 fun -(other: Timespec): Timespec `{
78 time_t s
= self-
>tv_sec
- other-
>tv_sec
;
79 long ns
= self-
>tv_nsec
- other-
>tv_nsec
;
84 struct timespec
* tv
= malloc
(sizeof
(struct timespec
));
85 tv-
>tv_sec
= s
; tv-
>tv_nsec
= ns
;
89 # Number of whole seconds of elapsed time.
94 # Rest of the elapsed time (a fraction of a second).
96 # Number of nanoseconds.
101 # Elapsed time in microseconds, with both whole seconds and the rest
103 # May cause an `Int` overflow, use only with a low number of seconds.
105 return self-
>tv_sec
*1000000 + self-
>tv_nsec
/1000;
108 # Elapsed time in milliseconds, with both whole seconds and the rest
110 # May cause an `Int` overflow, use only with a low number of seconds.
112 return self-
>tv_sec
*1000 + self-
>tv_nsec
/1000000;
115 # Number of seconds as a `Float`
117 # Incurs a loss of precision, but the result is pretty to print.
119 return (double
)self-
>tv_sec
+ 0.000000001 * self-
>tv_nsec
;
122 redef fun to_s do return "{to_f}s"
125 # Keeps track of real time
128 # var clock = new Clock
130 # # sleeping at least 1s
132 # assert clock.total >= 1.0
133 # assert clock.lapse >= 1.0
135 # # sleeping at least 5ms
137 # assert clock.total >= 1.005
138 # assert clock.lapse >= 0.005
141 super FinalizableOnce
143 # TODO use less mallocs
146 protected var time_at_beginning = new Timespec.monotonic_now
148 # Time at last time a lapse method was called
149 protected var time_at_last_lapse = new Timespec.monotonic_now
151 # Smallest time frame reported by clock
152 fun resolution: Timespec `{
153 struct timespec
* tv
= malloc
( sizeof
(struct timespec
) );
154 #if defined(__MACH__) && !defined(CLOCK_REALTIME)
157 mach_msg_type_number_t count
;
158 host_get_clock_service
(mach_host_self
(), SYSTEM_CLOCK, &cclock
);
159 clock_get_attributes
(cclock
, CLOCK_GET_TIME_RES, (clock_attr_t
)&nsecs
, &count
);
160 mach_port_deallocate
(mach_task_self
(), cclock
);
164 clock_getres
( CLOCK_MONOTONIC, tv
);
169 # Seconds since the creation of this instance
172 var now = new Timespec.monotonic_now
173 var diff = now - time_at_beginning
180 # Seconds since the last call to `lapse
`
183 var nt = new Timespec.monotonic_now
184 var dt = nt - time_at_last_lapse
187 time_at_last_lapse.free
188 time_at_last_lapse = nt
192 # Seconds since the last call to `lapse
`, without resetting the lapse counter
193 fun peek_lapse: Float
195 var nt = new Timespec.monotonic_now
196 var dt = nt - time_at_last_lapse
203 redef fun finalize_once
205 time_at_beginning.free
206 time_at_last_lapse.free