Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / native_client / tests / clock / clock_irt_test.c
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 #include <inttypes.h>
8 #include <stdio.h>
9 #include <time.h>
10 #include <sys/types.h>
11 #include <sys/time.h>
12
13 #include "native_client/src/untrusted/irt/irt.h"
14
15 #define DEFERED_STRINGIFY(symbol) #symbol
16 #define SHOW(symbol)                                  \
17   do {                                                \
18     printf("Current definition for %s is \"%s\".\n",  \
19            #symbol, DEFERED_STRINGIFY(symbol));       \
20   } while (0)
21
22 void ShowCurrentDefinitions(void) {
23   SHOW(CLOCK_REALTIME);
24   SHOW(CLOCK_MONOTONIC);
25   SHOW(CLOCK_PROCESS_CPUTIME_ID);
26   SHOW(CLOCK_THREAD_CPUTIME_ID);
27 }
28 /*
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
36  * others.)
37  *
38  * TODO(bsy): remove out when the toolchains have correct definitions
39  * for these preprocessor symbols.
40  */
41 #undef CLOCK_REALTIME
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)
49
50 struct timespec;
51
52 /*
53  * Basic functionality test: the syscalls are present.
54  */
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) {
59   struct timespec       ts;
60
61   if (0 != (*func)(clk_id, &ts)) {
62     fprintf(stderr, "%s\n", error_string);
63     return 1;
64   }
65   printf("%30s: %lld.%09lu\n", success_name,
66          (int64_t) ts.tv_sec, (unsigned long) ts.tv_nsec);
67   return 0;
68 }
69
70 int main(void) {
71   struct nacl_irt_clock ti;
72   int                   errs = 0;
73
74   ShowCurrentDefinitions();
75
76   if (0 == nacl_interface_query(NACL_IRT_CLOCK_v0_1, &ti, sizeof(ti))) {
77     fprintf(stderr, "IRT hook is not available\n");
78     return 1;
79   }
80
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");
105   return errs;
106 }