larson.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. printf("trololo1\n");
  80. /* generate a random permutation of the chunks */
  81. for(cblks = num_chunks; cblks > 0 ; cblks--) {
  82. victim = lran2(&rgen) % cblks;
  83. tmp = blkp[victim];
  84. blkp[victim] = blkp[cblks-1];
  85. blkp[cblks-1] = (char *) tmp;
  86. }
  87. for(cblks=0; cblks < 4 * num_chunks; cblks++) {
  88. victim = lran2(&rgen) % num_chunks;
  89. custom_free(myheap, blkp[victim]);
  90. printf("trololo2\n");
  91. blk_size = min_size + lran2(&rgen) % (max_size - min_size);
  92. blkp[victim] = (char *) custom_malloc(myheap, (size_t) blk_size);
  93. blksize[victim] = blk_size;
  94. assert(blkp[victim] != NULL);
  95. printf("trololo3\n");
  96. }
  97. }
  98. static void * exercise_heap( void *pinput) {
  99. thread_data *pdea;
  100. int cblks = 0;
  101. int victim;
  102. long blk_size;
  103. int range;
  104. heap_t *myheap;
  105. int heap_id;
  106. heap_id = map_thread_heap();
  107. myheap = &myallocator->heaps[heap_id];
  108. if( stopflag ) return 0;
  109. pdea = (thread_data *) pinput;
  110. pdea->finished = FALSE;
  111. pdea->cThreads++;
  112. range = pdea->max_size - pdea->min_size;
  113. /* allocate NumBlocks chunks of random size */
  114. for(cblks=0; cblks < pdea->NumBlocks; cblks++) {
  115. victim = lran2(&pdea->rgen)%pdea->asize;
  116. custom_free(myheap, pdea->array[victim]);
  117. pdea->cFrees++;
  118. blk_size = pdea->min_size+lran2(&pdea->rgen)%range;
  119. pdea->array[victim] = (char *) custom_malloc(myheap, (size_t) blk_size);
  120. pdea->blksize[victim] = blk_size;
  121. assert(pdea->array[victim] != NULL);
  122. pdea->cAllocs++;
  123. /* Write something! */
  124. volatile char * chptr = ((char *) pdea->array[victim]);
  125. *chptr++ = 'a';
  126. volatile char ch = *((char *) pdea->array[victim]);
  127. *chptr = 'b';
  128. if( stopflag ) break;
  129. }
  130. // printf("Thread %u terminating: %d allocs, %d frees\n",
  131. // pdea->threadno, pdea->cAllocs, pdea->cFrees) ;
  132. pdea->finished = TRUE;
  133. if( !stopflag ) {
  134. _beginthread(exercise_heap, pdea);
  135. }
  136. return 0;
  137. }
  138. static void runthreads(long sleep_cnt, int min_threads, int max_threads, int chperthread, int num_rounds) {
  139. thread_data de_area[MAX_THREADS];
  140. thread_data *pdea;
  141. long ticks_per_sec;
  142. int prevthreads;
  143. int num_threads;
  144. int nperthread;
  145. int sum_threads;
  146. int sum_allocs;
  147. int sum_frees;
  148. int i;
  149. long start_cnt, end_cnt;
  150. _int64 ticks;
  151. double duration ;
  152. double rate_1 = 0, rate_n;
  153. double reqd_space;
  154. ULONG used_space;
  155. QueryPerformanceFrequency( &ticks_per_sec );
  156. pdea = &de_area[0];
  157. memset(&de_area[0], 0, sizeof(thread_data));
  158. prevthreads = 0 ;
  159. for(num_threads=min_threads; num_threads <= max_threads; num_threads++) {
  160. warmup(&blkp[prevthreads*chperthread], (num_threads-prevthreads)*chperthread );
  161. nperthread = chperthread ;
  162. stopflag = FALSE ;
  163. for(i = 0; i < num_threads; i++) {
  164. de_area[i].threadno = i+1 ;
  165. de_area[i].NumBlocks = num_rounds*nperthread;
  166. de_area[i].array = &blkp[i*nperthread];
  167. de_area[i].blksize = &blksize[i*nperthread];
  168. de_area[i].asize = nperthread;
  169. de_area[i].min_size = min_size;
  170. de_area[i].max_size = max_size;
  171. de_area[i].seed = lran2(&rgen);
  172. de_area[i].finished = 0;
  173. de_area[i].cAllocs = 0;
  174. de_area[i].cFrees = 0;
  175. de_area[i].cThreads = 0;
  176. de_area[i].finished = FALSE;
  177. lran2_init(&de_area[i].rgen, de_area[i].seed);
  178. _beginthread(exercise_heap, &de_area[i]);
  179. }
  180. QueryPerformanceCounter( &start_cnt );
  181. printf ("Sleeping for %ld seconds.\n", sleep_cnt);
  182. Sleep(sleep_cnt * 1000L) ;
  183. stopflag = TRUE ;
  184. for(i = 0; i < num_threads; i++) {
  185. while( !de_area[i].finished ) {
  186. sched_yield();
  187. }
  188. }
  189. QueryPerformanceCounter( &end_cnt );
  190. sum_frees = sum_allocs =0 ;
  191. sum_threads = 0 ;
  192. for(i=0;i< num_threads; i++){
  193. sum_allocs += de_area[i].cAllocs ;
  194. sum_frees += de_area[i].cFrees ;
  195. sum_threads += de_area[i].cThreads ;
  196. de_area[i].cAllocs = de_area[i].cFrees = 0;
  197. }
  198. ticks = end_cnt - start_cnt ;
  199. duration = (double)ticks/ticks_per_sec ;
  200. for(i = 0; i < num_threads; i++) {
  201. if( !de_area[i].finished ) {
  202. printf("Thread at %d not finished\n", i);
  203. }
  204. }
  205. rate_n = sum_allocs/duration ;
  206. if( rate_1 == 0){
  207. rate_1 = rate_n ;
  208. }
  209. reqd_space = (0.5*(min_size+max_size)*num_threads*chperthread) ;
  210. // used_space = CountReservedSpace() - init_space;
  211. used_space = 0;
  212. printf("%2d ", num_threads ) ;
  213. printf("%6.3f", duration ) ;
  214. printf("%6.3f", rate_n/rate_1 ) ;
  215. printf("%8.0f", sum_allocs/duration ) ;
  216. printf(" %6.3f %.3f", (double)used_space/(1024*1024), used_space/reqd_space) ;
  217. printf("\n") ;
  218. Sleep(5000L) ; // wait 5 sec for old threads to die
  219. prevthreads = num_threads;
  220. }
  221. }
  222. int main(void) {
  223. long sleep_cnt;
  224. int min_threads, max_threads;
  225. int num_chunks = 10000;
  226. int num_rounds;
  227. int chperthread;
  228. myallocator = dmm_init();
  229. printf("Larson benchmark\n");
  230. printf("runtime (sec): ") ;
  231. scanf ("%ld", &sleep_cnt);
  232. printf("chunk size (min,max): ") ;
  233. scanf("%d %d", &min_size, &max_size ) ;
  234. printf("threads (min, max): ") ;
  235. scanf("%d %d", &min_threads, &max_threads) ;
  236. pthread_setconcurrency(max_threads);
  237. printf("chunks/thread: ");
  238. scanf("%d", &chperthread );
  239. num_chunks = max_threads * chperthread ;
  240. if( num_chunks > MAX_BLOCKS ){
  241. printf("Max %d chunks - exiting\n", MAX_BLOCKS ) ;
  242. return 1;
  243. }
  244. printf("no of rounds: ");
  245. scanf("%d", &num_rounds );
  246. runthreads(sleep_cnt, min_threads, max_threads, chperthread, num_rounds) ;
  247. return 0;
  248. }