1 | /* $Id: tstPrfRT.cpp 29250 2010-05-09 17:53:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT testcase - profile some of the important functions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*******************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #include <iprt/initterm.h>
|
---|
31 | #include <iprt/time.h>
|
---|
32 | #include <iprt/log.h>
|
---|
33 | #include <iprt/stream.h>
|
---|
34 | #include <iprt/thread.h>
|
---|
35 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
36 | # include <iprt/asm-amd64-x86.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | void PrintResult(uint64_t u64Ticks, uint64_t u64MaxTicks, uint64_t u64MinTicks, unsigned cTimes, const char *pszOperation)
|
---|
40 | {
|
---|
41 | RTPrintf("tstPrfRT: %-32s %5lld / %5lld / %5lld ticks per call (%u calls %lld ticks)\n",
|
---|
42 | pszOperation, u64MinTicks, u64Ticks / (uint64_t)cTimes, u64MaxTicks, cTimes, u64Ticks);
|
---|
43 | }
|
---|
44 |
|
---|
45 | # define ITERATE(preexpr, expr, postexpr, cIterations) \
|
---|
46 | for (i = 0, u64TotalTS = 0, u64MinTS = ~0, u64MaxTS = 0; i < (cIterations); i++) \
|
---|
47 | { \
|
---|
48 | { preexpr } \
|
---|
49 | uint64_t u64StartTS = ASMReadTSC(); \
|
---|
50 | { expr } \
|
---|
51 | uint64_t u64ElapsedTS = ASMReadTSC() - u64StartTS; \
|
---|
52 | { postexpr } \
|
---|
53 | if (u64ElapsedTS > u64MinTS * 32) \
|
---|
54 | { \
|
---|
55 | i--; \
|
---|
56 | continue; \
|
---|
57 | } \
|
---|
58 | if (u64ElapsedTS < u64MinTS) \
|
---|
59 | u64MinTS = u64ElapsedTS; \
|
---|
60 | if (u64ElapsedTS > u64MaxTS) \
|
---|
61 | u64MaxTS = u64ElapsedTS; \
|
---|
62 | u64TotalTS += u64ElapsedTS; \
|
---|
63 | }
|
---|
64 |
|
---|
65 | #else /* !AMD64 && !X86 */
|
---|
66 |
|
---|
67 | void PrintResult(uint64_t cNs, uint64_t cNsMax, uint64_t cNsMin, unsigned cTimes, const char *pszOperation)
|
---|
68 | {
|
---|
69 | RTPrintf("tstPrfRT: %-32s %5lld / %5lld / %5lld ns per call (%u calls %lld ns)\n",
|
---|
70 | pszOperation, cNsMin, cNs / (uint64_t)cTimes, cNsMax, cTimes, cNs);
|
---|
71 | }
|
---|
72 |
|
---|
73 | # define ITERATE(preexpr, expr, postexpr, cIterations) \
|
---|
74 | for (i = 0, u64TotalTS = 0, u64MinTS = ~0, u64MaxTS = 0; i < (cIterations); i++) \
|
---|
75 | { \
|
---|
76 | { preexpr } \
|
---|
77 | uint64_t u64StartTS = RTTimeNanoTS(); \
|
---|
78 | { expr } \
|
---|
79 | uint64_t u64ElapsedTS = RTTimeNanoTS() - u64StartTS; \
|
---|
80 | { postexpr } \
|
---|
81 | if (u64ElapsedTS > u64MinTS * 32) \
|
---|
82 | { \
|
---|
83 | i--; \
|
---|
84 | continue; \
|
---|
85 | } \
|
---|
86 | if (u64ElapsedTS < u64MinTS) \
|
---|
87 | u64MinTS = u64ElapsedTS; \
|
---|
88 | if (u64ElapsedTS > u64MaxTS) \
|
---|
89 | u64MaxTS = u64ElapsedTS; \
|
---|
90 | u64TotalTS += u64ElapsedTS; \
|
---|
91 | }
|
---|
92 |
|
---|
93 | #endif /* !AMD64 && !X86 */
|
---|
94 |
|
---|
95 |
|
---|
96 | int main()
|
---|
97 | {
|
---|
98 | uint64_t u64TotalTS;
|
---|
99 | uint64_t u64MinTS;
|
---|
100 | uint64_t u64MaxTS;
|
---|
101 | unsigned i;
|
---|
102 |
|
---|
103 | RTR3Init();
|
---|
104 | RTPrintf("tstPrfRT: TESTING...\n");
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * RTTimeNanoTS, RTTimeProgramNanoTS, RTTimeMilliTS, and RTTimeProgramMilliTS.
|
---|
108 | */
|
---|
109 | ITERATE(RT_NOTHING, RTTimeNanoTS();, RT_NOTHING, 1000000);
|
---|
110 | PrintResult(u64TotalTS, u64MaxTS, u64MinTS, i, "RTTimeNanoTS");
|
---|
111 |
|
---|
112 | ITERATE(RT_NOTHING, RTTimeProgramNanoTS();, RT_NOTHING, 1000000);
|
---|
113 | PrintResult(u64TotalTS, u64MaxTS, u64MinTS, i, "RTTimeProgramNanoTS");
|
---|
114 |
|
---|
115 | ITERATE(RT_NOTHING, RTTimeMilliTS();, RT_NOTHING, 1000000);
|
---|
116 | PrintResult(u64TotalTS, u64MaxTS, u64MinTS, i, "RTTimeMilliTS");
|
---|
117 |
|
---|
118 | ITERATE(RT_NOTHING, RTTimeProgramMilliTS();, RT_NOTHING, 1000000);
|
---|
119 | PrintResult(u64TotalTS, u64MaxTS, u64MinTS, i, "RTTimeProgramMilliTS");
|
---|
120 |
|
---|
121 | /*
|
---|
122 | * RTTimeNow
|
---|
123 | */
|
---|
124 | RTTIMESPEC Time;
|
---|
125 | ITERATE(RT_NOTHING, RTTimeNow(&Time);, RT_NOTHING, 1000000);
|
---|
126 | PrintResult(u64TotalTS, u64MaxTS, u64MinTS, i, "RTTimeNow");
|
---|
127 |
|
---|
128 | /*
|
---|
129 | * RTLogDefaultInstance()
|
---|
130 | */
|
---|
131 | ITERATE(RT_NOTHING, RTLogDefaultInstance();, RT_NOTHING, 1000000);
|
---|
132 | PrintResult(u64TotalTS, u64MaxTS, u64MinTS, i, "RTLogDefaultInstance");
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * RTThreadSelf and RTThreadNativeSelf
|
---|
136 | */
|
---|
137 | ITERATE(RT_NOTHING, RTThreadSelf();, RT_NOTHING, 1000000);
|
---|
138 | PrintResult(u64TotalTS, u64MaxTS, u64MinTS, i, "RTThreadSelf");
|
---|
139 |
|
---|
140 | ITERATE(RT_NOTHING, RTThreadNativeSelf();, RT_NOTHING, 1000000);
|
---|
141 | PrintResult(u64TotalTS, u64MaxTS, u64MinTS, i, "RTThreadNativeSelf");
|
---|
142 |
|
---|
143 | RTPrintf("tstPrtRT: DONE\n");
|
---|
144 | return 0;
|
---|
145 | }
|
---|