9fans archive / 1995 / 12 / 26 /    prev next

From: Scott Schwartz schwartz@gal...
Subject: more microbenchmarks, threads
Date: Sat, 9 Dec 1995 00:22:44 -0500

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....>
Message-ID:  <1995Dec6.125003.2562@pic...>, 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);
}