1 /*
2 * benchtest4.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 trylock/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 old_mutex_t ox;
56 pthread_mutexattr_t ma;
57 struct _timeb currSysTimeStart;
58 struct _timeb currSysTimeStop;
59 long durationMilliSecs;
60 long overHeadMilliSecs = 0;
61
62 #define GetDurationMilliSecs(_TStart, _TStop) ((_TStop.time*1000+_TStop.millitm) \
63 - (_TStart.time*1000+_TStart.millitm))
64
65 /*
66 * Dummy use of j, otherwise the loop may be removed by the optimiser
67 * when doing the overhead timing with an empty loop.
68 */
69 #define TESTSTART \
70 { int i, j = 0, k = 0; _ftime(&currSysTimeStart); for (i = 0; i < ITERATIONS; i++) { j++;
71
72 #define TESTSTOP \
73 }; _ftime(&currSysTimeStop); if (j + k == i) j++; }
74
75
76 void
oldRunTest(char * testNameString,int mType)77 oldRunTest (char * testNameString, int mType)
78 {
79 }
80
81
82 void
runTest(char * testNameString,int mType)83 runTest (char * testNameString, int mType)
84 {
85 #ifdef PTW32_MUTEX_TYPES
86 pthread_mutexattr_settype(&ma, mType);
87 #endif
88 pthread_mutex_init(&mx, &ma);
89
90 TESTSTART
91 (void) pthread_mutex_trylock(&mx);
92 (void) pthread_mutex_unlock(&mx);
93 TESTSTOP
94
95 pthread_mutex_destroy(&mx);
96
97 durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
98
99 printf( "%-45s %15ld %15.3f\n",
100 testNameString,
101 durationMilliSecs,
102 (float) durationMilliSecs * 1E3 / ITERATIONS);
103 }
104
105
106 int
main(int argc,char * argv[])107 main (int argc, char *argv[])
108 {
109 pthread_mutexattr_init(&ma);
110
111 printf( "=============================================================================\n");
112 printf( "Trylock plus unlock on an unlocked mutex.\n");
113 printf( "%ld iterations.\n\n", ITERATIONS);
114 printf( "%-45s %15s %15s\n",
115 "Test",
116 "Total(msec)",
117 "average(usec)");
118 printf( "-----------------------------------------------------------------------------\n");
119
120 /*
121 * Time the loop overhead so we can subtract it from the actual test times.
122 */
123
124 TESTSTART
125 TESTSTOP
126
127 durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
128 overHeadMilliSecs = durationMilliSecs;
129
130 old_mutex_use = OLD_WIN32CS;
131 assert(old_mutex_init(&ox, NULL) == 0);
132 TESTSTART
133 (void) old_mutex_trylock(&ox);
134 (void) old_mutex_unlock(&ox);
135 TESTSTOP
136 assert(old_mutex_destroy(&ox) == 0);
137 durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
138 printf( "%-45s %15ld %15.3f\n",
139 "Old PT Mutex using a Critical Section (WNT)",
140 durationMilliSecs,
141 (float) durationMilliSecs * 1E3 / ITERATIONS);
142
143 old_mutex_use = OLD_WIN32MUTEX;
144 assert(old_mutex_init(&ox, NULL) == 0);
145 TESTSTART
146 (void) old_mutex_trylock(&ox);
147 (void) old_mutex_unlock(&ox);
148 TESTSTOP
149 assert(old_mutex_destroy(&ox) == 0);
150 durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
151 printf( "%-45s %15ld %15.3f\n",
152 "Old PT Mutex using a Win32 Mutex (W9x)",
153 durationMilliSecs,
154 (float) durationMilliSecs * 1E3 / ITERATIONS);
155
156 printf( ".............................................................................\n");
157
158 /*
159 * Now we can start the actual tests
160 */
161 #ifdef PTW32_MUTEX_TYPES
162 runTest("PTHREAD_MUTEX_DEFAULT (W9x,WNT)", PTHREAD_MUTEX_DEFAULT);
163
164 runTest("PTHREAD_MUTEX_NORMAL (W9x,WNT)", PTHREAD_MUTEX_NORMAL);
165
166 runTest("PTHREAD_MUTEX_ERRORCHECK (W9x,WNT)", PTHREAD_MUTEX_ERRORCHECK);
167
168 runTest("PTHREAD_MUTEX_RECURSIVE (W9x,WNT)", PTHREAD_MUTEX_RECURSIVE);
169 #else
170 runTest("Non-blocking lock", 0);
171 #endif
172
173 printf( "=============================================================================\n");
174
175 /*
176 * End of tests.
177 */
178
179 pthread_mutexattr_destroy(&ma);
180
181 return 0;
182 }
183