Back to index

lightning-sunbird  0.9+nobinonly
priotest.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * File:        priotest.c
00040  * Purpose:     testing priorities
00041  */
00042 
00043 #ifdef XP_MAC
00044 #error "This test does not run on Macintosh"
00045 #else
00046 
00047 
00048 #include "prcmon.h"
00049 #include "prinit.h"
00050 #include "prinrval.h"
00051 #include "prlock.h"
00052 #include "prlog.h"
00053 #include "prmon.h"
00054 #include "prprf.h"
00055 #include "prthread.h"
00056 #include "prtypes.h"
00057 
00058 #include "plerror.h"
00059 #include "plgetopt.h"
00060 
00061 #include <stdio.h>
00062 #include <stdlib.h>
00063 
00064 #define DEFAULT_DURATION 5
00065 
00066 static PRBool failed = PR_FALSE;
00067 static PRIntervalTime oneSecond;
00068 static PRFileDesc *debug_out = NULL;
00069 static PRBool debug_mode = PR_FALSE;
00070 
00071 static PRUint32 PerSecond(PRIntervalTime timein)
00072 {
00073     PRUint32 loop = 0;
00074     while (((PRIntervalTime)(PR_IntervalNow()) - timein) < oneSecond)
00075         loop += 1;
00076     return loop;
00077 }  /* PerSecond */
00078 
00079 static void PR_CALLBACK Low(void *arg)
00080 {
00081     PRUint32 t3 = 0, t2 = 0, t1 = 0, t0, *tn = (PRUint32*)arg;
00082     while (1)
00083     {
00084         t0 = PerSecond(PR_IntervalNow());
00085         *tn = (t3 + 3 * t2 + 3 * t1 + t0) / 8;
00086         t3 = t2; t2 = t1; t1 = t0;
00087     }
00088 }  /* Low */
00089 
00090 static void PR_CALLBACK High(void *arg)
00091 {
00092     PRUint32 t3 = 0, t2 = 0, t1 = 0, t0, *tn = (PRUint32*)arg;
00093     while (1)
00094     {
00095         PRIntervalTime timein = PR_IntervalNow();
00096         PR_Sleep(oneSecond >> 2);  /* 0.25 seconds */
00097         t0 = PerSecond(timein);
00098         *tn = (t3 + 3 * t2 + 3 * t1 + t0) / 8;
00099         t3 = t2; t2 = t1; t1 = t0;
00100     }
00101 }  /* High */
00102 
00103 static void Help(void)
00104 {
00105     PR_fprintf(
00106               debug_out, "Usage: priotest [-d] [-c n]\n");
00107     PR_fprintf(
00108               debug_out, "-c n\tduration of test in seconds (default: %d)\n", DEFAULT_DURATION);
00109     PR_fprintf(
00110         debug_out, "-d\tturn on debugging output (default: FALSE)\n");
00111 }  /* Help */
00112 
00113 static void RudimentaryTests(void)
00114 {
00115     /*
00116     ** Try some rudimentary tests like setting valid priority and
00117     ** getting it back, or setting invalid priorities and getting
00118     ** back a valid answer.
00119     */
00120     PRThreadPriority priority;
00121     PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_URGENT);
00122     priority = PR_GetThreadPriority(PR_GetCurrentThread());
00123     failed = ((PR_TRUE == failed) || (PR_PRIORITY_URGENT != priority))
00124         ? PR_TRUE : PR_FALSE;
00125     if (debug_mode && (PR_PRIORITY_URGENT != priority))
00126     {
00127         PR_fprintf(debug_out, "PR_[S/G]etThreadPriority() failed\n");
00128     }
00129 
00130 
00131     PR_SetThreadPriority(
00132         PR_GetCurrentThread(), (PRThreadPriority)(PR_PRIORITY_FIRST - 1));
00133     priority = PR_GetThreadPriority(PR_GetCurrentThread());
00134     failed = ((PR_TRUE == failed) || (PR_PRIORITY_FIRST != priority))
00135         ? PR_TRUE : PR_FALSE;
00136     if (debug_mode && (PR_PRIORITY_FIRST != priority))
00137     {
00138         PR_fprintf(debug_out, "PR_SetThreadPriority(-1) failed\n");
00139     }
00140 
00141     PR_SetThreadPriority(
00142         PR_GetCurrentThread(), (PRThreadPriority)(PR_PRIORITY_LAST + 1));
00143     priority = PR_GetThreadPriority(PR_GetCurrentThread());
00144     failed = ((PR_TRUE == failed) || (PR_PRIORITY_LAST != priority))
00145         ? PR_TRUE : PR_FALSE;
00146     if (debug_mode && (PR_PRIORITY_LAST != priority))
00147     {
00148         PR_fprintf(debug_out, "PR_SetThreadPriority(+1) failed\n");
00149     }
00150 
00151 }  /* RudimentataryTests */
00152 
00153 static void CreateThreads(PRUint32 *lowCount, PRUint32 *highCount)
00154 {
00155     (void)PR_CreateThread(
00156         PR_USER_THREAD, Low, lowCount, PR_PRIORITY_LOW,
00157         PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
00158     (void)PR_CreateThread(
00159         PR_USER_THREAD, High, highCount, PR_PRIORITY_HIGH,
00160         PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
00161 } /* CreateThreads */
00162 
00163 PRIntn main(PRIntn argc, char **argv)
00164 {
00165     PLOptStatus os;
00166     PRIntn duration = DEFAULT_DURATION;
00167     PRUint32 totalCount, highCount = 0, lowCount = 0;
00168        PLOptState *opt = PL_CreateOptState(argc, argv, "hdc:");
00169 
00170     debug_out = PR_STDOUT;
00171     oneSecond = PR_SecondsToInterval(1);
00172 
00173        while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00174     {
00175               if (PL_OPT_BAD == os) continue;
00176         switch (opt->option)
00177         {
00178         case 'd':  /* debug mode */
00179                      debug_mode = PR_TRUE;
00180             break;
00181         case 'c':  /* test duration */
00182                      duration = atoi(opt->value);
00183             break;
00184         case 'h':  /* help message */
00185          default:
00186                      Help();
00187                      return 2;
00188         }
00189     }
00190        PL_DestroyOptState(opt);
00191     PR_STDIO_INIT();
00192 
00193     if (duration == 0) duration = DEFAULT_DURATION;
00194 
00195     RudimentaryTests();
00196 
00197     printf("Priority test: running for %d seconds\n\n", duration);
00198 
00199     (void)PerSecond(PR_IntervalNow());
00200     totalCount = PerSecond(PR_IntervalNow());
00201 
00202     PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_URGENT);
00203 
00204     if (debug_mode)
00205     {
00206         PR_fprintf(debug_out,
00207                   "The high priority thread should get approximately three\n");
00208         PR_fprintf( debug_out,
00209                   "times what the low priority thread manages. A maximum of \n");
00210         PR_fprintf( debug_out, "%d cycles are available.\n\n", totalCount);
00211     }
00212 
00213     duration = (duration + 4) / 5;
00214     CreateThreads(&lowCount, &highCount);
00215     while (duration--)
00216     {
00217         PRIntn loop = 5;
00218         while (loop--) PR_Sleep(oneSecond);
00219         if (debug_mode)
00220             PR_fprintf(debug_out, "high : low :: %d : %d\n", highCount, lowCount);
00221     }
00222 
00223 
00224     PR_ProcessExit((failed) ? 1 : 0);
00225 
00226        PR_ASSERT(!"You can't get here -- but you did!");
00227        return 1;  /* or here */
00228 
00229 }  /* main */
00230 
00231 #endif  /* ifdef XP_MAC */
00232 
00233 /* priotest.c */