2 * Copyright (c) 2011 Petr Koupy
3 * Copyright (c) 2011 Jiri Zarevucky
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 /** @addtogroup libposix
33 /** @file Time measurement support.
36 #define LIBPOSIX_INTERNAL
37 #define __POSIX_DEF__(x) posix_##x
39 #include "internal/common.h"
40 #include "posix/time.h"
42 #include "posix/ctype.h"
43 #include "posix/errno.h"
44 #include "posix/signal.h"
45 #include "posix/assert.h"
47 #include "libc/malloc.h"
48 #include "libc/task.h"
49 #include "libc/stats.h"
50 #include "libc/stdbool.h"
51 #include "libc/stddef.h"
52 #include "libc/thread.h"
53 #include "libc/sys/time.h"
55 // TODO: test everything in this file
57 /* In some places in this file, phrase "normalized broken-down time" is used.
58 * This means time broken down to components (year, month, day, hour, min, sec),
59 * in which every component is in its proper bounds. Non-normalized time could
60 * e.g. be 2011-54-5 29:13:-5, which would semantically mean start of year 2011
61 * + 53 months + 4 days + 29 hours + 13 minutes - 5 seconds.
66 char *posix_tzname
[2];
69 * Set timezone conversion information.
71 void posix_tzset(void)
73 // TODO: read environment
74 posix_tzname
[0] = (char *) "GMT";
75 posix_tzname
[1] = (char *) "GMT";
81 * Get the time in seconds
83 * @param t If t is non-NULL, the return value is also stored in the memory
85 * @return On success, the value of time in seconds since the Epoch
86 * is returned. On error, (time_t)-1 is returned.
88 time_t posix_time(time_t *t
)
94 * Converts a time value to a broken-down UTC time.
96 * @param timer Time to convert.
97 * @param result Structure to store the result to.
98 * @return Value of result on success, NULL on overflow.
100 struct tm
*posix_gmtime_r(const time_t *restrict timer
,
101 struct tm
*restrict result
)
103 int rc
= time_utc2tm(*timer
, result
);
113 * Converts a time value to a broken-down UTC time.
114 * (non reentrant version)
116 * @param timep Time to convert
117 * @return Pointer to a statically allocated structure that stores
118 * the result, NULL in case of error.
120 struct tm
*posix_gmtime(const time_t *restrict timep
)
122 static struct tm result
;
124 return posix_gmtime_r(timep
, &result
);
128 * Converts a time value to a broken-down local time.
130 * @param timer Time to convert.
131 * @param result Structure to store the result to.
132 * @return Value of result on success, NULL on overflow.
134 struct tm
*posix_localtime_r(const time_t *restrict timer
,
135 struct tm
*restrict result
)
137 // TODO: deal with timezone
138 // currently assumes system and all times are in GMT
139 return posix_gmtime_r(timer
, result
);
143 * Converts a time value to a broken-down local time.
144 * (non reentrant version)
146 * @param timep Time to convert.
147 * @return Pointer to a statically allocated structure that stores
148 * the result, NULL in case of error.
150 struct tm
*posix_localtime(const time_t *restrict timep
)
152 static struct tm result
;
154 return posix_localtime_r(timep
, &result
);
158 * Converts broken-down time to a string in format
159 * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
161 * @param timeptr Broken-down time structure.
162 * @param buf Buffer to store string to, must be at least ASCTIME_BUF_LEN
164 * @return Value of buf.
166 char *posix_asctime_r(const struct tm
*restrict timeptr
,
169 time_tm2str(timeptr
, buf
);
174 * Convers broken-down time to a string in format
175 * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
176 * (non reentrant version)
178 * @param timeptr Broken-down time structure.
179 * @return Pointer to a statically allocated buffer that stores
180 * the result, NULL in case of error.
182 char *posix_asctime(const struct tm
*restrict timeptr
)
184 static char buf
[ASCTIME_BUF_LEN
];
186 return posix_asctime_r(timeptr
, buf
);
190 * Converts the calendar time to a string in format
191 * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
193 * @param timer Time to convert.
194 * @param buf Buffer to store string to. Must be at least ASCTIME_BUF_LEN
196 * @return Pointer to buf on success, NULL on failure.
198 char *posix_ctime_r(const time_t *timer
, char *buf
)
200 int r
= time_local2str(*timer
, buf
);
210 * Converts the calendar time to a string in format
211 * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
212 * (non reentrant version)
214 * @param timep Time to convert.
215 * @return Pointer to a statically allocated buffer that stores
216 * the result, NULL in case of error.
218 char *posix_ctime(const time_t *timep
)
220 static char buf
[ASCTIME_BUF_LEN
];
222 return posix_ctime_r(timep
, buf
);
226 * Get clock resolution. Only CLOCK_REALTIME is supported.
228 * @param clock_id Clock ID.
229 * @param res Pointer to the variable where the resolution is to be written.
230 * @return 0 on success, -1 with errno set on failure.
232 int posix_clock_getres(posix_clockid_t clock_id
, struct posix_timespec
*res
)
239 res
->tv_nsec
= 1000; /* Microsecond resolution. */
248 * Get time. Only CLOCK_REALTIME is supported.
250 * @param clock_id ID of the clock to query.
251 * @param tp Pointer to the variable where the time is to be written.
252 * @return 0 on success, -1 with errno on failure.
254 int posix_clock_gettime(posix_clockid_t clock_id
, struct posix_timespec
*tp
)
262 gettimeofday(&tv
, NULL
);
263 tp
->tv_sec
= tv
.tv_sec
;
264 tp
->tv_nsec
= tv
.tv_usec
* 1000;
273 * Set time on a specified clock. As HelenOS doesn't support this yet,
274 * this function always fails.
276 * @param clock_id ID of the clock to set.
277 * @param tp Time to set.
278 * @return 0 on success, -1 with errno on failure.
280 int posix_clock_settime(posix_clockid_t clock_id
,
281 const struct posix_timespec
*tp
)
287 // TODO: setting clock
288 // FIXME: HelenOS doesn't actually support hardware
299 * Sleep on a specified clock.
301 * @param clock_id ID of the clock to sleep on (only CLOCK_REALTIME supported).
302 * @param flags Flags (none supported).
303 * @param rqtp Sleep time.
304 * @param rmtp Remaining time is written here if sleep is interrupted.
305 * @return 0 on success, -1 with errno set on failure.
307 int posix_clock_nanosleep(posix_clockid_t clock_id
, int flags
,
308 const struct posix_timespec
*rqtp
, struct posix_timespec
*rmtp
)
310 assert(rqtp
!= NULL
);
311 assert(rmtp
!= NULL
);
315 // TODO: interruptible sleep
316 if (rqtp
->tv_sec
!= 0) {
317 thread_sleep(rqtp
->tv_sec
);
319 if (rqtp
->tv_nsec
!= 0) {
320 thread_usleep(rqtp
->tv_nsec
/ 1000);
330 * Get CPU time used since the process invocation.
332 * @return Consumed CPU cycles by this process or -1 if not available.
334 posix_clock_t
posix_clock(void)
336 posix_clock_t total_cycles
= -1;
337 stats_task_t
*task_stats
= stats_get_task(task_get_id());
339 total_cycles
= (posix_clock_t
) (task_stats
->kcycles
+
340 task_stats
->ucycles
);