1
/* $Id: timestamp.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
27
* \page page_pjlib_timestamp_test Test: Timestamp
29
* This file provides implementation of timestamp_test()
31
* \section timestamp_test_sec Scope of the Test
33
* This tests whether timestamp API works.
36
* - pj_get_timestamp_freq()
37
* - pj_get_timestamp()
42
* This file is <b>pjlib-test/timestamp.c</b>
44
* \include pjlib-test/timestamp.c
47
#if INCLUDE_TIMESTAMP_TEST
49
#define THIS_FILE "timestamp"
51
static int timestamp_accuracy()
53
pj_timestamp freq, t1, t2;
54
pj_time_val tv1, tv2, tvtmp;
55
pj_int64_t msec, tics;
58
PJ_LOG(3,(THIS_FILE, "...testing frequency accuracy (pls wait)"));
60
pj_get_timestamp_freq(&freq);
62
/* Get the start time */
63
pj_gettimeofday(&tvtmp);
65
pj_gettimeofday(&tv1);
66
} while (PJ_TIME_VAL_EQ(tvtmp, tv1));
67
pj_get_timestamp(&t1);
69
/* Sleep for 10 seconds */
70
pj_thread_sleep(10000);
73
pj_gettimeofday(&tvtmp);
75
pj_gettimeofday(&tv2);
76
} while (PJ_TIME_VAL_EQ(tvtmp, tv2));
77
pj_get_timestamp(&t2);
79
/* Get the elapsed time */
80
PJ_TIME_VAL_SUB(tv2, tv1);
81
msec = PJ_TIME_VAL_MSEC(tv2);
83
/* Check that the frequency match the elapsed time */
84
tics = t2.u64 - t1.u64;
85
diff = tics - (msec * freq.u64 / 1000);
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 "
93
(pj_uint32_t)(diff * 1000000 / freq.u64),
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 "
101
(pj_uint32_t)(diff * 1000000 / freq.u64),
104
PJ_LOG(3,(THIS_FILE, "....good. Timestamp is accurate down to"
112
int timestamp_test(void)
114
enum { CONSECUTIVE_LOOP = 100 };
116
pj_timestamp freq, t1, t2;
117
pj_time_val tv1, tv2;
121
PJ_LOG(3,(THIS_FILE, "...Testing timestamp (high res time)"));
123
/* Get and display timestamp frequency. */
124
if ((rc=pj_get_timestamp_freq(&freq)) != PJ_SUCCESS) {
125
app_perror("...ERROR: get timestamp freq", rc);
129
PJ_LOG(3,(THIS_FILE, "....frequency: hiword=%lu loword=%lu",
130
freq.u32.hi, freq.u32.lo));
132
PJ_LOG(3,(THIS_FILE, "...checking if time can run backwards (pls wait).."));
135
* Check if consecutive readings should yield timestamp value
136
* that is bigger than previous value.
137
* First we get the first timestamp.
139
rc = pj_get_timestamp(&t1);
140
if (rc != PJ_SUCCESS) {
141
app_perror("...ERROR: pj_get_timestamp", rc);
144
rc = pj_gettimeofday(&tv1);
145
if (rc != PJ_SUCCESS) {
146
app_perror("...ERROR: pj_gettimeofday", rc);
149
for (i=0; i<CONSECUTIVE_LOOP; ++i) {
151
pj_thread_sleep(pj_rand() % 100);
153
rc = pj_get_timestamp(&t2);
154
if (rc != PJ_SUCCESS) {
155
app_perror("...ERROR: pj_get_timestamp", rc);
158
rc = pj_gettimeofday(&tv2);
159
if (rc != PJ_SUCCESS) {
160
app_perror("...ERROR: pj_gettimeofday", rc);
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))
168
PJ_LOG(3,(THIS_FILE, "...ERROR: timestamp run backwards!"));
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!"));
180
* Simple test to time some loop.
182
PJ_LOG(3,(THIS_FILE, "....testing simple 1000000 loop"));
185
/* Mark start time. */
186
if ((rc=pj_get_timestamp(&t1)) != PJ_SUCCESS) {
187
app_perror("....error: cat't get timestamp", rc);
192
for (i=0; i<1000000; ++i) {
193
/* Try to do something so that smart compilers wont
194
* remove this silly loop.
202
pj_get_timestamp(&t2);
204
/* Get elapsed time in usec. */
205
elapsed = pj_elapsed_usec(&t1, &t2);
206
PJ_LOG(3,(THIS_FILE, "....elapsed: %u usec", (unsigned)elapsed));
208
/* See if elapsed time is "reasonable".
209
* This should be good even on 50Mhz embedded powerpc.
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)",
216
t1.u32.hi, t1.u32.lo, t2.u32.hi, t2.u32.lo));
220
/* Testing time/timestamp accuracy */
221
rc = timestamp_accuracy();
230
/* To prevent warning about "translation unit is empty"
231
* when this test is disabled.
233
int dummy_timestamp_test;
234
#endif /* INCLUDE_TIMESTAMP_TEST */