larson.c 7.8 KB

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