Back to index

lightning-sunbird  0.9+nobinonly
inrval.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039 ** file:            inrval.c
00040 ** description:     Interval conversion test.
00041 ** Modification History:
00042 ** 15-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
00043 **              The debug mode will print all of the printfs associated with this test.
00044 **                    The regress mode will be the default mode. Since the regress tool limits
00045 **           the output to a one line status:PASS or FAIL,all of the printf statements
00046 **                    have been handled with an if (debug_mode) statement.
00047 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
00048 **                   recognize the return code from tha main program.
00049 **/
00050 /***********************************************************************
00051 ** Includes
00052 ***********************************************************************/
00053 /* Used to get the command line option */
00054 #include "plgetopt.h"
00055 
00056 #include "prinit.h"
00057 #ifdef XP_MAC
00058 #include "pralarm.h"
00059 #else
00060 #include "obsolete/pralarm.h"
00061 #endif
00062 
00063 #include "prio.h"
00064 #include "prprf.h"
00065 #include "prlock.h"
00066 #include "prlong.h"
00067 #include "prcvar.h"
00068 #include "prinrval.h"
00069 #include "prtime.h"
00070 
00071 #include "plgetopt.h"
00072 
00073 #include <stdio.h>
00074 #include <stdlib.h>
00075 
00076 static PRIntn debug_mode;
00077 static PRFileDesc *output;
00078 
00079 
00080 static void TestConversions(void)
00081 {
00082     PRIntervalTime ticks = PR_TicksPerSecond();
00083 
00084        if (debug_mode) {
00085     PR_fprintf(output, "PR_TicksPerSecond: %ld\n\n", ticks);
00086     PR_fprintf(output, "PR_SecondsToInterval(1): %ld\n", PR_SecondsToInterval(1));
00087     PR_fprintf(output, "PR_MillisecondsToInterval(1000): %ld\n", PR_MillisecondsToInterval(1000));
00088     PR_fprintf(output, "PR_MicrosecondsToInterval(1000000): %ld\n\n", PR_MicrosecondsToInterval(1000000));
00089 
00090     PR_fprintf(output, "PR_SecondsToInterval(3): %ld\n", PR_SecondsToInterval(3));
00091     PR_fprintf(output, "PR_MillisecondsToInterval(3000): %ld\n", PR_MillisecondsToInterval(3000));
00092     PR_fprintf(output, "PR_MicrosecondsToInterval(3000000): %ld\n\n", PR_MicrosecondsToInterval(3000000));
00093 
00094     PR_fprintf(output, "PR_IntervalToSeconds(%ld): %ld\n", ticks, PR_IntervalToSeconds(ticks));
00095     PR_fprintf(output, "PR_IntervalToMilliseconds(%ld): %ld\n", ticks, PR_IntervalToMilliseconds(ticks));
00096     PR_fprintf(output, "PR_IntervalToMicroseconds(%ld): %ld\n\n", ticks, PR_IntervalToMicroseconds(ticks));
00097 
00098     ticks *= 3;
00099     PR_fprintf(output, "PR_IntervalToSeconds(%ld): %ld\n", ticks, PR_IntervalToSeconds(ticks));
00100     PR_fprintf(output, "PR_IntervalToMilliseconds(%ld): %ld\n", ticks, PR_IntervalToMilliseconds(ticks));
00101     PR_fprintf(output, "PR_IntervalToMicroseconds(%ld): %ld\n\n", ticks, PR_IntervalToMicroseconds(ticks));
00102        } /*end debug mode */
00103 }  /* TestConversions */
00104 
00105 static void TestIntervalOverhead(void)
00106 {
00107     /* Hopefully the optimizer won't delete this function */
00108     PRUint32 elapsed, per_call, loops = 1000000;
00109 
00110     PRIntervalTime timeout, timein = PR_IntervalNow();
00111     while (--loops > 0)
00112         timeout = PR_IntervalNow();
00113 
00114     elapsed = 1000U * PR_IntervalToMicroseconds(timeout - timein);
00115     per_call = elapsed / 1000000U;
00116     PR_fprintf(
00117         output, "Overhead of 'PR_IntervalNow()' is %u nsecs\n\n", per_call);
00118 }  /* TestIntervalOverhead */
00119 
00120 static void TestNowOverhead(void)
00121 {
00122     PRTime timeout, timein;
00123     PRInt32 overhead, loops = 1000000;
00124     PRInt64 elapsed, per_call, ten23rd, ten26th;
00125 
00126     LL_I2L(ten23rd, 1000);
00127     LL_I2L(ten26th, 1000000);
00128 
00129     timein = PR_Now();
00130     while (--loops > 0)
00131         timeout = PR_Now();
00132 
00133     LL_SUB(elapsed, timeout, timein);
00134     LL_MUL(elapsed, elapsed, ten23rd);
00135     LL_DIV(per_call, elapsed, ten26th);
00136     LL_L2I(overhead, per_call);
00137     PR_fprintf(
00138         output, "Overhead of 'PR_Now()' is %u nsecs\n\n", overhead);
00139 }  /* TestNowOverhead */
00140 
00141 static void TestIntervals(void)
00142 {
00143     PRStatus rv;
00144     PRUint32 delta;
00145     PRInt32 seconds;
00146     PRUint64 elapsed, thousand;
00147     PRTime timein, timeout;
00148     PRLock *ml = PR_NewLock();
00149     PRCondVar *cv = PR_NewCondVar(ml);
00150     for (seconds = 0; seconds < 10; ++seconds)
00151     {
00152         PRIntervalTime ticks = PR_SecondsToInterval(seconds);
00153         PR_Lock(ml);
00154         timein = PR_Now();
00155         rv = PR_WaitCondVar(cv, ticks);
00156         timeout = PR_Now();
00157         PR_Unlock(ml);
00158         LL_SUB(elapsed, timeout, timein);
00159         LL_I2L(thousand, 1000);
00160         LL_DIV(elapsed, elapsed, thousand);
00161         LL_L2UI(delta, elapsed);
00162         if (debug_mode) PR_fprintf(output, 
00163             "TestIntervals: %swaiting %ld seconds took %ld msecs\n",
00164             ((rv == PR_SUCCESS) ? "" : "FAILED "), seconds, delta);
00165     }
00166     PR_DestroyCondVar(cv);
00167     PR_DestroyLock(ml);
00168     if (debug_mode) PR_fprintf(output, "\n");
00169 }  /* TestIntervals */
00170 
00171 static PRIntn PR_CALLBACK RealMain(int argc, char** argv)
00172 {
00173     PRUint32 vcpu, cpus = 0, loops = 1000;
00174 
00175        /* The command line argument: -d is used to determine if the test is being run
00176        in debug mode. The regress tool requires only one line output:PASS or FAIL.
00177        All of the printfs associated with this test has been handled with a if (debug_mode)
00178        test.
00179        Usage: test_name -d
00180        */
00181 
00182  /* main test */
00183        
00184        PLOptStatus os;
00185        PLOptState *opt = PL_CreateOptState(argc, argv, "dl:c:");
00186        while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00187     {
00188               if (PL_OPT_BAD == os) continue;
00189         switch (opt->option)
00190         {
00191         case 'd':  /* debug mode */
00192                      debug_mode = 1;
00193             break;
00194         case 'c':  /* concurrency counter */
00195                      cpus = atoi(opt->value);
00196             break;
00197         case 'l':  /* loop counter */
00198                      loops = atoi(opt->value);
00199             break;
00200          default:
00201             break;
00202         }
00203     }
00204        PL_DestroyOptState(opt);
00205        
00206     output = PR_GetSpecialFD(PR_StandardOutput);
00207     PR_fprintf(output, "inrval: Examine stdout to determine results.\n");
00208 
00209     if (cpus == 0) cpus = 8;
00210     if (loops == 0) loops = 1000;
00211 
00212     if (debug_mode > 0)
00213     {
00214         PR_fprintf(output, "Inrval: Using %d loops\n", loops);
00215         PR_fprintf(output, "Inrval: Using 1 and %d cpu(s)\n", cpus);
00216     }
00217 
00218     for (vcpu = 1; vcpu <= cpus; vcpu += cpus - 1)
00219     {
00220         if (debug_mode)
00221             PR_fprintf(output, "\nInrval: Using %d CPU(s)\n\n", vcpu);
00222         PR_SetConcurrency(vcpu);
00223 
00224         TestNowOverhead();
00225         TestIntervalOverhead();
00226         TestConversions();
00227         TestIntervals();
00228     }
00229         
00230     return 0;
00231 }
00232 
00233 
00234 PRIntn main(PRIntn argc, char *argv[])
00235 {
00236     PRIntn rv;
00237     
00238     PR_STDIO_INIT();
00239     rv = PR_Initialize(RealMain, argc, argv, 0);
00240     return rv;
00241 }  /* main */
00242