• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * benchtest5.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  * - Semaphore
39  *   Single thread iteration over post/wait for a semaphore.
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 ITERATIONS      1000000L
52 
53 sem_t sema;
54 HANDLE w32sema;
55 
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
reportTest(char * testNameString)78 reportTest (char * testNameString)
79 {
80   durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
81 
82   printf( "%-45s %15ld %15.3f\n",
83 	    testNameString,
84           durationMilliSecs,
85           (float) durationMilliSecs * 1E3 / ITERATIONS);
86 }
87 
88 
89 int
main(int argc,char * argv[])90 main (int argc, char *argv[])
91 {
92   printf( "=============================================================================\n");
93   printf( "\nOperations on a semaphore.\n%ld iterations\n\n",
94           ITERATIONS);
95   printf( "%-45s %15s %15s\n",
96 	    "Test",
97 	    "Total(msec)",
98 	    "average(usec)");
99   printf( "-----------------------------------------------------------------------------\n");
100 
101   /*
102    * Time the loop overhead so we can subtract it from the actual test times.
103    */
104 
105   TESTSTART
106   assert(1 == one);
107   TESTSTOP
108 
109   durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
110   overHeadMilliSecs = durationMilliSecs;
111 
112 
113   /*
114    * Now we can start the actual tests
115    */
116   assert((w32sema = CreateSemaphore(NULL, (long) 0, (long) ITERATIONS, NULL)) != 0);
117   TESTSTART
118   assert(ReleaseSemaphore(w32sema, 1, NULL) != zero);
119   TESTSTOP
120   assert(CloseHandle(w32sema) != 0);
121 
122   reportTest("W32 Post with no waiters");
123 
124 
125   assert((w32sema = CreateSemaphore(NULL, (long) ITERATIONS, (long) ITERATIONS, NULL)) != 0);
126   TESTSTART
127   assert(WaitForSingleObject(w32sema, INFINITE) == WAIT_OBJECT_0);
128   TESTSTOP
129   assert(CloseHandle(w32sema) != 0);
130 
131   reportTest("W32 Wait without blocking");
132 
133 
134   assert(sem_init(&sema, 0, 0) == 0);
135   TESTSTART
136   assert(sem_post(&sema) == zero);
137   TESTSTOP
138   assert(sem_destroy(&sema) == 0);
139 
140   reportTest("POSIX Post with no waiters");
141 
142 
143   assert(sem_init(&sema, 0, ITERATIONS) == 0);
144   TESTSTART
145   assert(sem_wait(&sema) == zero);
146   TESTSTOP
147   assert(sem_destroy(&sema) == 0);
148 
149   reportTest("POSIX Wait without blocking");
150 
151 
152   printf( "=============================================================================\n");
153 
154   /*
155    * End of tests.
156    */
157 
158   return 0;
159 }
160