source: mainline/uspace/lib/posix/source/time.c@ c280d7e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c280d7e was 78188e5, checked in by Jiri Svoboda <jiri@…>, 8 years ago

stdbool.h should not include libarch/types.h

  • Property mode set to 100644
File size: 8.9 KB
Line 
1/*
2 * Copyright (c) 2011 Petr Koupy
3 * Copyright (c) 2011 Jiri Zarevucky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
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.
17 *
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.
28 */
29
30/** @addtogroup libposix
31 * @{
32 */
33/** @file Time measurement support.
34 */
35
36#define LIBPOSIX_INTERNAL
37#define __POSIX_DEF__(x) posix_##x
38
39#include "internal/common.h"
40#include "posix/time.h"
41
42#include "posix/ctype.h"
43#include "posix/errno.h"
44#include "posix/signal.h"
45#include "posix/assert.h"
46
47#include "libc/fibril_synch.h"
48#include "libc/malloc.h"
49#include "libc/task.h"
50#include "libc/stats.h"
51#include "libc/stddef.h"
52#include "libc/sys/time.h"
53
54// TODO: test everything in this file
55
56/* In some places in this file, phrase "normalized broken-down time" is used.
57 * This means time broken down to components (year, month, day, hour, min, sec),
58 * in which every component is in its proper bounds. Non-normalized time could
59 * e.g. be 2011-54-5 29:13:-5, which would semantically mean start of year 2011
60 * + 53 months + 4 days + 29 hours + 13 minutes - 5 seconds.
61 */
62
63int posix_daylight;
64long posix_timezone;
65char *posix_tzname[2];
66
67/**
68 * Set timezone conversion information.
69 */
70void posix_tzset(void)
71{
72 // TODO: read environment
73 posix_tzname[0] = (char *) "GMT";
74 posix_tzname[1] = (char *) "GMT";
75 posix_daylight = 0;
76 posix_timezone = 0;
77}
78
79/**
80 * Get the time in seconds
81 *
82 * @param t If t is non-NULL, the return value is also stored in the memory
83 * pointed to by t.
84 * @return On success, the value of time in seconds since the Epoch
85 * is returned. On error, (time_t)-1 is returned.
86 */
87time_t posix_time(time_t *t)
88{
89 return time(t);
90}
91
92/**
93 * Converts a time value to a broken-down UTC time.
94 *
95 * @param timer Time to convert.
96 * @param result Structure to store the result to.
97 * @return Value of result on success, NULL on overflow.
98 */
99struct tm *posix_gmtime_r(const time_t *restrict timer,
100 struct tm *restrict result)
101{
102 int rc = time_utc2tm(*timer, result);
103 if (rc != EOK) {
104 errno = rc;
105 return NULL;
106 }
107
108 return result;
109}
110
111/**
112 * Converts a time value to a broken-down UTC time.
113 * (non reentrant version)
114 *
115 * @param timep Time to convert
116 * @return Pointer to a statically allocated structure that stores
117 * the result, NULL in case of error.
118 */
119struct tm *posix_gmtime(const time_t *restrict timep)
120{
121 static struct tm result;
122
123 return posix_gmtime_r(timep, &result);
124}
125
126/**
127 * Converts a time value to a broken-down local time.
128 *
129 * @param timer Time to convert.
130 * @param result Structure to store the result to.
131 * @return Value of result on success, NULL on overflow.
132 */
133struct tm *posix_localtime_r(const time_t *restrict timer,
134 struct tm *restrict result)
135{
136 // TODO: deal with timezone
137 // currently assumes system and all times are in GMT
138 return posix_gmtime_r(timer, result);
139}
140
141/**
142 * Converts a time value to a broken-down local time.
143 * (non reentrant version)
144 *
145 * @param timep Time to convert.
146 * @return Pointer to a statically allocated structure that stores
147 * the result, NULL in case of error.
148 */
149struct tm *posix_localtime(const time_t *restrict timep)
150{
151 static struct tm result;
152
153 return posix_localtime_r(timep, &result);
154}
155
156/**
157 * Converts broken-down time to a string in format
158 * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
159 *
160 * @param timeptr Broken-down time structure.
161 * @param buf Buffer to store string to, must be at least ASCTIME_BUF_LEN
162 * bytes long.
163 * @return Value of buf.
164 */
165char *posix_asctime_r(const struct tm *restrict timeptr,
166 char *restrict buf)
167{
168 time_tm2str(timeptr, buf);
169 return buf;
170}
171
172/**
173 * Convers broken-down time to a string in format
174 * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
175 * (non reentrant version)
176 *
177 * @param timeptr Broken-down time structure.
178 * @return Pointer to a statically allocated buffer that stores
179 * the result, NULL in case of error.
180 */
181char *posix_asctime(const struct tm *restrict timeptr)
182{
183 static char buf[ASCTIME_BUF_LEN];
184
185 return posix_asctime_r(timeptr, buf);
186}
187
188/**
189 * Converts the calendar time to a string in format
190 * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
191 *
192 * @param timer Time to convert.
193 * @param buf Buffer to store string to. Must be at least ASCTIME_BUF_LEN
194 * bytes long.
195 * @return Pointer to buf on success, NULL on failure.
196 */
197char *posix_ctime_r(const time_t *timer, char *buf)
198{
199 int r = time_local2str(*timer, buf);
200 if (r != EOK) {
201 errno = r;
202 return NULL;
203 }
204
205 return buf;
206}
207
208/**
209 * Converts the calendar time to a string in format
210 * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
211 * (non reentrant version)
212 *
213 * @param timep Time to convert.
214 * @return Pointer to a statically allocated buffer that stores
215 * the result, NULL in case of error.
216 */
217char *posix_ctime(const time_t *timep)
218{
219 static char buf[ASCTIME_BUF_LEN];
220
221 return posix_ctime_r(timep, buf);
222}
223
224/**
225 * Get clock resolution. Only CLOCK_REALTIME is supported.
226 *
227 * @param clock_id Clock ID.
228 * @param res Pointer to the variable where the resolution is to be written.
229 * @return 0 on success, -1 with errno set on failure.
230 */
231int posix_clock_getres(posix_clockid_t clock_id, struct posix_timespec *res)
232{
233 assert(res != NULL);
234
235 switch (clock_id) {
236 case CLOCK_REALTIME:
237 res->tv_sec = 0;
238 res->tv_nsec = 1000; /* Microsecond resolution. */
239 return 0;
240 default:
241 errno = EINVAL;
242 return -1;
243 }
244}
245
246/**
247 * Get time. Only CLOCK_REALTIME is supported.
248 *
249 * @param clock_id ID of the clock to query.
250 * @param tp Pointer to the variable where the time is to be written.
251 * @return 0 on success, -1 with errno on failure.
252 */
253int posix_clock_gettime(posix_clockid_t clock_id, struct posix_timespec *tp)
254{
255 assert(tp != NULL);
256
257 switch (clock_id) {
258 case CLOCK_REALTIME:
259 ;
260 struct timeval tv;
261 gettimeofday(&tv, NULL);
262 tp->tv_sec = tv.tv_sec;
263 tp->tv_nsec = tv.tv_usec * 1000;
264 return 0;
265 default:
266 errno = EINVAL;
267 return -1;
268 }
269}
270
271/**
272 * Set time on a specified clock. As HelenOS doesn't support this yet,
273 * this function always fails.
274 *
275 * @param clock_id ID of the clock to set.
276 * @param tp Time to set.
277 * @return 0 on success, -1 with errno on failure.
278 */
279int posix_clock_settime(posix_clockid_t clock_id,
280 const struct posix_timespec *tp)
281{
282 assert(tp != NULL);
283
284 switch (clock_id) {
285 case CLOCK_REALTIME:
286 // TODO: setting clock
287 // FIXME: HelenOS doesn't actually support hardware
288 // clock yet
289 errno = EPERM;
290 return -1;
291 default:
292 errno = EINVAL;
293 return -1;
294 }
295}
296
297/**
298 * Sleep on a specified clock.
299 *
300 * @param clock_id ID of the clock to sleep on (only CLOCK_REALTIME supported).
301 * @param flags Flags (none supported).
302 * @param rqtp Sleep time.
303 * @param rmtp Remaining time is written here if sleep is interrupted.
304 * @return 0 on success, -1 with errno set on failure.
305 */
306int posix_clock_nanosleep(posix_clockid_t clock_id, int flags,
307 const struct posix_timespec *rqtp, struct posix_timespec *rmtp)
308{
309 assert(rqtp != NULL);
310 assert(rmtp != NULL);
311
312 switch (clock_id) {
313 case CLOCK_REALTIME:
314 // TODO: interruptible sleep
315 if (rqtp->tv_sec != 0) {
316 fibril_sleep(rqtp->tv_sec);
317 }
318 if (rqtp->tv_nsec != 0) {
319 fibril_usleep(rqtp->tv_nsec / 1000);
320 }
321 return 0;
322 default:
323 errno = EINVAL;
324 return -1;
325 }
326}
327
328/**
329 * Get CPU time used since the process invocation.
330 *
331 * @return Consumed CPU cycles by this process or -1 if not available.
332 */
333posix_clock_t posix_clock(void)
334{
335 posix_clock_t total_cycles = -1;
336 stats_task_t *task_stats = stats_get_task(task_get_id());
337 if (task_stats) {
338 total_cycles = (posix_clock_t) (task_stats->kcycles +
339 task_stats->ucycles);
340 free(task_stats);
341 task_stats = 0;
342 }
343
344 return total_cycles;
345}
346
347/** @}
348 */
Note: See TracBrowser for help on using the repository browser.