Posted By


keigoi on 09/13/09

Tagged


Statistics


Viewed 412 times
Favorited by 1 user(s)

Related snippets


pthread sleeper fairness


/ Published in: C
Save to your folder(s)

<p>this snippet is for checking fairness of context switching in your environment.</p><p>the snippet does not runs two threads directly, but it switches two threads explicitly, by using a monitor (a mutex and a condition variable).</p><p>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. </p><p>compile with:<code>gcc -lpthread filename.c</code></p><p>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.</p><p>after 1 second, the program outputs statistics and terminates.</p><p><code>SIGALRM. thread0: 1899, thread1: 2333, switch:20</code></p><p>if your pthread library is fair, the first two numbers are nearly equal to each other, and the third number is 20 or so.</p><p>unfortunately, my Mac OS X (10.5.8) has turned to be unfair:</p><p><code>SIGALRM. thread0: 4086, thread1: 215, switch:2</code></p><p>on the contrary, linux (debian 5.0, kenel 2.6.26) has shown to be fair. please post your results in the comments.</p><p>The linux kernel 2.6 (>= 2.6.23) is likely to be fair, because it is equipped with the <a href=\"http://en.wikipedia.org/wiki/Completely_Fair_Scheduler\">Completely Fair Scheduler</a>. This scheduler is aware of \'sleeper fairness\'.</p><p><strong>see also:</strong><a href=\"http://snipplr.com/view/19509/\">http://snipplr.com/view/19509/</a> if your pthread is shown to be \'unfair\', this O\'Caml snippet will also stops for a while.</p><p><strong>UPDATE: </strong> added <code>volatile</code> to the shared variables.</p>


Copy this code and paste it in your HTML
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <unistd.h>
  5. #include <pthread.h>
  6. #include <signal.h>
  7. #include <sys/time.h>
  8.  
  9. #define TIMEOUT 50000 // context switch rate, in microseconds
  10. #define WAIT_COUNT 0x10000 // busy wait count
  11. #define EXIT_AFTER 1 // exit after 1 sec
  12.  
  13.  
  14. // monitor
  15. pthread_mutex_t mutex;
  16. pthread_cond_t cond;
  17.  
  18. volatile int locked; // the `world' is locked or not
  19. volatile int waiters; // number of waiting threads
  20. volatile int signal_has_come; // 1 if context switch signal has come
  21.  
  22. // thread that fire periodic signal for context switch
  23. void* thread_tick(void* arg) {
  24. while(1) {
  25. // fprintf(stdout, "tick\n"); fflush(stdout);
  26. signal_has_come = 1;
  27. struct timespec to;
  28. to.tv_sec = 0;
  29. to.tv_nsec = TIMEOUT * 1000;
  30. nanosleep(&to, 0);
  31. }
  32. }
  33.  
  34. // release the global lock
  35. void unlock_world() {
  36. pthread_mutex_lock(&mutex);
  37. locked = 0;
  38. pthread_mutex_unlock(&mutex);
  39. // fprintf(stdout, "released global lock (%d)\n", (int)pthread_self()); fflush(stdout);
  40. pthread_cond_signal(&cond);
  41. }
  42.  
  43. // obtain the global lock
  44. void lock_world() {
  45. pthread_mutex_lock(&mutex);
  46. while(locked) {
  47. // fprintf(stdout, "locked. sleeping.. (%d)\n", (int)pthread_self()); fflush(stdout);
  48. waiters++;
  49. pthread_cond_wait(&cond, &mutex);
  50. waiters--;
  51. }
  52. locked = 1;
  53. pthread_mutex_unlock(&mutex);
  54. // fprintf(stdout,"got global lock. restart. (%d)\n", (int)pthread_self());
  55. }
  56.  
  57. // yield to switch to the other thread
  58. void yield() {
  59. if(0==waiters) return;
  60. unlock_world();
  61. sched_yield();
  62. lock_world();
  63. }
  64.  
  65. void check_signal_and_yield() {
  66. if(signal_has_come) {
  67. signal_has_come=0;
  68. yield();
  69. }
  70. }
  71.  
  72. int hit_count[2]; // count how many times the loop is executed, for each thread
  73. int prev;
  74. int switch_count;
  75.  
  76. void work(int i) {
  77. while(1) {
  78.  
  79. // count
  80. hit_count[i]++;
  81. if(i!=prev) { prev=i; switch_count++;}
  82.  
  83. // yield
  84. check_signal_and_yield();
  85.  
  86. // fprintf(stdout,"working. %d\n", (int)pthread_self());
  87.  
  88. // busy waiting
  89. int i;
  90. for(i=0; i<WAIT_COUNT; i++);
  91. }
  92. }
  93.  
  94.  
  95. void* thread_start(void* arg) {
  96. // fprintf(stdout, "new thread start. %d\n", (int)pthread_self()); fflush(stdout);
  97. lock_world();
  98. work(1);
  99. return 0;
  100. }
  101.  
  102.  
  103. // exit after specified second.
  104. void interrupt_handler(int sig) {
  105. fprintf(stdout, "SIGALRM. thread0: %d, thread1: %d, switch:%d\n", hit_count[0], hit_count[1], switch_count); fflush(stdout);
  106. exit(0);
  107. }
  108.  
  109. // set up the stop watch
  110. void set_stop_alarm() {
  111. void(*oldact)(int);
  112. oldact = signal(SIGALRM, interrupt_handler);
  113. assert(SIG_ERR!=oldact);
  114. struct itimerval itv;
  115. itv.it_value.tv_sec = EXIT_AFTER;
  116. itv.it_value.tv_usec = 0;
  117. itv.it_interval = itv.it_value;
  118. setitimer(ITIMER_REAL, &itv, 0);
  119. }
  120.  
  121. int main(int argc, char** argv) {
  122.  
  123. // set up the stop watch
  124. set_stop_alarm();
  125.  
  126. // set up mutex and condition variable for monitor
  127. int r = pthread_mutex_init(&mutex, 0);
  128. assert(0==r);
  129. r = pthread_cond_init(&cond, 0);
  130. assert(0==r);
  131.  
  132. // start the sub thread
  133. pthread_t th;
  134. pthread_create(&th, 0, thread_start, 0);
  135.  
  136. // start the tick thread
  137. pthread_t th2;
  138. pthread_create(&th2, 0, thread_tick, 0);
  139.  
  140. // start the main thread
  141. lock_world();
  142. work(0);
  143. return 0;
  144. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.