1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998-2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | /*
|
---|
39 | * File: priotest.c
|
---|
40 | * Purpose: testing priorities
|
---|
41 | */
|
---|
42 |
|
---|
43 | #ifdef XP_MAC
|
---|
44 | #error "This test does not run on Macintosh"
|
---|
45 | #else
|
---|
46 |
|
---|
47 |
|
---|
48 | #include "prcmon.h"
|
---|
49 | #include "prinit.h"
|
---|
50 | #include "prinrval.h"
|
---|
51 | #include "prlock.h"
|
---|
52 | #include "prlog.h"
|
---|
53 | #include "prmon.h"
|
---|
54 | #include "prprf.h"
|
---|
55 | #include "prthread.h"
|
---|
56 | #include "prtypes.h"
|
---|
57 |
|
---|
58 | #include "plerror.h"
|
---|
59 | #include "plgetopt.h"
|
---|
60 |
|
---|
61 | #include <stdio.h>
|
---|
62 | #include <stdlib.h>
|
---|
63 |
|
---|
64 | #define DEFAULT_DURATION 5
|
---|
65 |
|
---|
66 | static PRBool failed = PR_FALSE;
|
---|
67 | static PRIntervalTime oneSecond;
|
---|
68 | static PRFileDesc *debug_out = NULL;
|
---|
69 | static PRBool debug_mode = PR_FALSE;
|
---|
70 |
|
---|
71 | static PRUint32 PerSecond(PRIntervalTime timein)
|
---|
72 | {
|
---|
73 | PRUint32 loop = 0;
|
---|
74 | while (((PRIntervalTime)(PR_IntervalNow()) - timein) < oneSecond)
|
---|
75 | loop += 1;
|
---|
76 | return loop;
|
---|
77 | } /* PerSecond */
|
---|
78 |
|
---|
79 | static void PR_CALLBACK Low(void *arg)
|
---|
80 | {
|
---|
81 | PRUint32 t3 = 0, t2 = 0, t1 = 0, t0, *tn = (PRUint32*)arg;
|
---|
82 | while (1)
|
---|
83 | {
|
---|
84 | t0 = PerSecond(PR_IntervalNow());
|
---|
85 | *tn = (t3 + 3 * t2 + 3 * t1 + t0) / 8;
|
---|
86 | t3 = t2; t2 = t1; t1 = t0;
|
---|
87 | }
|
---|
88 | } /* Low */
|
---|
89 |
|
---|
90 | static void PR_CALLBACK High(void *arg)
|
---|
91 | {
|
---|
92 | PRUint32 t3 = 0, t2 = 0, t1 = 0, t0, *tn = (PRUint32*)arg;
|
---|
93 | while (1)
|
---|
94 | {
|
---|
95 | PRIntervalTime timein = PR_IntervalNow();
|
---|
96 | PR_Sleep(oneSecond >> 2); /* 0.25 seconds */
|
---|
97 | t0 = PerSecond(timein);
|
---|
98 | *tn = (t3 + 3 * t2 + 3 * t1 + t0) / 8;
|
---|
99 | t3 = t2; t2 = t1; t1 = t0;
|
---|
100 | }
|
---|
101 | } /* High */
|
---|
102 |
|
---|
103 | static void Help(void)
|
---|
104 | {
|
---|
105 | PR_fprintf(
|
---|
106 | debug_out, "Usage: priotest [-d] [-c n]\n");
|
---|
107 | PR_fprintf(
|
---|
108 | debug_out, "-c n\tduration of test in seconds (default: %d)\n", DEFAULT_DURATION);
|
---|
109 | PR_fprintf(
|
---|
110 | debug_out, "-d\tturn on debugging output (default: FALSE)\n");
|
---|
111 | } /* Help */
|
---|
112 |
|
---|
113 | static void RudimentaryTests(void)
|
---|
114 | {
|
---|
115 | /*
|
---|
116 | ** Try some rudimentary tests like setting valid priority and
|
---|
117 | ** getting it back, or setting invalid priorities and getting
|
---|
118 | ** back a valid answer.
|
---|
119 | */
|
---|
120 | PRThreadPriority priority;
|
---|
121 | PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_URGENT);
|
---|
122 | priority = PR_GetThreadPriority(PR_GetCurrentThread());
|
---|
123 | failed = ((PR_TRUE == failed) || (PR_PRIORITY_URGENT != priority))
|
---|
124 | ? PR_TRUE : PR_FALSE;
|
---|
125 | if (debug_mode && (PR_PRIORITY_URGENT != priority))
|
---|
126 | {
|
---|
127 | PR_fprintf(debug_out, "PR_[S/G]etThreadPriority() failed\n");
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | PR_SetThreadPriority(
|
---|
132 | PR_GetCurrentThread(), (PRThreadPriority)(PR_PRIORITY_FIRST - 1));
|
---|
133 | priority = PR_GetThreadPriority(PR_GetCurrentThread());
|
---|
134 | failed = ((PR_TRUE == failed) || (PR_PRIORITY_FIRST != priority))
|
---|
135 | ? PR_TRUE : PR_FALSE;
|
---|
136 | if (debug_mode && (PR_PRIORITY_FIRST != priority))
|
---|
137 | {
|
---|
138 | PR_fprintf(debug_out, "PR_SetThreadPriority(-1) failed\n");
|
---|
139 | }
|
---|
140 |
|
---|
141 | PR_SetThreadPriority(
|
---|
142 | PR_GetCurrentThread(), (PRThreadPriority)(PR_PRIORITY_LAST + 1));
|
---|
143 | priority = PR_GetThreadPriority(PR_GetCurrentThread());
|
---|
144 | failed = ((PR_TRUE == failed) || (PR_PRIORITY_LAST != priority))
|
---|
145 | ? PR_TRUE : PR_FALSE;
|
---|
146 | if (debug_mode && (PR_PRIORITY_LAST != priority))
|
---|
147 | {
|
---|
148 | PR_fprintf(debug_out, "PR_SetThreadPriority(+1) failed\n");
|
---|
149 | }
|
---|
150 |
|
---|
151 | } /* RudimentataryTests */
|
---|
152 |
|
---|
153 | static void CreateThreads(PRUint32 *lowCount, PRUint32 *highCount)
|
---|
154 | {
|
---|
155 | (void)PR_CreateThread(
|
---|
156 | PR_USER_THREAD, Low, lowCount, PR_PRIORITY_LOW,
|
---|
157 | PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
|
---|
158 | (void)PR_CreateThread(
|
---|
159 | PR_USER_THREAD, High, highCount, PR_PRIORITY_HIGH,
|
---|
160 | PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
|
---|
161 | } /* CreateThreads */
|
---|
162 |
|
---|
163 | PRIntn main(PRIntn argc, char **argv)
|
---|
164 | {
|
---|
165 | PLOptStatus os;
|
---|
166 | PRIntn duration = DEFAULT_DURATION;
|
---|
167 | PRUint32 totalCount, highCount = 0, lowCount = 0;
|
---|
168 | PLOptState *opt = PL_CreateOptState(argc, argv, "hdc:");
|
---|
169 |
|
---|
170 | debug_out = PR_STDOUT;
|
---|
171 | oneSecond = PR_SecondsToInterval(1);
|
---|
172 |
|
---|
173 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
|
---|
174 | {
|
---|
175 | if (PL_OPT_BAD == os) continue;
|
---|
176 | switch (opt->option)
|
---|
177 | {
|
---|
178 | case 'd': /* debug mode */
|
---|
179 | debug_mode = PR_TRUE;
|
---|
180 | break;
|
---|
181 | case 'c': /* test duration */
|
---|
182 | duration = atoi(opt->value);
|
---|
183 | break;
|
---|
184 | case 'h': /* help message */
|
---|
185 | default:
|
---|
186 | Help();
|
---|
187 | return 2;
|
---|
188 | }
|
---|
189 | }
|
---|
190 | PL_DestroyOptState(opt);
|
---|
191 | PR_STDIO_INIT();
|
---|
192 |
|
---|
193 | if (duration == 0) duration = DEFAULT_DURATION;
|
---|
194 |
|
---|
195 | RudimentaryTests();
|
---|
196 |
|
---|
197 | printf("Priority test: running for %d seconds\n\n", duration);
|
---|
198 |
|
---|
199 | (void)PerSecond(PR_IntervalNow());
|
---|
200 | totalCount = PerSecond(PR_IntervalNow());
|
---|
201 |
|
---|
202 | PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_URGENT);
|
---|
203 |
|
---|
204 | if (debug_mode)
|
---|
205 | {
|
---|
206 | PR_fprintf(debug_out,
|
---|
207 | "The high priority thread should get approximately three\n");
|
---|
208 | PR_fprintf( debug_out,
|
---|
209 | "times what the low priority thread manages. A maximum of \n");
|
---|
210 | PR_fprintf( debug_out, "%d cycles are available.\n\n", totalCount);
|
---|
211 | }
|
---|
212 |
|
---|
213 | duration = (duration + 4) / 5;
|
---|
214 | CreateThreads(&lowCount, &highCount);
|
---|
215 | while (duration--)
|
---|
216 | {
|
---|
217 | PRIntn loop = 5;
|
---|
218 | while (loop--) PR_Sleep(oneSecond);
|
---|
219 | if (debug_mode)
|
---|
220 | PR_fprintf(debug_out, "high : low :: %d : %d\n", highCount, lowCount);
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | PR_ProcessExit((failed) ? 1 : 0);
|
---|
225 |
|
---|
226 | PR_ASSERT(!"You can't get here -- but you did!");
|
---|
227 | return 1; /* or here */
|
---|
228 |
|
---|
229 | } /* main */
|
---|
230 |
|
---|
231 | #endif /* ifdef XP_MAC */
|
---|
232 |
|
---|
233 | /* priotest.c */
|
---|