1 /*
2 * benchtest1.c
3 *
4 *
5 * --------------------------------------------------------------------------
6 *
7 * Pthreads-win32 - POSIX Threads Library for Win32
8 * Copyright(C) 1998 John E. Bossom
9 * Copyright(C) 1999,2005 Pthreads-win32 contributors
10 *
11 * Contact Email: rpj@callisto.canberra.edu.au
12 *
13 * The current list of contributors is contained
14 * in the file CONTRIBUTORS included with the source
15 * code distribution. The list can also be seen at the
16 * following World Wide Web location:
17 * http://sources.redhat.com/pthreads-win32/contributors.html
18 *
19 * This library is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU Lesser General Public
21 * License as published by the Free Software Foundation; either
22 * version 2 of the License, or (at your option) any later version.
23 *
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
28 *
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library in the file COPYING.LIB;
31 * if not, write to the Free Software Foundation, Inc.,
32 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
33 *
34 * --------------------------------------------------------------------------
35 *
36 * Measure time taken to complete an elementary operation.
37 *
38 * - Mutex
39 * Single thread iteration over lock/unlock for each mutex type.
40 */
41
42 #include "test.h"
43 #include <sys/timeb.h>
44
45 #ifdef __GNUC__
46 #include <stdlib.h>
47 #endif
48
49 #include "benchtest.h"
50
51 #define PTW32_MUTEX_TYPES
52 #define ITERATIONS 10000000L
53
54 pthread_mutex_t mx;
55 pthread_mutexattr_t ma;
56 struct _timeb currSysTimeStart;
57 struct _timeb currSysTimeStop;
58 long durationMilliSecs;
59 long overHeadMilliSecs = 0;
60 int one = 1;
61 int zero = 0;
62
63 #define GetDurationMilliSecs(_TStart, _TStop) ((_TStop.time*1000+_TStop.millitm) \
64 - (_TStart.time*1000+_TStart.millitm))
65
66 /*
67 * Dummy use of j, otherwise the loop may be removed by the optimiser
68 * when doing the overhead timing with an empty loop.
69 */
70 #define TESTSTART \
71 { int i, j = 0, k = 0; _ftime(&currSysTimeStart); for (i = 0; i < ITERATIONS; i++) { j++;
72
73 #define TESTSTOP \
74 }; _ftime(&currSysTimeStop); if (j + k == i) j++; }
75
76
77 void
runTest(char * testNameString,int mType)78 runTest (char * testNameString, int mType)
79 {
80 #ifdef PTW32_MUTEX_TYPES
81 assert(pthread_mutexattr_settype(&ma, mType) == 0);
82 #endif
83 assert(pthread_mutex_init(&mx, &ma) == 0);
84
85 TESTSTART
86 assert(pthread_mutex_lock(&mx) == zero);
87 assert(pthread_mutex_unlock(&mx) == zero);
88 TESTSTOP
89
90 assert(pthread_mutex_destroy(&mx) == 0);
91
92 durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
93
94 printf( "%-45s %15ld %15.3f\n",
95 testNameString,
96 durationMilliSecs,
97 (float) durationMilliSecs * 1E3 / ITERATIONS);
98 }
99
100
101 int
main(int argc,char * argv[])102 main (int argc, char *argv[])
103 {
104 int i = 0;
105 CRITICAL_SECTION cs;
106 old_mutex_t ox;
107 pthread_mutexattr_init(&ma);
108
109 printf( "=============================================================================\n");
110 printf( "\nLock plus unlock on an unlocked mutex.\n%ld iterations\n\n",
111 ITERATIONS);
112 printf( "%-45s %15s %15s\n",
113 "Test",
114 "Total(msec)",
115 "average(usec)");
116 printf( "-----------------------------------------------------------------------------\n");
117
118 /*
119 * Time the loop overhead so we can subtract it from the actual test times.
120 */
121
122 TESTSTART
123 assert(1 == one);
124 assert(1 == one);
125 TESTSTOP
126
127 durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
128 overHeadMilliSecs = durationMilliSecs;
129
130
131 TESTSTART
132 assert((dummy_call(&i), 1) == one);
133 assert((dummy_call(&i), 1) == one);
134 TESTSTOP
135
136 durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
137
138 printf( "%-45s %15ld %15.3f\n",
139 "Dummy call x 2",
140 durationMilliSecs,
141 (float) durationMilliSecs * 1E3 / ITERATIONS);
142
143
144 TESTSTART
145 assert((interlocked_inc_with_conditionals(&i), 1) == one);
146 assert((interlocked_dec_with_conditionals(&i), 1) == one);
147 TESTSTOP
148
149 durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
150
151 printf( "%-45s %15ld %15.3f\n",
152 "Dummy call -> Interlocked with cond x 2",
153 durationMilliSecs,
154 (float) durationMilliSecs * 1E3 / ITERATIONS);
155
156
157 TESTSTART
158 assert((InterlockedIncrement((LPLONG)&i), 1) == (LONG)one);
159 assert((InterlockedDecrement((LPLONG)&i), 1) == (LONG)one);
160 TESTSTOP
161
162 durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
163
164 printf( "%-45s %15ld %15.3f\n",
165 "InterlockedOp x 2",
166 durationMilliSecs,
167 (float) durationMilliSecs * 1E3 / ITERATIONS);
168
169
170 InitializeCriticalSection(&cs);
171
172 TESTSTART
173 assert((EnterCriticalSection(&cs), 1) == one);
174 assert((LeaveCriticalSection(&cs), 1) == one);
175 TESTSTOP
176
177 DeleteCriticalSection(&cs);
178
179 durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
180
181 printf( "%-45s %15ld %15.3f\n",
182 "Simple Critical Section",
183 durationMilliSecs,
184 (float) durationMilliSecs * 1E3 / ITERATIONS);
185
186
187 old_mutex_use = OLD_WIN32CS;
188 assert(old_mutex_init(&ox, NULL) == 0);
189
190 TESTSTART
191 assert(old_mutex_lock(&ox) == zero);
192 assert(old_mutex_unlock(&ox) == zero);
193 TESTSTOP
194
195 assert(old_mutex_destroy(&ox) == 0);
196
197 durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
198
199 printf( "%-45s %15ld %15.3f\n",
200 "Old PT Mutex using a Critical Section (WNT)",
201 durationMilliSecs,
202 (float) durationMilliSecs * 1E3 / ITERATIONS);
203
204
205 old_mutex_use = OLD_WIN32MUTEX;
206 assert(old_mutex_init(&ox, NULL) == 0);
207
208 TESTSTART
209 assert(old_mutex_lock(&ox) == zero);
210 assert(old_mutex_unlock(&ox) == zero);
211 TESTSTOP
212
213 assert(old_mutex_destroy(&ox) == 0);
214
215 durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
216
217 printf( "%-45s %15ld %15.3f\n",
218 "Old PT Mutex using a Win32 Mutex (W9x)",
219 durationMilliSecs,
220 (float) durationMilliSecs * 1E3 / ITERATIONS);
221
222 printf( ".............................................................................\n");
223
224 /*
225 * Now we can start the actual tests
226 */
227 #ifdef PTW32_MUTEX_TYPES
228 runTest("PTHREAD_MUTEX_DEFAULT (W9x,WNT)", PTHREAD_MUTEX_DEFAULT);
229
230 runTest("PTHREAD_MUTEX_NORMAL (W9x,WNT)", PTHREAD_MUTEX_NORMAL);
231
232 runTest("PTHREAD_MUTEX_ERRORCHECK (W9x,WNT)", PTHREAD_MUTEX_ERRORCHECK);
233
234 runTest("PTHREAD_MUTEX_RECURSIVE (W9x,WNT)", PTHREAD_MUTEX_RECURSIVE);
235 #else
236 runTest("Non-blocking lock", 0);
237 #endif
238
239 printf( "=============================================================================\n");
240
241 /*
242 * End of tests.
243 */
244
245 pthread_mutexattr_destroy(&ma);
246
247 one = i; /* Dummy assignment to avoid 'variable unused' warning */
248 return 0;
249 }
250