~ubuntu-branches/ubuntu/raring/virtualbox-ose/raring

« back to all changes in this revision

Viewing changes to src/VBox/Runtime/testcase/tstTimer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2011-01-30 23:27:25 UTC
  • mfrom: (0.3.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20110130232725-2ouajjd2ggdet0zd
Tags: 4.0.2-dfsg-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Add Apport hook.
    - debian/virtualbox-ose.files/source_virtualbox-ose.py
    - debian/virtualbox-ose.install
  - Drop *-source packages.
* Drop ubuntu-01-fix-build-gcc45.patch, fixed upstream.
* Drop ubuntu-02-as-needed.patch, added to the Debian package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: tstTimer.cpp $ */
 
1
/* $Id: tstTimer.cpp 32431 2010-09-11 18:02:17Z vboxsync $ */
2
2
/** @file
3
3
 * IPRT Testcase - Timers.
4
4
 */
5
5
 
6
6
/*
7
 
 * Copyright (C) 2006-2007 Oracle Corporation
 
7
 * Copyright (C) 2006-2010 Oracle Corporation
8
8
 *
9
9
 * This file is part of VirtualBox Open Source Edition (OSE), as
10
10
 * available from http://www.virtualbox.org. This file is free software;
33
33
#include <iprt/initterm.h>
34
34
#include <iprt/stream.h>
35
35
#include <iprt/err.h>
 
36
#include <iprt/string.h>
36
37
 
37
38
 
38
39
 
43
44
static volatile uint64_t gu64Min;
44
45
static volatile uint64_t gu64Max;
45
46
static volatile uint64_t gu64Prev;
 
47
static volatile uint64_t gu64Norm;
 
48
 
 
49
static uint32_t cFrequency[200];
46
50
 
47
51
static DECLCALLBACK(void) TimerCallback(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
48
52
{
56
60
            gu64Min = u64Delta;
57
61
        if (u64Delta > gu64Max)
58
62
            gu64Max = u64Delta;
 
63
        int i = (int)(  RT_ELEMENTS(cFrequency)
 
64
                      - (u64Delta * (RT_ELEMENTS(cFrequency) / 2) / gu64Norm));
 
65
        if (i >= 0 && i < (int)RT_ELEMENTS(cFrequency))
 
66
            cFrequency[i]++;
59
67
    }
60
68
    gu64Prev = u64Now;
61
69
}
115
123
     */
116
124
    static struct
117
125
    {
118
 
        unsigned uMilliesInterval;
 
126
        unsigned uMicroInterval;
119
127
        unsigned uMilliesWait;
120
128
        unsigned cLower;
121
129
        unsigned cUpper;
122
130
    } aTests[] =
123
131
    {
124
 
        { 32, 2000, 0, 0 },
125
 
        { 20, 2000, 0, 0 },
126
 
        { 10, 2000, 0, 0 },
127
 
        { 8,  2000, 0, 0 },
128
 
        { 2,  2000, 0, 0 },
129
 
        { 1,  2000, 0, 0 }
 
132
        { 32000, 2000, 0, 0 },
 
133
        { 20000, 2000, 0, 0 },
 
134
        { 10000, 2000, 0, 0 },
 
135
        {  8000, 2000, 0, 0 },
 
136
        {  2000, 2000, 0, 0 },
 
137
        {  1000, 2000, 0, 0 },
 
138
        {   500, 5000, 0, 0 },
 
139
        {   200, 5000, 0, 0 },
 
140
        {   100, 5000, 0, 0 }
130
141
    };
131
142
 
132
143
    unsigned i = 0;
133
144
    for (i = 0; i < RT_ELEMENTS(aTests); i++)
134
145
    {
135
 
        aTests[i].cLower = (aTests[i].uMilliesWait - aTests[i].uMilliesWait / 10) / aTests[i].uMilliesInterval;
136
 
        aTests[i].cUpper = (aTests[i].uMilliesWait + aTests[i].uMilliesWait / 10) / aTests[i].uMilliesInterval;
 
146
        aTests[i].cLower = (aTests[i].uMilliesWait*1000 - aTests[i].uMilliesWait*100) / aTests[i].uMicroInterval;
 
147
        aTests[i].cUpper = (aTests[i].uMilliesWait*1000 + aTests[i].uMilliesWait*100) / aTests[i].uMicroInterval;
 
148
        gu64Norm = aTests[i].uMicroInterval*1000;
137
149
 
138
150
        RTPrintf("\n"
139
 
                 "tstTimer: TESTING - %d ms interval, %d ms wait, expects %d-%d ticks.\n",
140
 
                 aTests[i].uMilliesInterval, aTests[i].uMilliesWait, aTests[i].cLower, aTests[i].cUpper);
 
151
                 "tstTimer: TESTING - %d us interval, %d ms wait, expects %d-%d ticks.\n",
 
152
                 aTests[i].uMicroInterval, aTests[i].uMilliesWait, aTests[i].cLower, aTests[i].cUpper);
141
153
 
142
154
        /*
143
155
         * Start timer which ticks every 10ms.
147
159
        gu64Max = 0;
148
160
        gu64Min = UINT64_MAX;
149
161
        gu64Prev = 0;
 
162
        RT_ZERO(cFrequency);
150
163
#ifdef RT_OS_WINDOWS
151
 
        rc = RTTimerCreate(&pTimer, aTests[i].uMilliesInterval, TimerCallback, NULL);
 
164
        if (aTests[i].uMicroInterval < 1000)
 
165
            continue;
 
166
        rc = RTTimerCreate(&pTimer, aTests[i].uMicroInterval / 1000, TimerCallback, NULL);
152
167
#else
153
 
        rc = RTTimerCreateEx(&pTimer, aTests[i].uMilliesInterval * (uint64_t)1000000, 0, TimerCallback, NULL);
 
168
        rc = RTTimerCreateEx(&pTimer, aTests[i].uMicroInterval * (uint64_t)1000, 0, TimerCallback, NULL);
154
169
#endif
155
170
        if (RT_FAILURE(rc))
156
171
        {
157
 
            RTPrintf("tstTimer: FAILURE - RTTimerCreateEx(,%u*1M,,,) -> %Rrc\n", aTests[i].uMilliesInterval, rc);
 
172
            RTPrintf("tstTimer: FAILURE - RTTimerCreateEx(,%u*1M,,,) -> %Rrc\n", aTests[i].uMicroInterval, rc);
158
173
            cErrors++;
159
174
            continue;
160
175
        }
167
182
        rc = RTTimerStart(pTimer, 0);
168
183
        if (RT_FAILURE(rc))
169
184
        {
170
 
            RTPrintf("tstTimer: FAILURE - RTTimerStart(,0) -> %Rrc\n", aTests[i].uMilliesInterval, rc);
 
185
            RTPrintf("tstTimer: FAILURE - RTTimerStart(,0) -> %Rrc\n", aTests[i].uMicroInterval, rc);
171
186
            cErrors++;
172
187
        }
173
188
#endif
187
202
 
188
203
        RTPrintf("tstTimer: uTS=%RI64 (%RU64 - %RU64)\n", uTSDiff, uTSBegin, uTSEnd);
189
204
        unsigned cTicks = gcTicks;
190
 
        RTThreadSleep(aTests[i].uMilliesInterval * 3);
 
205
        RTThreadSleep(aTests[i].uMicroInterval/1000 * 3);
191
206
        if (gcTicks != cTicks)
192
207
        {
193
208
            RTPrintf("tstTimer: FAILURE - RTTimerDestroy() didn't really stop the timer! gcTicks=%d cTicks=%d\n", gcTicks, cTicks);
211
226
        else
212
227
            RTPrintf("tstTimer: OK      - gcTicks=%d",  gcTicks);
213
228
        RTPrintf(" min=%RU64 max=%RU64\n", gu64Min, gu64Max);
 
229
 
 
230
        for (int j = 0; j < (int)RT_ELEMENTS(cFrequency); j++)
 
231
        {
 
232
            uint32_t len = cFrequency[j] * 70 / gcTicks;
 
233
            uint32_t deviation = j - RT_ELEMENTS(cFrequency) / 2;
 
234
            uint64_t u64FreqPercent = (uint64_t)cFrequency[j] * 10000 / gcTicks;
 
235
            uint64_t u64FreqPercentFrac = u64FreqPercent % 100;
 
236
            u64FreqPercent = u64FreqPercent / 100;
 
237
            RTPrintf("%+4d%c %6u %3llu.%02llu%% ",
 
238
                    deviation, deviation == 0 ? ' ' : '%', cFrequency[j],
 
239
                    u64FreqPercent, u64FreqPercentFrac);
 
240
            for (unsigned k = 0; k < len; k++)
 
241
                RTPrintf("*");
 
242
            RTPrintf("\n");
 
243
        }
214
244
    }
215
245
 
216
246
    /*