priotest.c (5523B)
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 /* 7 * File: priotest.c 8 * Purpose: testing priorities 9 */ 10 11 #include "prcmon.h" 12 #include "prinit.h" 13 #include "prinrval.h" 14 #include "prlock.h" 15 #include "prlog.h" 16 #include "prmon.h" 17 #include "prprf.h" 18 #include "prthread.h" 19 #include "prtypes.h" 20 21 #include "plerror.h" 22 #include "plgetopt.h" 23 24 #include <stdio.h> 25 #include <stdlib.h> 26 27 #define DEFAULT_DURATION 5 28 29 static PRBool failed = PR_FALSE; 30 static PRIntervalTime oneSecond; 31 static PRFileDesc* debug_out = NULL; 32 static PRBool debug_mode = PR_FALSE; 33 34 static PRUint32 PerSecond(PRIntervalTime timein) { 35 PRUint32 loop = 0; 36 while (((PRIntervalTime)(PR_IntervalNow()) - timein) < oneSecond) { 37 loop += 1; 38 } 39 return loop; 40 } /* PerSecond */ 41 42 static void PR_CALLBACK Low(void* arg) { 43 PRUint32 t3 = 0, t2 = 0, t1 = 0, t0, *tn = (PRUint32*)arg; 44 while (1) { 45 t0 = PerSecond(PR_IntervalNow()); 46 *tn = (t3 + 3 * t2 + 3 * t1 + t0) / 8; 47 t3 = t2; 48 t2 = t1; 49 t1 = t0; 50 } 51 } /* Low */ 52 53 static void PR_CALLBACK High(void* arg) { 54 PRUint32 t3 = 0, t2 = 0, t1 = 0, t0, *tn = (PRUint32*)arg; 55 while (1) { 56 PRIntervalTime timein = PR_IntervalNow(); 57 PR_Sleep(oneSecond >> 2); /* 0.25 seconds */ 58 t0 = PerSecond(timein); 59 *tn = (t3 + 3 * t2 + 3 * t1 + t0) / 8; 60 t3 = t2; 61 t2 = t1; 62 t1 = t0; 63 } 64 } /* High */ 65 66 static void Help(void) { 67 PR_fprintf(debug_out, "Usage: priotest [-d] [-c n]\n"); 68 PR_fprintf(debug_out, "-c n\tduration of test in seconds (default: %d)\n", 69 DEFAULT_DURATION); 70 PR_fprintf(debug_out, "-d\tturn on debugging output (default: FALSE)\n"); 71 } /* Help */ 72 73 static void RudimentaryTests(void) { 74 /* 75 ** Try some rudimentary tests like setting valid priority and 76 ** getting it back, or setting invalid priorities and getting 77 ** back a valid answer. 78 */ 79 PRThreadPriority priority; 80 PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_URGENT); 81 priority = PR_GetThreadPriority(PR_GetCurrentThread()); 82 failed = ((PR_TRUE == failed) || (PR_PRIORITY_URGENT != priority)) ? PR_TRUE 83 : PR_FALSE; 84 if (debug_mode && (PR_PRIORITY_URGENT != priority)) { 85 PR_fprintf(debug_out, "PR_[S/G]etThreadPriority() failed\n"); 86 } 87 88 PR_SetThreadPriority(PR_GetCurrentThread(), 89 (PRThreadPriority)(PR_PRIORITY_FIRST - 1)); 90 priority = PR_GetThreadPriority(PR_GetCurrentThread()); 91 failed = ((PR_TRUE == failed) || (PR_PRIORITY_FIRST != priority)) ? PR_TRUE 92 : PR_FALSE; 93 if (debug_mode && (PR_PRIORITY_FIRST != priority)) { 94 PR_fprintf(debug_out, "PR_SetThreadPriority(-1) failed\n"); 95 } 96 97 PR_SetThreadPriority(PR_GetCurrentThread(), 98 (PRThreadPriority)(PR_PRIORITY_LAST + 1)); 99 priority = PR_GetThreadPriority(PR_GetCurrentThread()); 100 failed = ((PR_TRUE == failed) || (PR_PRIORITY_LAST != priority)) ? PR_TRUE 101 : PR_FALSE; 102 if (debug_mode && (PR_PRIORITY_LAST != priority)) { 103 PR_fprintf(debug_out, "PR_SetThreadPriority(+1) failed\n"); 104 } 105 106 } /* RudimentataryTests */ 107 108 static void CreateThreads(PRUint32* lowCount, PRUint32* highCount) { 109 (void)PR_CreateThread(PR_USER_THREAD, Low, lowCount, PR_PRIORITY_LOW, 110 PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); 111 (void)PR_CreateThread(PR_USER_THREAD, High, highCount, PR_PRIORITY_HIGH, 112 PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); 113 } /* CreateThreads */ 114 115 int main(int argc, char** argv) { 116 PLOptStatus os; 117 PRIntn duration = DEFAULT_DURATION; 118 PRUint32 totalCount, highCount = 0, lowCount = 0; 119 PLOptState* opt = PL_CreateOptState(argc, argv, "hdc:"); 120 121 debug_out = PR_STDOUT; 122 oneSecond = PR_SecondsToInterval(1); 123 124 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { 125 if (PL_OPT_BAD == os) { 126 continue; 127 } 128 switch (opt->option) { 129 case 'd': /* debug mode */ 130 debug_mode = PR_TRUE; 131 break; 132 case 'c': /* test duration */ 133 duration = atoi(opt->value); 134 break; 135 case 'h': /* help message */ 136 default: 137 Help(); 138 return 2; 139 } 140 } 141 PL_DestroyOptState(opt); 142 143 if (duration == 0) { 144 duration = DEFAULT_DURATION; 145 } 146 147 RudimentaryTests(); 148 149 printf("Priority test: running for %d seconds\n\n", duration); 150 151 (void)PerSecond(PR_IntervalNow()); 152 totalCount = PerSecond(PR_IntervalNow()); 153 154 PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_URGENT); 155 156 if (debug_mode) { 157 PR_fprintf(debug_out, 158 "The high priority thread should get approximately three\n"); 159 PR_fprintf(debug_out, 160 "times what the low priority thread manages. A maximum of \n"); 161 PR_fprintf(debug_out, "%d cycles are available.\n\n", totalCount); 162 } 163 164 duration = (duration + 4) / 5; 165 CreateThreads(&lowCount, &highCount); 166 while (duration--) { 167 PRIntn loop = 5; 168 while (loop--) { 169 PR_Sleep(oneSecond); 170 } 171 if (debug_mode) { 172 PR_fprintf(debug_out, "high : low :: %d : %d\n", highCount, lowCount); 173 } 174 } 175 176 PR_ProcessExit((failed) ? 1 : 0); 177 178 PR_NOT_REACHED("You can't get here -- but you did!"); 179 return 1; /* or here */ 180 181 } /* main */ 182 183 /* priotest.c */