[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"
|
---|
| 43 | #include "posix/errno.h"
|
---|
| 44 | #include "posix/signal.h"
|
---|
| 45 | #include "posix/assert.h"
|
---|
[324d46b] | 46 |
|
---|
[9a99aa5] | 47 | #include "libc/fibril_synch.h"
|
---|
[06cb827] | 48 | #include "libc/malloc.h"
|
---|
[a6d908c1] | 49 | #include "libc/task.h"
|
---|
| 50 | #include "libc/stats.h"
|
---|
[3e6a98c5] | 51 | #include "libc/stdbool.h"
|
---|
[582a0b8] | 52 | #include "libc/stddef.h"
|
---|
[3f466c33] | 53 | #include "libc/sys/time.h"
|
---|
[2fc5072] | 54 |
|
---|
[324d46b] | 55 | // TODO: test everything in this file
|
---|
| 56 |
|
---|
[e6165be] | 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.
|
---|
| 62 | */
|
---|
| 63 |
|
---|
[324d46b] | 64 | int posix_daylight;
|
---|
| 65 | long posix_timezone;
|
---|
| 66 | char *posix_tzname[2];
|
---|
| 67 |
|
---|
[55b1efd] | 68 | /**
|
---|
| 69 | * Set timezone conversion information.
|
---|
[4cf8ca6] | 70 | */
|
---|
[324d46b] | 71 | void posix_tzset(void)
|
---|
| 72 | {
|
---|
| 73 | // TODO: read environment
|
---|
| 74 | posix_tzname[0] = (char *) "GMT";
|
---|
| 75 | posix_tzname[1] = (char *) "GMT";
|
---|
| 76 | posix_daylight = 0;
|
---|
| 77 | posix_timezone = 0;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[ed29fe4] | 80 | /**
|
---|
| 81 | * Get the time in seconds
|
---|
| 82 | *
|
---|
| 83 | * @param t If t is non-NULL, the return value is also stored in the memory
|
---|
| 84 | * pointed to by t.
|
---|
| 85 | * @return On success, the value of time in seconds since the Epoch
|
---|
| 86 | * is returned. On error, (time_t)-1 is returned.
|
---|
| 87 | */
|
---|
| 88 | time_t posix_time(time_t *t)
|
---|
| 89 | {
|
---|
| 90 | return time(t);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[55b1efd] | 93 | /**
|
---|
| 94 | * Converts a time value to a broken-down UTC time.
|
---|
[4cf8ca6] | 95 | *
|
---|
[e6165be] | 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.
|
---|
[4cf8ca6] | 99 | */
|
---|
[f8b6d34c] | 100 | struct tm *posix_gmtime_r(const time_t *restrict timer,
|
---|
| 101 | struct tm *restrict result)
|
---|
[324d46b] | 102 | {
|
---|
[664fc031] | 103 | int rc = time_utc2tm(*timer, result);
|
---|
[f7ea5400] | 104 | if (rc != EOK) {
|
---|
| 105 | errno = rc;
|
---|
[324d46b] | 106 | return NULL;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | return result;
|
---|
[2fc5072] | 110 | }
|
---|
| 111 |
|
---|
[f7ea5400] | 112 | /**
|
---|
| 113 | * Converts a time value to a broken-down UTC time.
|
---|
| 114 | * (non reentrant version)
|
---|
| 115 | *
|
---|
| 116 | * @param timep Time to convert
|
---|
| 117 | * @return Pointer to a statically allocated structure that stores
|
---|
| 118 | * the result, NULL in case of error.
|
---|
| 119 | */
|
---|
| 120 | struct tm *posix_gmtime(const time_t *restrict timep)
|
---|
| 121 | {
|
---|
| 122 | static struct tm result;
|
---|
| 123 |
|
---|
| 124 | return posix_gmtime_r(timep, &result);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[55b1efd] | 127 | /**
|
---|
| 128 | * Converts a time value to a broken-down local time.
|
---|
[4cf8ca6] | 129 | *
|
---|
[e6165be] | 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.
|
---|
[4cf8ca6] | 133 | */
|
---|
[f8b6d34c] | 134 | struct tm *posix_localtime_r(const time_t *restrict timer,
|
---|
| 135 | struct tm *restrict result)
|
---|
[3f466c33] | 136 | {
|
---|
| 137 | // TODO: deal with timezone
|
---|
| 138 | // currently assumes system and all times are in GMT
|
---|
| 139 | return posix_gmtime_r(timer, result);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[f7ea5400] | 142 | /**
|
---|
| 143 | * Converts a time value to a broken-down local time.
|
---|
| 144 | * (non reentrant version)
|
---|
| 145 | *
|
---|
| 146 | * @param timep Time to convert.
|
---|
| 147 | * @return Pointer to a statically allocated structure that stores
|
---|
| 148 | * the result, NULL in case of error.
|
---|
| 149 | */
|
---|
| 150 | struct tm *posix_localtime(const time_t *restrict timep)
|
---|
| 151 | {
|
---|
| 152 | static struct tm result;
|
---|
| 153 |
|
---|
| 154 | return posix_localtime_r(timep, &result);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[55b1efd] | 157 | /**
|
---|
| 158 | * Converts broken-down time to a string in format
|
---|
| 159 | * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
|
---|
[e6165be] | 160 | *
|
---|
| 161 | * @param timeptr Broken-down time structure.
|
---|
| 162 | * @param buf Buffer to store string to, must be at least ASCTIME_BUF_LEN
|
---|
| 163 | * bytes long.
|
---|
| 164 | * @return Value of buf.
|
---|
[4cf8ca6] | 165 | */
|
---|
[f8b6d34c] | 166 | char *posix_asctime_r(const struct tm *restrict timeptr,
|
---|
[324d46b] | 167 | char *restrict buf)
|
---|
| 168 | {
|
---|
[664fc031] | 169 | time_tm2str(timeptr, buf);
|
---|
[f7ea5400] | 170 | return buf;
|
---|
| 171 | }
|
---|
[324d46b] | 172 |
|
---|
[f7ea5400] | 173 | /**
|
---|
| 174 | * Convers broken-down time to a string in format
|
---|
| 175 | * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
|
---|
| 176 | * (non reentrant version)
|
---|
| 177 | *
|
---|
| 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.
|
---|
| 181 | */
|
---|
| 182 | char *posix_asctime(const struct tm *restrict timeptr)
|
---|
| 183 | {
|
---|
| 184 | static char buf[ASCTIME_BUF_LEN];
|
---|
[324d46b] | 185 |
|
---|
[f7ea5400] | 186 | return posix_asctime_r(timeptr, buf);
|
---|
[2fc5072] | 187 | }
|
---|
| 188 |
|
---|
[55b1efd] | 189 | /**
|
---|
[f7ea5400] | 190 | * Converts the calendar time to a string in format
|
---|
| 191 | * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
|
---|
[4cf8ca6] | 192 | *
|
---|
[e6165be] | 193 | * @param timer Time to convert.
|
---|
| 194 | * @param buf Buffer to store string to. Must be at least ASCTIME_BUF_LEN
|
---|
| 195 | * bytes long.
|
---|
[f7ea5400] | 196 | * @return Pointer to buf on success, NULL on failure.
|
---|
[4cf8ca6] | 197 | */
|
---|
[3f466c33] | 198 | char *posix_ctime_r(const time_t *timer, char *buf)
|
---|
| 199 | {
|
---|
[664fc031] | 200 | int r = time_local2str(*timer, buf);
|
---|
[f7ea5400] | 201 | if (r != EOK) {
|
---|
| 202 | errno = r;
|
---|
[3f466c33] | 203 | return NULL;
|
---|
| 204 | }
|
---|
[f7ea5400] | 205 |
|
---|
| 206 | return buf;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | /**
|
---|
| 210 | * Converts the calendar time to a string in format
|
---|
| 211 | * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
|
---|
| 212 | * (non reentrant version)
|
---|
| 213 | *
|
---|
| 214 | * @param timep Time to convert.
|
---|
| 215 | * @return Pointer to a statically allocated buffer that stores
|
---|
| 216 | * the result, NULL in case of error.
|
---|
| 217 | */
|
---|
| 218 | char *posix_ctime(const time_t *timep)
|
---|
| 219 | {
|
---|
| 220 | static char buf[ASCTIME_BUF_LEN];
|
---|
| 221 |
|
---|
| 222 | return posix_ctime_r(timep, buf);
|
---|
[2fc5072] | 223 | }
|
---|
| 224 |
|
---|
[55b1efd] | 225 | /**
|
---|
| 226 | * Get clock resolution. Only CLOCK_REALTIME is supported.
|
---|
[4cf8ca6] | 227 | *
|
---|
[e6165be] | 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.
|
---|
[4cf8ca6] | 231 | */
|
---|
[3f466c33] | 232 | int posix_clock_getres(posix_clockid_t clock_id, struct posix_timespec *res)
|
---|
| 233 | {
|
---|
| 234 | assert(res != NULL);
|
---|
| 235 |
|
---|
| 236 | switch (clock_id) {
|
---|
| 237 | case CLOCK_REALTIME:
|
---|
| 238 | res->tv_sec = 0;
|
---|
| 239 | res->tv_nsec = 1000; /* Microsecond resolution. */
|
---|
| 240 | return 0;
|
---|
| 241 | default:
|
---|
| 242 | errno = EINVAL;
|
---|
| 243 | return -1;
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[55b1efd] | 247 | /**
|
---|
| 248 | * Get time. Only CLOCK_REALTIME is supported.
|
---|
[4cf8ca6] | 249 | *
|
---|
[e6165be] | 250 | * @param clock_id ID of the clock to query.
|
---|
| 251 | * @param tp Pointer to the variable where the time is to be written.
|
---|
[55b1efd] | 252 | * @return 0 on success, -1 with errno on failure.
|
---|
[4cf8ca6] | 253 | */
|
---|
[3f466c33] | 254 | int posix_clock_gettime(posix_clockid_t clock_id, struct posix_timespec *tp)
|
---|
| 255 | {
|
---|
| 256 | assert(tp != NULL);
|
---|
| 257 |
|
---|
| 258 | switch (clock_id) {
|
---|
| 259 | case CLOCK_REALTIME:
|
---|
| 260 | ;
|
---|
| 261 | struct timeval tv;
|
---|
| 262 | gettimeofday(&tv, NULL);
|
---|
| 263 | tp->tv_sec = tv.tv_sec;
|
---|
| 264 | tp->tv_nsec = tv.tv_usec * 1000;
|
---|
| 265 | return 0;
|
---|
| 266 | default:
|
---|
| 267 | errno = EINVAL;
|
---|
| 268 | return -1;
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[55b1efd] | 272 | /**
|
---|
| 273 | * Set time on a specified clock. As HelenOS doesn't support this yet,
|
---|
| 274 | * this function always fails.
|
---|
[4cf8ca6] | 275 | *
|
---|
[e6165be] | 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.
|
---|
[4cf8ca6] | 279 | */
|
---|
[3f466c33] | 280 | int posix_clock_settime(posix_clockid_t clock_id,
|
---|
| 281 | const struct posix_timespec *tp)
|
---|
| 282 | {
|
---|
| 283 | assert(tp != NULL);
|
---|
| 284 |
|
---|
| 285 | switch (clock_id) {
|
---|
| 286 | case CLOCK_REALTIME:
|
---|
| 287 | // TODO: setting clock
|
---|
| 288 | // FIXME: HelenOS doesn't actually support hardware
|
---|
| 289 | // clock yet
|
---|
| 290 | errno = EPERM;
|
---|
| 291 | return -1;
|
---|
| 292 | default:
|
---|
| 293 | errno = EINVAL;
|
---|
| 294 | return -1;
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
| 297 |
|
---|
[55b1efd] | 298 | /**
|
---|
| 299 | * Sleep on a specified clock.
|
---|
[4cf8ca6] | 300 | *
|
---|
[e6165be] | 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.
|
---|
[4cf8ca6] | 306 | */
|
---|
[3f466c33] | 307 | int posix_clock_nanosleep(posix_clockid_t clock_id, int flags,
|
---|
| 308 | const struct posix_timespec *rqtp, struct posix_timespec *rmtp)
|
---|
| 309 | {
|
---|
| 310 | assert(rqtp != NULL);
|
---|
| 311 | assert(rmtp != NULL);
|
---|
| 312 |
|
---|
| 313 | switch (clock_id) {
|
---|
| 314 | case CLOCK_REALTIME:
|
---|
| 315 | // TODO: interruptible sleep
|
---|
| 316 | if (rqtp->tv_sec != 0) {
|
---|
[9a99aa5] | 317 | fibril_sleep(rqtp->tv_sec);
|
---|
[3f466c33] | 318 | }
|
---|
| 319 | if (rqtp->tv_nsec != 0) {
|
---|
[9a99aa5] | 320 | fibril_usleep(rqtp->tv_nsec / 1000);
|
---|
[3f466c33] | 321 | }
|
---|
| 322 | return 0;
|
---|
| 323 | default:
|
---|
| 324 | errno = EINVAL;
|
---|
| 325 | return -1;
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[55b1efd] | 329 | /**
|
---|
| 330 | * Get CPU time used since the process invocation.
|
---|
[06cb827] | 331 | *
|
---|
| 332 | * @return Consumed CPU cycles by this process or -1 if not available.
|
---|
[823a929] | 333 | */
|
---|
| 334 | posix_clock_t posix_clock(void)
|
---|
| 335 | {
|
---|
[06cb827] | 336 | posix_clock_t total_cycles = -1;
|
---|
| 337 | stats_task_t *task_stats = stats_get_task(task_get_id());
|
---|
| 338 | if (task_stats) {
|
---|
[cb948777] | 339 | total_cycles = (posix_clock_t) (task_stats->kcycles +
|
---|
| 340 | task_stats->ucycles);
|
---|
[a12f7f1] | 341 | free(task_stats);
|
---|
| 342 | task_stats = 0;
|
---|
[06cb827] | 343 | }
|
---|
| 344 |
|
---|
| 345 | return total_cycles;
|
---|
[823a929] | 346 | }
|
---|
| 347 |
|
---|
[2fc5072] | 348 | /** @}
|
---|
| 349 | */
|
---|