tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

inrval.c (6660B)


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