home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / priotest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.7 KB  |  235 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:        priotest.c
  21.  * Purpose:     testing priorities
  22.  */
  23.  
  24. #include "prcmon.h"
  25. #include "prinit.h"
  26. #include "prinrval.h"
  27. #include "prlock.h"
  28. #include "prlog.h"
  29. #include "prmon.h"
  30. #include "prprf.h"
  31. #include "prthread.h"
  32. #include "prtypes.h"
  33.  
  34. #include "plerror.h"
  35. #include "plgetopt.h"
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39.  
  40. #ifdef XP_MAC
  41. #include "prlog.h"
  42. unsigned int PR_fprintf(PRFileDesc *fd, const char *fmt, ...)
  43. {
  44. PR_LogPrint(fmt);
  45. return 0;
  46. }
  47. #define printf PR_LogPrint
  48. extern void SetupMacPrintfLog(char *logFile);
  49. #endif
  50.  
  51. #define DEFAULT_DURATION 20
  52.  
  53. static PRBool failed = PR_FALSE;
  54. static PRIntervalTime oneSecond;
  55. static PRFileDesc *debug_out = NULL;
  56.  
  57. static PRUint32 PerSecond(PRIntervalTime timein)
  58. {
  59.     PRUint32 loop = 0;
  60.     while (((PRIntervalTime)(PR_IntervalNow()) - timein) < oneSecond)
  61.         loop += 1;
  62.     return loop;
  63. }  /* PerSecond */
  64.  
  65. static void PR_CALLBACK Low(void *arg)
  66. {
  67.     PRUint32 t3 = 0, t2 = 0, t1 = 0, t0, *tn = (PRUint32*)arg;
  68.     while (1)
  69.     {
  70.         t0 = PerSecond(PR_IntervalNow());
  71.         *tn = (t3 + 3 * t2 + 3 * t1 + t0) / 8;
  72.         t3 = t2; t2 = t1; t1 = t0;
  73.     }
  74. }  /* Low */
  75.  
  76. static void PR_CALLBACK High(void *arg)
  77. {
  78.     PRUint32 t3 = 0, t2 = 0, t1 = 0, t0, *tn = (PRUint32*)arg;
  79.     while (1)
  80.     {
  81.         PRIntervalTime timein = PR_IntervalNow();
  82.         PR_Sleep(oneSecond >> 2);  /* 0.25 seconds */
  83.         t0 = PerSecond(timein);
  84.         *tn = (t3 + 3 * t2 + 3 * t1 + t0) / 8;
  85.         t3 = t2; t2 = t1; t1 = t0;
  86.     }
  87. }  /* High */
  88.  
  89. static void Help(void)
  90. {
  91.     PR_fprintf(
  92.         debug_out, "Usage: priotest [-d] [-c n]\n");
  93.     PR_fprintf(
  94.         debug_out, "-c n\tduration of test in seconds (default: %d)\n", DEFAULT_DURATION);
  95.     PR_fprintf(
  96.         debug_out, "-d\tturn on debugging output (default: FALSE)\n");
  97. }  /* Help */
  98.  
  99. PRIntn main(PRIntn argc, char **argv)
  100. {
  101.     PLOptStatus os;
  102.     PRThread *low, *high;
  103.     PRThreadPriority priority;
  104.     PRBool debug_mode = PR_FALSE;
  105.     PRIntn duration = DEFAULT_DURATION;
  106.     PRUint32 totalCount, highCount = 0, lowCount = 0;
  107.     PLOptState *opt = PL_CreateOptState(argc, argv, "hdc:");
  108.  
  109.  
  110. #ifdef XP_MAC
  111.     SetupMacPrintfLog("priotest.log");
  112.     debug_mode = PR_TRUE;
  113. #endif
  114.  
  115.     debug_out = PR_STDOUT;
  116.     oneSecond = PR_SecondsToInterval(1);
  117.  
  118.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  119.     {
  120.         if (PL_OPT_BAD == os) continue;
  121.         switch (opt->option)
  122.         {
  123.         case 'd':  /* debug mode */
  124.             debug_mode = PR_TRUE;
  125.             break;
  126.         case 'c':  /* test duration */
  127.             duration = atoi(opt->value);
  128.             break;
  129.         case 'h':  /* help message */
  130.          default:
  131.             Help();
  132.             return 2;
  133.         }
  134.     }
  135.     PL_DestroyOptState(opt);
  136.     PR_STDIO_INIT();
  137.  
  138.     if (duration == 0) duration = DEFAULT_DURATION;
  139.  
  140.     printf("Priority test: running for %d seconds\n\n", duration);
  141.  
  142.     (void)PerSecond(PR_IntervalNow());
  143.     totalCount = PerSecond(PR_IntervalNow());
  144.  
  145.     /*
  146.     ** Try some rudimentary tests like setting valid priority and
  147.     ** getting it back, or setting invalid priorities and getting
  148.     ** back a valid answer.
  149.     */
  150.     PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_URGENT);
  151.     priority = PR_GetThreadPriority(PR_GetCurrentThread());
  152.     failed = ((PR_TRUE == failed) || (PR_PRIORITY_URGENT != priority))
  153.         ? PR_TRUE : PR_FALSE;
  154.     if (debug_mode && (PR_PRIORITY_URGENT != priority))
  155.     {
  156.         PR_fprintf(debug_out, "PR_[S/G]etThreadPriority() failed\n");
  157.     }
  158.  
  159. /*
  160.     The following test is bogus.  PRThreadPriority is enum and (PR_PRIORITY_FIRST - 1)
  161.     evaluates to 255 (at least on Mac).  This causes the pinning test in PR_SetThreadPriority
  162.     to fail.  That test in PR_SetThreadPriority should be looked at.
  163. */
  164. #if 0
  165.     PR_SetThreadPriority(
  166.         PR_GetCurrentThread(), (PRThreadPriority)(PR_PRIORITY_FIRST - 1));
  167.     priority = PR_GetThreadPriority(PR_GetCurrentThread());
  168.     failed = ((PR_TRUE == failed) || (PR_PRIORITY_FIRST != priority))
  169.         ? PR_TRUE : PR_FALSE;
  170.     if (debug_mode && (PR_PRIORITY_FIRST != priority))
  171.     {
  172.         PR_fprintf(debug_out, "PR_SetThreadPriority(-1) failed\n");
  173.     }
  174. #endif
  175.  
  176.     PR_SetThreadPriority(
  177.         PR_GetCurrentThread(), (PRThreadPriority)(PR_PRIORITY_LAST + 1));
  178.     priority = PR_GetThreadPriority(PR_GetCurrentThread());
  179.     failed = ((PR_TRUE == failed) || (PR_PRIORITY_LAST != priority))
  180.         ? PR_TRUE : PR_FALSE;
  181.     if (debug_mode && (PR_PRIORITY_LAST != priority))
  182.     {
  183.         PR_fprintf(debug_out, "PR_SetThreadPriority(+1) failed\n");
  184.     }
  185.  
  186.  
  187.     PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_URGENT);
  188.  
  189.     if (debug_mode)
  190.     {
  191.         PR_fprintf(debug_out,
  192.             "The high priority thread should get approximately three\n");
  193.         PR_fprintf( debug_out,
  194.             "times what the low priority thread manages. A maximum of \n");
  195.         PR_fprintf( debug_out, "%d cycles are available.\n\n", totalCount);
  196.     }
  197.  
  198. #ifdef XP_MAC
  199.     if (debug_mode)
  200.     {
  201.         PR_fprintf(debug_out,
  202.             "On Mac, this test should hang indefinitely \n");
  203.         PR_fprintf( debug_out,
  204.             "because low priority task never gives up time \n");
  205.         PR_fprintf( debug_out, "and the program will hang there.\n");
  206.     }
  207.  
  208. #endif
  209.  
  210.     low = PR_CreateThread(
  211.         PR_USER_THREAD, Low, &lowCount, PR_PRIORITY_LOW,
  212.         PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
  213.  
  214.     high = PR_CreateThread(
  215.         PR_USER_THREAD, High, &highCount, PR_PRIORITY_HIGH,
  216.         PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
  217.  
  218.     duration = (duration + 4) / 5;
  219.     while (duration--)
  220.     {
  221.         PRIntn loop = 5;
  222.         while (loop--) PR_Sleep(oneSecond);
  223.         if (debug_mode)
  224.             PR_fprintf(debug_out, "high : low :: %d : %d\n", highCount, lowCount);
  225.     }
  226.  
  227.     PR_ProcessExit((failed) ? 1 : 0);
  228.  
  229.     PR_ASSERT(!"Can't get here");
  230.     return 1;  /* or here */
  231.  
  232. }  /* main */
  233.  
  234. /* priotest.c */
  235.