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.
10 #include <sys/types.h>
13 #include "native_client/src/untrusted/irt/irt.h"
15 #define DEFERED_STRINGIFY(symbol) #symbol
16 #define SHOW(symbol) \
18 printf("Current definition for %s is \"%s\".\n", \
19 #symbol, DEFERED_STRINGIFY(symbol)); \
22 void ShowCurrentDefinitions(void) {
24 SHOW(CLOCK_MONOTONIC);
25 SHOW(CLOCK_PROCESS_CPUTIME_ID);
26 SHOW(CLOCK_THREAD_CPUTIME_ID);
29 * We would normally just use the values of CLOCK_REALTIME, etc from
30 * <time.h> as required by POSIX. Unfortunately, the POSIX-required
31 * names CLOCK_REALTIME are different in the newlib toolchain from the
32 * values used by the service runtime -- and from the values used in
33 * the glibc toolchain! -- so we have to have the following hack
34 * until the toolchains are fixed (and DEPS rolled) to make it all
35 * consistent. (Newlib defines CLOCK_REALTIME but none of the
38 * TODO(bsy): remove out when the toolchains have correct definitions
39 * for these preprocessor symbols.
42 #undef CLOCK_MONOTONIC
43 #undef CLOCK_PROCESS_CPUTIME_ID
44 #undef CLOCK_THREAD_CPUTIME_ID
45 #define CLOCK_REALTIME (0)
46 #define CLOCK_MONOTONIC (1)
47 #define CLOCK_PROCESS_CPUTIME_ID (2)
48 #define CLOCK_THREAD_CPUTIME_ID (3)
53 * Basic functionality test: the syscalls are present.
55 int TimeTest(int (*func)(nacl_irt_clockid_t clk_id, struct timespec *ts),
56 nacl_irt_clockid_t clk_id,
57 char const *error_string,
58 char const *success_name) {
61 if (0 != (*func)(clk_id, &ts)) {
62 fprintf(stderr, "%s\n", error_string);
65 printf("%30s: %lld.%09lu\n", success_name,
66 (int64_t) ts.tv_sec, (unsigned long) ts.tv_nsec);
71 struct nacl_irt_clock ti;
74 ShowCurrentDefinitions();
76 if (0 == nacl_interface_query(NACL_IRT_CLOCK_v0_1, &ti, sizeof(ti))) {
77 fprintf(stderr, "IRT hook is not available\n");
81 errs += TimeTest(ti.clock_getres, CLOCK_REALTIME,
82 "clock_getres on realtime clock failed",
83 "Realtime clock resolution");
84 errs += TimeTest(ti.clock_getres, CLOCK_MONOTONIC,
85 "clock_getres on monotonic clock failed",
86 "Monotonic clock resolution");
87 errs += TimeTest(ti.clock_getres, CLOCK_PROCESS_CPUTIME_ID,
88 "clock_getres on process CPU-time clock failed",
89 "Process CPU-time clock resolution");
90 errs += TimeTest(ti.clock_getres, CLOCK_THREAD_CPUTIME_ID,
91 "clock_getres on thread CPU-time clock failed",
92 "Thread CPU-time clock resolution");
93 errs += TimeTest(ti.clock_gettime, CLOCK_REALTIME,
94 "clock_gettime on realtime clock failed",
95 "Realtime clock value");
96 errs += TimeTest(ti.clock_gettime, CLOCK_MONOTONIC,
97 "clock_gettime on monotonic clock failed",
98 "Monotonic clock value");
99 errs += TimeTest(ti.clock_gettime, CLOCK_PROCESS_CPUTIME_ID,
100 "clock_gettime on process CPU-time clock failed",
101 "Process CPU-time clock value");
102 errs += TimeTest(ti.clock_gettime, CLOCK_THREAD_CPUTIME_ID,
103 "clock_gettime on thread CPU-time clock failed",
104 "Thread CPU-time clock value");