1 /*
2 * Copyright (C) 2012-2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <inttypes.h>
18 #include <lk/macros.h>
19 #include <stddef.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <trusty/time.h>
24
25 #include <lib/unittest/unittest.h>
26 #include <trusty_unittest.h>
27
28 #define TLOG_TAG "timertest"
29
nop(void)30 static void __attribute__((noinline)) nop(void) {
31 __UNUSED static int i;
32 i++;
33 }
34
35 struct timer_unittest {
36 struct unittest unittest;
37 bool loop;
38 };
39
40 /* Timer constants (nanoseconds) */
41 #define ONE_US (1000ULL)
42 #define ONE_MS (1000ULL * ONE_US)
43 #define ONE_S (1000ULL * ONE_MS)
44
45 #define TIMER_TEST_NOP_LOOP_COUNT (100000000)
46 #define TIMER_TEST_MS_SLEEP_LOOP_COUNT (1000)
47
check_timestamps(int64_t t1,int64_t delta_min,int64_t delta_max,const char * name)48 static void check_timestamps(int64_t t1,
49 int64_t delta_min,
50 int64_t delta_max,
51 const char* name) {
52 int64_t delta;
53 int64_t t2 = t1 - 1;
54
55 trusty_gettime(0, &t2);
56 delta = t2 - t1;
57
58 EXPECT_EQ(false, (delta < delta_min || delta > delta_max),
59 "bad timestamp after %s: t1 %" PRId64 ", t2 %" PRId64
60 ", delta %" PRId64 ", min %" PRId64 " max %" PRId64 "\n",
61 name, t1, t2, t2 - t1, delta_min, delta_max);
62 }
63
TEST(TimerTest,BusyLoop)64 TEST(TimerTest, BusyLoop) {
65 int i;
66 int64_t ts = 0;
67
68 trusty_gettime(0, &ts);
69 for (i = 0; i < TIMER_TEST_NOP_LOOP_COUNT; i++)
70 nop();
71 check_timestamps(ts, TIMER_TEST_NOP_LOOP_COUNT / 100,
72 TIMER_TEST_NOP_LOOP_COUNT * 10000ULL, "nop loop");
73 }
74
TEST(TimerTest,NanoSleepOneMilliSecond)75 TEST(TimerTest, NanoSleepOneMilliSecond) {
76 int i;
77 int64_t ts = 0;
78
79 trusty_gettime(0, &ts);
80 for (i = 0; i < TIMER_TEST_MS_SLEEP_LOOP_COUNT; i++)
81 trusty_nanosleep(0, 0, ONE_MS);
82 check_timestamps(ts, TIMER_TEST_MS_SLEEP_LOOP_COUNT * ONE_MS,
83 TIMER_TEST_MS_SLEEP_LOOP_COUNT * ONE_MS * 10, "ms loop");
84 }
85
TEST(TimerTest,NanoSleepTenSeconds)86 TEST(TimerTest, NanoSleepTenSeconds) {
87 int64_t ts = 0;
88
89 trusty_gettime(0, &ts);
90 trusty_nanosleep(0, 0, 10ULL * ONE_S);
91 check_timestamps(ts, ONE_S * 10, ONE_S * 11, "10s sleep");
92 }
93
timer_test(struct unittest * test)94 static bool timer_test(struct unittest* test) {
95 struct timer_unittest* timer_test =
96 containerof(test, struct timer_unittest, unittest);
97 bool passed;
98
99 do {
100 passed = RUN_ALL_SUITE_TESTS("TimerTest");
101 } while (timer_test->loop);
102
103 return passed;
104 }
105
106 /* Repeatedly expire a 1 micro-second timer for about 60 seconds */
TEST(TimerStressTest,NanoSleepStressTestSixtySeconds)107 TEST(TimerStressTest, NanoSleepStressTestSixtySeconds) {
108 int64_t end = 0, now = 0;
109 int i, remaining, last_remaining = 0;
110
111 trusty_gettime(0, &now);
112 end = now + (ONE_S * 60);
113
114 while (now < end) {
115 remaining = (end - now) / ONE_S;
116 if (remaining != last_remaining) {
117 trusty_unittest_printf("[ INFO ] remaining %ds\n", remaining);
118 last_remaining = remaining;
119 }
120
121 for (i = 0; i < 8192; i++) {
122 trusty_nanosleep(0, 0, ONE_US);
123 }
124
125 trusty_gettime(0, &now);
126 }
127 }
128
timer_stress_test(struct unittest * test)129 static bool timer_stress_test(struct unittest* test) {
130 struct timer_unittest* timer_test =
131 containerof(test, struct timer_unittest, unittest);
132 bool passed;
133
134 do {
135 passed = RUN_ALL_SUITE_TESTS("TimerStressTest");
136 } while (timer_test->loop);
137
138 return passed;
139 }
140
141 #define PORT_BASE "com.android.timer-unittest"
142
main(void)143 int main(void) {
144 static struct timer_unittest timer_unittests[] = {
145 {
146 .unittest =
147 {
148 .port_name = PORT_BASE,
149 .run_test = timer_test,
150 },
151 .loop = false,
152 },
153 {
154 .unittest =
155 {
156 .port_name = PORT_BASE ".loop",
157 .run_test = timer_test,
158 },
159 .loop = true,
160 },
161 {
162 .unittest =
163 {
164 .port_name = PORT_BASE ".stress",
165 .run_test = timer_stress_test,
166 },
167 .loop = false,
168 }};
169 static struct unittest* unittests[countof(timer_unittests)];
170
171 for (size_t i = 0; i < countof(timer_unittests); i++) {
172 unittests[i] = &timer_unittests[i].unittest;
173 }
174
175 return unittest_main(unittests, countof(unittests));
176 }
177