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