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: switch.c michael@0: ** Description: trying to time context switches michael@0: */ michael@0: michael@0: #include "prinit.h" michael@0: #include "prcvar.h" michael@0: #include "prmem.h" michael@0: #include "prinrval.h" michael@0: #include "prlock.h" michael@0: #include "prlog.h" michael@0: #include "prthread.h" michael@0: #include "prprf.h" michael@0: michael@0: #include "plerror.h" michael@0: #include "plgetopt.h" michael@0: michael@0: #include "private/pprio.h" michael@0: michael@0: #include michael@0: michael@0: #define INNER_LOOPS 100 michael@0: #define DEFAULT_LOOPS 100 michael@0: #define DEFAULT_THREADS 10 michael@0: michael@0: static PRFileDesc *debug_out = NULL; michael@0: static PRBool debug_mode = PR_FALSE, verbosity = PR_FALSE, failed = PR_FALSE; michael@0: michael@0: typedef struct Shared michael@0: { michael@0: PRLock *ml; michael@0: PRCondVar *cv; michael@0: PRBool twiddle; michael@0: PRThread *thread; michael@0: struct Shared *next; michael@0: } Shared; michael@0: michael@0: static void Help(void) michael@0: { michael@0: debug_out = PR_STDOUT; michael@0: michael@0: PR_fprintf( michael@0: debug_out, "Usage: >./switch [-c n] [-t n] [-d] [-v] [-G] [-C n]\n"); michael@0: PR_fprintf( michael@0: debug_out, "-c n\tloops at thread level (default: %d)\n", DEFAULT_LOOPS); michael@0: PR_fprintf( michael@0: debug_out, "-t n\tnumber of threads (default: %d)\n", DEFAULT_THREADS); michael@0: PR_fprintf(debug_out, "-d\tturn on debugging output (default: FALSE)\n"); michael@0: PR_fprintf(debug_out, "-v\tturn on verbose output (default: FALSE)\n"); michael@0: PR_fprintf(debug_out, "-G\tglobal threads only (default: FALSE)\n"); michael@0: PR_fprintf(debug_out, "-C n\tconcurrency setting (default: 1)\n"); michael@0: } /* Help */ michael@0: michael@0: static void PR_CALLBACK Notified(void *arg) michael@0: { michael@0: Shared *shared = (Shared*)arg; michael@0: PRStatus status = PR_SUCCESS; michael@0: while (PR_SUCCESS == status) michael@0: { michael@0: PR_Lock(shared->ml); michael@0: while (shared->twiddle && (PR_SUCCESS == status)) michael@0: status = PR_WaitCondVar(shared->cv, PR_INTERVAL_NO_TIMEOUT); michael@0: if (verbosity) PR_fprintf(debug_out, "+"); michael@0: shared->twiddle = PR_TRUE; michael@0: shared->next->twiddle = PR_FALSE; michael@0: PR_NotifyCondVar(shared->next->cv); michael@0: PR_Unlock(shared->ml); michael@0: } michael@0: } /* Notified */ michael@0: michael@0: static Shared home; michael@0: PRIntn PR_CALLBACK Switch(PRIntn argc, char **argv) michael@0: { michael@0: PLOptStatus os; michael@0: PRStatus status; michael@0: PRBool help = PR_FALSE; michael@0: PRUintn concurrency = 1; michael@0: Shared *shared, *link; michael@0: PRIntervalTime timein, timeout; michael@0: PRThreadScope thread_scope = PR_LOCAL_THREAD; michael@0: PRUintn thread_count, inner_count, loop_count, average; michael@0: PRUintn thread_limit = DEFAULT_THREADS, loop_limit = DEFAULT_LOOPS; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "hdvc:t:C:G"); 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 'v': /* verbose mode */ michael@0: verbosity = PR_TRUE; michael@0: case 'd': /* debug mode */ michael@0: debug_mode = PR_TRUE; michael@0: break; michael@0: case 'c': /* loop counter */ michael@0: loop_limit = atoi(opt->value); michael@0: break; michael@0: case 't': /* thread limit */ michael@0: thread_limit = atoi(opt->value); michael@0: break; michael@0: case 'C': /* Concurrency limit */ michael@0: concurrency = atoi(opt->value); michael@0: break; michael@0: case 'G': /* global threads only */ michael@0: thread_scope = PR_GLOBAL_THREAD; michael@0: break; michael@0: case 'h': /* help message */ michael@0: Help(); michael@0: help = PR_TRUE; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: if (help) return -1; michael@0: michael@0: if (PR_TRUE == debug_mode) michael@0: { michael@0: debug_out = PR_STDOUT; michael@0: PR_fprintf(debug_out, "Test parameters\n"); michael@0: PR_fprintf(debug_out, "\tThreads involved: %d\n", thread_limit); michael@0: PR_fprintf(debug_out, "\tIteration limit: %d\n", loop_limit); michael@0: PR_fprintf(debug_out, "\tConcurrency: %d\n", concurrency); michael@0: PR_fprintf( michael@0: debug_out, "\tThread type: %s\n", michael@0: (PR_GLOBAL_THREAD == thread_scope) ? "GLOBAL" : "LOCAL"); michael@0: } michael@0: michael@0: PR_SetConcurrency(concurrency); michael@0: michael@0: link = &home; michael@0: home.ml = PR_NewLock(); michael@0: home.cv = PR_NewCondVar(home.ml); michael@0: home.twiddle = PR_FALSE; michael@0: home.next = NULL; michael@0: michael@0: timeout = 0; michael@0: michael@0: for (thread_count = 1; thread_count <= thread_limit; ++thread_count) michael@0: { michael@0: shared = PR_NEWZAP(Shared); michael@0: michael@0: shared->ml = home.ml; michael@0: shared->cv = PR_NewCondVar(home.ml); michael@0: shared->twiddle = PR_TRUE; michael@0: shared->next = link; michael@0: link = shared; michael@0: michael@0: shared->thread = PR_CreateThread( michael@0: PR_USER_THREAD, Notified, shared, michael@0: PR_PRIORITY_HIGH, thread_scope, michael@0: PR_JOINABLE_THREAD, 0); michael@0: PR_ASSERT(shared->thread != NULL); michael@0: if (NULL == shared->thread) michael@0: failed = PR_TRUE; michael@0: } michael@0: michael@0: for (loop_count = 1; loop_count <= loop_limit; ++loop_count) michael@0: { michael@0: timein = PR_IntervalNow(); michael@0: for (inner_count = 0; inner_count < INNER_LOOPS; ++inner_count) michael@0: { michael@0: PR_Lock(home.ml); michael@0: home.twiddle = PR_TRUE; michael@0: shared->twiddle = PR_FALSE; michael@0: PR_NotifyCondVar(shared->cv); michael@0: while (home.twiddle) michael@0: { michael@0: status = PR_WaitCondVar(home.cv, PR_INTERVAL_NO_TIMEOUT); michael@0: if (PR_FAILURE == status) michael@0: failed = PR_TRUE; michael@0: } michael@0: PR_Unlock(home.ml); michael@0: } michael@0: timeout += (PR_IntervalNow() - timein); michael@0: } michael@0: michael@0: if (debug_mode) michael@0: { michael@0: average = PR_IntervalToMicroseconds(timeout) michael@0: / (INNER_LOOPS * loop_limit * thread_count); michael@0: PR_fprintf( michael@0: debug_out, "Average switch times %d usecs for %d threads\n", michael@0: average, thread_limit); michael@0: } michael@0: michael@0: link = shared; michael@0: for (thread_count = 1; thread_count <= thread_limit; ++thread_count) michael@0: { michael@0: if (&home == link) break; michael@0: status = PR_Interrupt(link->thread); michael@0: if (PR_SUCCESS != status) michael@0: { michael@0: failed = PR_TRUE; michael@0: if (debug_mode) michael@0: PL_FPrintError(debug_out, "Failed to interrupt"); michael@0: } michael@0: link = link->next; michael@0: } michael@0: michael@0: for (thread_count = 1; thread_count <= thread_limit; ++thread_count) michael@0: { michael@0: link = shared->next; michael@0: status = PR_JoinThread(shared->thread); michael@0: if (PR_SUCCESS != status) michael@0: { michael@0: failed = PR_TRUE; michael@0: if (debug_mode) michael@0: PL_FPrintError(debug_out, "Failed to join"); michael@0: } michael@0: PR_DestroyCondVar(shared->cv); michael@0: PR_DELETE(shared); michael@0: if (&home == link) break; michael@0: shared = link; michael@0: } michael@0: PR_DestroyCondVar(home.cv); michael@0: PR_DestroyLock(home.ml); michael@0: michael@0: PR_fprintf(PR_STDOUT, ((failed) ? "FAILED\n" : "PASSED\n")); michael@0: return ((failed) ? 1 : 0); michael@0: } /* Switch */ michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRIntn result; michael@0: PR_STDIO_INIT(); michael@0: result = PR_Initialize(Switch, argc, argv, 0); michael@0: return result; michael@0: } /* main */ michael@0: michael@0: /* switch.c */