nsprpub/pr/tests/inrval.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 ** file: inrval.c
michael@0 8 ** description: Interval conversion test.
michael@0 9 ** Modification History:
michael@0 10 ** 15-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
michael@0 11 ** The debug mode will print all of the printfs associated with this test.
michael@0 12 ** The regress mode will be the default mode. Since the regress tool limits
michael@0 13 ** the output to a one line status:PASS or FAIL,all of the printf statements
michael@0 14 ** have been handled with an if (debug_mode) statement.
michael@0 15 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
michael@0 16 ** recognize the return code from tha main program.
michael@0 17 **/
michael@0 18 /***********************************************************************
michael@0 19 ** Includes
michael@0 20 ***********************************************************************/
michael@0 21 /* Used to get the command line option */
michael@0 22 #include "plgetopt.h"
michael@0 23
michael@0 24 #include "prinit.h"
michael@0 25 #include "obsolete/pralarm.h"
michael@0 26
michael@0 27 #include "prio.h"
michael@0 28 #include "prprf.h"
michael@0 29 #include "prlock.h"
michael@0 30 #include "prlong.h"
michael@0 31 #include "prcvar.h"
michael@0 32 #include "prinrval.h"
michael@0 33 #include "prtime.h"
michael@0 34
michael@0 35 #include "plgetopt.h"
michael@0 36
michael@0 37 #include <stdio.h>
michael@0 38 #include <stdlib.h>
michael@0 39
michael@0 40 static PRIntn debug_mode;
michael@0 41 static PRFileDesc *output;
michael@0 42
michael@0 43
michael@0 44 static void TestConversions(void)
michael@0 45 {
michael@0 46 PRIntervalTime ticks = PR_TicksPerSecond();
michael@0 47
michael@0 48 if (debug_mode) {
michael@0 49 PR_fprintf(output, "PR_TicksPerSecond: %ld\n\n", ticks);
michael@0 50 PR_fprintf(output, "PR_SecondsToInterval(1): %ld\n", PR_SecondsToInterval(1));
michael@0 51 PR_fprintf(output, "PR_MillisecondsToInterval(1000): %ld\n", PR_MillisecondsToInterval(1000));
michael@0 52 PR_fprintf(output, "PR_MicrosecondsToInterval(1000000): %ld\n\n", PR_MicrosecondsToInterval(1000000));
michael@0 53
michael@0 54 PR_fprintf(output, "PR_SecondsToInterval(3): %ld\n", PR_SecondsToInterval(3));
michael@0 55 PR_fprintf(output, "PR_MillisecondsToInterval(3000): %ld\n", PR_MillisecondsToInterval(3000));
michael@0 56 PR_fprintf(output, "PR_MicrosecondsToInterval(3000000): %ld\n\n", PR_MicrosecondsToInterval(3000000));
michael@0 57
michael@0 58 PR_fprintf(output, "PR_IntervalToSeconds(%ld): %ld\n", ticks, PR_IntervalToSeconds(ticks));
michael@0 59 PR_fprintf(output, "PR_IntervalToMilliseconds(%ld): %ld\n", ticks, PR_IntervalToMilliseconds(ticks));
michael@0 60 PR_fprintf(output, "PR_IntervalToMicroseconds(%ld): %ld\n\n", ticks, PR_IntervalToMicroseconds(ticks));
michael@0 61
michael@0 62 ticks *= 3;
michael@0 63 PR_fprintf(output, "PR_IntervalToSeconds(%ld): %ld\n", ticks, PR_IntervalToSeconds(ticks));
michael@0 64 PR_fprintf(output, "PR_IntervalToMilliseconds(%ld): %ld\n", ticks, PR_IntervalToMilliseconds(ticks));
michael@0 65 PR_fprintf(output, "PR_IntervalToMicroseconds(%ld): %ld\n\n", ticks, PR_IntervalToMicroseconds(ticks));
michael@0 66 } /*end debug mode */
michael@0 67 } /* TestConversions */
michael@0 68
michael@0 69 static void TestIntervalOverhead(void)
michael@0 70 {
michael@0 71 /* Hopefully the optimizer won't delete this function */
michael@0 72 PRUint32 elapsed, per_call, loops = 1000000;
michael@0 73
michael@0 74 PRIntervalTime timeout, timein = PR_IntervalNow();
michael@0 75 while (--loops > 0)
michael@0 76 timeout = PR_IntervalNow();
michael@0 77
michael@0 78 elapsed = 1000U * PR_IntervalToMicroseconds(timeout - timein);
michael@0 79 per_call = elapsed / 1000000U;
michael@0 80 PR_fprintf(
michael@0 81 output, "Overhead of 'PR_IntervalNow()' is %u nsecs\n\n", per_call);
michael@0 82 } /* TestIntervalOverhead */
michael@0 83
michael@0 84 static void TestNowOverhead(void)
michael@0 85 {
michael@0 86 PRTime timeout, timein;
michael@0 87 PRInt32 overhead, loops = 1000000;
michael@0 88 PRInt64 elapsed, per_call, ten23rd, ten26th;
michael@0 89
michael@0 90 LL_I2L(ten23rd, 1000);
michael@0 91 LL_I2L(ten26th, 1000000);
michael@0 92
michael@0 93 timein = PR_Now();
michael@0 94 while (--loops > 0)
michael@0 95 timeout = PR_Now();
michael@0 96
michael@0 97 LL_SUB(elapsed, timeout, timein);
michael@0 98 LL_MUL(elapsed, elapsed, ten23rd);
michael@0 99 LL_DIV(per_call, elapsed, ten26th);
michael@0 100 LL_L2I(overhead, per_call);
michael@0 101 PR_fprintf(
michael@0 102 output, "Overhead of 'PR_Now()' is %u nsecs\n\n", overhead);
michael@0 103 } /* TestNowOverhead */
michael@0 104
michael@0 105 static void TestIntervals(void)
michael@0 106 {
michael@0 107 PRStatus rv;
michael@0 108 PRUint32 delta;
michael@0 109 PRInt32 seconds;
michael@0 110 PRUint64 elapsed, thousand;
michael@0 111 PRTime timein, timeout;
michael@0 112 PRLock *ml = PR_NewLock();
michael@0 113 PRCondVar *cv = PR_NewCondVar(ml);
michael@0 114 for (seconds = 0; seconds < 10; ++seconds)
michael@0 115 {
michael@0 116 PRIntervalTime ticks = PR_SecondsToInterval(seconds);
michael@0 117 PR_Lock(ml);
michael@0 118 timein = PR_Now();
michael@0 119 rv = PR_WaitCondVar(cv, ticks);
michael@0 120 timeout = PR_Now();
michael@0 121 PR_Unlock(ml);
michael@0 122 LL_SUB(elapsed, timeout, timein);
michael@0 123 LL_I2L(thousand, 1000);
michael@0 124 LL_DIV(elapsed, elapsed, thousand);
michael@0 125 LL_L2UI(delta, elapsed);
michael@0 126 if (debug_mode) PR_fprintf(output,
michael@0 127 "TestIntervals: %swaiting %ld seconds took %ld msecs\n",
michael@0 128 ((rv == PR_SUCCESS) ? "" : "FAILED "), seconds, delta);
michael@0 129 }
michael@0 130 PR_DestroyCondVar(cv);
michael@0 131 PR_DestroyLock(ml);
michael@0 132 if (debug_mode) PR_fprintf(output, "\n");
michael@0 133 } /* TestIntervals */
michael@0 134
michael@0 135 static PRIntn PR_CALLBACK RealMain(int argc, char** argv)
michael@0 136 {
michael@0 137 PRUint32 vcpu, cpus = 0, loops = 1000;
michael@0 138
michael@0 139 /* The command line argument: -d is used to determine if the test is being run
michael@0 140 in debug mode. The regress tool requires only one line output:PASS or FAIL.
michael@0 141 All of the printfs associated with this test has been handled with a if (debug_mode)
michael@0 142 test.
michael@0 143 Usage: test_name -d
michael@0 144 */
michael@0 145
michael@0 146 /* main test */
michael@0 147
michael@0 148 PLOptStatus os;
michael@0 149 PLOptState *opt = PL_CreateOptState(argc, argv, "dl:c:");
michael@0 150 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
michael@0 151 {
michael@0 152 if (PL_OPT_BAD == os) continue;
michael@0 153 switch (opt->option)
michael@0 154 {
michael@0 155 case 'd': /* debug mode */
michael@0 156 debug_mode = 1;
michael@0 157 break;
michael@0 158 case 'c': /* concurrency counter */
michael@0 159 cpus = atoi(opt->value);
michael@0 160 break;
michael@0 161 case 'l': /* loop counter */
michael@0 162 loops = atoi(opt->value);
michael@0 163 break;
michael@0 164 default:
michael@0 165 break;
michael@0 166 }
michael@0 167 }
michael@0 168 PL_DestroyOptState(opt);
michael@0 169
michael@0 170 output = PR_GetSpecialFD(PR_StandardOutput);
michael@0 171 PR_fprintf(output, "inrval: Examine stdout to determine results.\n");
michael@0 172
michael@0 173 if (cpus == 0) cpus = 8;
michael@0 174 if (loops == 0) loops = 1000;
michael@0 175
michael@0 176 if (debug_mode > 0)
michael@0 177 {
michael@0 178 PR_fprintf(output, "Inrval: Using %d loops\n", loops);
michael@0 179 PR_fprintf(output, "Inrval: Using 1 and %d cpu(s)\n", cpus);
michael@0 180 }
michael@0 181
michael@0 182 for (vcpu = 1; vcpu <= cpus; vcpu += cpus - 1)
michael@0 183 {
michael@0 184 if (debug_mode)
michael@0 185 PR_fprintf(output, "\nInrval: Using %d CPU(s)\n\n", vcpu);
michael@0 186 PR_SetConcurrency(vcpu);
michael@0 187
michael@0 188 TestNowOverhead();
michael@0 189 TestIntervalOverhead();
michael@0 190 TestConversions();
michael@0 191 TestIntervals();
michael@0 192 }
michael@0 193
michael@0 194 return 0;
michael@0 195 }
michael@0 196
michael@0 197
michael@0 198 int main(int argc, char **argv)
michael@0 199 {
michael@0 200 PRIntn rv;
michael@0 201
michael@0 202 PR_STDIO_INIT();
michael@0 203 rv = PR_Initialize(RealMain, argc, argv, 0);
michael@0 204 return rv;
michael@0 205 } /* main */
michael@0 206

mercurial