VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/lock.c@ 1

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.2 KB
Line 
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/*
39** File: lock.c
40** Purpose: test basic locking functions
41**
42** Modification History:
43** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
44** The debug mode will print all of the printfs associated with this test.
45** The regress mode will be the default mode. Since the regress tool limits
46** the output to a one line status:PASS or FAIL,all of the printf statements
47** have been handled with an if (debug_mode) statement.
48** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
49** recognize the return code from tha main program.
50**
51** 11-Aug-97 LarryH. Win16 port of NSPR.
52** - Added "PASS", "FAIL" messages on completion.
53** - Change stack variables to static scope variables
54** because of shadow-stack use by Win16
55** - Added PR_CALLBACK attribute to functions called by NSPR
56** - Added command line arguments:
57** - l <num> to control the number of loops
58** - c <num> to control the number of CPUs.
59** (was positional argv).
60**
61**
62***********************************************************************/
63
64/***********************************************************************
65** Includes
66***********************************************************************/
67/* Used to get the command line option */
68#include "plgetopt.h"
69
70#include "prio.h"
71#include "prcmon.h"
72#include "prinit.h"
73#include "prinrval.h"
74#include "prprf.h"
75#include "prlock.h"
76#include "prlog.h"
77#include "prmon.h"
78#include "prmem.h"
79#include "prthread.h"
80#include "prtypes.h"
81
82#include "plstr.h"
83
84#include <stdlib.h>
85
86#if defined(XP_UNIX)
87#include <string.h>
88#endif
89
90#ifdef XP_MAC
91#include "prlog.h"
92#define printf PR_LogPrint
93extern void SetupMacPrintfLog(char *logFile);
94#endif
95
96static PRIntn failed_already=0;
97static PRFileDesc *std_err = NULL;
98static PRBool verbosity = PR_FALSE;
99static PRBool debug_mode = PR_FALSE;
100
101const static PRIntervalTime contention_interval = 50;
102
103typedef struct LockContentious_s {
104 PRLock *ml;
105 PRInt32 loops;
106 PRUint32 contender;
107 PRUint32 contentious;
108 PRIntervalTime overhead;
109 PRIntervalTime interval;
110} LockContentious_t;
111
112typedef struct MonitorContentious_s {
113 PRMonitor *ml;
114 PRInt32 loops;
115 PRUint32 contender;
116 PRUint32 contentious;
117 PRIntervalTime overhead;
118 PRIntervalTime interval;
119} MonitorContentious_t;
120
121
122static PRIntervalTime Sleeper(PRUint32 loops)
123{
124 PRIntervalTime predicted = 0;
125 while (loops-- > 0)
126 {
127 predicted += contention_interval;
128 (void)PR_Sleep(contention_interval);
129 }
130 return predicted;
131} /* Sleeper */
132
133/*
134** BASIC LOCKS
135*/
136static PRIntervalTime MakeLock(PRUint32 loops)
137{
138 PRLock *ml = NULL;
139 while (loops-- > 0)
140 {
141 ml = PR_NewLock();
142 PR_DestroyLock(ml);
143 ml = NULL;
144 }
145 return 0;
146} /* MakeLock */
147
148static PRIntervalTime NonContentiousLock(PRUint32 loops)
149{
150 PRLock *ml = NULL;
151 ml = PR_NewLock();
152 while (loops-- > 0)
153 {
154 PR_Lock(ml);
155 PR_Unlock(ml);
156 }
157 PR_DestroyLock(ml);
158 return 0;
159} /* NonContentiousLock */
160
161static void PR_CALLBACK LockContender(void *arg)
162{
163 LockContentious_t *contention = (LockContentious_t*)arg;
164 while (contention->loops-- > 0)
165 {
166 PR_Lock(contention->ml);
167 contention->contender+= 1;
168 contention->overhead += contention->interval;
169 PR_Sleep(contention->interval);
170 PR_Unlock(contention->ml);
171 }
172} /* LockContender */
173
174static PRIntervalTime ContentiousLock(PRUint32 loops)
175{
176 PRStatus status;
177 PRThread *thread = NULL;
178 LockContentious_t * contention;
179 PRIntervalTime rv, overhead, timein = PR_IntervalNow();
180
181 contention = PR_NEWZAP(LockContentious_t);
182 contention->loops = loops;
183 contention->overhead = 0;
184 contention->ml = PR_NewLock();
185 contention->interval = contention_interval;
186 thread = PR_CreateThread(
187 PR_USER_THREAD, LockContender, contention,
188 PR_PRIORITY_LOW, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
189 PR_ASSERT(thread != NULL);
190
191 overhead = PR_IntervalNow() - timein;
192
193 while (contention->loops-- > 0)
194 {
195 PR_Lock(contention->ml);
196 contention->contentious+= 1;
197 contention->overhead += contention->interval;
198 PR_Sleep(contention->interval);
199 PR_Unlock(contention->ml);
200 }
201
202 timein = PR_IntervalNow();
203 status = PR_JoinThread(thread);
204 PR_DestroyLock(contention->ml);
205 overhead += (PR_IntervalNow() - timein);
206 rv = overhead + contention->overhead;
207 if (verbosity)
208 PR_fprintf(
209 std_err, "Access ratio: %u to %u\n",
210 contention->contentious, contention->contender);
211 PR_Free(contention);
212 return rv;
213} /* ContentiousLock */
214
215/*
216** MONITORS
217*/
218static PRIntervalTime MakeMonitor(PRUint32 loops)
219{
220 PRMonitor *ml = NULL;
221 while (loops-- > 0)
222 {
223 ml = PR_NewMonitor();
224 PR_DestroyMonitor(ml);
225 ml = NULL;
226 }
227 return 0;
228} /* MakeMonitor */
229
230static PRIntervalTime NonContentiousMonitor(PRUint32 loops)
231{
232 PRMonitor *ml = NULL;
233 ml = PR_NewMonitor();
234 while (loops-- > 0)
235 {
236 PR_EnterMonitor(ml);
237 PR_ExitMonitor(ml);
238 }
239 PR_DestroyMonitor(ml);
240 return 0;
241} /* NonContentiousMonitor */
242
243static void PR_CALLBACK TryEntry(void *arg)
244{
245 PRMonitor *ml = (PRMonitor*)arg;
246 if (debug_mode) PR_fprintf(std_err, "Reentrant thread created\n");
247 PR_EnterMonitor(ml);
248 if (debug_mode) PR_fprintf(std_err, "Reentrant thread acquired monitor\n");
249 PR_ExitMonitor(ml);
250 if (debug_mode) PR_fprintf(std_err, "Reentrant thread released monitor\n");
251} /* TryEntry */
252
253static PRIntervalTime ReentrantMonitor(PRUint32 loops)
254{
255 PRStatus status;
256 PRThread *thread;
257 PRMonitor *ml = PR_NewMonitor();
258 if (debug_mode) PR_fprintf(std_err, "\nMonitor created for reentrant test\n");
259
260 PR_EnterMonitor(ml);
261 PR_EnterMonitor(ml);
262 if (debug_mode) PR_fprintf(std_err, "Monitor acquired twice\n");
263
264 thread = PR_CreateThread(
265 PR_USER_THREAD, TryEntry, ml,
266 PR_PRIORITY_LOW, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
267 PR_ASSERT(thread != NULL);
268 PR_Sleep(PR_SecondsToInterval(1));
269
270 PR_ExitMonitor(ml);
271 if (debug_mode) PR_fprintf(std_err, "Monitor released first time\n");
272
273 PR_ExitMonitor(ml);
274 if (debug_mode) PR_fprintf(std_err, "Monitor released second time\n");
275
276 status = PR_JoinThread(thread);
277 if (debug_mode) PR_fprintf(std_err,
278 "Reentrant thread joined %s\n",
279 (status == PR_SUCCESS) ? "successfully" : "in error");
280
281 PR_DestroyMonitor(ml);
282 return 0;
283} /* ReentrantMonitor */
284
285static void PR_CALLBACK MonitorContender(void *arg)
286{
287 MonitorContentious_t *contention = (MonitorContentious_t*)arg;
288 while (contention->loops-- > 0)
289 {
290 PR_EnterMonitor(contention->ml);
291 contention->contender+= 1;
292 contention->overhead += contention->interval;
293 PR_Sleep(contention->interval);
294 PR_ExitMonitor(contention->ml);
295 }
296} /* MonitorContender */
297
298static PRUint32 ContentiousMonitor(PRUint32 loops)
299{
300 PRStatus status;
301 PRThread *thread = NULL;
302 MonitorContentious_t * contention;
303 PRIntervalTime rv, overhead, timein = PR_IntervalNow();
304
305 contention = PR_NEWZAP(MonitorContentious_t);
306 contention->loops = loops;
307 contention->overhead = 0;
308 contention->ml = PR_NewMonitor();
309 contention->interval = contention_interval;
310 thread = PR_CreateThread(
311 PR_USER_THREAD, MonitorContender, contention,
312 PR_PRIORITY_LOW, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
313 PR_ASSERT(thread != NULL);
314
315 overhead = PR_IntervalNow() - timein;
316
317 while (contention->loops-- > 0)
318 {
319 PR_EnterMonitor(contention->ml);
320 contention->contentious+= 1;
321 contention->overhead += contention->interval;
322 PR_Sleep(contention->interval);
323 PR_ExitMonitor(contention->ml);
324 }
325
326 timein = PR_IntervalNow();
327 status = PR_JoinThread(thread);
328 PR_DestroyMonitor(contention->ml);
329 overhead += (PR_IntervalNow() - timein);
330 rv = overhead + contention->overhead;
331 if (verbosity)
332 PR_fprintf(
333 std_err, "Access ratio: %u to %u\n",
334 contention->contentious, contention->contender);
335 PR_Free(contention);
336 return rv;
337} /* ContentiousMonitor */
338
339/*
340** CACHED MONITORS
341*/
342static PRIntervalTime NonContentiousCMonitor(PRUint32 loops)
343{
344 MonitorContentious_t contention;
345 while (loops-- > 0)
346 {
347 PR_CEnterMonitor(&contention);
348 PR_CExitMonitor(&contention);
349 }
350 return 0;
351} /* NonContentiousCMonitor */
352
353static void PR_CALLBACK Contender(void *arg)
354{
355 MonitorContentious_t *contention = (MonitorContentious_t*)arg;
356 while (contention->loops-- > 0)
357 {
358 PR_CEnterMonitor(contention);
359 contention->contender+= 1;
360 contention->overhead += contention->interval;
361 PR_Sleep(contention->interval);
362 PR_CExitMonitor(contention);
363 }
364} /* Contender */
365
366static PRIntervalTime ContentiousCMonitor(PRUint32 loops)
367{
368 PRStatus status;
369 PRThread *thread = NULL;
370 MonitorContentious_t * contention;
371 PRIntervalTime overhead, timein = PR_IntervalNow();
372
373 contention = PR_NEWZAP(MonitorContentious_t);
374 contention->ml = NULL;
375 contention->loops = loops;
376 contention->interval = contention_interval;
377 thread = PR_CreateThread(
378 PR_USER_THREAD, Contender, contention,
379 PR_PRIORITY_LOW, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
380 PR_ASSERT(thread != NULL);
381
382 overhead = PR_IntervalNow() - timein;
383
384 while (contention->loops-- > 0)
385 {
386 PR_CEnterMonitor(contention);
387 contention->contentious+= 1;
388 contention->overhead += contention->interval;
389 PR_Sleep(contention->interval);
390 PR_CExitMonitor(contention);
391 }
392
393 timein = PR_IntervalNow();
394 status = PR_JoinThread(thread);
395 overhead += (PR_IntervalNow() - timein);
396 overhead += overhead + contention->overhead;
397 if (verbosity)
398 PR_fprintf(
399 std_err, "Access ratio: %u to %u\n",
400 contention->contentious, contention->contender);
401 PR_Free(contention);
402 return overhead;
403} /* ContentiousCMonitor */
404
405static PRIntervalTime Test(
406 const char* msg, PRUint32 (*test)(PRUint32 loops),
407 PRUint32 loops, PRIntervalTime overhead)
408{
409 /*
410 * overhead - overhead not measured by the test.
411 * duration - wall clock time it took to perform test.
412 * predicted - extra time test says should not be counted
413 *
414 * Time accountable to the test is duration - overhead - predicted
415 * All times are Intervals and accumulated for all iterations.
416 */
417 PRFloat64 elapsed;
418 PRIntervalTime accountable, duration;
419 PRUintn spaces = PL_strlen(msg);
420 PRIntervalTime timeout, timein = PR_IntervalNow();
421 PRIntervalTime predicted = test(loops);
422 timeout = PR_IntervalNow();
423 duration = timeout - timein;
424
425 if (debug_mode)
426 {
427 accountable = duration - predicted;
428 accountable -= overhead;
429 elapsed = (PRFloat64)PR_IntervalToMicroseconds(accountable);
430 PR_fprintf(PR_STDOUT, "%s:", msg);
431 while (spaces++ < 50) PR_fprintf(PR_STDOUT, " ");
432 if ((PRInt32)accountable < 0)
433 PR_fprintf(PR_STDOUT, "*****.** usecs/iteration\n");
434 else
435 PR_fprintf(PR_STDOUT, "%8.2f usecs/iteration\n", elapsed/loops);
436 }
437 return duration;
438} /* Test */
439
440int main(int argc, char **argv)
441{
442 PRBool rv = PR_TRUE;
443 PRIntervalTime duration;
444 PRUint32 cpu, cpus = 2, loops = 100;
445
446
447 PR_STDIO_INIT();
448 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
449 {
450 /* The command line argument: -d is used to determine if the test is being run
451 in debug mode. The regress tool requires only one line output:PASS or FAIL.
452 All of the printfs associated with this test has been handled with a if (debug_mode)
453 test.
454 Command line argument -l <num> sets the number of loops.
455 Command line argument -c <num> sets the number of cpus.
456 Usage: lock [-d] [-l <num>] [-c <num>]
457 */
458 PLOptStatus os;
459 PLOptState *opt = PL_CreateOptState(argc, argv, "dvl:c:");
460 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
461 {
462 if (PL_OPT_BAD == os) continue;
463 switch (opt->option)
464 {
465 case 'd': /* debug mode */
466 debug_mode = PR_TRUE;
467 break;
468 case 'v': /* debug mode */
469 verbosity = PR_TRUE;
470 break;
471 case 'l': /* number of loops */
472 loops = atoi(opt->value);
473 break;
474 case 'c': /* number of cpus */
475 cpus = atoi(opt->value);
476 break;
477 default:
478 break;
479 }
480 }
481 PL_DestroyOptState(opt);
482 }
483
484 /* main test */
485 PR_SetConcurrency(8);
486
487#ifdef XP_MAC
488 SetupMacPrintfLog("lock.log");
489 debug_mode = 1;
490#endif
491
492 if (loops == 0) loops = 100;
493 if (debug_mode)
494 {
495 std_err = PR_STDERR;
496 PR_fprintf(std_err, "Lock: Using %d loops\n", loops);
497 }
498
499 if (cpus == 0) cpus = 2;
500 if (debug_mode) PR_fprintf(std_err, "Lock: Using %d cpu(s)\n", cpus);
501
502 (void)Sleeper(10); /* try filling in the caches */
503
504 for (cpu = 1; cpu <= cpus; ++cpu)
505 {
506 if (debug_mode) PR_fprintf(std_err, "\nLock: Using %d CPU(s)\n", cpu);
507 PR_SetConcurrency(cpu);
508
509 duration = Test("Overhead of PR_Sleep", Sleeper, loops, 0);
510 duration = 0;
511
512 (void)Test("Lock creation/deletion", MakeLock, loops, 0);
513 (void)Test("Lock non-contentious locking/unlocking", NonContentiousLock, loops, 0);
514 (void)Test("Lock contentious locking/unlocking", ContentiousLock, loops, duration);
515 (void)Test("Monitor creation/deletion", MakeMonitor, loops, 0);
516 (void)Test("Monitor non-contentious locking/unlocking", NonContentiousMonitor, loops, 0);
517 (void)Test("Monitor contentious locking/unlocking", ContentiousMonitor, loops, duration);
518
519 (void)Test("Cached monitor non-contentious locking/unlocking", NonContentiousCMonitor, loops, 0);
520 (void)Test("Cached monitor contentious locking/unlocking", ContentiousCMonitor, loops, duration);
521
522 (void)ReentrantMonitor(loops);
523 }
524
525 if (debug_mode)
526 PR_fprintf(
527 std_err, "%s: test %s\n", "Lock(mutex) test",
528 ((rv) ? "passed" : "failed"));
529 else {
530 if (!rv)
531 failed_already=1;
532 }
533
534 if(failed_already)
535 {
536 PR_fprintf(PR_STDOUT, "FAIL\n");
537 return 1;
538 }
539 else
540 {
541 PR_fprintf(PR_STDOUT, "PASS\n");
542 return 0;
543 }
544
545} /* main */
546
547/* testlock.c */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette