1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
//========================================================================
// Multithreading benchmark program, based on the GLFW multi threading
// support.
//
// This program can be used to get an idea of what to expect in terms of
// multithreading granularity performance.
//
// As a "bonus", this program demonstrates how to create a signal
// primitive using the GLFW mutex and condition variable primitives.
//
// Here are some benchmark results:
// (Note: these are not exact measurments, since they are subject to
// varying CPU-loads etc. Some tested systems are multi-user systems
// which were running under anything but optimal conditions)
//
// +------------+-------+-------------+-------------------+------------+
// | Processor | CPUs | OS | Context switches | Mean sleep |
// | | | | per second | time (ms) |
// +------------+-------+-------------+-------------------+------------+
// |Athlon | 1 | Linux | 161942 | 20.000 |
// |710 MHz | | 2.4.3 | | |
// +------------+-------+-------------+-------------------+------------+
// |Athlon | 1 | MS Win2k | 525230 | 10.014 |
// |710 MHz | | | | |
// +------------+-------+-------------+-------------------+------------+
// |Athlon | 1 | MS Win 98 | 23564 | 4.947 |
// |710 MHz | | | | |
// +------------+-------+-------------+-------------------+------------+
// |Pentium III | 1 | MS NT 4.0 | 304694 | 10.014 |
// |500 MHz | | | | |
// +------------+-------+-------------+-------------------+------------+
// |UltraSPARC2 | 6 | SunOS 5.6 | 120867 | 19.355 |
// |400 MHz | | | | |
// +------------+-------+-------------+-------------------+------------+
// |Alpha 21264 | 1 | OSF1 | 131993 | 3.097 |
// |500 MHz | | | | |
// +------------+-------+-------------+-------------------+------------+
// |Alpha 21264 | 2 | OSF1 | 40836 | 1.397 |
// |500 MHz | | | | |
// +------------+-------+-------------+-------------------+------------+
// |68020 (emu) | 1 | AmigaOS 3.1 | 50425 | 40.060 |
// |~200 MHz | | (WinUAE) | | |
// +------------+-------+-------------+-------------------+------------+
//
//========================================================================
#include <stdio.h>
#include <GL/glfw.h>
typedef struct {
GLFWcond cond;
GLFWmutex mutex;
int flag;
} signal_t;
signal_t gotoA, gotoB;
GLFWcond threadDone;
GLFWmutex doneMutex;
int doneCount;
int gotoACount, gotoBCount;
#define MAX_COUNT 10000
//------------------------------------------------------------------------
// InitSignal()
//------------------------------------------------------------------------
void InitSignal( signal_t *s )
{
s->cond = glfwCreateCond();
s->mutex = glfwCreateMutex();
s->flag = 0;
}
//------------------------------------------------------------------------
// KillSignal()
//------------------------------------------------------------------------
void KillSignal( signal_t *s )
{
glfwDestroyCond( s->cond );
glfwDestroyMutex( s->mutex );
s->flag = 0;
}
//------------------------------------------------------------------------
// WaitSignal()
//------------------------------------------------------------------------
void WaitSignal( signal_t *s )
{
glfwLockMutex( s->mutex );
while( !s->flag )
{
glfwWaitCond( s->cond, s->mutex, GLFW_INFINITY );
}
s->flag = 0;
glfwUnlockMutex( s->mutex );
}
//------------------------------------------------------------------------
// SetSignal()
//------------------------------------------------------------------------
void SetSignal( signal_t *s )
{
glfwLockMutex( s->mutex );
s->flag = 1;
glfwUnlockMutex( s->mutex );
glfwSignalCond( s->cond );
}
//------------------------------------------------------------------------
// threadAfun()
//------------------------------------------------------------------------
void GLFWCALL threadAfun( void * arg )
{
int done;
do
{
done = (gotoACount >= MAX_COUNT);
if( !done )
{
gotoACount ++;
SetSignal( &gotoB );
WaitSignal( &gotoA );
}
}
while( !done );
SetSignal( &gotoB );
glfwLockMutex( doneMutex );
doneCount ++;
glfwUnlockMutex( doneMutex );
glfwSignalCond( threadDone );
}
//------------------------------------------------------------------------
// threadBfun()
//------------------------------------------------------------------------
void GLFWCALL threadBfun( void * arg )
{
int done;
do
{
done = (gotoBCount >= MAX_COUNT);
if( !done )
{
gotoBCount ++;
SetSignal( &gotoA );
WaitSignal( &gotoB );
}
}
while( !done );
SetSignal( &gotoA );
glfwLockMutex( doneMutex );
doneCount ++;
glfwUnlockMutex( doneMutex );
glfwSignalCond( threadDone );
}
//------------------------------------------------------------------------
// main()
//------------------------------------------------------------------------
int main( void )
{
GLFWthread threadA, threadB;
double t1, t2, csps;
int done, count, i;
gotoACount = gotoBCount = doneCount = 0;
// Initialize GLFW
if( !glfwInit() )
{
return 0;
}
// Print some program information
printf( "\nMultithreading benchmarking program\n" );
printf( "-----------------------------------\n\n" );
printf( "This program consists of two tests. In the first test " );
printf( "two threads are created,\n" );
printf( "which continously signal/wait each other. This forces " );
printf( "the execution to\n" );
printf( "alternate between the two threads, and gives a measure " );
printf( "of the thread\n" );
printf( "synchronization granularity. In the second test, the " );
printf( "main thread is repeatedly\n" );
printf( "put to sleep for a very short interval using glfwSleep. " );
printf( "The average sleep time\n" );
printf( "is measured, which tells the minimum supported sleep " );
printf( "interval.\n\n" );
printf( "Results:\n" );
printf( "--------\n\n" );
printf( "Number of CPUs: %d\n\n", glfwGetNumberOfProcessors() );
fflush( stdout );
//------------------------------------------------------------------------
// 1) Benchmark thread synchronization granularity
//------------------------------------------------------------------------
// Init mutexes and conditions
doneMutex = glfwCreateMutex();
threadDone = glfwCreateCond();
InitSignal( &gotoA );
InitSignal( &gotoB );
// Create threads A & B
threadA = glfwCreateThread( threadAfun, NULL );
threadB = glfwCreateThread( threadBfun, NULL );
if( threadA == -1 || threadB == -1 )
{
glfwLockMutex( doneMutex );
doneCount = 2;
glfwUnlockMutex( doneMutex );
}
// Wait for both threads to be done
t1 = glfwGetTime();
glfwLockMutex( doneMutex );
do
{
done = (doneCount == 2);
if( !done )
{
glfwWaitCond( threadDone, doneMutex, GLFW_INFINITY );
}
}
while( !done );
glfwUnlockMutex( doneMutex );
t2 = glfwGetTime();
// Display results
count = gotoACount + gotoBCount;
csps = (double)count / (t2-t1);
printf( "Test 1: %.0f context switches / second (%.3f us/switch)\n",
csps, 1e6/csps );
fflush( stdout );
// Wait for threads to die
glfwWaitThread( threadA, GLFW_WAIT );
glfwWaitThread( threadB, GLFW_WAIT );
// Destroy mutexes and conditions
glfwDestroyMutex( doneMutex );
glfwDestroyCond( threadDone );
KillSignal( &gotoA );
KillSignal( &gotoB );
//------------------------------------------------------------------------
// 2) Benchmark thread sleep granularity
//------------------------------------------------------------------------
// Find an initial estimate
t1 = glfwGetTime();
for( i = 0; i < 10; i ++ )
{
glfwSleep( 0.0001 );
}
t2 = glfwGetTime();
// Sleep for roughly 1 s
count = (int)(1.0 / ((t2-t1)/10.0));
t1 = glfwGetTime();
for( i = 0; i < count; i ++ )
{
glfwSleep( 0.0001 );
}
t2 = glfwGetTime();
// Display results
printf( "Test 2: %.3f ms / sleep (mean)\n\n",
1000.0 * (t2-t1) / (double)count );
// Terminate GLFW
glfwTerminate();
return 0;
}
|