larson.c 8.8 KB

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