| 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 | #define LIBPOSIX_INTERNAL
|
|---|
| 37 |
|
|---|
| 38 | /* Must be first. */
|
|---|
| 39 | #include "posix/stdbool.h"
|
|---|
| 40 |
|
|---|
| 41 | #include "internal/common.h"
|
|---|
| 42 | #include "posix/time.h"
|
|---|
| 43 |
|
|---|
| 44 | #include "posix/ctype.h"
|
|---|
| 45 | #include "posix/errno.h"
|
|---|
| 46 | #include "posix/signal.h"
|
|---|
| 47 | #include "posix/assert.h"
|
|---|
| 48 |
|
|---|
| 49 | #include "libc/malloc.h"
|
|---|
| 50 | #include "libc/task.h"
|
|---|
| 51 | #include "libc/stats.h"
|
|---|
| 52 | #include "libc/sys/time.h"
|
|---|
| 53 |
|
|---|
| 54 | // TODO: test everything in this file
|
|---|
| 55 |
|
|---|
| 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 |
|
|---|
| 63 | int posix_daylight;
|
|---|
| 64 | long posix_timezone;
|
|---|
| 65 | char *posix_tzname[2];
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * Set timezone conversion information.
|
|---|
| 69 | */
|
|---|
| 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 |
|
|---|
| 79 | /**
|
|---|
| 80 | * Converts a time value to a broken-down UTC time.
|
|---|
| 81 | *
|
|---|
| 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.
|
|---|
| 85 | */
|
|---|
| 86 | struct tm *posix_gmtime_r(const time_t *restrict timer,
|
|---|
| 87 | struct tm *restrict result)
|
|---|
| 88 | {
|
|---|
| 89 | int rc = time_utc2tm(*timer, result);
|
|---|
| 90 | if (rc != EOK) {
|
|---|
| 91 | errno = rc;
|
|---|
| 92 | return NULL;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | return result;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 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 |
|
|---|
| 113 | /**
|
|---|
| 114 | * Converts a time value to a broken-down local time.
|
|---|
| 115 | *
|
|---|
| 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.
|
|---|
| 119 | */
|
|---|
| 120 | struct tm *posix_localtime_r(const time_t *restrict timer,
|
|---|
| 121 | struct tm *restrict result)
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 143 | /**
|
|---|
| 144 | * Converts broken-down time to a string in format
|
|---|
| 145 | * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
|
|---|
| 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.
|
|---|
| 151 | */
|
|---|
| 152 | char *posix_asctime_r(const struct tm *restrict timeptr,
|
|---|
| 153 | char *restrict buf)
|
|---|
| 154 | {
|
|---|
| 155 | time_tm2str(timeptr, buf);
|
|---|
| 156 | return buf;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 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];
|
|---|
| 171 |
|
|---|
| 172 | return posix_asctime_r(timeptr, buf);
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | /**
|
|---|
| 176 | * Converts the calendar time to a string in format
|
|---|
| 177 | * "Sun Jan 1 00:00:00 1970\n" (Obsolete)
|
|---|
| 178 | *
|
|---|
| 179 | * @param timer Time to convert.
|
|---|
| 180 | * @param buf Buffer to store string to. Must be at least ASCTIME_BUF_LEN
|
|---|
| 181 | * bytes long.
|
|---|
| 182 | * @return Pointer to buf on success, NULL on failure.
|
|---|
| 183 | */
|
|---|
| 184 | char *posix_ctime_r(const time_t *timer, char *buf)
|
|---|
| 185 | {
|
|---|
| 186 | int r = time_local2str(*timer, buf);
|
|---|
| 187 | if (r != EOK) {
|
|---|
| 188 | errno = r;
|
|---|
| 189 | return NULL;
|
|---|
| 190 | }
|
|---|
| 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);
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /**
|
|---|
| 212 | * Get clock resolution. Only CLOCK_REALTIME is supported.
|
|---|
| 213 | *
|
|---|
| 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.
|
|---|
| 217 | */
|
|---|
| 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 |
|
|---|
| 233 | /**
|
|---|
| 234 | * Get time. Only CLOCK_REALTIME is supported.
|
|---|
| 235 | *
|
|---|
| 236 | * @param clock_id ID of the clock to query.
|
|---|
| 237 | * @param tp Pointer to the variable where the time is to be written.
|
|---|
| 238 | * @return 0 on success, -1 with errno on failure.
|
|---|
| 239 | */
|
|---|
| 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 |
|
|---|
| 258 | /**
|
|---|
| 259 | * Set time on a specified clock. As HelenOS doesn't support this yet,
|
|---|
| 260 | * this function always fails.
|
|---|
| 261 | *
|
|---|
| 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.
|
|---|
| 265 | */
|
|---|
| 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 |
|
|---|
| 284 | /**
|
|---|
| 285 | * Sleep on a specified clock.
|
|---|
| 286 | *
|
|---|
| 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.
|
|---|
| 292 | */
|
|---|
| 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 |
|
|---|
| 315 | /**
|
|---|
| 316 | * Get CPU time used since the process invocation.
|
|---|
| 317 | *
|
|---|
| 318 | * @return Consumed CPU cycles by this process or -1 if not available.
|
|---|
| 319 | */
|
|---|
| 320 | posix_clock_t posix_clock(void)
|
|---|
| 321 | {
|
|---|
| 322 | posix_clock_t total_cycles = -1;
|
|---|
| 323 | stats_task_t *task_stats = stats_get_task(task_get_id());
|
|---|
| 324 | if (task_stats) {
|
|---|
| 325 | total_cycles = (posix_clock_t) (task_stats->kcycles +
|
|---|
| 326 | task_stats->ucycles);
|
|---|
| 327 | free(task_stats);
|
|---|
| 328 | task_stats = 0;
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | return total_cycles;
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | /** @}
|
|---|
| 335 | */
|
|---|