nsprpub/pr/tests/priotest.c

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

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

mercurial