Back to index

lightning-sunbird  0.9+nobinonly
lockfile.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:        lockfile.c
00040 ** Purpose:     test basic locking functions
00041 **              Just because this times stuff, don't think its a perforamnce
00042 **              test!!!
00043 **
00044 ** Modification History:
00045 ** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
00046 **              The debug mode will print all of the printfs associated with this test.
00047 **                    The regress mode will be the default mode. Since the regress tool limits
00048 **           the output to a one line status:PASS or FAIL,all of the printf statements
00049 **                    have been handled with an if (debug_mode) statement.
00050 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
00051 **                   recognize the return code from tha main program.
00052 ***********************************************************************/
00053 /***********************************************************************
00054 ** Includes
00055 ***********************************************************************/
00056 /* Used to get the command line option */
00057 #include "plgetopt.h"
00058 
00059 #include "prcmon.h"
00060 #include "prerror.h"
00061 #include "prinit.h"
00062 #include "prinrval.h"
00063 #include "prlock.h"
00064 #include "prlog.h"
00065 #include "prmon.h"
00066 #include "prthread.h"
00067 #include "prtypes.h"
00068 
00069 #ifndef XP_MAC
00070 #include "private/pprio.h"
00071 #else
00072 #include "pprio.h"
00073 #endif
00074 
00075 #include <stdio.h>
00076 #include <stdlib.h>
00077 #include <string.h>
00078 
00079 #ifdef XP_MAC
00080 #include "prlog.h"
00081 #define printf PR_LogPrint
00082 extern void SetupMacPrintfLog(char *logFile);
00083 #endif
00084 
00085 PRIntn failed_already=0;
00086 PRIntn debug_mode;
00087 
00088 const static PRIntervalTime contention_interval = 50;
00089 
00090 typedef struct LockContentious_s {
00091     PRLock *ml;
00092     PRInt32 loops;
00093     PRIntervalTime overhead;
00094     PRIntervalTime interval;
00095 } LockContentious_t;
00096 
00097 #define LOCKFILE "prlock.fil"
00098 
00099 
00100 
00101 static PRIntervalTime NonContentiousLock(PRInt32 loops)
00102 {
00103     PRFileDesc *_lockfile;
00104     while (loops-- > 0)
00105     {
00106         _lockfile = PR_Open(LOCKFILE, PR_CREATE_FILE|PR_RDWR, 0666);
00107         if (!_lockfile) {
00108             if (debug_mode) printf(
00109                 "could not create lockfile: %d [%d]\n",
00110                 PR_GetError(), PR_GetOSError());
00111             return PR_INTERVAL_NO_TIMEOUT;
00112         }
00113         PR_LockFile(_lockfile);
00114         PR_UnlockFile(_lockfile);
00115         PR_Close(_lockfile);
00116     }
00117     return 0;
00118 }  /* NonContentiousLock */
00119 
00120 static void PR_CALLBACK LockContender(void *arg)
00121 {
00122     LockContentious_t *contention = (LockContentious_t*)arg;
00123     PRFileDesc *_lockfile;
00124     while (contention->loops-- > 0)
00125     {
00126         _lockfile = PR_Open(LOCKFILE, PR_CREATE_FILE|PR_RDWR, 0666);
00127         if (!_lockfile) {
00128             if (debug_mode) printf(
00129                 "could not create lockfile: %d [%d]\n",
00130                 PR_GetError(), PR_GetOSError());
00131             break;
00132         }
00133         PR_LockFile(_lockfile);
00134         PR_Sleep(contention->interval);
00135         PR_UnlockFile(_lockfile);
00136         PR_Close(_lockfile);
00137     }
00138 
00139 }  /* LockContender */
00140 
00141 /*
00142 ** Win16 requires things passed to Threads not be on the stack
00143 */
00144 static LockContentious_t contention;
00145 
00146 static PRIntervalTime ContentiousLock(PRInt32 loops)
00147 {
00148     PRStatus status;
00149     PRThread *thread = NULL;
00150     PRIntervalTime overhead, timein = PR_IntervalNow();
00151 
00152     contention.loops = loops;
00153     contention.overhead = 0;
00154     contention.ml = PR_NewLock();
00155     contention.interval = contention_interval;
00156     thread = PR_CreateThread(
00157         PR_USER_THREAD, LockContender, &contention,
00158         PR_PRIORITY_LOW, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
00159     PR_ASSERT(thread != NULL);
00160 
00161     overhead = PR_IntervalNow() - timein;
00162 
00163     while (contention.loops > 0)
00164     {
00165         PR_Lock(contention.ml);
00166         contention.overhead += contention.interval;
00167         PR_Sleep(contention.interval);
00168         PR_Unlock(contention.ml);
00169     }
00170 
00171     timein = PR_IntervalNow();
00172     status = PR_JoinThread(thread);
00173     PR_DestroyLock(contention.ml);
00174     overhead += (PR_IntervalNow() - timein);
00175     return overhead + contention.overhead;
00176 }  /* ContentiousLock */
00177 
00178 static PRIntervalTime Test(
00179     const char* msg, PRIntervalTime (*test)(PRInt32 loops),
00180     PRInt32 loops, PRIntervalTime overhead)
00181 { 
00182     /*
00183      * overhead - overhead not measured by the test.
00184      * duration - wall clock time it took to perform test.
00185      * predicted - extra time test says should not be counted 
00186      *
00187      * Time accountable to the test is duration - overhead - predicted
00188      * All times are Intervals and accumulated for all iterations.
00189      */
00190     PRFloat64 elapsed;
00191     PRIntervalTime accountable, duration;    
00192     PRUintn spaces = strlen(msg);
00193     PRIntervalTime timeout, timein = PR_IntervalNow();
00194     PRIntervalTime predicted = test(loops);
00195     timeout = PR_IntervalNow();
00196     duration = timeout - timein;
00197     accountable = duration - predicted;
00198     accountable -= overhead;
00199     elapsed = (PRFloat64)PR_IntervalToMicroseconds(accountable);
00200     if (debug_mode) printf("%s:", msg);
00201     while (spaces++ < 50) if (debug_mode) printf(" ");
00202     if ((PRInt32)accountable < 0) {
00203         if (debug_mode) printf("*****.** usecs/iteration\n");
00204     } else {
00205         if (debug_mode) printf("%8.2f usecs/iteration\n", elapsed/loops);
00206     }
00207     return duration;
00208 }  /* Test */
00209 
00210 int main(int argc,  char **argv)
00211 {
00212     PRIntervalTime duration;
00213     PRUint32 cpu, cpus = 2;
00214     PRInt32 loops = 100;
00215 
00216        
00217        /* The command line argument: -d is used to determine if the test is being run
00218        in debug mode. The regress tool requires only one line output:PASS or FAIL.
00219        All of the printfs associated with this test has been handled with a if (debug_mode)
00220        test.
00221        Usage: test_name -d
00222        */
00223        PLOptStatus os;
00224        PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
00225        while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00226     {
00227               if (PL_OPT_BAD == os) continue;
00228         switch (opt->option)
00229         {
00230         case 'd':  /* debug mode */
00231                      debug_mode = 1;
00232             break;
00233          default:
00234             break;
00235         }
00236     }
00237        PL_DestroyOptState(opt);
00238 
00239  /* main test */
00240        
00241     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
00242     PR_STDIO_INIT();
00243 
00244 #ifdef XP_MAC
00245        SetupMacPrintfLog("lockfile.log");
00246        debug_mode = 1;
00247 #endif
00248 
00249     if (argc > 1) loops = atoi(argv[1]);
00250     if (loops == 0) loops = 100;
00251     if (debug_mode) printf("Lock: Using %d loops\n", loops);
00252 
00253     cpus = (argc < 3) ? 2 : atoi(argv[2]);
00254     if (cpus == 0) cpus = 2;
00255     if (debug_mode) printf("Lock: Using %d cpu(s)\n", cpus);
00256 
00257 
00258     for (cpu = 1; cpu <= cpus; ++cpu)
00259     {
00260         if (debug_mode) printf("\nLockFile: Using %d CPU(s)\n", cpu);
00261         PR_SetConcurrency(cpu);
00262         
00263         duration = Test("LockFile non-contentious locking/unlocking", NonContentiousLock, loops, 0);
00264         (void)Test("LockFile contentious locking/unlocking", ContentiousLock, loops, duration);
00265     }
00266 
00267     PR_Delete(LOCKFILE);  /* try to get rid of evidence */
00268 
00269     if (debug_mode) printf("%s: test %s\n", "Lock(mutex) test", ((failed_already) ? "failed" : "passed"));
00270        if(failed_already)   
00271               return 1;
00272        else
00273               return 0;
00274 }  /* main */
00275 
00276 /* testlock.c */