michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * File: priotest.c michael@0: * Purpose: testing priorities michael@0: */ michael@0: michael@0: #include "prcmon.h" michael@0: #include "prinit.h" michael@0: #include "prinrval.h" michael@0: #include "prlock.h" michael@0: #include "prlog.h" michael@0: #include "prmon.h" michael@0: #include "prprf.h" michael@0: #include "prthread.h" michael@0: #include "prtypes.h" michael@0: michael@0: #include "plerror.h" michael@0: #include "plgetopt.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #define DEFAULT_DURATION 5 michael@0: michael@0: static PRBool failed = PR_FALSE; michael@0: static PRIntervalTime oneSecond; michael@0: static PRFileDesc *debug_out = NULL; michael@0: static PRBool debug_mode = PR_FALSE; michael@0: michael@0: static PRUint32 PerSecond(PRIntervalTime timein) michael@0: { michael@0: PRUint32 loop = 0; michael@0: while (((PRIntervalTime)(PR_IntervalNow()) - timein) < oneSecond) michael@0: loop += 1; michael@0: return loop; michael@0: } /* PerSecond */ michael@0: michael@0: static void PR_CALLBACK Low(void *arg) michael@0: { michael@0: PRUint32 t3 = 0, t2 = 0, t1 = 0, t0, *tn = (PRUint32*)arg; michael@0: while (1) michael@0: { michael@0: t0 = PerSecond(PR_IntervalNow()); michael@0: *tn = (t3 + 3 * t2 + 3 * t1 + t0) / 8; michael@0: t3 = t2; t2 = t1; t1 = t0; michael@0: } michael@0: } /* Low */ michael@0: michael@0: static void PR_CALLBACK High(void *arg) michael@0: { michael@0: PRUint32 t3 = 0, t2 = 0, t1 = 0, t0, *tn = (PRUint32*)arg; michael@0: while (1) michael@0: { michael@0: PRIntervalTime timein = PR_IntervalNow(); michael@0: PR_Sleep(oneSecond >> 2); /* 0.25 seconds */ michael@0: t0 = PerSecond(timein); michael@0: *tn = (t3 + 3 * t2 + 3 * t1 + t0) / 8; michael@0: t3 = t2; t2 = t1; t1 = t0; michael@0: } michael@0: } /* High */ michael@0: michael@0: static void Help(void) michael@0: { michael@0: PR_fprintf( michael@0: debug_out, "Usage: priotest [-d] [-c n]\n"); michael@0: PR_fprintf( michael@0: debug_out, "-c n\tduration of test in seconds (default: %d)\n", DEFAULT_DURATION); michael@0: PR_fprintf( michael@0: debug_out, "-d\tturn on debugging output (default: FALSE)\n"); michael@0: } /* Help */ michael@0: michael@0: static void RudimentaryTests(void) michael@0: { michael@0: /* michael@0: ** Try some rudimentary tests like setting valid priority and michael@0: ** getting it back, or setting invalid priorities and getting michael@0: ** back a valid answer. michael@0: */ michael@0: PRThreadPriority priority; michael@0: PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_URGENT); michael@0: priority = PR_GetThreadPriority(PR_GetCurrentThread()); michael@0: failed = ((PR_TRUE == failed) || (PR_PRIORITY_URGENT != priority)) michael@0: ? PR_TRUE : PR_FALSE; michael@0: if (debug_mode && (PR_PRIORITY_URGENT != priority)) michael@0: { michael@0: PR_fprintf(debug_out, "PR_[S/G]etThreadPriority() failed\n"); michael@0: } michael@0: michael@0: michael@0: PR_SetThreadPriority( michael@0: PR_GetCurrentThread(), (PRThreadPriority)(PR_PRIORITY_FIRST - 1)); michael@0: priority = PR_GetThreadPriority(PR_GetCurrentThread()); michael@0: failed = ((PR_TRUE == failed) || (PR_PRIORITY_FIRST != priority)) michael@0: ? PR_TRUE : PR_FALSE; michael@0: if (debug_mode && (PR_PRIORITY_FIRST != priority)) michael@0: { michael@0: PR_fprintf(debug_out, "PR_SetThreadPriority(-1) failed\n"); michael@0: } michael@0: michael@0: PR_SetThreadPriority( michael@0: PR_GetCurrentThread(), (PRThreadPriority)(PR_PRIORITY_LAST + 1)); michael@0: priority = PR_GetThreadPriority(PR_GetCurrentThread()); michael@0: failed = ((PR_TRUE == failed) || (PR_PRIORITY_LAST != priority)) michael@0: ? PR_TRUE : PR_FALSE; michael@0: if (debug_mode && (PR_PRIORITY_LAST != priority)) michael@0: { michael@0: PR_fprintf(debug_out, "PR_SetThreadPriority(+1) failed\n"); michael@0: } michael@0: michael@0: } /* RudimentataryTests */ michael@0: michael@0: static void CreateThreads(PRUint32 *lowCount, PRUint32 *highCount) michael@0: { michael@0: (void)PR_CreateThread( michael@0: PR_USER_THREAD, Low, lowCount, PR_PRIORITY_LOW, michael@0: PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); michael@0: (void)PR_CreateThread( michael@0: PR_USER_THREAD, High, highCount, PR_PRIORITY_HIGH, michael@0: PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); michael@0: } /* CreateThreads */ michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PLOptStatus os; michael@0: PRIntn duration = DEFAULT_DURATION; michael@0: PRUint32 totalCount, highCount = 0, lowCount = 0; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "hdc:"); michael@0: michael@0: debug_out = PR_STDOUT; michael@0: oneSecond = PR_SecondsToInterval(1); michael@0: michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) michael@0: { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) michael@0: { michael@0: case 'd': /* debug mode */ michael@0: debug_mode = PR_TRUE; michael@0: break; michael@0: case 'c': /* test duration */ michael@0: duration = atoi(opt->value); michael@0: break; michael@0: case 'h': /* help message */ michael@0: default: michael@0: Help(); michael@0: return 2; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: PR_STDIO_INIT(); michael@0: michael@0: if (duration == 0) duration = DEFAULT_DURATION; michael@0: michael@0: RudimentaryTests(); michael@0: michael@0: printf("Priority test: running for %d seconds\n\n", duration); michael@0: michael@0: (void)PerSecond(PR_IntervalNow()); michael@0: totalCount = PerSecond(PR_IntervalNow()); michael@0: michael@0: PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_URGENT); michael@0: michael@0: if (debug_mode) michael@0: { michael@0: PR_fprintf(debug_out, michael@0: "The high priority thread should get approximately three\n"); michael@0: PR_fprintf( debug_out, michael@0: "times what the low priority thread manages. A maximum of \n"); michael@0: PR_fprintf( debug_out, "%d cycles are available.\n\n", totalCount); michael@0: } michael@0: michael@0: duration = (duration + 4) / 5; michael@0: CreateThreads(&lowCount, &highCount); michael@0: while (duration--) michael@0: { michael@0: PRIntn loop = 5; michael@0: while (loop--) PR_Sleep(oneSecond); michael@0: if (debug_mode) michael@0: PR_fprintf(debug_out, "high : low :: %d : %d\n", highCount, lowCount); michael@0: } michael@0: michael@0: michael@0: PR_ProcessExit((failed) ? 1 : 0); michael@0: michael@0: PR_ASSERT(!"You can't get here -- but you did!"); michael@0: return 1; /* or here */ michael@0: michael@0: } /* main */ michael@0: michael@0: /* priotest.c */