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