1 : /*
2 : * Copyright (c) 2012 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can be
4 : * found in the LICENSE file.
5 : */
6 :
7 : /*
8 : * NaCl Server Runtime time abstraction layer.
9 : * This is the host-OS-dependent implementation.
10 : */
11 :
12 : #include <sys/time.h>
13 : #include <time.h>
14 :
15 : #include "native_client/src/include/nacl_macros.h"
16 : #include "native_client/src/shared/platform/nacl_log.h"
17 : #include "native_client/src/shared/platform/nacl_time.h"
18 : #include "native_client/src/shared/platform/posix/nacl_time_types.h"
19 :
20 : #include "native_client/src/trusted/service_runtime/linux/nacl_syscall_inl.h"
21 :
22 : #define NANOS_PER_UNIT (1000*1000*1000)
23 :
24 : static struct NaClTimeState gNaClTimeState;
25 :
26 0 : void NaClAllowLowResolutionTimeOfDay(void) {
27 : /* Always use high resolution timer. */
28 0 : }
29 :
30 353 : void NaClTimeInternalInit(struct NaClTimeState *ntsp) {
31 353 : ntsp->time_resolution_ns = NACL_NANOS_PER_MICRO;
32 353 : }
33 :
34 59 : void NaClTimeInternalFini(struct NaClTimeState *ntsp) {
35 : UNREFERENCED_PARAMETER(ntsp);
36 59 : }
37 :
38 0 : uint64_t NaClTimerResolutionNsInternal(struct NaClTimeState *ntsp) {
39 0 : return ntsp->time_resolution_ns;
40 : }
41 :
42 353 : void NaClTimeInit(void) {
43 353 : NaClTimeInternalInit(&gNaClTimeState);
44 353 : }
45 :
46 59 : void NaClTimeFini(void) {
47 59 : NaClTimeInternalFini(&gNaClTimeState);
48 59 : }
49 :
50 0 : uint64_t NaClTimerResolutionNanoseconds(void) {
51 0 : return NaClTimerResolutionNsInternal(&gNaClTimeState);
52 : }
53 :
54 7708 : int NaClGetTimeOfDay(struct nacl_abi_timeval *tv) {
55 : struct timeval sys_tv;
56 : int retval;
57 :
58 7708 : retval = gettimeofday(&sys_tv, NULL);
59 7708 : if (0 == retval) {
60 7708 : tv->nacl_abi_tv_sec = sys_tv.tv_sec;
61 7708 : tv->nacl_abi_tv_usec = sys_tv.tv_usec;
62 : }
63 :
64 7708 : retval = NaClXlateSysRet(retval);
65 7708 : return retval;
66 : }
67 :
68 208 : int NaClNanosleep(struct nacl_abi_timespec const *req,
69 : struct nacl_abi_timespec *rem) {
70 : struct timespec host_req;
71 : struct timespec host_rem;
72 : struct timespec *host_remptr;
73 : int retval;
74 :
75 208 : host_req.tv_sec = req->tv_sec;
76 208 : host_req.tv_nsec = req->tv_nsec;
77 208 : if (NULL == rem) {
78 147 : host_remptr = NULL;
79 : } else {
80 61 : host_remptr = &host_rem;
81 : }
82 208 : NaClLog(4,
83 : "nanosleep(%"NACL_PRIxPTR", %"NACL_PRIxPTR")\n",
84 : (uintptr_t) &host_req,
85 : (uintptr_t) host_remptr);
86 412 : NaClLog(4, "nanosleep(time = %"NACL_PRId64".%09"NACL_PRId64" S)\n",
87 412 : (int64_t) host_req.tv_sec, (int64_t) host_req.tv_nsec);
88 206 : if (host_req.tv_nsec > NANOS_PER_UNIT) {
89 0 : NaClLog(4, "tv_nsec too large %"NACL_PRId64"\n",
90 0 : (int64_t) host_req.tv_nsec);
91 : }
92 206 : retval = nanosleep(&host_req, host_remptr);
93 203 : NaClLog(4, " returned %d\n", retval);
94 :
95 202 : if (0 != retval && EINTR == errno && NULL != rem) {
96 0 : rem->tv_sec = host_rem.tv_sec;
97 0 : rem->tv_nsec = host_rem.tv_nsec;
98 : }
99 202 : retval = NaClXlateSysRet(retval);
100 202 : return retval;
101 : }
|