home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / switch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.2 KB  |  256 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:            switch.c
  21. ** Description:     trying to time context switches
  22. */
  23.  
  24. #include "prinit.h"
  25. #include "prcvar.h"
  26. #include "prmem.h"
  27. #include "prinrval.h"
  28. #include "prlock.h"
  29. #include "prlog.h"
  30. #include "prthread.h"
  31. #include "prprf.h"
  32.  
  33. #include "plerror.h"
  34. #include "plgetopt.h"
  35.  
  36. #if defined(XP_MAC)
  37. #include "pprio.h"
  38. #define printf PR_LogPrint
  39. #else
  40. #include "private/pprio.h"
  41. #endif
  42.  
  43. #include <stdlib.h>
  44.  
  45. #define INNER_LOOPS 100
  46. #define DEFAULT_LOOPS 100
  47. #define DEFAULT_THREADS 10
  48.  
  49. static PRFileDesc *debug_out = NULL;
  50. static PRBool debug_mode = PR_FALSE, verbosity = PR_FALSE, failed = PR_FALSE;
  51.  
  52. typedef struct Shared
  53. {
  54.     PRLock *ml;
  55.     PRCondVar *cv;
  56.     PRBool twiddle;
  57.     PRThread *thread;
  58.     struct Shared *next;
  59. } Shared;
  60.  
  61. static void Help(void)
  62. {
  63.     debug_out = PR_STDOUT;
  64.  
  65.     PR_fprintf(
  66.         debug_out, "Usage: >./switch [-d] [-c n] [-t n] [-T n] [-G]\n");
  67.     PR_fprintf(
  68.         debug_out, "-c n\tloops at thread level (default: %d)\n", DEFAULT_LOOPS);
  69.     PR_fprintf(
  70.         debug_out, "-t n\tnumber of threads (default: %d)\n", DEFAULT_THREADS);
  71.     PR_fprintf(debug_out, "-d\tturn on debugging output (default: FALSE)\n");
  72.     PR_fprintf(debug_out, "-v\tturn on verbose output (default: FALSE)\n");
  73.     PR_fprintf(debug_out, "-G n\tglobal threads only (default: FALSE)\n");
  74.     PR_fprintf(debug_out, "-C n\tconcurrency setting (default: 1)\n");
  75. }  /* Help */
  76.  
  77. static void PR_CALLBACK Notified(void *arg)
  78. {
  79.     Shared *shared = (Shared*)arg;
  80.     PRStatus status = PR_SUCCESS;
  81.     while (PR_SUCCESS == status)
  82.     {
  83.         PR_Lock(shared->ml);
  84.         while (shared->twiddle && (PR_SUCCESS == status))
  85.             status = PR_WaitCondVar(shared->cv, PR_INTERVAL_NO_TIMEOUT);
  86.         if (verbosity) PR_fprintf(debug_out, "+");
  87.         shared->twiddle = PR_TRUE;
  88.         shared->next->twiddle = PR_FALSE;
  89.         PR_NotifyCondVar(shared->next->cv);
  90.         PR_Unlock(shared->ml);
  91.     }
  92. }  /* Notified */
  93.  
  94. static Shared home;
  95. PRIntn PR_CALLBACK Switch(PRIntn argc, char **argv)
  96. {
  97.     PLOptStatus os;
  98.     PRStatus status;
  99.     PRBool help = PR_FALSE;
  100.     PRUintn concurrency = 1;
  101.     Shared *shared, *link;
  102.     PRIntervalTime timein, timeout;
  103.     PRThreadScope thread_scope = PR_LOCAL_THREAD;
  104.     PRUintn thread_count, inner_count, loop_count, average;
  105.     PRUintn thread_limit = DEFAULT_THREADS, loop_limit = DEFAULT_LOOPS;
  106.     PLOptState *opt = PL_CreateOptState(argc, argv, "hdvc:t:C:G");
  107.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  108.     {
  109.         if (PL_OPT_BAD == os) continue;
  110.         switch (opt->option)
  111.         {
  112.         case 'v':  /* verbose mode */
  113.             verbosity = PR_TRUE;
  114.         case 'd':  /* debug mode */
  115.             debug_mode = PR_TRUE;
  116.             break;
  117.         case 'c':  /* loop counter */
  118.             loop_limit = atoi(opt->value);
  119.             break;
  120.         case 't':  /* thread limit */
  121.             thread_limit = atoi(opt->value);
  122.             break;
  123.         case 'C':  /* Concurrency limit */
  124.             concurrency = atoi(opt->value);
  125.             break;
  126.         case 'G':  /* global threads only */
  127.             thread_scope = PR_GLOBAL_THREAD;
  128.             break;
  129.         case 'h':  /* help message */
  130.             Help();
  131.             help = PR_TRUE;
  132.             break;
  133.          default:
  134.             break;
  135.         }
  136.     }
  137.     PL_DestroyOptState(opt);
  138.  
  139.     if (help) return -1;
  140.  
  141.     if (PR_TRUE == debug_mode)
  142.     {
  143.         debug_out = PR_STDOUT;
  144.         PR_fprintf(debug_out, "Test parameters\n");
  145.         PR_fprintf(debug_out, "\tThreads involved: %d\n", thread_limit);
  146.         PR_fprintf(debug_out, "\tIteration limit: %d\n", loop_limit);
  147.         PR_fprintf(debug_out, "\tConcurrency: %d\n", concurrency);
  148.         PR_fprintf(
  149.             debug_out, "\tThread type: %s\n",
  150.             (PR_GLOBAL_THREAD == thread_scope) ? "GLOBAL" : "LOCAL");
  151.     }
  152.  
  153.     PR_SetConcurrency(concurrency);
  154.  
  155.     link = &home;
  156.     home.ml = PR_NewLock();
  157.     home.cv = PR_NewCondVar(home.ml);
  158.     home.twiddle = PR_FALSE;
  159.     home.next = NULL;
  160.  
  161.     timeout = 0;
  162.  
  163.     for (thread_count = 1; thread_count <= thread_limit; ++thread_count)
  164.     {
  165.         shared = PR_NEWZAP(Shared);
  166.  
  167.         shared->ml = home.ml;
  168.         shared->cv = PR_NewCondVar(home.ml);
  169.         shared->twiddle = PR_TRUE;
  170.         shared->next = link;
  171.         link = shared;
  172.  
  173.         shared->thread = PR_CreateThread(
  174.             PR_USER_THREAD, Notified, shared,
  175.             PR_PRIORITY_HIGH, thread_scope,
  176.             PR_JOINABLE_THREAD, 0);
  177.         PR_ASSERT(shared->thread != NULL);
  178.         if (NULL == shared->thread)
  179.             failed = PR_TRUE;
  180.     }
  181.  
  182.     for (loop_count = 1; loop_count <= loop_limit; ++loop_count)
  183.     {
  184.         timein = PR_IntervalNow();
  185.         for (inner_count = 0; inner_count < INNER_LOOPS; ++inner_count)
  186.         {
  187.             PR_Lock(home.ml);
  188.             home.twiddle = PR_TRUE;
  189.             shared->twiddle = PR_FALSE;
  190.             PR_NotifyCondVar(shared->cv);
  191.             while (home.twiddle)
  192.             {
  193.                 status = PR_WaitCondVar(home.cv, PR_INTERVAL_NO_TIMEOUT);
  194.                 if (PR_FAILURE == status)
  195.                     failed = PR_TRUE;
  196.             }
  197.             PR_Unlock(home.ml);
  198.         }
  199.         timeout += (PR_IntervalNow() - timein);
  200.     }
  201.  
  202.     if (debug_mode)
  203.     {
  204.         average = PR_IntervalToMicroseconds(timeout)
  205.             / (INNER_LOOPS * loop_limit * thread_count);
  206.         PR_fprintf(
  207.             debug_out, "Average switch times %d usecs for %d threads\n",
  208.             average, thread_limit);
  209.     }
  210.  
  211.     link = shared;
  212.     for (thread_count = 1; thread_count <= thread_limit; ++thread_count)
  213.     {
  214.         if (&home == link) break;
  215.         status = PR_Interrupt(link->thread);
  216.         if (PR_SUCCESS != status)
  217.         {
  218.             failed = PR_TRUE;
  219.             if (debug_mode)
  220.                 PL_FPrintError(debug_out, "Failed to interrupt");
  221.         }
  222.         link = link->next; 
  223.     }
  224.  
  225.     for (thread_count = 1; thread_count <= thread_limit; ++thread_count)
  226.     {
  227.         link = shared->next;
  228.         status = PR_JoinThread(shared->thread);
  229.         if (PR_SUCCESS != status)
  230.         {
  231.             failed = PR_TRUE;
  232.             if (debug_mode)
  233.                 PL_FPrintError(debug_out, "Failed to join");
  234.         }
  235.         PR_DestroyCondVar(shared->cv);
  236.         PR_DELETE(shared);
  237.         if (&home == link) break;
  238.         shared = link;
  239.     }
  240.     PR_DestroyCondVar(home.cv);
  241.     PR_DestroyLock(home.ml);
  242.  
  243.     PR_fprintf(PR_STDOUT, ((failed) ? "FAILED\n" : "PASSED\n"));
  244.     return ((failed) ? 1 : 0);
  245. }  /* Switch */
  246.  
  247. PRIntn main(PRIntn argc, char **argv)
  248. {
  249.     PRIntn result;
  250.     PR_STDIO_INIT();
  251.     result = PR_Initialize(Switch, argc, argv, 0);
  252.     return result;
  253. }  /* main */
  254.  
  255. /* switch.c */
  256.