larson.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <sys/time.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <unistd.h>
  7. #include <dmmlib/dmmlib.h>
  8. #include "lran2.h"
  9. #define MAX_THREADS 100
  10. #define MAX_BLOCKS 1000000
  11. #ifndef BOOLEAN
  12. #define BOOLEAN
  13. enum BOOLEAN { FALSE, TRUE };
  14. #endif /* BOOLEAN */
  15. typedef void * LPVOID;
  16. typedef unsigned long ULONG;
  17. typedef long long _int64;
  18. typedef void * VoidFunction (void *);
  19. typedef struct thr_data {
  20. int threadno;
  21. int NumBlocks;
  22. long seed;
  23. int min_size;
  24. int max_size;
  25. char **array;
  26. long *blksize;
  27. int asize;
  28. int cAllocs;
  29. int cFrees;
  30. int cThreads;
  31. int cBytesAlloced;
  32. volatile int finished;
  33. struct lran2_st rgen;
  34. } thread_data;
  35. int volatile stopflag = FALSE;
  36. int min_size = 10, max_size = 500;
  37. struct lran2_st rgen;
  38. char *blkp[MAX_BLOCKS];
  39. long blksize[MAX_BLOCKS];
  40. static void QueryPerformanceFrequency(long *x) {
  41. *x = 1000000L;
  42. }
  43. static void QueryPerformanceCounter (long *x) {
  44. struct timeval tv;
  45. gettimeofday(&tv, NULL);
  46. *x = tv.tv_sec * 1000000L + tv.tv_usec;
  47. }
  48. static void Sleep(long x) {
  49. // printf ("sleeping for %ld seconds.\n", x/1000);
  50. sleep((unsigned int) (x/1000));
  51. }
  52. static void _beginthread(VoidFunction x, void * z) {
  53. pthread_t pt;
  54. pthread_attr_t pa;
  55. pthread_attr_init (&pa);
  56. // printf ("creating a thread.\n");
  57. pthread_create(&pt, &pa, x, z);
  58. }
  59. static void warmup(char **blkp, int num_chunks) {
  60. int cblks;
  61. long victim;
  62. long blk_size;
  63. LPVOID tmp;
  64. for(cblks = 0; cblks < num_chunks; cblks++) {
  65. blk_size = min_size + lran2(&rgen) % (max_size - min_size);
  66. blkp[cblks] = (char *) custom_malloc((size_t) blk_size);
  67. blksize[cblks] = blk_size;
  68. assert(blkp[cblks] != NULL);
  69. }
  70. /* generate a random permutation of the chunks */
  71. for(cblks = num_chunks; cblks > 0 ; cblks--) {
  72. victim = lran2(&rgen) % cblks;
  73. tmp = blkp[victim];
  74. blkp[victim] = blkp[cblks-1];
  75. blkp[cblks-1] = (char *) tmp;
  76. }
  77. for(cblks=0; cblks < 4 * num_chunks; cblks++) {
  78. victim = lran2(&rgen) % num_chunks;
  79. custom_free(blkp[victim]);
  80. blk_size = min_size + lran2(&rgen) % (max_size - min_size);
  81. blkp[victim] = (char *) custom_malloc((size_t) blk_size);
  82. blksize[victim] = blk_size;
  83. assert(blkp[victim] != NULL);
  84. }
  85. }
  86. static void * exercise_heap( void *pinput) {
  87. thread_data *pdea;
  88. int cblks = 0;
  89. long victim;
  90. long blk_size;
  91. int range;
  92. if( stopflag ) return 0;
  93. pdea = (thread_data *) pinput;
  94. pdea->finished = FALSE;
  95. pdea->cThreads++;
  96. range = pdea->max_size - pdea->min_size;
  97. /* allocate NumBlocks chunks of random size */
  98. for(cblks=0; cblks < pdea->NumBlocks; cblks++) {
  99. victim = lran2(&pdea->rgen)%pdea->asize;
  100. custom_free(pdea->array[victim]);
  101. pdea->cFrees++;
  102. blk_size = pdea->min_size+lran2(&pdea->rgen)%range;
  103. pdea->array[victim] = (char *) custom_malloc((size_t) blk_size);
  104. pdea->blksize[victim] = blk_size;
  105. assert(pdea->array[victim] != NULL);
  106. pdea->cAllocs++;
  107. /* Write something! */
  108. volatile char * chptr = ((char *) pdea->array[victim]);
  109. *chptr++ = 'a';
  110. volatile char ch = *((char *) pdea->array[victim]);
  111. *chptr = 'b';
  112. if( stopflag ) break;
  113. }
  114. // printf("Thread %u terminating: %d allocs, %d frees\n",
  115. // pdea->threadno, pdea->cAllocs, pdea->cFrees) ;
  116. pdea->finished = TRUE;
  117. if( !stopflag ) {
  118. _beginthread(exercise_heap, pdea);
  119. }
  120. return 0;
  121. }
  122. static void runthreads(long sleep_cnt, int min_threads, int max_threads, int chperthread, int num_rounds) {
  123. thread_data de_area[MAX_THREADS];
  124. thread_data *pdea;
  125. long ticks_per_sec;
  126. int prevthreads;
  127. int num_threads;
  128. int nperthread;
  129. int sum_threads;
  130. int sum_allocs;
  131. int sum_frees;
  132. int i;
  133. long start_cnt, end_cnt;
  134. _int64 ticks;
  135. double duration ;
  136. double rate_1 = 0, rate_n;
  137. double reqd_space;
  138. ULONG used_space;
  139. QueryPerformanceFrequency( &ticks_per_sec );
  140. pdea = &de_area[0];
  141. memset(&de_area[0], 0, sizeof(thread_data));
  142. prevthreads = 0 ;
  143. for(num_threads=min_threads; num_threads <= max_threads; num_threads++) {
  144. warmup(&blkp[prevthreads*chperthread], (num_threads-prevthreads)*chperthread );
  145. nperthread = chperthread ;
  146. stopflag = FALSE ;
  147. for(i = 0; i < num_threads; i++) {
  148. de_area[i].threadno = i+1 ;
  149. de_area[i].NumBlocks = num_rounds*nperthread;
  150. de_area[i].array = &blkp[i*nperthread];
  151. de_area[i].blksize = &blksize[i*nperthread];
  152. de_area[i].asize = nperthread;
  153. de_area[i].min_size = min_size;
  154. de_area[i].max_size = max_size;
  155. de_area[i].seed = lran2(&rgen);
  156. de_area[i].finished = 0;
  157. de_area[i].cAllocs = 0;
  158. de_area[i].cFrees = 0;
  159. de_area[i].cThreads = 0;
  160. de_area[i].finished = FALSE;
  161. lran2_init(&de_area[i].rgen, de_area[i].seed);
  162. _beginthread(exercise_heap, &de_area[i]);
  163. }
  164. QueryPerformanceCounter( &start_cnt );
  165. printf ("Sleeping for %ld seconds.\n", sleep_cnt);
  166. Sleep(sleep_cnt * 1000L) ;
  167. stopflag = TRUE ;
  168. for(i = 0; i < num_threads; i++) {
  169. while( !de_area[i].finished ) {
  170. sched_yield();
  171. }
  172. }
  173. QueryPerformanceCounter( &end_cnt );
  174. sum_frees = sum_allocs =0 ;
  175. sum_threads = 0 ;
  176. for(i=0;i< num_threads; i++){
  177. sum_allocs += de_area[i].cAllocs ;
  178. sum_frees += de_area[i].cFrees ;
  179. sum_threads += de_area[i].cThreads ;
  180. de_area[i].cAllocs = de_area[i].cFrees = 0;
  181. }
  182. ticks = end_cnt - start_cnt ;
  183. duration = (double)(ticks/ticks_per_sec);
  184. for(i = 0; i < num_threads; i++) {
  185. if( !de_area[i].finished ) {
  186. printf("Thread at %d not finished\n", i);
  187. }
  188. }
  189. rate_n = sum_allocs/duration ;
  190. if( rate_1 == 0){
  191. rate_1 = rate_n ;
  192. }
  193. reqd_space = (0.5*(min_size+max_size)*num_threads*chperthread) ;
  194. // used_space = CountReservedSpace() - init_space;
  195. used_space = 0;
  196. printf("%2d ", num_threads ) ;
  197. printf("%6.3f", duration ) ;
  198. printf("%6.3f", rate_n/rate_1 ) ;
  199. printf("%8.0f", sum_allocs/duration ) ;
  200. printf(" %6.3f %.3f", (double)(used_space/(1024*1024)), (used_space/reqd_space));
  201. printf("\n") ;
  202. Sleep(5000L) ; // wait 5 sec for old threads to die
  203. prevthreads = num_threads;
  204. }
  205. }
  206. int main(void) {
  207. long sleep_cnt;
  208. int min_threads, max_threads;
  209. int num_chunks = 10000;
  210. int num_rounds;
  211. int chperthread;
  212. printf("Larson benchmark\n");
  213. printf("runtime (sec): ") ;
  214. //scanf ("%ld", &sleep_cnt);
  215. sleep_cnt = 10;
  216. printf("chunk size (min,max): ") ;
  217. //scanf("%d %d", &min_size, &max_size ) ;
  218. min_size = 1024;
  219. max_size = 5012;
  220. printf("threads (min, max): ") ;
  221. //scanf("%d %d", &min_threads, &max_threads) ;
  222. min_threads = 1;
  223. max_threads = 1;
  224. pthread_setconcurrency(max_threads);
  225. printf("chunks/thread: ");
  226. //scanf("%d", &chperthread );
  227. chperthread = 1;
  228. num_chunks = max_threads * chperthread ;
  229. if( num_chunks > MAX_BLOCKS ){
  230. printf("Max %d chunks - exiting\n", MAX_BLOCKS ) ;
  231. return 1;
  232. }
  233. printf("no of rounds: ");
  234. //scanf("%d", &num_rounds );
  235. num_rounds = 1;
  236. runthreads(sleep_cnt, min_threads, max_threads, chperthread, num_rounds) ;
  237. return 0;
  238. }