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