larson.c 8.1 KB

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