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: inrval.c michael@0: ** description: Interval conversion test. michael@0: ** Modification History: michael@0: ** 15-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: /*********************************************************************** michael@0: ** Includes michael@0: ***********************************************************************/ michael@0: /* Used to get the command line option */ michael@0: #include "plgetopt.h" michael@0: michael@0: #include "prinit.h" michael@0: #include "obsolete/pralarm.h" michael@0: michael@0: #include "prio.h" michael@0: #include "prprf.h" michael@0: #include "prlock.h" michael@0: #include "prlong.h" michael@0: #include "prcvar.h" michael@0: #include "prinrval.h" michael@0: #include "prtime.h" michael@0: michael@0: #include "plgetopt.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: static PRIntn debug_mode; michael@0: static PRFileDesc *output; michael@0: michael@0: michael@0: static void TestConversions(void) michael@0: { michael@0: PRIntervalTime ticks = PR_TicksPerSecond(); michael@0: michael@0: if (debug_mode) { michael@0: PR_fprintf(output, "PR_TicksPerSecond: %ld\n\n", ticks); michael@0: PR_fprintf(output, "PR_SecondsToInterval(1): %ld\n", PR_SecondsToInterval(1)); michael@0: PR_fprintf(output, "PR_MillisecondsToInterval(1000): %ld\n", PR_MillisecondsToInterval(1000)); michael@0: PR_fprintf(output, "PR_MicrosecondsToInterval(1000000): %ld\n\n", PR_MicrosecondsToInterval(1000000)); michael@0: michael@0: PR_fprintf(output, "PR_SecondsToInterval(3): %ld\n", PR_SecondsToInterval(3)); michael@0: PR_fprintf(output, "PR_MillisecondsToInterval(3000): %ld\n", PR_MillisecondsToInterval(3000)); michael@0: PR_fprintf(output, "PR_MicrosecondsToInterval(3000000): %ld\n\n", PR_MicrosecondsToInterval(3000000)); michael@0: michael@0: PR_fprintf(output, "PR_IntervalToSeconds(%ld): %ld\n", ticks, PR_IntervalToSeconds(ticks)); michael@0: PR_fprintf(output, "PR_IntervalToMilliseconds(%ld): %ld\n", ticks, PR_IntervalToMilliseconds(ticks)); michael@0: PR_fprintf(output, "PR_IntervalToMicroseconds(%ld): %ld\n\n", ticks, PR_IntervalToMicroseconds(ticks)); michael@0: michael@0: ticks *= 3; michael@0: PR_fprintf(output, "PR_IntervalToSeconds(%ld): %ld\n", ticks, PR_IntervalToSeconds(ticks)); michael@0: PR_fprintf(output, "PR_IntervalToMilliseconds(%ld): %ld\n", ticks, PR_IntervalToMilliseconds(ticks)); michael@0: PR_fprintf(output, "PR_IntervalToMicroseconds(%ld): %ld\n\n", ticks, PR_IntervalToMicroseconds(ticks)); michael@0: } /*end debug mode */ michael@0: } /* TestConversions */ michael@0: michael@0: static void TestIntervalOverhead(void) michael@0: { michael@0: /* Hopefully the optimizer won't delete this function */ michael@0: PRUint32 elapsed, per_call, loops = 1000000; michael@0: michael@0: PRIntervalTime timeout, timein = PR_IntervalNow(); michael@0: while (--loops > 0) michael@0: timeout = PR_IntervalNow(); michael@0: michael@0: elapsed = 1000U * PR_IntervalToMicroseconds(timeout - timein); michael@0: per_call = elapsed / 1000000U; michael@0: PR_fprintf( michael@0: output, "Overhead of 'PR_IntervalNow()' is %u nsecs\n\n", per_call); michael@0: } /* TestIntervalOverhead */ michael@0: michael@0: static void TestNowOverhead(void) michael@0: { michael@0: PRTime timeout, timein; michael@0: PRInt32 overhead, loops = 1000000; michael@0: PRInt64 elapsed, per_call, ten23rd, ten26th; michael@0: michael@0: LL_I2L(ten23rd, 1000); michael@0: LL_I2L(ten26th, 1000000); michael@0: michael@0: timein = PR_Now(); michael@0: while (--loops > 0) michael@0: timeout = PR_Now(); michael@0: michael@0: LL_SUB(elapsed, timeout, timein); michael@0: LL_MUL(elapsed, elapsed, ten23rd); michael@0: LL_DIV(per_call, elapsed, ten26th); michael@0: LL_L2I(overhead, per_call); michael@0: PR_fprintf( michael@0: output, "Overhead of 'PR_Now()' is %u nsecs\n\n", overhead); michael@0: } /* TestNowOverhead */ michael@0: michael@0: static void TestIntervals(void) michael@0: { michael@0: PRStatus rv; michael@0: PRUint32 delta; michael@0: PRInt32 seconds; michael@0: PRUint64 elapsed, thousand; michael@0: PRTime timein, timeout; michael@0: PRLock *ml = PR_NewLock(); michael@0: PRCondVar *cv = PR_NewCondVar(ml); michael@0: for (seconds = 0; seconds < 10; ++seconds) michael@0: { michael@0: PRIntervalTime ticks = PR_SecondsToInterval(seconds); michael@0: PR_Lock(ml); michael@0: timein = PR_Now(); michael@0: rv = PR_WaitCondVar(cv, ticks); michael@0: timeout = PR_Now(); michael@0: PR_Unlock(ml); michael@0: LL_SUB(elapsed, timeout, timein); michael@0: LL_I2L(thousand, 1000); michael@0: LL_DIV(elapsed, elapsed, thousand); michael@0: LL_L2UI(delta, elapsed); michael@0: if (debug_mode) PR_fprintf(output, michael@0: "TestIntervals: %swaiting %ld seconds took %ld msecs\n", michael@0: ((rv == PR_SUCCESS) ? "" : "FAILED "), seconds, delta); michael@0: } michael@0: PR_DestroyCondVar(cv); michael@0: PR_DestroyLock(ml); michael@0: if (debug_mode) PR_fprintf(output, "\n"); michael@0: } /* TestIntervals */ michael@0: michael@0: static PRIntn PR_CALLBACK RealMain(int argc, char** argv) michael@0: { michael@0: PRUint32 vcpu, cpus = 0, loops = 1000; 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: Usage: test_name -d michael@0: */ michael@0: michael@0: /* main test */ michael@0: michael@0: PLOptStatus os; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "dl: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 = 1; michael@0: break; michael@0: case 'c': /* concurrency counter */ michael@0: cpus = atoi(opt->value); michael@0: break; michael@0: case 'l': /* loop counter */ michael@0: loops = 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: output = PR_GetSpecialFD(PR_StandardOutput); michael@0: PR_fprintf(output, "inrval: Examine stdout to determine results.\n"); michael@0: michael@0: if (cpus == 0) cpus = 8; michael@0: if (loops == 0) loops = 1000; michael@0: michael@0: if (debug_mode > 0) michael@0: { michael@0: PR_fprintf(output, "Inrval: Using %d loops\n", loops); michael@0: PR_fprintf(output, "Inrval: Using 1 and %d cpu(s)\n", cpus); michael@0: } michael@0: michael@0: for (vcpu = 1; vcpu <= cpus; vcpu += cpus - 1) michael@0: { michael@0: if (debug_mode) michael@0: PR_fprintf(output, "\nInrval: Using %d CPU(s)\n\n", vcpu); michael@0: PR_SetConcurrency(vcpu); michael@0: michael@0: TestNowOverhead(); michael@0: TestIntervalOverhead(); michael@0: TestConversions(); michael@0: TestIntervals(); michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRIntn rv; michael@0: michael@0: PR_STDIO_INIT(); michael@0: rv = PR_Initialize(RealMain, argc, argv, 0); michael@0: return rv; michael@0: } /* main */ michael@0: