~ubuntu-branches/ubuntu/maverick/sflphone/maverick

« back to all changes in this revision

Viewing changes to sflphone-common/libs/pjproject/pjlib/src/pjlib-test/timestamp.c

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2010-06-03 15:59:46 UTC
  • Revision ID: james.westby@ubuntu.com-20100603155946-ybe8d8o8zx8lp0m8
Tags: upstream-0.9.8.3
ImportĀ upstreamĀ versionĀ 0.9.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: timestamp.c 2394 2008-12-23 17:27:53Z bennylp $ */
 
2
/* 
 
3
 * Copyright (C) 2008-2009 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
 *  Additional permission under GNU GPL version 3 section 7:
 
21
 *
 
22
 *  If you modify this program, or any covered work, by linking or
 
23
 *  combining it with the OpenSSL project's OpenSSL library (or a
 
24
 *  modified version of that library), containing parts covered by the
 
25
 *  terms of the OpenSSL or SSLeay licenses, Teluu Inc. (http://www.teluu.com)
 
26
 *  grants you additional permission to convey the resulting work.
 
27
 *  Corresponding Source for a non-source form of such a combination
 
28
 *  shall include the source code for the parts of OpenSSL used as well
 
29
 *  as that of the covered work.
 
30
 */
 
31
#include "test.h"
 
32
#include <pj/os.h>
 
33
#include <pj/log.h>
 
34
#include <pj/rand.h>
 
35
 
 
36
 
 
37
/**
 
38
 * \page page_pjlib_timestamp_test Test: Timestamp
 
39
 *
 
40
 * This file provides implementation of timestamp_test()
 
41
 *
 
42
 * \section timestamp_test_sec Scope of the Test
 
43
 *
 
44
 * This tests whether timestamp API works.
 
45
 *
 
46
 * API tested:
 
47
 *  - pj_get_timestamp_freq()
 
48
 *  - pj_get_timestamp()
 
49
 *  - pj_elapsed_usec()
 
50
 *  - PJ_LOG()
 
51
 *
 
52
 *
 
53
 * This file is <b>pjlib-test/timestamp.c</b>
 
54
 *
 
55
 * \include pjlib-test/timestamp.c
 
56
 */
 
57
 
 
58
#if INCLUDE_TIMESTAMP_TEST
 
59
 
 
60
#define THIS_FILE   "timestamp"
 
61
 
 
62
static int timestamp_accuracy()
 
63
{
 
64
    pj_timestamp freq, t1, t2;
 
65
    pj_time_val tv1, tv2, tvtmp;
 
66
    pj_int64_t msec, tics;
 
67
    pj_int64_t diff;
 
68
 
 
69
    PJ_LOG(3,(THIS_FILE, "...testing frequency accuracy (pls wait)"));
 
70
 
 
71
    pj_get_timestamp_freq(&freq);
 
72
 
 
73
    /* Get the start time */
 
74
    pj_gettimeofday(&tvtmp);
 
75
    do {
 
76
        pj_gettimeofday(&tv1);
 
77
    } while (PJ_TIME_VAL_EQ(tvtmp, tv1));
 
78
    pj_get_timestamp(&t1);
 
79
 
 
80
    /* Sleep for 10 seconds */
 
81
    pj_thread_sleep(10000);
 
82
 
 
83
    /* Get end time */
 
84
    pj_gettimeofday(&tvtmp);
 
85
    do {
 
86
        pj_gettimeofday(&tv2);
 
87
    } while (PJ_TIME_VAL_EQ(tvtmp, tv2));
 
88
    pj_get_timestamp(&t2);
 
89
 
 
90
    /* Get the elapsed time */
 
91
    PJ_TIME_VAL_SUB(tv2, tv1);
 
92
    msec = PJ_TIME_VAL_MSEC(tv2);
 
93
 
 
94
    /* Check that the frequency match the elapsed time */
 
95
    tics = t2.u64 - t1.u64;
 
96
    diff = tics - (msec * freq.u64 / 1000);
 
97
    if (diff < 0)
 
98
        diff = -diff;
 
99
 
 
100
    /* Only allow 1 msec mismatch */
 
101
    if (diff > (pj_int64_t)(freq.u64 / 1000)) {
 
102
        PJ_LOG(3,(THIS_FILE, "....error: timestamp drifted by %d usec after "
 
103
                             "%d msec", 
 
104
                             (pj_uint32_t)(diff * 1000000 / freq.u64), 
 
105
                             msec));
 
106
        return -2000;
 
107
 
 
108
    /* Otherwise just print warning if timestamp drifted by >1 usec */
 
109
    } else if (diff > (pj_int64_t)(freq.u64 / 1000000)) {
 
110
        PJ_LOG(3,(THIS_FILE, "....warning: timestamp drifted by %d usec after "
 
111
                             "%d msec", 
 
112
                             (pj_uint32_t)(diff * 1000000 / freq.u64), 
 
113
                             msec));
 
114
    } else {
 
115
        PJ_LOG(3,(THIS_FILE, "....good. Timestamp is accurate down to"
 
116
                             " nearest usec."));
 
117
    }
 
118
 
 
119
    return 0;
 
120
}
 
121
 
 
122
 
 
123
int timestamp_test(void)
 
