michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: ** File: lock.c michael@0: ** Purpose: test basic locking functions michael@0: ** michael@0: ** Modification History: michael@0: ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. michael@0: ** The debug mode will print all of the printfs associated with this test. michael@0: ** The regress mode will be the default mode. Since the regress tool limits michael@0: ** the output to a one line status:PASS or FAIL,all of the printf statements michael@0: ** have been handled with an if (debug_mode) statement. michael@0: ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to michael@0: ** recognize the return code from tha main program. michael@0: ** michael@0: ** 11-Aug-97 LarryH. Win16 port of NSPR. michael@0: ** - Added "PASS", "FAIL" messages on completion. michael@0: ** - Change stack variables to static scope variables michael@0: ** because of shadow-stack use by Win16 michael@0: ** - Added PR_CALLBACK attribute to functions called by NSPR michael@0: ** - Added command line arguments: michael@0: ** - l to control the number of loops michael@0: ** - c to control the number of CPUs. michael@0: ** (was positional argv). michael@0: ** michael@0: ** michael@0: ***********************************************************************/ michael@0: michael@0: /*********************************************************************** michael@0: ** Includes michael@0: ***********************************************************************/ michael@0: /* Used to get the command line option */ michael@0: #include "plgetopt.h" michael@0: michael@0: #include "prio.h" michael@0: #include "prcmon.h" michael@0: #include "prinit.h" michael@0: #include "prinrval.h" michael@0: #include "prprf.h" michael@0: #include "prlock.h" michael@0: #include "prlog.h" michael@0: #include "prmon.h" michael@0: #include "prmem.h" michael@0: #include "prthread.h" michael@0: #include "prtypes.h" michael@0: michael@0: #include "plstr.h" michael@0: michael@0: #include michael@0: michael@0: #if defined(XP_UNIX) michael@0: #include michael@0: #endif michael@0: michael@0: static PRIntn failed_already=0; michael@0: static PRFileDesc *std_err = NULL; michael@0: static PRBool verbosity = PR_FALSE; michael@0: static PRBool debug_mode = PR_FALSE; michael@0: michael@0: const static PRIntervalTime contention_interval = 50; michael@0: michael@0: typedef struct LockContentious_s { michael@0: PRLock *ml; michael@0: PRInt32 loops; michael@0: PRUint32 contender; michael@0: PRUint32 contentious; michael@0: PRIntervalTime overhead; michael@0: PRIntervalTime interval; michael@0: } LockContentious_t; michael@0: michael@0: typedef struct MonitorContentious_s { michael@0: PRMonitor *ml; michael@0: PRInt32 loops; michael@0: PRUint32 contender; michael@0: PRUint32 contentious; michael@0: PRIntervalTime overhead; michael@0: PRIntervalTime interval; michael@0: } MonitorContentious_t; michael@0: michael@0: michael@0: static PRIntervalTime Sleeper(PRUint32 loops) michael@0: { michael@0: PRIntervalTime predicted = 0; michael@0: while (loops-- > 0) michael@0: { michael@0: predicted += contention_interval; michael@0: (void)PR_Sleep(contention_interval); michael@0: } michael@0: return predicted; michael@0: } /* Sleeper */ michael@0: michael@0: /* michael@0: ** BASIC LOCKS michael@0: */ michael@0: static PRIntervalTime MakeLock(PRUint32 loops) michael@0: { michael@0: PRLock *ml = NULL; michael@0: while (loops-- > 0) michael@0: { michael@0: ml = PR_NewLock(); michael@0: PR_DestroyLock(ml); michael@0: ml = NULL; michael@0: } michael@0: return 0; michael@0: } /* MakeLock */ michael@0: michael@0: static PRIntervalTime NonContentiousLock(PRUint32 loops) michael@0: { michael@0: PRLock *ml = NULL; michael@0: ml = PR_NewLock(); michael@0: while (loops-- > 0) michael@0: { michael@0: PR_Lock(ml); michael@0: PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(ml); michael@0: PR_Unlock(ml); michael@0: } michael@0: PR_DestroyLock(ml); michael@0: return 0; michael@0: } /* NonContentiousLock */ michael@0: michael@0: static void PR_CALLBACK LockContender(void *arg) michael@0: { michael@0: LockContentious_t *contention = (LockContentious_t*)arg; michael@0: while (contention->loops-- > 0) michael@0: { michael@0: PR_Lock(contention->ml); michael@0: PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(contention->ml); michael@0: contention->contender+= 1; michael@0: contention->overhead += contention->interval; michael@0: PR_Sleep(contention->interval); michael@0: PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(contention->ml); michael@0: PR_Unlock(contention->ml); michael@0: } michael@0: } /* LockContender */ michael@0: michael@0: static PRIntervalTime ContentiousLock(PRUint32 loops) michael@0: { michael@0: PRStatus status; michael@0: PRThread *thread = NULL; michael@0: LockContentious_t * contention; michael@0: PRIntervalTime rv, overhead, timein = PR_IntervalNow(); michael@0: michael@0: contention = PR_NEWZAP(LockContentious_t); michael@0: contention->loops = loops; michael@0: contention->overhead = 0; michael@0: contention->ml = PR_NewLock(); michael@0: contention->interval = contention_interval; michael@0: thread = PR_CreateThread( michael@0: PR_USER_THREAD, LockContender, contention, michael@0: PR_PRIORITY_LOW, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); michael@0: PR_ASSERT(thread != NULL); michael@0: michael@0: overhead = PR_IntervalNow() - timein; michael@0: michael@0: while (contention->loops-- > 0) michael@0: { michael@0: PR_Lock(contention->ml); michael@0: PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(contention->ml); michael@0: contention->contentious+= 1; michael@0: contention->overhead += contention->interval; michael@0: PR_Sleep(contention->interval); michael@0: PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(contention->ml); michael@0: PR_Unlock(contention->ml); michael@0: } michael@0: michael@0: timein = PR_IntervalNow(); michael@0: status = PR_JoinThread(thread); michael@0: PR_DestroyLock(contention->ml); michael@0: overhead += (PR_IntervalNow() - timein); michael@0: rv = overhead + contention->overhead; michael@0: if (verbosity) michael@0: PR_fprintf( michael@0: std_err, "Access ratio: %u to %u\n", michael@0: contention->contentious, contention->contender); michael@0: PR_Free(contention); michael@0: return rv; michael@0: } /* ContentiousLock */ michael@0: michael@0: /* michael@0: ** MONITORS michael@0: */ michael@0: static PRIntervalTime MakeMonitor(PRUint32 loops) michael@0: { michael@0: PRMonitor *ml = NULL; michael@0: while (loops-- > 0) michael@0: { michael@0: ml = PR_NewMonitor(); michael@0: PR_DestroyMonitor(ml); michael@0: ml = NULL; michael@0: } michael@0: return 0; michael@0: } /* MakeMonitor */ michael@0: michael@0: static PRIntervalTime NonContentiousMonitor(PRUint32 loops) michael@0: { michael@0: PRMonitor *ml = NULL; michael@0: ml = PR_NewMonitor(); michael@0: while (loops-- > 0) michael@0: { michael@0: PR_EnterMonitor(ml); michael@0: PR_ASSERT_CURRENT_THREAD_IN_MONITOR(ml); michael@0: PR_ExitMonitor(ml); michael@0: } michael@0: PR_DestroyMonitor(ml); michael@0: return 0; michael@0: } /* NonContentiousMonitor */ michael@0: michael@0: static void PR_CALLBACK TryEntry(void *arg) michael@0: { michael@0: PRMonitor *ml = (PRMonitor*)arg; michael@0: if (debug_mode) PR_fprintf(std_err, "Reentrant thread created\n"); michael@0: PR_EnterMonitor(ml); michael@0: PR_ASSERT_CURRENT_THREAD_IN_MONITOR(ml); michael@0: if (debug_mode) PR_fprintf(std_err, "Reentrant thread acquired monitor\n"); michael@0: PR_ExitMonitor(ml); michael@0: if (debug_mode) PR_fprintf(std_err, "Reentrant thread released monitor\n"); michael@0: } /* TryEntry */ michael@0: michael@0: static PRIntervalTime ReentrantMonitor(PRUint32 loops) michael@0: { michael@0: PRStatus status; michael@0: PRThread *thread; michael@0: PRMonitor *ml = PR_NewMonitor(); michael@0: if (debug_mode) PR_fprintf(std_err, "\nMonitor created for reentrant test\n"); michael@0: michael@0: PR_EnterMonitor(ml); michael@0: PR_ASSERT_CURRENT_THREAD_IN_MONITOR(ml); michael@0: PR_EnterMonitor(ml); michael@0: PR_ASSERT_CURRENT_THREAD_IN_MONITOR(ml); michael@0: if (debug_mode) PR_fprintf(std_err, "Monitor acquired twice\n"); michael@0: michael@0: thread = PR_CreateThread( michael@0: PR_USER_THREAD, TryEntry, ml, michael@0: PR_PRIORITY_LOW, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); michael@0: PR_ASSERT(thread != NULL); michael@0: PR_Sleep(PR_SecondsToInterval(1)); michael@0: PR_ASSERT_CURRENT_THREAD_IN_MONITOR(ml); michael@0: michael@0: PR_ExitMonitor(ml); michael@0: PR_ASSERT_CURRENT_THREAD_IN_MONITOR(ml); michael@0: if (debug_mode) PR_fprintf(std_err, "Monitor released first time\n"); michael@0: michael@0: PR_ExitMonitor(ml); michael@0: if (debug_mode) PR_fprintf(std_err, "Monitor released second time\n"); michael@0: michael@0: status = PR_JoinThread(thread); michael@0: if (debug_mode) PR_fprintf(std_err, michael@0: "Reentrant thread joined %s\n", michael@0: (status == PR_SUCCESS) ? "successfully" : "in error"); michael@0: michael@0: PR_DestroyMonitor(ml); michael@0: return 0; michael@0: } /* ReentrantMonitor */ michael@0: michael@0: static void PR_CALLBACK MonitorContender(void *arg) michael@0: { michael@0: MonitorContentious_t *contention = (MonitorContentious_t*)arg; michael@0: while (contention->loops-- > 0) michael@0: { michael@0: PR_EnterMonitor(contention->ml); michael@0: PR_ASSERT_CURRENT_THREAD_IN_MONITOR(contention->ml); michael@0: contention->contender+= 1; michael@0: contention->overhead += contention->interval; michael@0: PR_Sleep(contention->interval); michael@0: PR_ASSERT_CURRENT_THREAD_IN_MONITOR(contention->ml); michael@0: PR_ExitMonitor(contention->ml); michael@0: } michael@0: } /* MonitorContender */ michael@0: michael@0: static PRUint32 ContentiousMonitor(PRUint32 loops) michael@0: { michael@0: PRStatus status; michael@0: PRThread *thread = NULL; michael@0: MonitorContentious_t * contention; michael@0: PRIntervalTime rv, overhead, timein = PR_IntervalNow(); michael@0: michael@0: contention = PR_NEWZAP(MonitorContentious_t); michael@0: contention->loops = loops; michael@0: contention->overhead = 0; michael@0: contention->ml = PR_NewMonitor(); michael@0: contention->interval = contention_interval; michael@0: thread = PR_CreateThread( michael@0: PR_USER_THREAD, MonitorContender, contention, michael@0: PR_PRIORITY_LOW, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); michael@0: PR_ASSERT(thread != NULL); michael@0: michael@0: overhead = PR_IntervalNow() - timein; michael@0: michael@0: while (contention->loops-- > 0) michael@0: { michael@0: PR_EnterMonitor(contention->ml); michael@0: PR_ASSERT_CURRENT_THREAD_IN_MONITOR(contention->ml); michael@0: contention->contentious+= 1; michael@0: contention->overhead += contention->interval; michael@0: PR_Sleep(contention->interval); michael@0: PR_ASSERT_CURRENT_THREAD_IN_MONITOR(contention->ml); michael@0: PR_ExitMonitor(contention->ml); michael@0: } michael@0: michael@0: timein = PR_IntervalNow(); michael@0: status = PR_JoinThread(thread); michael@0: PR_DestroyMonitor(contention->ml); michael@0: overhead += (PR_IntervalNow() - timein); michael@0: rv = overhead + contention->overhead; michael@0: if (verbosity) michael@0: PR_fprintf( michael@0: std_err, "Access ratio: %u to %u\n", michael@0: contention->contentious, contention->contender); michael@0: PR_Free(contention); michael@0: return rv; michael@0: } /* ContentiousMonitor */ michael@0: michael@0: /* michael@0: ** CACHED MONITORS michael@0: */ michael@0: static PRIntervalTime NonContentiousCMonitor(PRUint32 loops) michael@0: { michael@0: MonitorContentious_t contention; michael@0: while (loops-- > 0) michael@0: { michael@0: PR_CEnterMonitor(&contention); michael@0: PR_CExitMonitor(&contention); michael@0: } michael@0: return 0; michael@0: } /* NonContentiousCMonitor */ michael@0: michael@0: static void PR_CALLBACK Contender(void *arg) michael@0: { michael@0: MonitorContentious_t *contention = (MonitorContentious_t*)arg; michael@0: while (contention->loops-- > 0) michael@0: { michael@0: PR_CEnterMonitor(contention); michael@0: contention->contender+= 1; michael@0: contention->overhead += contention->interval; michael@0: PR_Sleep(contention->interval); michael@0: PR_CExitMonitor(contention); michael@0: } michael@0: } /* Contender */ michael@0: michael@0: static PRIntervalTime ContentiousCMonitor(PRUint32 loops) michael@0: { michael@0: PRStatus status; michael@0: PRThread *thread = NULL; michael@0: MonitorContentious_t * contention; michael@0: PRIntervalTime overhead, timein = PR_IntervalNow(); michael@0: michael@0: contention = PR_NEWZAP(MonitorContentious_t); michael@0: contention->ml = NULL; michael@0: contention->loops = loops; michael@0: contention->interval = contention_interval; michael@0: thread = PR_CreateThread( michael@0: PR_USER_THREAD, Contender, contention, michael@0: PR_PRIORITY_LOW, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); michael@0: PR_ASSERT(thread != NULL); michael@0: michael@0: overhead = PR_IntervalNow() - timein; michael@0: michael@0: while (contention->loops-- > 0) michael@0: { michael@0: PR_CEnterMonitor(contention); michael@0: contention->contentious+= 1; michael@0: contention->overhead += contention->interval; michael@0: PR_Sleep(contention->interval); michael@0: PR_CExitMonitor(contention); michael@0: } michael@0: michael@0: timein = PR_IntervalNow(); michael@0: status = PR_JoinThread(thread); michael@0: overhead += (PR_IntervalNow() - timein); michael@0: overhead += overhead + contention->overhead; michael@0: if (verbosity) michael@0: PR_fprintf( michael@0: std_err, "Access ratio: %u to %u\n", michael@0: contention->contentious, contention->contender); michael@0: PR_Free(contention); michael@0: return overhead; michael@0: } /* ContentiousCMonitor */ michael@0: michael@0: static PRIntervalTime Test( michael@0: const char* msg, PRUint32 (*test)(PRUint32 loops), michael@0: PRUint32 loops, PRIntervalTime overhead) michael@0: { michael@0: /* michael@0: * overhead - overhead not measured by the test. michael@0: * duration - wall clock time it took to perform test. michael@0: * predicted - extra time test says should not be counted michael@0: * michael@0: * Time accountable to the test is duration - overhead - predicted michael@0: * All times are Intervals and accumulated for all iterations. michael@0: */ michael@0: PRFloat64 elapsed; michael@0: PRIntervalTime accountable, duration; michael@0: PRUintn spaces = PL_strlen(msg); michael@0: PRIntervalTime timeout, timein = PR_IntervalNow(); michael@0: PRIntervalTime predicted = test(loops); michael@0: timeout = PR_IntervalNow(); michael@0: duration = timeout - timein; michael@0: michael@0: if (debug_mode) michael@0: { michael@0: accountable = duration - predicted; michael@0: accountable -= overhead; michael@0: elapsed = (PRFloat64)PR_IntervalToMicroseconds(accountable); michael@0: PR_fprintf(PR_STDOUT, "%s:", msg); michael@0: while (spaces++ < 50) PR_fprintf(PR_STDOUT, " "); michael@0: if ((PRInt32)accountable < 0) michael@0: PR_fprintf(PR_STDOUT, "*****.** usecs/iteration\n"); michael@0: else michael@0: PR_fprintf(PR_STDOUT, "%8.2f usecs/iteration\n", elapsed/loops); michael@0: } michael@0: return duration; michael@0: } /* Test */ michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRBool rv = PR_TRUE; michael@0: PRIntervalTime duration; michael@0: PRUint32 cpu, cpus = 2, loops = 100; michael@0: michael@0: michael@0: PR_STDIO_INIT(); michael@0: PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); michael@0: { michael@0: /* The command line argument: -d is used to determine if the test is being run michael@0: in debug mode. The regress tool requires only one line output:PASS or FAIL. michael@0: All of the printfs associated with this test has been handled with a if (debug_mode) michael@0: test. michael@0: Command line argument -l sets the number of loops. michael@0: Command line argument -c sets the number of cpus. michael@0: Usage: lock [-d] [-l ] [-c ] michael@0: */ michael@0: PLOptStatus os; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "dvl:c:"); michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) michael@0: { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) michael@0: { michael@0: case 'd': /* debug mode */ michael@0: debug_mode = PR_TRUE; michael@0: break; michael@0: case 'v': /* debug mode */ michael@0: verbosity = PR_TRUE; michael@0: break; michael@0: case 'l': /* number of loops */ michael@0: loops = atoi(opt->value); michael@0: break; michael@0: case 'c': /* number of cpus */ michael@0: cpus = atoi(opt->value); michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: } michael@0: michael@0: /* main test */ michael@0: PR_SetConcurrency(8); michael@0: michael@0: if (loops == 0) loops = 100; michael@0: if (debug_mode) michael@0: { michael@0: std_err = PR_STDERR; michael@0: PR_fprintf(std_err, "Lock: Using %d loops\n", loops); michael@0: } michael@0: michael@0: if (cpus == 0) cpus = 2; michael@0: if (debug_mode) PR_fprintf(std_err, "Lock: Using %d cpu(s)\n", cpus); michael@0: michael@0: (void)Sleeper(10); /* try filling in the caches */ michael@0: michael@0: for (cpu = 1; cpu <= cpus; ++cpu) michael@0: { michael@0: if (debug_mode) PR_fprintf(std_err, "\nLock: Using %d CPU(s)\n", cpu); michael@0: PR_SetConcurrency(cpu); michael@0: michael@0: duration = Test("Overhead of PR_Sleep", Sleeper, loops, 0); michael@0: duration = 0; michael@0: michael@0: (void)Test("Lock creation/deletion", MakeLock, loops, 0); michael@0: (void)Test("Lock non-contentious locking/unlocking", NonContentiousLock, loops, 0); michael@0: (void)Test("Lock contentious locking/unlocking", ContentiousLock, loops, duration); michael@0: (void)Test("Monitor creation/deletion", MakeMonitor, loops, 0); michael@0: (void)Test("Monitor non-contentious locking/unlocking", NonContentiousMonitor, loops, 0); michael@0: (void)Test("Monitor contentious locking/unlocking", ContentiousMonitor, loops, duration); michael@0: michael@0: (void)Test("Cached monitor non-contentious locking/unlocking", NonContentiousCMonitor, loops, 0); michael@0: (void)Test("Cached monitor contentious locking/unlocking", ContentiousCMonitor, loops, duration); michael@0: michael@0: (void)ReentrantMonitor(loops); michael@0: } michael@0: michael@0: if (debug_mode) michael@0: PR_fprintf( michael@0: std_err, "%s: test %s\n", "Lock(mutex) test", michael@0: ((rv) ? "passed" : "failed")); michael@0: else { michael@0: if (!rv) michael@0: failed_already=1; michael@0: } michael@0: michael@0: if(failed_already) michael@0: { michael@0: PR_fprintf(PR_STDOUT, "FAIL\n"); michael@0: return 1; michael@0: } michael@0: else michael@0: { michael@0: PR_fprintf(PR_STDOUT, "PASS\n"); michael@0: return 0; michael@0: } michael@0: michael@0: } /* main */ michael@0: michael@0: /* testlock.c */