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