this snippet is for checking fairness of context switching in your environment.
the snippet does not runs two threads directly, but it switches two threads explicitly, by using a monitor (a mutex and a condition variable).
Some programming languges take such indirect concurrent execution mechanism for native threads. Programming languges which support \"native threads\" but does not support conccurent GC must stop the whole process in order to collect garbages safely. Since native threads generally are not capable of disabling preemption, the runtime must pause all threads except for one that runs GC.
compile with:<br/>gcc -lpthread filename.c
there is a `global lock\' and two threads. each thread runs work() in the same way, only except its parameter. each of two threads increments the counter hit_count[i] where i is the id of the thread.
after 1 second, the program outputs statistics and terminates.
SIGALRM. thread0: 1899, thread1: 2333, switch:20
if your pthread library is fair, the first two numbers are nearly equal to each other, and the third number is 20 or so.
unfortunately, my Mac OS X (10.5.8) has turned to be unfair:
SIGALRM. thread0: 4086, thread1: 215, switch:2
on the contrary, linux (debian 5.0, kenel 2.6.26) has shown to be fair. please post your results in the comments.
The linux kernel 2.6 (>= 2.6.23) is likely to be fair, because it is equipped with the Completely Fair Scheduler. This scheduler is aware of \'sleeper fairness\'.
see also:http://snipplr.com/view/19509/ if your pthread is shown to be \'unfair\', this O\'Caml snippet will also stops for a while.
UPDATE: added volatile to the shared variables.
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <unistd.h> #include <pthread.h> #include <signal.h> #include <sys/time.h> #define TIMEOUT 50000 // context switch rate, in microseconds #define WAIT_COUNT 0x10000 // busy wait count #define EXIT_AFTER 1 // exit after 1 sec // monitor pthread_mutex_t mutex; pthread_cond_t cond; volatile int locked; // the `world' is locked or not volatile int waiters; // number of waiting threads volatile int signal_has_come; // 1 if context switch signal has come // thread that fire periodic signal for context switch void* thread_tick(void* arg) { while(1) { // fprintf(stdout, "tick\n"); fflush(stdout); signal_has_come = 1; struct timespec to; to.tv_sec = 0; to.tv_nsec = TIMEOUT * 1000; nanosleep(&to, 0); } } // release the global lock void unlock_world() { pthread_mutex_lock(&mutex); locked = 0; pthread_mutex_unlock(&mutex); // fprintf(stdout, "released global lock (%d)\n", (int)pthread_self()); fflush(stdout); pthread_cond_signal(&cond); } // obtain the global lock void lock_world() { pthread_mutex_lock(&mutex); while(locked) { // fprintf(stdout, "locked. sleeping.. (%d)\n", (int)pthread_self()); fflush(stdout); waiters++; pthread_cond_wait(&cond, &mutex); waiters--; } locked = 1; pthread_mutex_unlock(&mutex); // fprintf(stdout,"got global lock. restart. (%d)\n", (int)pthread_self()); } // yield to switch to the other thread void yield() { if(0==waiters) return; unlock_world(); sched_yield(); lock_world(); } void check_signal_and_yield() { if(signal_has_come) { signal_has_come=0; yield(); } } int hit_count[2]; // count how many times the loop is executed, for each thread int prev; int switch_count; void work(int i) { while(1) { // count hit_count[i]++; if(i!=prev) { prev=i; switch_count++;} // yield check_signal_and_yield(); // fprintf(stdout,"working. %d\n", (int)pthread_self()); // busy waiting int i; for(i=0; i<WAIT_COUNT; i++); } } void* thread_start(void* arg) { // fprintf(stdout, "new thread start. %d\n", (int)pthread_self()); fflush(stdout); lock_world(); work(1); return 0; } // exit after specified second. void interrupt_handler(int sig) { fprintf(stdout, "SIGALRM. thread0: %d, thread1: %d, switch:%d\n", hit_count[0], hit_count[1], switch_count); fflush(stdout); exit(0); } // set up the stop watch void set_stop_alarm() { void(*oldact)(int); oldact = signal(SIGALRM, interrupt_handler); assert(SIG_ERR!=oldact); struct itimerval itv; itv.it_value.tv_sec = EXIT_AFTER; itv.it_value.tv_usec = 0; itv.it_interval = itv.it_value; setitimer(ITIMER_REAL, &itv, 0); } int main(int argc, char** argv) { // set up the stop watch set_stop_alarm(); // set up mutex and condition variable for monitor int r = pthread_mutex_init(&mutex, 0); assert(0==r); r = pthread_cond_init(&cond, 0); assert(0==r); // start the sub thread pthread_t th; pthread_create(&th, 0, thread_start, 0); // start the tick thread pthread_t th2; pthread_create(&th2, 0, thread_tick, 0); // start the main thread lock_world(); work(0); return 0; }
Comments
Subscribe to comments
You need to login to post a comment.

Surprise, surprise: I just tested my Linux box and obtained
as the most unfair of several runs with the unchanged program. Setting EXIT_AFTER to 10 gives
as the worst result. With EXIT_AFTER=100 the worst case is:
As one can see, the results become fairer as the test time and the number of task switches increases.
Probable reason: My kernel was compiled with the option
CONFIG_HZ_250=y. This means the interrupt timer frequency on which the scheduler depends is set to 250 Hz. The kernel config help calls this the compromise between 100Hz (for servers, few interrupts that would reduce overall performance) and 1000Hz (for desktops, fastest response to user events). A low timer frequency gives infrequent task switches and therefore little "fairness". Stock distribution kernels tend to be built with 1000Hz timer frequency, hence the fair results you saw. Look at/boot/config-to check.Thanks for the program! I will test some other Linux boxes I have access to which run distribution kernels and post the results.
That should have been: Look at
/boot/config-to ckeck.Another try: Look at
/boot/config-<your kernel version> to check. Sorry this took so long.Thanks for comments, deepsoul!
Actually, this code simulates context switching in O'Caml programming language (native code, compiled with ocamlopt). I suppose other scripting languages like Ruby and Python might take similar scheduling mechanism. Sorry for incomplete explanation. I will revise it.
After googlin' some words like
fairness pthread schedulingetc, I found this wikipedia page:Completely Fair Scheduler
according to this, the kernel which version is greater or equal to 2.6.23, which is supposed to be equipped with CFS, will provide CPU time to each threads fairly, even if a thread waits for long time because of locked mutex.
I suppose that the reason of unfairness in other OSs is that the `fairness' of their scheduling algorithm does not take into account sleeping threads.
I found CONFIG_HZ in my linux (kernel 2.6.26, which is "fair"):
hence I suppose that CONFIGHZ parameter is irrelevant for fairness in this case.Oh well, there goes that theory. It would have been crass if a factor of 4 in the timer frequency had caused a factor of 100 in the timescale at which scheduling becomes fair, so this puts the world right in a way.
But I have in the mean time tested three other linux systems. All (including the one above) run kernels with versions 2.6.23 or later, so use the CFS scheduler. Except for one, they gave the same results as the first machine above - fairness only at minute timescales. This one is distinguished by being a single-core CPU. The others are dual-core or (one of them) an Intel Atom which is treated as dual-core due to hyperthreading. This is the cause of the discrepancy: When I restrict the program to run on one core only (with the command
taskset 0x1), the scheduling is perfectly fair on all machines at the default test time of one second! Your program seemed to run almost exclusively on one CPU anyway, but I saw this only visually from xosview, so it may simply give inaccurate results on multi-cores.Finally, I tested a windoze box (compiled and run via Cygwin). It behaved not quite as fairly as the single-core linux results, but decently. A typical result: