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.cpp michael@0: ** Description: trying to time context switches michael@0: */ michael@0: michael@0: #include "rccv.h" michael@0: #include "rcinrval.h" michael@0: #include "rclock.h" michael@0: #include "rcthread.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include 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: class Home: public RCCondition michael@0: { michael@0: public: michael@0: virtual ~Home(); michael@0: Home(Home *link, RCLock* ml); michael@0: michael@0: public: michael@0: Home *next; michael@0: RCLock* ml; michael@0: PRBool twiddle; michael@0: }; /* Home */ michael@0: michael@0: Home::~Home() { } michael@0: michael@0: Home::Home(Home *link, RCLock* lock): RCCondition(lock) michael@0: { michael@0: ml = lock; michael@0: next = link; michael@0: twiddle = PR_FALSE; michael@0: } /* Home::Home */ michael@0: michael@0: class Shared: public Home, public RCThread michael@0: { michael@0: public: michael@0: Shared(RCThread::Scope scope, Home* link, RCLock* ml); michael@0: michael@0: private: michael@0: ~Shared(); michael@0: void RootFunction(); michael@0: }; /* Shared */ michael@0: michael@0: Shared::Shared(RCThread::Scope scope, Home* link, RCLock* lock): michael@0: Home(link, lock), RCThread(scope, RCThread::joinable) { } michael@0: michael@0: Shared::~Shared() { } michael@0: michael@0: void Shared::RootFunction() michael@0: { michael@0: PRStatus status = PR_SUCCESS; michael@0: while (PR_SUCCESS == status) michael@0: { michael@0: RCEnter entry(ml); michael@0: while (twiddle && (PR_SUCCESS == status)) status = Wait(); michael@0: if (verbosity) PR_fprintf(debug_out, "+"); michael@0: twiddle = PR_TRUE; michael@0: next->twiddle = PR_FALSE; michael@0: next->Notify(); michael@0: } michael@0: } /* Shared::RootFunction */ 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 [-d] [-c n] [-t n] [-T n] [-G]\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 n\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: PRIntn main(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: RCThread::Scope thread_scope = RCThread::local; 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 = RCThread::global; 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: /* michael@0: ** The interesting part starts here michael@0: */ michael@0: RCLock lock; michael@0: Shared* shared; michael@0: Home home(NULL, &lock); michael@0: Home* link = &home; michael@0: RCInterval timein, timeout = 0; michael@0: michael@0: /* Build up the string of objects */ michael@0: for (thread_count = 1; thread_count <= thread_limit; ++thread_count) michael@0: { michael@0: shared = new Shared(thread_scope, link, &lock); michael@0: shared->Start(); /* make it run */ michael@0: link = (Home*)shared; michael@0: } michael@0: michael@0: /* Pass the message around the horn a few times */ michael@0: for (loop_count = 1; loop_count <= loop_limit; ++loop_count) michael@0: { michael@0: timein.SetToNow(); michael@0: for (inner_count = 0; inner_count < INNER_LOOPS; ++inner_count) michael@0: { michael@0: RCEnter entry(&lock); michael@0: home.twiddle = PR_TRUE; michael@0: shared->twiddle = PR_FALSE; michael@0: shared->Notify(); michael@0: while (home.twiddle) michael@0: { michael@0: failed = (PR_FAILURE == home.Wait()) ? PR_TRUE : PR_FALSE; michael@0: } michael@0: } michael@0: timeout += (RCInterval(RCInterval::now) - timein); michael@0: } michael@0: michael@0: /* Figure out how well we did */ michael@0: if (debug_mode) michael@0: { michael@0: average = timeout.ToMicroseconds() 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: /* Start reclamation process */ 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 = ((Shared*)link)->Interrupt(); 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 = shared->Join(); 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: if (&home == link) break; michael@0: shared = (Shared*)link; michael@0: } michael@0: michael@0: PR_fprintf(PR_STDOUT, ((failed) ? "FAILED\n" : "PASSED\n")); michael@0: michael@0: failed |= (PR_SUCCESS == RCPrimordialThread::Cleanup()); michael@0: michael@0: return ((failed) ? 1 : 0); michael@0: } /* main */ michael@0: michael@0: /* switch.c */