inrval.c 6.8 KB

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