1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
14 #define DPRINTF(arg) if (_debug_on) printf arg
16 #include "obsolete/prsem.h"
22 #define DEFAULT_COUNT 1000
26 static void nop(int a, int b, int c)
30 static void LocalProcedureCall(void)
34 for (i = 0; i < count; i++) {
39 static void DLLProcedureCall(void)
43 PRThread *self = PR_GetCurrentThread();
45 for (i = 0; i < count; i++) {
46 state = PR_GetThreadState(self);
55 for (i = 0; i < count; i++) {
60 static void Interval(void)
65 for (i = 0; i < count; i++) {
66 time = PR_IntervalNow();
70 static void IdleLock(void)
74 for (i = 0; i < count; i++) {
80 static void IdleMonitor(void)
84 for (i = 0; i < count; i++) {
90 static void IdleCMonitor(void)
94 for (i = 0; i < count; i++) {
95 PR_CEnterMonitor((void*)7);
96 PR_CExitMonitor((void*)7);
100 /************************************************************************/
102 static void PR_CALLBACK dull(void *arg)
106 static void CDThread(void)
109 int num_threads = count;
112 * Cannot create too many threads
114 if (num_threads > 1000)
117 for (i = 0; i < num_threads; i++) {
118 PRThread *t = PR_CreateThread(PR_USER_THREAD,
122 PR_UNJOINABLE_THREAD,
125 fprintf(stderr, "CDThread: cannot create thread %3d\n", i);
127 DPRINTF(("CDThread: created thread %3d \n",i));
136 static void PR_CALLBACK CXReader(void *arg)
140 PR_EnterMonitor(mon);
142 for (i = 0; i < n; i++) {
144 DPRINTF(("CXReader: thread = 0x%lx waiting\n",
145 PR_GetCurrentThread()));
146 PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
153 PR_EnterMonitor(mon2);
156 PR_ExitMonitor(mon2);
157 DPRINTF(("CXReader: thread = 0x%lx exiting\n", PR_GetCurrentThread()));
160 static void PR_CALLBACK CXWriter(void *arg)
164 PR_EnterMonitor(mon);
166 for (i = 0; i < n; i++) {
168 DPRINTF(("CXWriter: thread = 0x%lx waiting\n",
169 PR_GetCurrentThread()));
170 PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
177 PR_EnterMonitor(mon2);
180 PR_ExitMonitor(mon2);
181 DPRINTF(("CXWriter: thread = 0x%lx exiting\n", PR_GetCurrentThread()));
184 static void ContextSwitch(PRThreadScope scope1, PRThreadScope scope2)
188 PR_EnterMonitor(mon2);
192 t1 = PR_CreateThread(PR_USER_THREAD,
196 PR_UNJOINABLE_THREAD,
199 fprintf(stderr, "ContextSwitch: cannot create thread\n");
201 DPRINTF(("ContextSwitch: created %s thread = 0x%lx\n",
202 (scope1 == PR_GLOBAL_THREAD ?
203 "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD"),
206 t2 = PR_CreateThread(PR_USER_THREAD,
210 PR_UNJOINABLE_THREAD,
213 fprintf(stderr, "ContextSwitch: cannot create thread\n");
215 DPRINTF(("ContextSwitch: created %s thread = 0x%lx\n",
216 (scope2 == PR_GLOBAL_THREAD ?
217 "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD"),
221 /* Wait for both of the threads to exit */
223 PR_Wait(mon2, PR_INTERVAL_NO_TIMEOUT);
225 PR_ExitMonitor(mon2);
228 static void ContextSwitchUU(void)
230 ContextSwitch(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
233 static void ContextSwitchUK(void)
235 ContextSwitch(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
238 static void ContextSwitchKU(void)
240 ContextSwitch(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
243 static void ContextSwitchKK(void)
245 ContextSwitch(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
248 /************************************************************************/
250 static void PR_CALLBACK SemaThread(void *argSema)
252 PRSemaphore **sem = (PRSemaphore **)argSema;
256 for (i = 0; i < n; i++) {
257 DPRINTF(("SemaThread: thread = 0x%lx waiting on sem = 0x%lx\n",
258 PR_GetCurrentThread(), sem[0]));
260 DPRINTF(("SemaThread: thread = 0x%lx posting on sem = 0x%lx\n",
261 PR_GetCurrentThread(), sem[1]));
265 PR_EnterMonitor(mon2);
268 PR_ExitMonitor(mon2);
269 DPRINTF(("SemaThread: thread = 0x%lx exiting\n", PR_GetCurrentThread()));
272 static PRSemaphore *sem_set1[2];
273 static PRSemaphore *sem_set2[2];
275 static void SemaContextSwitch(PRThreadScope scope1, PRThreadScope scope2)
278 sem_set1[0] = PR_NewSem(1);
279 sem_set1[1] = PR_NewSem(0);
280 sem_set2[0] = sem_set1[1];
281 sem_set2[1] = sem_set1[0];
283 PR_EnterMonitor(mon2);
287 t1 = PR_CreateThread(PR_USER_THREAD,
292 PR_UNJOINABLE_THREAD,
295 fprintf(stderr, "SemaContextSwitch: cannot create thread\n");
297 DPRINTF(("SemaContextSwitch: created %s thread = 0x%lx\n",
298 (scope1 == PR_GLOBAL_THREAD ?
299 "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD"),
302 t2 = PR_CreateThread(PR_USER_THREAD,
307 PR_UNJOINABLE_THREAD,
310 fprintf(stderr, "SemaContextSwitch: cannot create thread\n");
312 DPRINTF(("SemaContextSwitch: created %s thread = 0x%lx\n",
313 (scope2 == PR_GLOBAL_THREAD ?
314 "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD"),
318 /* Wait for both of the threads to exit */
320 PR_Wait(mon2, PR_INTERVAL_NO_TIMEOUT);
322 PR_ExitMonitor(mon2);
324 PR_DestroySem(sem_set1[0]);
325 PR_DestroySem(sem_set1[1]);
328 static void SemaContextSwitchUU(void)
330 SemaContextSwitch(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
333 static void SemaContextSwitchUK(void)
335 SemaContextSwitch(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
338 static void SemaContextSwitchKU(void)
340 SemaContextSwitch(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
343 static void SemaContextSwitchKK(void)
345 SemaContextSwitch(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
349 /************************************************************************/
351 static void Measure(void (*func)(void), const char *msg)
353 PRIntervalTime start, stop;
356 start = PR_IntervalNow();
358 stop = PR_IntervalNow() - start;
359 d = (double)PR_IntervalToMicroseconds(stop);
361 printf("%40s: %6.2f usec\n", msg, d / count);
364 int main(int argc, char **argv)
367 PLOptState *opt = PL_CreateOptState(argc, argv, "dc:");
368 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
370 if (PL_OPT_BAD == os) continue;
373 case 'd': /* debug mode */
376 case 'c': /* loop count */
377 count = atoi(opt->value);
383 PL_DestroyOptState(opt);
385 if (0 == count) count = DEFAULT_COUNT;
387 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
388 PR_BlockClockInterrupts();
389 PR_UnblockClockInterrupts();
393 mon = PR_NewMonitor();
394 mon2 = PR_NewMonitor();
396 Measure(LocalProcedureCall, "local procedure call overhead");
397 Measure(DLLProcedureCall, "DLL procedure call overhead");
398 Measure(Now, "current calendar time");
399 Measure(Interval, "interval time");
400 Measure(IdleLock, "idle lock lock/unlock pair");
401 Measure(IdleMonitor, "idle monitor entry/exit pair");
402 Measure(IdleCMonitor, "idle cache monitor entry/exit pair");
403 Measure(CDThread, "create/destroy thread pair");
404 Measure(ContextSwitchUU, "context switch - user/user");
405 Measure(ContextSwitchUK, "context switch - user/kernel");
406 Measure(ContextSwitchKU, "context switch - kernel/user");
407 Measure(ContextSwitchKK, "context switch - kernel/kernel");
408 Measure(SemaContextSwitchUU, "sema context switch - user/user");
409 Measure(SemaContextSwitchUK, "sema context switch - user/kernel");
410 Measure(SemaContextSwitchKU, "sema context switch - kernel/user");
411 Measure(SemaContextSwitchKK, "sema context switch - kernel/kernel");
413 printf("--------------\n");
414 printf("Adding 7 additional CPUs\n");
416 PR_SetConcurrency(8);
417 printf("--------------\n");
419 Measure(LocalProcedureCall, "local procedure call overhead");
420 Measure(DLLProcedureCall, "DLL procedure call overhead");
421 Measure(Now, "current calendar time");
422 Measure(Interval, "interval time");
423 Measure(IdleLock, "idle lock lock/unlock pair");
424 Measure(IdleMonitor, "idle monitor entry/exit pair");
425 Measure(IdleCMonitor, "idle cache monitor entry/exit pair");
426 Measure(CDThread, "create/destroy thread pair");
427 Measure(ContextSwitchUU, "context switch - user/user");
428 Measure(ContextSwitchUK, "context switch - user/kernel");
429 Measure(ContextSwitchKU, "context switch - kernel/user");
430 Measure(ContextSwitchKK, "context switch - kernel/kernel");
431 Measure(SemaContextSwitchUU, "sema context switch - user/user");
432 Measure(SemaContextSwitchUK, "sema context switch - user/kernel");
433 Measure(SemaContextSwitchKU, "sema context switch - kernel/user");
434 Measure(SemaContextSwitchKK, "sema context switch - kernel/kernel");
436 PR_DestroyLock(lock);
437 PR_DestroyMonitor(mon);
438 PR_DestroyMonitor(mon2);