124
{
 
125
    enum { CONSECUTIVE_LOOP = 100 };
 
126
    volatile unsigned i;
 
127
    pj_timestamp freq, t1, t2;
 
128
    pj_time_val tv1, tv2;
 
129
    unsigned elapsed;
 
130
    pj_status_t rc;
 
131
 
 
132
    PJ_LOG(3,(THIS_FILE, "...Testing timestamp (high res time)"));
 
133
    
 
134
    /* Get and display timestamp frequency. */
 
135
    if ((rc=pj_get_timestamp_freq(&freq)) != PJ_SUCCESS) {
 
136
        app_perror("...ERROR: get timestamp freq", rc);
 
137
        return -1000;
 
138
    }
 
139
 
 
140
    PJ_LOG(3,(THIS_FILE, "....frequency: hiword=%lu loword=%lu", 
 
141
                        freq.u32.hi, freq.u32.lo));
 
142
 
 
143
    PJ_LOG(3,(THIS_FILE, "...checking if time can run backwards (pls wait).."));
 
144
 
 
145
    /*
 
146
     * Check if consecutive readings should yield timestamp value
 
147
     * that is bigger than previous value.
 
148
     * First we get the first timestamp.
 
149
     */
 
150
    rc = pj_get_timestamp(&t1);
 
151
    if (rc != PJ_SUCCESS) {
 
152
        app_perror("...ERROR: pj_get_timestamp", rc);
 
153
        return -1001;
 
154
    }
 
155
    rc = pj_gettimeofday(&tv1);
 
156
    if (rc != PJ_SUCCESS) {
 
157
        app_perror("...ERROR: pj_gettimeofday", rc);
 
158
        return -1002;
 
159
    }
 
160
    for (i=0; i<CONSECUTIVE_LOOP; ++i) {
 
161
        
 
162
        pj_thread_sleep(pj_rand() % 100);
 
163
 
 
164
        rc = pj_get_timestamp(&t2);
 
165
        if (rc != PJ_SUCCESS) {
 
166
            app_perror("...ERROR: pj_get_timestamp", rc);
 
167
            return -1003;
 
168
        }
 
169
        rc = pj_gettimeofday(&tv2);
 
170
        if (rc != PJ_SUCCESS) {
 
171
            app_perror("...ERROR: pj_gettimeofday", rc);
 
172
            return -1004;
 
173
        }
 
174
 
 
175
        /* compare t2 with t1, expecting t2 >= t1. */
 
176
        if (t2.u32.hi < t1.u32.hi ||
 
177
            (t2.u32.hi == t1.u32.hi && t2.u32.lo < t1.u32.lo))
 
178
        {
 
179
            PJ_LOG(3,(THIS_FILE, "...ERROR: timestamp run backwards!"));
 
180
            return -1005;
 
181
        }
 
182
 
 
183
        /* compare tv2 with tv1, expecting tv2 >= tv1. */
 
184
        if (PJ_TIME_VAL_LT(tv2, tv1)) {
 
185
            PJ_LOG(3,(THIS_FILE, "...ERROR: time run backwards!"));
 
186
            return -1006;
 
187
        }
 
188
    }
 
189
 
 
190
    /* 
 
191
     * Simple test to time some loop. 
 
192
     */
 
193
    PJ_LOG(3,(THIS_FILE, "....testing simple 1000000 loop"));
 
194
 
 
195
 
 
196
    /* Mark start time. */
 
197
    if ((rc=pj_get_timestamp(&t1)) != PJ_SUCCESS) {
 
198
        app_perror("....error: cat't get timestamp", rc);
 
199
        return -1010;
 
200
    }
 
201
 
 
202
    /* Loop.. */
 
203
    for (i=0; i<1000000; ++i) {
 
204
        /* Try to do something so that smart compilers wont
 
205
         * remove this silly loop.
 
206
         */
 
207
        null_func();
 
208
    }
 
209
 
 
210
    pj_thread_sleep(0);
 
211
 
 
212
    /* Mark end time. */
 
213
    pj_get_timestamp(&t2);
 
214
 
 
215
    /* Get elapsed time in usec. */
 
216
    elapsed = pj_elapsed_usec(&t1, &t2);
 
217
    PJ_LOG(3,(THIS_FILE, "....elapsed: %u usec", (unsigned)elapsed));
 
218
 
 
219
    /* See if elapsed time is "reasonable". 
 
220
     * This should be good even on 50Mhz embedded powerpc.
 
221
     */
 
222
    if (elapsed < 1 || elapsed > 1000000) {
 
223
        PJ_LOG(3,(THIS_FILE, "....error: elapsed time outside window (%u, "
 
224
                             "t1.u32.hi=%u, t1.u32.lo=%u, "
 
225
                             "t2.u32.hi=%u, t2.u32.lo=%u)",
 
226
                             elapsed, 
 
227
                             t1.u32.hi, t1.u32.lo, t2.u32.hi, t2.u32.lo));
 
228
        return -1030;
 
229
    }
 
230
 
 
231
    /* Testing time/timestamp accuracy */
 
232
    rc = timestamp_accuracy();
 
233
    if (rc != 0)
 
234
        return rc;
 
235
 
 
236
    return 0;
 
237
}
 
238
 
 
239
 
 
240
#else
 
241
/* To prevent warning about "translation unit is empty"
 
242
 * when this test is disabled. 
 
243
 */
 
244
int dummy_timestamp_test;
 
245
#endif  /* INCLUDE_TIMESTAMP_TEST */
 
246