1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/priotest.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,194 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * File: priotest.c 1.11 + * Purpose: testing priorities 1.12 + */ 1.13 + 1.14 +#include "prcmon.h" 1.15 +#include "prinit.h" 1.16 +#include "prinrval.h" 1.17 +#include "prlock.h" 1.18 +#include "prlog.h" 1.19 +#include "prmon.h" 1.20 +#include "prprf.h" 1.21 +#include "prthread.h" 1.22 +#include "prtypes.h" 1.23 + 1.24 +#include "plerror.h" 1.25 +#include "plgetopt.h" 1.26 + 1.27 +#include <stdio.h> 1.28 +#include <stdlib.h> 1.29 + 1.30 +#define DEFAULT_DURATION 5 1.31 + 1.32 +static PRBool failed = PR_FALSE; 1.33 +static PRIntervalTime oneSecond; 1.34 +static PRFileDesc *debug_out = NULL; 1.35 +static PRBool debug_mode = PR_FALSE; 1.36 + 1.37 +static PRUint32 PerSecond(PRIntervalTime timein) 1.38 +{ 1.39 + PRUint32 loop = 0; 1.40 + while (((PRIntervalTime)(PR_IntervalNow()) - timein) < oneSecond) 1.41 + loop += 1; 1.42 + return loop; 1.43 +} /* PerSecond */ 1.44 + 1.45 +static void PR_CALLBACK Low(void *arg) 1.46 +{ 1.47 + PRUint32 t3 = 0, t2 = 0, t1 = 0, t0, *tn = (PRUint32*)arg; 1.48 + while (1) 1.49 + { 1.50 + t0 = PerSecond(PR_IntervalNow()); 1.51 + *tn = (t3 + 3 * t2 + 3 * t1 + t0) / 8; 1.52 + t3 = t2; t2 = t1; t1 = t0; 1.53 + } 1.54 +} /* Low */ 1.55 + 1.56 +static void PR_CALLBACK High(void *arg) 1.57 +{ 1.58 + PRUint32 t3 = 0, t2 = 0, t1 = 0, t0, *tn = (PRUint32*)arg; 1.59 + while (1) 1.60 + { 1.61 + PRIntervalTime timein = PR_IntervalNow(); 1.62 + PR_Sleep(oneSecond >> 2); /* 0.25 seconds */ 1.63 + t0 = PerSecond(timein); 1.64 + *tn = (t3 + 3 * t2 + 3 * t1 + t0) / 8; 1.65 + t3 = t2; t2 = t1; t1 = t0; 1.66 + } 1.67 +} /* High */ 1.68 + 1.69 +static void Help(void) 1.70 +{ 1.71 + PR_fprintf( 1.72 + debug_out, "Usage: priotest [-d] [-c n]\n"); 1.73 + PR_fprintf( 1.74 + debug_out, "-c n\tduration of test in seconds (default: %d)\n", DEFAULT_DURATION); 1.75 + PR_fprintf( 1.76 + debug_out, "-d\tturn on debugging output (default: FALSE)\n"); 1.77 +} /* Help */ 1.78 + 1.79 +static void RudimentaryTests(void) 1.80 +{ 1.81 + /* 1.82 + ** Try some rudimentary tests like setting valid priority and 1.83 + ** getting it back, or setting invalid priorities and getting 1.84 + ** back a valid answer. 1.85 + */ 1.86 + PRThreadPriority priority; 1.87 + PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_URGENT); 1.88 + priority = PR_GetThreadPriority(PR_GetCurrentThread()); 1.89 + failed = ((PR_TRUE == failed) || (PR_PRIORITY_URGENT != priority)) 1.90 + ? PR_TRUE : PR_FALSE; 1.91 + if (debug_mode && (PR_PRIORITY_URGENT != priority)) 1.92 + { 1.93 + PR_fprintf(debug_out, "PR_[S/G]etThreadPriority() failed\n"); 1.94 + } 1.95 + 1.96 + 1.97 + PR_SetThreadPriority( 1.98 + PR_GetCurrentThread(), (PRThreadPriority)(PR_PRIORITY_FIRST - 1)); 1.99 + priority = PR_GetThreadPriority(PR_GetCurrentThread()); 1.100 + failed = ((PR_TRUE == failed) || (PR_PRIORITY_FIRST != priority)) 1.101 + ? PR_TRUE : PR_FALSE; 1.102 + if (debug_mode && (PR_PRIORITY_FIRST != priority)) 1.103 + { 1.104 + PR_fprintf(debug_out, "PR_SetThreadPriority(-1) failed\n"); 1.105 + } 1.106 + 1.107 + PR_SetThreadPriority( 1.108 + PR_GetCurrentThread(), (PRThreadPriority)(PR_PRIORITY_LAST + 1)); 1.109 + priority = PR_GetThreadPriority(PR_GetCurrentThread()); 1.110 + failed = ((PR_TRUE == failed) || (PR_PRIORITY_LAST != priority)) 1.111 + ? PR_TRUE : PR_FALSE; 1.112 + if (debug_mode && (PR_PRIORITY_LAST != priority)) 1.113 + { 1.114 + PR_fprintf(debug_out, "PR_SetThreadPriority(+1) failed\n"); 1.115 + } 1.116 + 1.117 +} /* RudimentataryTests */ 1.118 + 1.119 +static void CreateThreads(PRUint32 *lowCount, PRUint32 *highCount) 1.120 +{ 1.121 + (void)PR_CreateThread( 1.122 + PR_USER_THREAD, Low, lowCount, PR_PRIORITY_LOW, 1.123 + PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); 1.124 + (void)PR_CreateThread( 1.125 + PR_USER_THREAD, High, highCount, PR_PRIORITY_HIGH, 1.126 + PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); 1.127 +} /* CreateThreads */ 1.128 + 1.129 +int main(int argc, char **argv) 1.130 +{ 1.131 + PLOptStatus os; 1.132 + PRIntn duration = DEFAULT_DURATION; 1.133 + PRUint32 totalCount, highCount = 0, lowCount = 0; 1.134 + PLOptState *opt = PL_CreateOptState(argc, argv, "hdc:"); 1.135 + 1.136 + debug_out = PR_STDOUT; 1.137 + oneSecond = PR_SecondsToInterval(1); 1.138 + 1.139 + while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) 1.140 + { 1.141 + if (PL_OPT_BAD == os) continue; 1.142 + switch (opt->option) 1.143 + { 1.144 + case 'd': /* debug mode */ 1.145 + debug_mode = PR_TRUE; 1.146 + break; 1.147 + case 'c': /* test duration */ 1.148 + duration = atoi(opt->value); 1.149 + break; 1.150 + case 'h': /* help message */ 1.151 + default: 1.152 + Help(); 1.153 + return 2; 1.154 + } 1.155 + } 1.156 + PL_DestroyOptState(opt); 1.157 + PR_STDIO_INIT(); 1.158 + 1.159 + if (duration == 0) duration = DEFAULT_DURATION; 1.160 + 1.161 + RudimentaryTests(); 1.162 + 1.163 + printf("Priority test: running for %d seconds\n\n", duration); 1.164 + 1.165 + (void)PerSecond(PR_IntervalNow()); 1.166 + totalCount = PerSecond(PR_IntervalNow()); 1.167 + 1.168 + PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_URGENT); 1.169 + 1.170 + if (debug_mode) 1.171 + { 1.172 + PR_fprintf(debug_out, 1.173 + "The high priority thread should get approximately three\n"); 1.174 + PR_fprintf( debug_out, 1.175 + "times what the low priority thread manages. A maximum of \n"); 1.176 + PR_fprintf( debug_out, "%d cycles are available.\n\n", totalCount); 1.177 + } 1.178 + 1.179 + duration = (duration + 4) / 5; 1.180 + CreateThreads(&lowCount, &highCount); 1.181 + while (duration--) 1.182 + { 1.183 + PRIntn loop = 5; 1.184 + while (loop--) PR_Sleep(oneSecond); 1.185 + if (debug_mode) 1.186 + PR_fprintf(debug_out, "high : low :: %d : %d\n", highCount, lowCount); 1.187 + } 1.188 + 1.189 + 1.190 + PR_ProcessExit((failed) ? 1 : 0); 1.191 + 1.192 + PR_ASSERT(!"You can't get here -- but you did!"); 1.193 + return 1; /* or here */ 1.194 + 1.195 +} /* main */ 1.196 + 1.197 +/* priotest.c */