Regarding KSE

TeJun Huh tj at atj.dyndns.org
Thu Jul 31 19:31:41 PDT 2003


  Hello, I'm currently evaluating various thread packages for a paper.
The focus is on aptness for Internet servers, so performance and
scalability are main concerns.  I'm trying to evaluate M:N threading
(scheduler activaton) and 1:1 threading using KSE and maybe
traditional libc_r on FreeBSD 5.1.

  I've encountered several problems with KSE.  I made a small test
case program to demonstrate the problem.  The program accepts one
argument telling it how many threads should be created.  The main
thread creates given number of threads with user allocated stack
(pthread_attr_setstack()).  After createing all threads the main
thread waits forever.  Each created thread reads fd 0 (stdin).  After
reading some input from stdin, it prints the input and exits.  The
source of test program is appended at the tail of this mail.

  First problem is that I can hang whole operating system (even
ctrl-alt-sec doesn't work).  Creating 160 threads and pressing ctrl-c
after all threads are created hang the kernel reliably.

  Secondly, I cannot get more than 150 threads active at once.  I
guess this is the limit of KSEs per process.  Is there any way to
increase this number?  I'm looking at ten thousands threads at least.

  Thanks in advance.

/*****************************************************************/
/***************** test program source follows *******************/
/* compile with '$ gcc -D_THREAD_SAFE -o test test.c -lkse' ******/
/*****************************************************************/

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

static void * service_startup(void *arg)
{
	char buf[64];
	int ret;
	printf("service startup %d\n", (int)arg);
	if ((ret = read(0, buf, sizeof(buf)-1)) < 0)
		printf("read failed %d\n", errno);
	else {
		buf[ret] = '\0';
		printf("read: \"%s\"\n", buf);
	}
	printf("exiting\n");
	return NULL;
}

#define STACK_SIZE 32768

int main(int argc, char **argv)
{
	static const struct timespec ts1sec = { 1, 0 };
	int nr, i;

	if (argc < 2) {
		fprintf(stderr, "babo\n");
		return 1;
	}

	nr = atoi(argv[1]);

	for (i = 0; i < nr; i++) {
		void *stack;
		pthread_t thr;
		pthread_attr_t attr;

		if ((stack = malloc(STACK_SIZE)) == NULL) {
			fprintf(stderr, "malloc failed\n");
			return 1;
		}

		pthread_attr_init(&attr);
		pthread_attr_setdetachstate(&attr, 1);
		pthread_attr_setstack(&attr, stack, STACK_SIZE);

		if (pthread_create(&thr, &attr, service_startup, (void *)i) < 0) {
			fprintf(stderr, "pthread_create failed\n");
			return 1;
		}
	}

	while (1)
		nanosleep(&ts1sec, NULL);

	return 0;
}



More information about the freebsd-threads mailing list