1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998-2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #include "nspr.h"
|
---|
39 | #include "plgetopt.h"
|
---|
40 |
|
---|
41 | #include <stdio.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 | #include <string.h>
|
---|
44 |
|
---|
45 | int _debug_on = 0;
|
---|
46 | #define DPRINTF(arg) if (_debug_on) printf arg
|
---|
47 |
|
---|
48 | #ifdef XP_MAC
|
---|
49 | #include "prlog.h"
|
---|
50 | #include "prsem.h"
|
---|
51 | #define printf PR_LogPrint
|
---|
52 | extern void SetupMacPrintfLog(char *logFile);
|
---|
53 | #else
|
---|
54 | #include "obsolete/prsem.h"
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | PRLock *lock;
|
---|
58 | PRMonitor *mon;
|
---|
59 | PRMonitor *mon2;
|
---|
60 |
|
---|
61 | #define DEFAULT_COUNT 1000
|
---|
62 |
|
---|
63 | PRInt32 count;
|
---|
64 |
|
---|
65 | static void nop(int a, int b, int c)
|
---|
66 | {
|
---|
67 | }
|
---|
68 |
|
---|
69 | static void LocalProcedureCall(void)
|
---|
70 | {
|
---|
71 | PRInt32 i;
|
---|
72 |
|
---|
73 | for (i = 0; i < count; i++) {
|
---|
74 | nop(i, i, 5);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | static void DLLProcedureCall(void)
|
---|
79 | {
|
---|
80 | PRInt32 i;
|
---|
81 | PRThreadState state;
|
---|
82 | PRThread *self = PR_CurrentThread();
|
---|
83 |
|
---|
84 | for (i = 0; i < count; i++) {
|
---|
85 | state = PR_GetThreadState(self);
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | static void Now(void)
|
---|
90 | {
|
---|
91 | PRInt32 i;
|
---|
92 | PRTime time;
|
---|
93 |
|
---|
94 | for (i = 0; i < count; i++) {
|
---|
95 | time = PR_Now();
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | static void Interval(void)
|
---|
100 | {
|
---|
101 | PRInt32 i;
|
---|
102 | PRIntervalTime time;
|
---|
103 |
|
---|
104 | for (i = 0; i < count; i++) {
|
---|
105 | time = PR_IntervalNow();
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | static void IdleLock(void)
|
---|
110 | {
|
---|
111 | PRInt32 i;
|
---|
112 |
|
---|
113 | for (i = 0; i < count; i++) {
|
---|
114 | PR_Lock(lock);
|
---|
115 | PR_Unlock(lock);
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | static void IdleMonitor(void)
|
---|
120 | {
|
---|
121 | PRInt32 i;
|
---|
122 |
|
---|
123 | for (i = 0; i < count; i++) {
|
---|
124 | PR_EnterMonitor(mon);
|
---|
125 | PR_ExitMonitor(mon);
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | static void IdleCMonitor(void)
|
---|
130 | {
|
---|
131 | PRInt32 i;
|
---|
132 |
|
---|
133 | for (i = 0; i < count; i++) {
|
---|
134 | PR_CEnterMonitor((void*)7);
|
---|
135 | PR_CExitMonitor((void*)7);
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | /************************************************************************/
|
---|
140 |
|
---|
141 | static void PR_CALLBACK dull(void *arg)
|
---|
142 | {
|
---|
143 | }
|
---|
144 |
|
---|
145 | static void CDThread(void)
|
---|
146 | {
|
---|
147 | PRInt32 i;
|
---|
148 | int num_threads = count;
|
---|
149 |
|
---|
150 | /*
|
---|
151 | * Cannot create too many threads
|
---|
152 | */
|
---|
153 | if (num_threads > 1000)
|
---|
154 | num_threads = 1000;
|
---|
155 |
|
---|
156 | for (i = 0; i < num_threads; i++) {
|
---|
157 | PRThread *t = PR_CreateThread(PR_USER_THREAD,
|
---|
158 | dull, 0,
|
---|
159 | PR_PRIORITY_NORMAL,
|
---|
160 | PR_LOCAL_THREAD,
|
---|
161 | PR_UNJOINABLE_THREAD,
|
---|
162 | 0);
|
---|
163 | if (NULL == t) {
|
---|
164 | fprintf(stderr, "CDThread: cannot create thread %3d\n", i);
|
---|
165 | } else {
|
---|
166 | DPRINTF(("CDThread: created thread %3d \n",i));
|
---|
167 | }
|
---|
168 | PR_Sleep(0);
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | static int alive;
|
---|
173 | static int cxq;
|
---|
174 |
|
---|
175 | static void PR_CALLBACK CXReader(void *arg)
|
---|
176 | {
|
---|
177 | PRInt32 i, n;
|
---|
178 |
|
---|
179 | PR_EnterMonitor(mon);
|
---|
180 | n = count / 2;
|
---|
181 | for (i = 0; i < n; i++) {
|
---|
182 | while (cxq == 0) {
|
---|
183 | DPRINTF(("CXReader: thread = 0x%lx waiting\n",
|
---|
184 | PR_GetCurrentThread()));
|
---|
185 | PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
|
---|
186 | }
|
---|
187 | --cxq;
|
---|
188 | PR_Notify(mon);
|
---|
189 | }
|
---|
190 | PR_ExitMonitor(mon);
|
---|
191 |
|
---|
192 | PR_EnterMonitor(mon2);
|
---|
193 | --alive;
|
---|
194 | PR_Notify(mon2);
|
---|
195 | PR_ExitMonitor(mon2);
|
---|
196 | DPRINTF(("CXReader: thread = 0x%lx exiting\n", PR_GetCurrentThread()));
|
---|
197 | }
|
---|
198 |
|
---|
199 | static void PR_CALLBACK CXWriter(void *arg)
|
---|
200 | {
|
---|
201 | PRInt32 i, n;
|
---|
202 |
|
---|
203 | PR_EnterMonitor(mon);
|
---|
204 | n = count / 2;
|
---|
205 | for (i = 0; i < n; i++) {
|
---|
206 | while (cxq == 1) {
|
---|
207 | DPRINTF(("CXWriter: thread = 0x%lx waiting\n",
|
---|
208 | PR_GetCurrentThread()));
|
---|
209 | PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
|
---|
210 | }
|
---|
211 | ++cxq;
|
---|
212 | PR_Notify(mon);
|
---|
213 | }
|
---|
214 | PR_ExitMonitor(mon);
|
---|
215 |
|
---|
216 | PR_EnterMonitor(mon2);
|
---|
217 | --alive;
|
---|
218 | PR_Notify(mon2);
|
---|
219 | PR_ExitMonitor(mon2);
|
---|
220 | DPRINTF(("CXWriter: thread = 0x%lx exiting\n", PR_GetCurrentThread()));
|
---|
221 | }
|
---|
222 |
|
---|
223 | static void ContextSwitch(PRThreadScope scope1, PRThreadScope scope2)
|
---|
224 | {
|
---|
225 | PRThread *t1, *t2;
|
---|
226 |
|
---|
227 | PR_EnterMonitor(mon2);
|
---|
228 | alive = 2;
|
---|
229 | cxq = 0;
|
---|
230 |
|
---|
231 | t1 = PR_CreateThread(PR_USER_THREAD,
|
---|
232 | CXReader, 0,
|
---|
233 | PR_PRIORITY_NORMAL,
|
---|
234 | scope1,
|
---|
235 | PR_UNJOINABLE_THREAD,
|
---|
236 | 0);
|
---|
237 | if (NULL == t1) {
|
---|
238 | fprintf(stderr, "ContextSwitch: cannot create thread\n");
|
---|
239 | } else {
|
---|
240 | DPRINTF(("ContextSwitch: created %s thread = 0x%lx\n",
|
---|
241 | (scope1 == PR_GLOBAL_THREAD ?
|
---|
242 | "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD"),
|
---|
243 | t1));
|
---|
244 | }
|
---|
245 | t2 = PR_CreateThread(PR_USER_THREAD,
|
---|
246 | CXWriter, 0,
|
---|
247 | PR_PRIORITY_NORMAL,
|
---|
248 | scope2,
|
---|
249 | PR_UNJOINABLE_THREAD,
|
---|
250 | 0);
|
---|
251 | if (NULL == t2) {
|
---|
252 | fprintf(stderr, "ContextSwitch: cannot create thread\n");
|
---|
253 | } else {
|
---|
254 | DPRINTF(("ContextSwitch: created %s thread = 0x%lx\n",
|
---|
255 | (scope2 == PR_GLOBAL_THREAD ?
|
---|
256 | "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD"),
|
---|
257 | t2));
|
---|
258 | }
|
---|
259 |
|
---|
260 | /* Wait for both of the threads to exit */
|
---|
261 | while (alive) {
|
---|
262 | PR_Wait(mon2, PR_INTERVAL_NO_TIMEOUT);
|
---|
263 | }
|
---|
264 | PR_ExitMonitor(mon2);
|
---|
265 | }
|
---|
266 |
|
---|
267 | static void ContextSwitchUU(void)
|
---|
268 | {
|
---|
269 | ContextSwitch(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
|
---|
270 | }
|
---|
271 |
|
---|
272 | static void ContextSwitchUK(void)
|
---|
273 | {
|
---|
274 | ContextSwitch(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
|
---|
275 | }
|
---|
276 |
|
---|
277 | static void ContextSwitchKU(void)
|
---|
278 | {
|
---|
279 | ContextSwitch(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
|
---|
280 | }
|
---|
281 |
|
---|
282 | static void ContextSwitchKK(void)
|
---|
283 | {
|
---|
284 | ContextSwitch(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
|
---|
285 | }
|
---|
286 |
|
---|
287 | /************************************************************************/
|
---|
288 |
|
---|
289 | static void PR_CALLBACK SemaThread(void *argSema)
|
---|
290 | {
|
---|
291 | PRSemaphore **sem = (PRSemaphore **)argSema;
|
---|
292 | PRInt32 i, n;
|
---|
293 |
|
---|
294 | n = count / 2;
|
---|
295 | for (i = 0; i < n; i++) {
|
---|
296 | DPRINTF(("SemaThread: thread = 0x%lx waiting on sem = 0x%lx\n",
|
---|
297 | PR_GetCurrentThread(), sem[0]));
|
---|
298 | PR_WaitSem(sem[0]);
|
---|
299 | DPRINTF(("SemaThread: thread = 0x%lx posting on sem = 0x%lx\n",
|
---|
300 | PR_GetCurrentThread(), sem[1]));
|
---|
301 | PR_PostSem(sem[1]);
|
---|
302 | }
|
---|
303 |
|
---|
304 | PR_EnterMonitor(mon2);
|
---|
305 | --alive;
|
---|
306 | PR_Notify(mon2);
|
---|
307 | PR_ExitMonitor(mon2);
|
---|
308 | DPRINTF(("SemaThread: thread = 0x%lx exiting\n", PR_GetCurrentThread()));
|
---|
309 | }
|
---|
310 |
|
---|
311 | static PRSemaphore *sem_set1[2];
|
---|
312 | static PRSemaphore *sem_set2[2];
|
---|
313 |
|
---|
314 | static void SemaContextSwitch(PRThreadScope scope1, PRThreadScope scope2)
|
---|
315 | {
|
---|
316 | PRThread *t1, *t2;
|
---|
317 | sem_set1[0] = PR_NewSem(1);
|
---|
318 | sem_set1[1] = PR_NewSem(0);
|
---|
319 | sem_set2[0] = sem_set1[1];
|
---|
320 | sem_set2[1] = sem_set1[0];
|
---|
321 |
|
---|
322 | PR_EnterMonitor(mon2);
|
---|
323 | alive = 2;
|
---|
324 | cxq = 0;
|
---|
325 |
|
---|
326 | t1 = PR_CreateThread(PR_USER_THREAD,
|
---|
327 | SemaThread,
|
---|
328 | sem_set1,
|
---|
329 | PR_PRIORITY_NORMAL,
|
---|
330 | scope1,
|
---|
331 | PR_UNJOINABLE_THREAD,
|
---|
332 | 0);
|
---|
333 | if (NULL == t1) {
|
---|
334 | fprintf(stderr, "SemaContextSwitch: cannot create thread\n");
|
---|
335 | } else {
|
---|
336 | DPRINTF(("SemaContextSwitch: created %s thread = 0x%lx\n",
|
---|
337 | (scope1 == PR_GLOBAL_THREAD ?
|
---|
338 | "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD"),
|
---|
339 | t1));
|
---|
340 | }
|
---|
341 | t2 = PR_CreateThread(PR_USER_THREAD,
|
---|
342 | SemaThread,
|
---|
343 | sem_set2,
|
---|
344 | PR_PRIORITY_NORMAL,
|
---|
345 | scope2,
|
---|
346 | PR_UNJOINABLE_THREAD,
|
---|
347 | 0);
|
---|
348 | if (NULL == t2) {
|
---|
349 | fprintf(stderr, "SemaContextSwitch: cannot create thread\n");
|
---|
350 | } else {
|
---|
351 | DPRINTF(("SemaContextSwitch: created %s thread = 0x%lx\n",
|
---|
352 | (scope2 == PR_GLOBAL_THREAD ?
|
---|
353 | "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD"),
|
---|
354 | t2));
|
---|
355 | }
|
---|
356 |
|
---|
357 | /* Wait for both of the threads to exit */
|
---|
358 | while (alive) {
|
---|
359 | PR_Wait(mon2, PR_INTERVAL_NO_TIMEOUT);
|
---|
360 | }
|
---|
361 | PR_ExitMonitor(mon2);
|
---|
362 |
|
---|
363 | PR_DestroySem(sem_set1[0]);
|
---|
364 | PR_DestroySem(sem_set1[1]);
|
---|
365 | }
|
---|
366 |
|
---|
367 | static void SemaContextSwitchUU(void)
|
---|
368 | {
|
---|
369 | SemaContextSwitch(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
|
---|
370 | }
|
---|
371 |
|
---|
372 | static void SemaContextSwitchUK(void)
|
---|
373 | {
|
---|
374 | SemaContextSwitch(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
|
---|
375 | }
|
---|
376 |
|
---|
377 | static void SemaContextSwitchKU(void)
|
---|
378 | {
|
---|
379 | SemaContextSwitch(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
|
---|
380 | }
|
---|
381 |
|
---|
382 | static void SemaContextSwitchKK(void)
|
---|
383 | {
|
---|
384 | SemaContextSwitch(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
|
---|
385 | }
|
---|
386 |
|
---|
387 |
|
---|
388 | /************************************************************************/
|
---|
389 |
|
---|
390 | static void Measure(void (*func)(void), const char *msg)
|
---|
391 | {
|
---|
392 | PRIntervalTime start, stop;
|
---|
393 | double d;
|
---|
394 |
|
---|
395 | start = PR_IntervalNow();
|
---|
396 | (*func)();
|
---|
397 | stop = PR_IntervalNow() - start;
|
---|
398 | d = (double)PR_IntervalToMicroseconds(stop);
|
---|
399 |
|
---|
400 | printf("%40s: %6.2f usec\n", msg, d / count);
|
---|
401 | }
|
---|
402 |
|
---|
403 | int main(int argc, char **argv)
|
---|
404 | {
|
---|
405 | PLOptStatus os;
|
---|
406 | PLOptState *opt = PL_CreateOptState(argc, argv, "dc:");
|
---|
407 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
|
---|
408 | {
|
---|
409 | if (PL_OPT_BAD == os) continue;
|
---|
410 | switch (opt->option)
|
---|
411 | {
|
---|
412 | case 'd': /* debug mode */
|
---|
413 | _debug_on = 1;
|
---|
414 | break;
|
---|
415 | case 'c': /* loop count */
|
---|
416 | count = atoi(opt->value);
|
---|
417 | break;
|
---|
418 | default:
|
---|
419 | break;
|
---|
420 | }
|
---|
421 | }
|
---|
422 | PL_DestroyOptState(opt);
|
---|
423 |
|
---|
424 | if (0 == count) count = DEFAULT_COUNT;
|
---|
425 |
|
---|
426 | PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
|
---|
427 | PR_BlockClockInterrupts();
|
---|
428 | PR_UnblockClockInterrupts();
|
---|
429 | PR_STDIO_INIT();
|
---|
430 |
|
---|
431 | #ifdef XP_MAC
|
---|
432 | SetupMacPrintfLog("perf.log");
|
---|
433 | #endif
|
---|
434 |
|
---|
435 | lock = PR_NewLock();
|
---|
436 | mon = PR_NewMonitor();
|
---|
437 | mon2 = PR_NewMonitor();
|
---|
438 |
|
---|
439 | Measure(LocalProcedureCall, "local procedure call overhead");
|
---|
440 | Measure(DLLProcedureCall, "DLL procedure call overhead");
|
---|
441 | Measure(Now, "current calendar time");
|
---|
442 | Measure(Interval, "interval time");
|
---|
443 | Measure(IdleLock, "idle lock lock/unlock pair");
|
---|
444 | Measure(IdleMonitor, "idle monitor entry/exit pair");
|
---|
445 | Measure(IdleCMonitor, "idle cache monitor entry/exit pair");
|
---|
446 | Measure(CDThread, "create/destroy thread pair");
|
---|
447 | Measure(ContextSwitchUU, "context switch - user/user");
|
---|
448 | Measure(ContextSwitchUK, "context switch - user/kernel");
|
---|
449 | Measure(ContextSwitchKU, "context switch - kernel/user");
|
---|
450 | Measure(ContextSwitchKK, "context switch - kernel/kernel");
|
---|
451 | Measure(SemaContextSwitchUU, "sema context switch - user/user");
|
---|
452 | Measure(SemaContextSwitchUK, "sema context switch - user/kernel");
|
---|
453 | Measure(SemaContextSwitchKU, "sema context switch - kernel/user");
|
---|
454 | Measure(SemaContextSwitchKK, "sema context switch - kernel/kernel");
|
---|
455 |
|
---|
456 | printf("--------------\n");
|
---|
457 | printf("Adding 7 additional CPUs\n");
|
---|
458 |
|
---|
459 | PR_SetConcurrency(8);
|
---|
460 | printf("--------------\n");
|
---|
461 |
|
---|
462 | Measure(LocalProcedureCall, "local procedure call overhead");
|
---|
463 | Measure(DLLProcedureCall, "DLL procedure call overhead");
|
---|
464 | Measure(Now, "current calendar time");
|
---|
465 | Measure(Interval, "interval time");
|
---|
466 | Measure(IdleLock, "idle lock lock/unlock pair");
|
---|
467 | Measure(IdleMonitor, "idle monitor entry/exit pair");
|
---|
468 | Measure(IdleCMonitor, "idle cache monitor entry/exit pair");
|
---|
469 | Measure(CDThread, "create/destroy thread pair");
|
---|
470 | Measure(ContextSwitchUU, "context switch - user/user");
|
---|
471 | Measure(ContextSwitchUK, "context switch - user/kernel");
|
---|
472 | Measure(ContextSwitchKU, "context switch - kernel/user");
|
---|
473 | Measure(ContextSwitchKK, "context switch - kernel/kernel");
|
---|
474 | Measure(SemaContextSwitchUU, "sema context switch - user/user");
|
---|
475 | Measure(SemaContextSwitchUK, "sema context switch - user/kernel");
|
---|
476 | Measure(SemaContextSwitchKU, "sema context switch - kernel/user");
|
---|
477 | Measure(SemaContextSwitchKK, "sema context switch - kernel/kernel");
|
---|
478 |
|
---|
479 | PR_DestroyLock(lock);
|
---|
480 | PR_DestroyMonitor(mon);
|
---|
481 | PR_DestroyMonitor(mon2);
|
---|
482 |
|
---|
483 | PR_Cleanup();
|
---|
484 | return 0;
|
---|
485 | }
|
---|