• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * benchtest3.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 a trylock on a locked mutex 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 *
trylockThread(void * arg)77 trylockThread (void * arg)
78 {
79   TESTSTART
80   (void) pthread_mutex_trylock(&mx);
81   TESTSTOP
82 
83   return NULL;
84 }
85 
86 
87 void *
oldTrylockThread(void * arg)88 oldTrylockThread (void * arg)
89 {
90   TESTSTART
91   (void) old_mutex_trylock(&ox);
92   TESTSTOP
93 
94   return NULL;
95 }
96 
97 
98 void
runTest(char * testNameString,int mType)99 runTest (char * testNameString, int mType)
100 {
101   pthread_t t;
102 
103 #ifdef PTW32_MUTEX_TYPES
104   (void) pthread_mutexattr_settype(&ma, mType);
105 #endif
106   assert(pthread_mutex_init(&mx, &ma) == 0);
107   assert(pthread_mutex_lock(&mx) == 0);
108   assert(pthread_create(&t, NULL, trylockThread, 0) == 0);
109   assert(pthread_join(t, NULL) == 0);
110   assert(pthread_mutex_unlock(&mx) == 0);
111   assert(pthread_mutex_destroy(&mx) == 0);
112 
113   durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
114 
115   printf( "%-45s %15ld %15.3f\n",
116 	    testNameString,
117           durationMilliSecs,
118           (float) durationMilliSecs * 1E3 / ITERATIONS);
119 }
120 
121 
122 int
main(int argc,char * argv[])123 main (int argc, char *argv[])
124 {
125   pthread_t t;
126 
127   assert(pthread_mutexattr_init(&ma) == 0);
128 
129   printf( "=============================================================================\n");
130   printf( "\nTrylock on a locked mutex.\n");
131   printf( "%ld iterations.\n\n", ITERATIONS);
132   printf( "%-45s %15s %15s\n",
133 	    "Test",
134 	    "Total(msec)",
135 	    "average(usec)");
136   printf( "-----------------------------------------------------------------------------\n");
137 
138   /*
139    * Time the loop overhead so we can subtract it from the actual test times.
140    */
141 
142   TESTSTART
143   TESTSTOP
144 
145   durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
146   overHeadMilliSecs = durationMilliSecs;
147 
148 
149   old_mutex_use = OLD_WIN32CS;
150   assert(old_mutex_init(&ox, NULL) == 0);
151   assert(old_mutex_lock(&ox) == 0);
152   assert(pthread_create(&t, NULL, oldTrylockThread, 0) == 0);
153   assert(pthread_join(t, NULL) == 0);
154   assert(old_mutex_unlock(&ox) == 0);
155   assert(old_mutex_destroy(&ox) == 0);
156   durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
157   printf( "%-45s %15ld %15.3f\n",
158 	    "Old PT Mutex using a Critical Section (WNT)",
159           durationMilliSecs,
160           (float) durationMilliSecs * 1E3 / ITERATIONS);
161 
162   old_mutex_use = OLD_WIN32MUTEX;
163   assert(old_mutex_init(&ox, NULL) == 0);
164   assert(old_mutex_lock(&ox) == 0);
165   assert(pthread_create(&t, NULL, oldTrylockThread, 0) == 0);
166   assert(pthread_join(t, NULL) == 0);
167   assert(old_mutex_unlock(&ox) == 0);
168   assert(old_mutex_destroy(&ox) == 0);
169   durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
170   printf( "%-45s %15ld %15.3f\n",
171 	    "Old PT Mutex using a Win32 Mutex (W9x)",
172           durationMilliSecs,
173           (float) durationMilliSecs * 1E3 / ITERATIONS);
174 
175   printf( ".............................................................................\n");
176 
177   /*
178    * Now we can start the actual tests
179    */
180 #ifdef PTW32_MUTEX_TYPES
181   runTest("PTHREAD_MUTEX_DEFAULT (W9x,WNT)", PTHREAD_MUTEX_DEFAULT);
182 
183   runTest("PTHREAD_MUTEX_NORMAL (W9x,WNT)", PTHREAD_MUTEX_NORMAL);
184 
185   runTest("PTHREAD_MUTEX_ERRORCHECK (W9x,WNT)", PTHREAD_MUTEX_ERRORCHECK);
186 
187   runTest("PTHREAD_MUTEX_RECURSIVE (W9x,WNT)", PTHREAD_MUTEX_RECURSIVE);
188 #else
189   runTest("Non-blocking lock", 0);
190 #endif
191 
192   printf( "=============================================================================\n");
193 
194   /*
195    * End of tests.
196    */
197 
198   pthread_mutexattr_destroy(&ma);
199 
200   return 0;
201 }
202