Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/timestamp.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "test.h"
20 #include <pj/os.h>
21 #include <pj/log.h>
22 #include <pj/rand.h>
23
24
25 /**
26 * \page page_pjlib_timestamp_test Test: Timestamp
27 *
28 * This file provides implementation of timestamp_test()
29 *
30 * \section timestamp_test_sec Scope of the Test
31 *
32 * This tests whether timestamp API works.
33 *
34 * API tested:
35 * - pj_get_timestamp_freq()
36 * - pj_get_timestamp()
37 * - pj_elapsed_usec()
38 * - PJ_LOG()
39 *
40 *
41 * This file is <b>pjlib-test/timestamp.c</b>
42 *
43 * \include pjlib-test/timestamp.c
44 */
45
46 #if INCLUDE_TIMESTAMP_TEST
47
48 #define THIS_FILE "timestamp"
49
50 static int timestamp_accuracy()
51 {
52 pj_timestamp freq, t1, t2;
53 pj_time_val tv1, tv2, tvtmp;
54 pj_int64_t msec, tics;
55 pj_int64_t diff;
56
57 PJ_LOG(3,(THIS_FILE, "...testing frequency accuracy (pls wait)"));
58
59 pj_get_timestamp_freq(&freq);
60
61 /* Get the start time */
62 pj_gettimeofday(&tvtmp);
63 do {
64 pj_gettimeofday(&tv1);
65 } while (PJ_TIME_VAL_EQ(tvtmp, tv1));
66 pj_get_timestamp(&t1);
67
68 /* Sleep for 10 seconds */
69 pj_thread_sleep(10000);
70
71 /* Get end time */
72 pj_gettimeofday(&tvtmp);
73 do {
74 pj_gettimeofday(&tv2);
75 } while (PJ_TIME_VAL_EQ(tvtmp, tv2));
76 pj_get_timestamp(&t2);
77
78 /* Get the elapsed time */
79 PJ_TIME_VAL_SUB(tv2, tv1);
80 msec = PJ_TIME_VAL_MSEC(tv2);
81
82 /* Check that the frequency match the elapsed time */
83 tics = t2.u64 - t1.u64;
84 diff = tics - (msec * freq.u64 / 1000);
85 if (diff < 0)
86 diff = -diff;
87
88 /* In CI mode, allow max 10 msec mismatch */
89 if (test_app.ut_app.prm_ci_mode) {
90 if (diff > (pj_int64_t)(freq.u64 / 100)) {
91 PJ_LOG(3,(THIS_FILE, "....error: timestamp drifted by %d usec after "
92 "%d msec",
93 (pj_uint32_t)(diff * 1000000 / freq.u64),
94 (int)msec));
95 return -2001;
96 }
97
98 /* Only allow 1 msec mismatch */
99 } else if (diff > (pj_int64_t)(freq.u64 / 1000)) {
100 PJ_LOG(3,(THIS_FILE, "....error: timestamp drifted by %d usec after "
101 "%d msec",
102 (pj_uint32_t)(diff * 1000000 / freq.u64),
103 (int)msec));
104 return -2000;
105
106 /* Otherwise just print warning if timestamp drifted by >1 usec */
107 } else if (diff > (pj_int64_t)(freq.u64 / 1000000)) {
108 PJ_LOG(3,(THIS_FILE, "....warning: timestamp drifted by %d usec after "
109 "%d msec",
110 (pj_uint32_t)(diff * 1000000 / freq.u64),
111 (int)msec));
112 } else {
113 PJ_LOG(3,(THIS_FILE, "....good. Timestamp is accurate down to"
114 " nearest usec."));
115 }
116
117 return 0;
118 }
119
120
121 int timestamp_test(void)
122 {
123 enum { CONSECUTIVE_LOOP = 100 };
124 volatile unsigned i;
125 pj_timestamp freq, t1, t2;
126 pj_time_val tv1, tv2;
127 unsigned elapsed;
128 pj_status_t rc;
129
130 PJ_LOG(3,(THIS_FILE, "...Testing timestamp (high res time)"));
131
132 /* Get and display timestamp frequency. */
133 if ((rc=pj_get_timestamp_freq(&freq)) != PJ_SUCCESS) {
134 app_perror("...ERROR: get timestamp freq", rc);
135 return -1000;
136 }
137
138 PJ_LOG(3,(THIS_FILE, "....frequency: hiword=%u loword=%u",
139 freq.u32.hi, freq.u32.lo));
140
141 PJ_LOG(3,(THIS_FILE, "...checking if time can run backwards (pls wait).."));
142
143 /*
144 * Check if consecutive readings should yield timestamp value
145 * that is bigger than previous value.
146 * First we get the first timestamp.
147 */
148 rc = pj_get_timestamp(&t1);
149 if (rc != PJ_SUCCESS) {
150 app_perror("...ERROR: pj_get_timestamp", rc);
151 return -1001;
152 }
153 rc = pj_gettimeofday(&tv1);
154 if (rc != PJ_SUCCESS) {
155 app_perror("...ERROR: pj_gettimeofday", rc);
156 return -1002;
157 }
158 for (i=0; i<CONSECUTIVE_LOOP; ++i) {
159
160 pj_thread_sleep(pj_rand() % 100);
161
162 rc = pj_get_timestamp(&t2);
163 if (rc != PJ_SUCCESS) {
164 app_perror("...ERROR: pj_get_timestamp", rc);
165 return -1003;
166 }
167 rc = pj_gettimeofday(&tv2);
168 if (rc != PJ_SUCCESS) {
169 app_perror("...ERROR: pj_gettimeofday", rc);
170 return -1004;
171 }
172
173 /* compare t2 with t1, expecting t2 >= t1. */
174 if (t2.u32.hi < t1.u32.hi ||
175 (t2.u32.hi == t1.u32.hi && t2.u32.lo < t1.u32.lo))
176 {
177 PJ_LOG(3,(THIS_FILE, "...ERROR: timestamp run backwards!"));
178 return -1005;
179 }
180
181 /* compare tv2 with tv1, expecting tv2 >= tv1. */
182 if (PJ_TIME_VAL_LT(tv2, tv1)) {
183 PJ_LOG(3,(THIS_FILE, "...ERROR: time run backwards!"));
184 return -1006;
185 }
186 }
187
188 /*
189 * Simple test to time some loop.
190 */
191 PJ_LOG(3,(THIS_FILE, "....testing simple 1000000 loop"));
192
193
194 /* Mark start time. */
195 if ((rc=pj_get_timestamp(&t1)) != PJ_SUCCESS) {
196 app_perror("....error: cat't get timestamp", rc);
197 return -1010;
198 }
199
200 /* Loop.. */
201 for (i=0; i<1000000; ++i) {
202 /* Try to do something so that smart compilers wont
203 * remove this silly loop.
204 */
205 null_func();
206 }
207
208 pj_thread_sleep(0);
209
210 /* Mark end time. */
211 pj_get_timestamp(&t2);
212
213 /* Get elapsed time in usec. */
214 elapsed = pj_elapsed_usec(&t1, &t2);
215 PJ_LOG(3,(THIS_FILE, "....elapsed: %u usec", (unsigned)elapsed));
216
217 /* See if elapsed time is "reasonable".
218 * This should be good even on 50Mhz embedded powerpc.
219 */
220 if (elapsed < 1 || elapsed > 1000000) {
221 PJ_LOG(3,(THIS_FILE, "....error: elapsed time outside window (%u, "
222 "t1.u32.hi=%u, t1.u32.lo=%u, "
223 "t2.u32.hi=%u, t2.u32.lo=%u)",
224 elapsed,
225 t1.u32.hi, t1.u32.lo, t2.u32.hi, t2.u32.lo));
226 return -1030;
227 }
228
229 /* Testing time/timestamp accuracy */
230 rc = timestamp_accuracy();
231 if (rc != 0)
232 return rc;
233
234 return 0;
235 }
236
237
238 #else
239 /* To prevent warning about "translation unit is empty"
240 * when this test is disabled.
241 */
242 int dummy_timestamp_test;
243 #endif /* INCLUDE_TIMESTAMP_TEST */
244