lockfile.c 7.2 KB

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