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