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