larson.c 8.8 KB

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