home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / lockfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.8 KB  |  261 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20. ** File:        lockfile.c
  21. ** Purpose:     test basic locking functions
  22. **              Just because this times stuff, don't think its a perforamnce
  23. **              test!!!
  24. **
  25. ** Modification History:
  26. ** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
  27. **             The debug mode will print all of the printfs associated with this test.
  28. **             The regress mode will be the default mode. Since the regress tool limits
  29. **           the output to a one line status:PASS or FAIL,all of the printf statements
  30. **             have been handled with an if (debug_mode) statement.
  31. ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
  32. **            recognize the return code from tha main program.
  33. ***********************************************************************/
  34. /***********************************************************************
  35. ** Includes
  36. ***********************************************************************/
  37. /* Used to get the command line option */
  38. #include "plgetopt.h"
  39.  
  40. #include "prcmon.h"
  41. #include "prerror.h"
  42. #include "prinit.h"
  43. #include "prinrval.h"
  44. #include "prlock.h"
  45. #include "prlog.h"
  46. #include "prmon.h"
  47. #include "prthread.h"
  48. #include "prtypes.h"
  49.  
  50. #ifndef XP_MAC
  51. #include "private/pprio.h"
  52. #else
  53. #include "pprio.h"
  54. #endif
  55.  
  56. #include <stdio.h>
  57. #include <stdlib.h>
  58. #include <string.h>
  59.  
  60. #ifdef XP_MAC
  61. #include "prlog.h"
  62. #define printf PR_LogPrint
  63. extern void SetupMacPrintfLog(char *logFile);
  64. #endif
  65.  
  66. PRIntn failed_already=0;
  67. PRIntn debug_mode;
  68.  
  69. const static PRIntervalTime contention_interval = 50;
  70.  
  71. typedef struct LockContentious_s {
  72.     PRLock *ml;
  73.     PRUint32 loops;
  74.     PRIntervalTime overhead;
  75.     PRIntervalTime interval;
  76. } LockContentious_t;
  77.  
  78. #define LOCKFILE "prlock.fil"
  79.  
  80.  
  81.  
  82. static PRIntervalTime NonContentiousLock(PRUint32 loops)
  83. {
  84.     PRFileDesc *_lockfile;
  85.     while (loops-- > 0)
  86.     {
  87.         _lockfile = PR_Open(LOCKFILE, PR_CREATE_FILE|PR_RDWR, 0666);
  88.         if (!_lockfile) {
  89.             if (debug_mode) printf(
  90.                 "could not create lockfile: %d [%d]\n",
  91.                 PR_GetError(), PR_GetOSError());
  92.             return PR_INTERVAL_NO_TIMEOUT;
  93.         }
  94.         PR_LockFile(_lockfile);
  95.         PR_UnlockFile(_lockfile);
  96.         PR_Close(_lockfile);
  97.     }
  98.     return 0;
  99. }  /* NonContentiousLock */
  100.  
  101. static void PR_CALLBACK LockContender(void *arg)
  102. {
  103.     LockContentious_t *contention = (LockContentious_t*)arg;
  104.     PRFileDesc *_lockfile;
  105.     while (contention->loops-- > 0)
  106.     {
  107.         _lockfile = PR_Open(LOCKFILE, PR_CREATE_FILE|PR_RDWR, 0666);
  108.         if (!_lockfile) {
  109.             if (debug_mode) printf(
  110.                 "could not create lockfile: %d [%d]\n",
  111.                 PR_GetError(), PR_GetOSError());
  112.             break;
  113.         }
  114.         PR_LockFile(_lockfile);
  115.         PR_Sleep(contention->interval);
  116.         PR_UnlockFile(_lockfile);
  117.         PR_Close(_lockfile);
  118.     }
  119.  
  120. }  /* LockContender */
  121.  
  122. /*
  123. ** Win16 requires things passed to Threads not be on the stack
  124. */
  125. static LockContentious_t contention;
  126.  
  127. static PRIntervalTime ContentiousLock(PRUint32 loops)
  128. {
  129.     PRStatus status;
  130.     PRThread *thread = NULL;
  131.     PRIntervalTime overhead, timein = PR_IntervalNow();
  132.  
  133.     contention.loops = loops;
  134.     contention.overhead = 0;
  135.     contention.ml = PR_NewLock();
  136.     contention.interval = contention_interval;
  137.     thread = PR_CreateThread(
  138.         PR_USER_THREAD, LockContender, &contention,
  139.         PR_PRIORITY_LOW, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
  140.     PR_ASSERT(thread != NULL);
  141.  
  142.     overhead = PR_IntervalNow() - timein;
  143.  
  144.     while (contention.loops > 0)
  145.     {
  146.         PR_Lock(contention.ml);
  147.         contention.overhead += contention.interval;
  148.         PR_Sleep(contention.interval);
  149.         PR_Unlock(contention.ml);
  150.     }
  151.  
  152.     timein = PR_IntervalNow();
  153.     status = PR_JoinThread(thread);
  154.     PR_DestroyLock(contention.ml);
  155.     overhead += (PR_IntervalNow() - timein);
  156.     return overhead + contention.overhead;
  157. }  /* ContentiousLock */
  158.  
  159. static PRIntervalTime Test(
  160.     const char* msg, PRIntervalTime (*test)(PRUint32 loops),
  161.     PRUint32 loops, PRIntervalTime overhead)
  162.     /*
  163.      * overhead - overhead not measured by the test.
  164.      * duration - wall clock time it took to perform test.
  165.      * predicted - extra time test says should not be counted 
  166.      *
  167.      * Time accountable to the test is duration - overhead - predicted
  168.      * All times are Intervals and accumulated for all iterations.
  169.      */
  170.     PRFloat64 elapsed;
  171.     PRIntervalTime accountable, duration;    
  172.     PRUintn spaces = strlen(msg);
  173.     PRIntervalTime timeout, timein = PR_IntervalNow();
  174.     PRIntervalTime predicted = test(loops);
  175.     timeout = PR_IntervalNow();
  176.     duration = timeout - timein;
  177.     accountable = duration - predicted;
  178.     accountable -= overhead;
  179.     elapsed = (PRFloat64)PR_IntervalToMicroseconds(accountable);
  180.     if (debug_mode) printf("%s:", msg);
  181.     while (spaces++ < 50) if (debug_mode) printf(" ");
  182.     if ((PRInt32)accountable < 0)
  183.         if (debug_mode) printf("*****.** usecs/iteration\n");
  184.     else
  185.         if (debug_mode) printf("%8.2f usecs/iteration\n", elapsed/loops);
  186.     return duration;
  187. }  /* Test */
  188.  
  189. int main(int argc,  char **argv)
  190. {
  191.     PRBool rv = PR_TRUE;
  192.     PRIntervalTime duration;
  193.     PRUint32 cpu, cpus = 2, loops = 100;
  194.  
  195.     
  196.     /* The command line argument: -d is used to determine if the test is being run
  197.     in debug mode. The regress tool requires only one line output:PASS or FAIL.
  198.     All of the printfs associated with this test has been handled with a if (debug_mode)
  199.     test.
  200.     Usage: test_name -d
  201.     */
  202.     PLOptStatus os;
  203.     PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
  204.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  205.     {
  206.         if (PL_OPT_BAD == os) continue;
  207.         switch (opt->option)
  208.         {
  209.         case 'd':  /* debug mode */
  210.             debug_mode = 1;
  211.             break;
  212.          default:
  213.             break;
  214.         }
  215.     }
  216.     PL_DestroyOptState(opt);
  217.  
  218.  /* main test */
  219.     
  220.     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  221.     PR_STDIO_INIT();
  222.  
  223. #ifdef XP_MAC
  224.     SetupMacPrintfLog("lockfile.log");
  225.     debug_mode = 1;
  226. #endif
  227.  
  228.     if (argc > 1) loops = atoi(argv[1]);
  229.     if (loops == 0) loops = 100;
  230.     if (debug_mode) printf("Lock: Using %d loops\n", loops);
  231.  
  232.     cpus = (argc < 3) ? 2 : atoi(argv[2]);
  233.     if (cpus == 0) cpus = 2;
  234.     if (debug_mode) printf("Lock: Using %d cpu(s)\n", cpus);
  235.  
  236.  
  237.     for (cpu = 1; cpu <= cpus; ++cpu)
  238.     {
  239.         if (debug_mode) printf("\nLockFile: Using %d CPU(s)\n", cpu);
  240.         PR_SetConcurrency(cpu);
  241.         
  242.         (void)Test("LockFile non-contentious locking/unlocking", NonContentiousLock, loops, 0);
  243.         (void)Test("LockFile contentious locking/unlocking", ContentiousLock, loops, duration);
  244.     }
  245.  
  246.     PR_Delete(LOCKFILE);  /* try to get rid of evidence */
  247.  
  248.     if (debug_mode) printf("%s: test %s\n", "Lock(mutex) test", ((rv) ? "passed" : "failed"));
  249.     else {
  250.         if (!rv)
  251.             failed_already=1;
  252.     }
  253.     if(failed_already)    
  254.         return 1;
  255.     else
  256.         return 0;
  257. }  /* main */
  258.  
  259. /* testlock.c */
  260.