1 | /* $Id: tstPrfRT.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT testcase - profile some of the important functions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 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 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/initterm.h>
|
---|
32 | #include <iprt/time.h>
|
---|
33 | #include <iprt/log.h>
|
---|
34 | #include <iprt/stream.h>
|
---|
35 | #include <iprt/thread.h>
|
---|
36 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
37 | # include <iprt/asm-amd64-x86.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | void PrintResult(uint64_t u64Ticks, uint64_t u64MaxTicks, uint64_t u64MinTicks, unsigned cTimes, const char *pszOperation)
|
---|
41 | {
|
---|
42 | RTPrintf("tstPrfRT: %-32s %5lld / %5lld / %5lld ticks per call (%u calls %lld ticks)\n",
|
---|
43 | pszOperation, u64MinTicks, u64Ticks / (uint64_t)cTimes, u64MaxTicks, cTimes, u64Ticks);
|
---|
44 | }
|
---|
45 |
|
---|
46 | # define ITERATE(preexpr, expr, postexpr, cIterations) \
|
---|
47 | AssertCompile(((cIterations) % 8) == 0); \
|
---|
48 | /* Min and max value. */ \
|
---|
49 | for (i = 0, u64MinTS = ~0, u64MaxTS = 0; i < (cIterations); i++) \
|
---|
50 | { \
|
---|
51 | { preexpr } \
|
---|
52 | uint64_t u64StartTS = ASMReadTSC(); \
|
---|
53 | { expr } \
|
---|
54 | uint64_t u64ElapsedTS = ASMReadTSC() - u64StartTS; \
|
---|
55 | { postexpr } \
|
---|
56 | if (u64ElapsedTS > u64MinTS * 32) \
|
---|
57 | { \
|
---|
58 | i--; \
|
---|
59 | continue; \
|
---|
60 | } \
|
---|
61 | if (u64ElapsedTS < u64MinTS) \
|
---|
62 | u64MinTS = u64ElapsedTS; \
|
---|
63 | if (u64ElapsedTS > u64MaxTS) \
|
---|
64 | u64MaxTS = u64ElapsedTS; \
|
---|
65 | } \
|
---|
66 | { \
|
---|
67 | /* Calculate a good average value (may be smaller than min). */ \
|
---|
68 | i = (cIterations); \
|
---|
69 | AssertRelease((i % 8) == 0); \
|
---|
70 | { preexpr } \
|
---|
71 | uint64_t u64StartTS = ASMReadTSC(); \
|
---|
72 | while (i != 0) \
|
---|
73 | { \
|
---|
74 | { expr } \
|
---|
75 | { expr } \
|
---|
76 | { expr } \
|
---|
77 | { expr } \
|
---|
78 | { expr } \
|
---|
79 | { expr } \
|
---|
80 | { expr } \
|
---|
81 | { expr } \
|
---|
82 | i -= 8; \
|
---|
83 | } \
|
---|
84 | u64TotalTS = ASMReadTSC() - u64StartTS; \
|
---|
85 | { postexpr } \
|
---|
86 | i = (cIterations); \
|
---|
87 | }
|
---|
88 |
|
---|
89 | #else /* !AMD64 && !X86 */
|
---|
90 |
|
---|
91 | void PrintResult(uint64_t cNs, uint64_t cNsMax, uint64_t cNsMin, unsigned cTimes, const char *pszOperation)
|
---|
92 | {
|
---|
93 | RTPrintf("tstPrfRT: %-32s %5lld / %5lld / %5lld ns per call (%u calls %lld ns)\n",
|
---|
94 | pszOperation, cNsMin, cNs / (uint64_t)cTimes, cNsMax, cTimes, cNs);
|
---|
95 | }
|
---|
96 |
|
---|
97 | # define ITERATE(preexpr, expr, postexpr, cIterations) \
|
---|
98 | for (i = 0, u64TotalTS = 0, u64MinTS = ~0, u64MaxTS = 0; i < (cIterations); i++) \
|
---|
99 | { \
|
---|
100 | { preexpr } \
|
---|
101 | uint64_t u64StartTS = RTTimeNanoTS(); \
|
---|
102 | { expr } \
|
---|
103 | uint64_t u64ElapsedTS = RTTimeNanoTS() - u64StartTS; \
|
---|
104 | { postexpr } \
|
---|
105 | if (u64ElapsedTS > u64MinTS * 32) \
|
---|
106 | { \
|
---|
107 | i--; \
|
---|
108 | continue; \
|
---|
109 | } \
|
---|
110 | if (u64ElapsedTS < u64MinTS) \
|
---|
111 | u64MinTS = u64ElapsedTS; \
|
---|
112 | if (u64ElapsedTS > u64MaxTS) \
|
---|
113 | u64MaxTS = u64ElapsedTS; \
|
---|
114 | u64TotalTS += u64ElapsedTS; \
|
---|
115 | }
|
---|
116 |
|
---|
117 | #endif /* !AMD64 && !X86 */
|
---|
118 |
|
---|
119 |
|
---|
120 | int main(int argc, char **argv)
|
---|
121 | {
|
---|
122 | uint64_t u64TotalTS;
|
---|
123 | uint64_t u64MinTS;
|
---|
124 | uint64_t u64MaxTS;
|
---|
125 | unsigned i;
|
---|
126 |
|
---|
127 | RTR3InitExeNoArguments(argc == 2 ? RTR3INIT_FLAGS_SUPLIB : 0);
|
---|
128 | RTPrintf("tstPrfRT: TESTING...\n");
|
---|
129 |
|
---|
130 | /*
|
---|
131 | * RTTimeNanoTS, RTTimeProgramNanoTS, RTTimeMilliTS, and RTTimeProgramMilliTS.
|
---|
132 | */
|
---|
133 | ITERATE(RT_NOTHING, RTTimeNanoTS();, RT_NOTHING, _1M * 32);
|
---|
134 | PrintResult(u64TotalTS, u64MaxTS, u64MinTS, i, "RTTimeNanoTS");
|
---|
135 |
|
---|
136 | ITERATE(RT_NOTHING, RTTimeProgramNanoTS();, RT_NOTHING, 1000000);
|
---|
137 | PrintResult(u64TotalTS, u64MaxTS, u64MinTS, i, "RTTimeProgramNanoTS");
|
---|
138 |
|
---|
139 | ITERATE(RT_NOTHING, RTTimeMilliTS();, RT_NOTHING, 1000000);
|
---|
140 | PrintResult(u64TotalTS, u64MaxTS, u64MinTS, i, "RTTimeMilliTS");
|
---|
141 |
|
---|
142 | ITERATE(RT_NOTHING, RTTimeProgramMilliTS();, RT_NOTHING, 1000000);
|
---|
143 | PrintResult(u64TotalTS, u64MaxTS, u64MinTS, i, "RTTimeProgramMilliTS");
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * RTTimeNow
|
---|
147 | */
|
---|
148 | RTTIMESPEC Time;
|
---|
149 | ITERATE(RT_NOTHING, RTTimeNow(&Time);, RT_NOTHING, 1000000);
|
---|
150 | PrintResult(u64TotalTS, u64MaxTS, u64MinTS, i, "RTTimeNow");
|
---|
151 |
|
---|
152 | /*
|
---|
153 | * RTLogDefaultInstance()
|
---|
154 | */
|
---|
155 | ITERATE(RT_NOTHING, RTLogDefaultInstance();, RT_NOTHING, 1000000);
|
---|
156 | PrintResult(u64TotalTS, u64MaxTS, u64MinTS, i, "RTLogDefaultInstance");
|
---|
157 |
|
---|
158 | /*
|
---|
159 | * RTThreadSelf and RTThreadNativeSelf
|
---|
160 | */
|
---|
161 | ITERATE(RT_NOTHING, RTThreadSelf();, RT_NOTHING, 1000000);
|
---|
162 | PrintResult(u64TotalTS, u64MaxTS, u64MinTS, i, "RTThreadSelf");
|
---|
163 |
|
---|
164 | ITERATE(RT_NOTHING, RTThreadNativeSelf();, RT_NOTHING, 1000000);
|
---|
165 | PrintResult(u64TotalTS, u64MaxTS, u64MinTS, i, "RTThreadNativeSelf");
|
---|
166 |
|
---|
167 | RTPrintf("tstPrtRT: DONE\n");
|
---|
168 | return 0;
|
---|
169 | }
|
---|