Received: from PACIFIC-CARRIER-ANNEX.MIT.EDU by po7.MIT.EDU (5.61/4.7) id AA00513; Sat, 9 Dec 95 00:58:25 EST
Received: from colossus.cse.psu.edu by MIT.EDU with SMTP
	id AA19850; Sat, 9 Dec 95 00:58:27 EST
Received: by colossus.cse.psu.edu id <79011>; Sat, 9 Dec 1995 00:48:30 -0500
Received: from galapagos.cse.psu.edu ([130.203.2.12]) by colossus.cse.psu.edu with SMTP id <79005>; Sat, 9 Dec 1995 00:38:18 -0500
Received: from localhost by galapagos.cse.psu.edu with SMTP id <12688>; Sat, 9 Dec 1995 00:22:57 -0500
To: 9fans@cse.psu.edu
Subject: more microbenchmarks, threads
Date: 	Sat, 9 Dec 1995 00:22:44 -0500
From: Scott Schwartz <schwartz@galapagos.cse.psu.edu>
Message-Id: <95Dec9.002257est.12688@galapagos.cse.psu.edu>
Sender: owner-9fans@cse.psu.edu
Precedence: bulk
Reply-To: 9fans@cse.psu.edu

Over on comp.programming.threads there's been some discussion of thread
performance under solaris based on some small programs that exercise
thread creation.  (See Steve Rogers' <steve@ct.picker.com>
Message-ID:  <1995Dec6.125003.2562@picker.com>, and subsequent.)

Inspired to comparare, I ran one of those programs on a Sparcstation 2
with Solaris.  Then I translated it---accurately I hope, although
rendezvous() isn't quite the same as a semaphore---for Plan 9, and ran
it on another SS2.  The results were about the same, except that under
Plan 9 real time was much greater than user+system.  Is the time
getting lost during scheduling, or something like that?

term% time k.out
COUNTER: 10000
0.20u 9.78s 43.26r 	 k.out

#include <u.h>
#include <libc.h>
#include <lock.h>

#define ITERATIONS 1000
#define MAX_THREAD 10

static Lock mutex;
static int counter = 0;

void thread_func(void)
{
    lock(&mutex);
    counter++;
    if ((counter % MAX_THREAD) == 0) {
        rendezvous(0, 1); 
    }
    unlock(&mutex);
}

int thr_create (void (*func)(void))
{
	int pid = rfork(RFPROC|RFMEM|RFNOWAIT);
	switch (pid) {
	case -1:
		fprint(2, "rfork failed: %r\n");
		exits("rfork");
	case 0:
		(*func)();
		exits(0);
	default:		
		return pid;
	}
}

void main(int argc, char *argv[])
{
	int ii, jj;
	int pid;

	counter = 0;
	lockinit();
	for ( ii = 0; ii < ITERATIONS; ii++ ) {
		for ( jj = 0; jj < MAX_THREAD; jj++ ) {
			pid = thr_create(thread_func);
		}
		rendezvous(0, 0);
	}
	fprint(2, "COUNTER: %d\n", counter);
	exits(0);
}


