1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/inrval.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,206 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 +** file: inrval.c 1.11 +** description: Interval conversion test. 1.12 +** Modification History: 1.13 +** 15-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. 1.14 +** The debug mode will print all of the printfs associated with this test. 1.15 +** The regress mode will be the default mode. Since the regress tool limits 1.16 +** the output to a one line status:PASS or FAIL,all of the printf statements 1.17 +** have been handled with an if (debug_mode) statement. 1.18 +** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to 1.19 +** recognize the return code from tha main program. 1.20 +**/ 1.21 +/*********************************************************************** 1.22 +** Includes 1.23 +***********************************************************************/ 1.24 +/* Used to get the command line option */ 1.25 +#include "plgetopt.h" 1.26 + 1.27 +#include "prinit.h" 1.28 +#include "obsolete/pralarm.h" 1.29 + 1.30 +#include "prio.h" 1.31 +#include "prprf.h" 1.32 +#include "prlock.h" 1.33 +#include "prlong.h" 1.34 +#include "prcvar.h" 1.35 +#include "prinrval.h" 1.36 +#include "prtime.h" 1.37 + 1.38 +#include "plgetopt.h" 1.39 + 1.40 +#include <stdio.h> 1.41 +#include <stdlib.h> 1.42 + 1.43 +static PRIntn debug_mode; 1.44 +static PRFileDesc *output; 1.45 + 1.46 + 1.47 +static void TestConversions(void) 1.48 +{ 1.49 + PRIntervalTime ticks = PR_TicksPerSecond(); 1.50 + 1.51 + if (debug_mode) { 1.52 + PR_fprintf(output, "PR_TicksPerSecond: %ld\n\n", ticks); 1.53 + PR_fprintf(output, "PR_SecondsToInterval(1): %ld\n", PR_SecondsToInterval(1)); 1.54 + PR_fprintf(output, "PR_MillisecondsToInterval(1000): %ld\n", PR_MillisecondsToInterval(1000)); 1.55 + PR_fprintf(output, "PR_MicrosecondsToInterval(1000000): %ld\n\n", PR_MicrosecondsToInterval(1000000)); 1.56 + 1.57 + PR_fprintf(output, "PR_SecondsToInterval(3): %ld\n", PR_SecondsToInterval(3)); 1.58 + PR_fprintf(output, "PR_MillisecondsToInterval(3000): %ld\n", PR_MillisecondsToInterval(3000)); 1.59 + PR_fprintf(output, "PR_MicrosecondsToInterval(3000000): %ld\n\n", PR_MicrosecondsToInterval(3000000)); 1.60 + 1.61 + PR_fprintf(output, "PR_IntervalToSeconds(%ld): %ld\n", ticks, PR_IntervalToSeconds(ticks)); 1.62 + PR_fprintf(output, "PR_IntervalToMilliseconds(%ld): %ld\n", ticks, PR_IntervalToMilliseconds(ticks)); 1.63 + PR_fprintf(output, "PR_IntervalToMicroseconds(%ld): %ld\n\n", ticks, PR_IntervalToMicroseconds(ticks)); 1.64 + 1.65 + ticks *= 3; 1.66 + PR_fprintf(output, "PR_IntervalToSeconds(%ld): %ld\n", ticks, PR_IntervalToSeconds(ticks)); 1.67 + PR_fprintf(output, "PR_IntervalToMilliseconds(%ld): %ld\n", ticks, PR_IntervalToMilliseconds(ticks)); 1.68 + PR_fprintf(output, "PR_IntervalToMicroseconds(%ld): %ld\n\n", ticks, PR_IntervalToMicroseconds(ticks)); 1.69 + } /*end debug mode */ 1.70 +} /* TestConversions */ 1.71 + 1.72 +static void TestIntervalOverhead(void) 1.73 +{ 1.74 + /* Hopefully the optimizer won't delete this function */ 1.75 + PRUint32 elapsed, per_call, loops = 1000000; 1.76 + 1.77 + PRIntervalTime timeout, timein = PR_IntervalNow(); 1.78 + while (--loops > 0) 1.79 + timeout = PR_IntervalNow(); 1.80 + 1.81 + elapsed = 1000U * PR_IntervalToMicroseconds(timeout - timein); 1.82 + per_call = elapsed / 1000000U; 1.83 + PR_fprintf( 1.84 + output, "Overhead of 'PR_IntervalNow()' is %u nsecs\n\n", per_call); 1.85 +} /* TestIntervalOverhead */ 1.86 + 1.87 +static void TestNowOverhead(void) 1.88 +{ 1.89 + PRTime timeout, timein; 1.90 + PRInt32 overhead, loops = 1000000; 1.91 + PRInt64 elapsed, per_call, ten23rd, ten26th; 1.92 + 1.93 + LL_I2L(ten23rd, 1000); 1.94 + LL_I2L(ten26th, 1000000); 1.95 + 1.96 + timein = PR_Now(); 1.97 + while (--loops > 0) 1.98 + timeout = PR_Now(); 1.99 + 1.100 + LL_SUB(elapsed, timeout, timein); 1.101 + LL_MUL(elapsed, elapsed, ten23rd); 1.102 + LL_DIV(per_call, elapsed, ten26th); 1.103 + LL_L2I(overhead, per_call); 1.104 + PR_fprintf( 1.105 + output, "Overhead of 'PR_Now()' is %u nsecs\n\n", overhead); 1.106 +} /* TestNowOverhead */ 1.107 + 1.108 +static void TestIntervals(void) 1.109 +{ 1.110 + PRStatus rv; 1.111 + PRUint32 delta; 1.112 + PRInt32 seconds; 1.113 + PRUint64 elapsed, thousand; 1.114 + PRTime timein, timeout; 1.115 + PRLock *ml = PR_NewLock(); 1.116 + PRCondVar *cv = PR_NewCondVar(ml); 1.117 + for (seconds = 0; seconds < 10; ++seconds) 1.118 + { 1.119 + PRIntervalTime ticks = PR_SecondsToInterval(seconds); 1.120 + PR_Lock(ml); 1.121 + timein = PR_Now(); 1.122 + rv = PR_WaitCondVar(cv, ticks); 1.123 + timeout = PR_Now(); 1.124 + PR_Unlock(ml); 1.125 + LL_SUB(elapsed, timeout, timein); 1.126 + LL_I2L(thousand, 1000); 1.127 + LL_DIV(elapsed, elapsed, thousand); 1.128 + LL_L2UI(delta, elapsed); 1.129 + if (debug_mode) PR_fprintf(output, 1.130 + "TestIntervals: %swaiting %ld seconds took %ld msecs\n", 1.131 + ((rv == PR_SUCCESS) ? "" : "FAILED "), seconds, delta); 1.132 + } 1.133 + PR_DestroyCondVar(cv); 1.134 + PR_DestroyLock(ml); 1.135 + if (debug_mode) PR_fprintf(output, "\n"); 1.136 +} /* TestIntervals */ 1.137 + 1.138 +static PRIntn PR_CALLBACK RealMain(int argc, char** argv) 1.139 +{ 1.140 + PRUint32 vcpu, cpus = 0, loops = 1000; 1.141 + 1.142 + /* The command line argument: -d is used to determine if the test is being run 1.143 + in debug mode. The regress tool requires only one line output:PASS or FAIL. 1.144 + All of the printfs associated with this test has been handled with a if (debug_mode) 1.145 + test. 1.146 + Usage: test_name -d 1.147 + */ 1.148 + 1.149 + /* main test */ 1.150 + 1.151 + PLOptStatus os; 1.152 + PLOptState *opt = PL_CreateOptState(argc, argv, "dl:c:"); 1.153 + while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) 1.154 + { 1.155 + if (PL_OPT_BAD == os) continue; 1.156 + switch (opt->option) 1.157 + { 1.158 + case 'd': /* debug mode */ 1.159 + debug_mode = 1; 1.160 + break; 1.161 + case 'c': /* concurrency counter */ 1.162 + cpus = atoi(opt->value); 1.163 + break; 1.164 + case 'l': /* loop counter */ 1.165 + loops = atoi(opt->value); 1.166 + break; 1.167 + default: 1.168 + break; 1.169 + } 1.170 + } 1.171 + PL_DestroyOptState(opt); 1.172 + 1.173 + output = PR_GetSpecialFD(PR_StandardOutput); 1.174 + PR_fprintf(output, "inrval: Examine stdout to determine results.\n"); 1.175 + 1.176 + if (cpus == 0) cpus = 8; 1.177 + if (loops == 0) loops = 1000; 1.178 + 1.179 + if (debug_mode > 0) 1.180 + { 1.181 + PR_fprintf(output, "Inrval: Using %d loops\n", loops); 1.182 + PR_fprintf(output, "Inrval: Using 1 and %d cpu(s)\n", cpus); 1.183 + } 1.184 + 1.185 + for (vcpu = 1; vcpu <= cpus; vcpu += cpus - 1) 1.186 + { 1.187 + if (debug_mode) 1.188 + PR_fprintf(output, "\nInrval: Using %d CPU(s)\n\n", vcpu); 1.189 + PR_SetConcurrency(vcpu); 1.190 + 1.191 + TestNowOverhead(); 1.192 + TestIntervalOverhead(); 1.193 + TestConversions(); 1.194 + TestIntervals(); 1.195 + } 1.196 + 1.197 + return 0; 1.198 +} 1.199 + 1.200 + 1.201 +int main(int argc, char **argv) 1.202 +{ 1.203 + PRIntn rv; 1.204 + 1.205 + PR_STDIO_INIT(); 1.206 + rv = PR_Initialize(RealMain, argc, argv, 0); 1.207 + return rv; 1.208 +} /* main */ 1.209 +