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