usermode/library/malloc-original/benchmarks/linux-scalability/linux-scalability.c

00001 /*
00002  *  malloc-test
00003  *  cel - Thu Jan  7 15:49:16 EST 1999
00004  *
00005  *  Benchmark libc's malloc, and check how well it
00006  *  can handle malloc requests from multiple threads.
00007  *
00008  *  Syntax:
00009  *  malloc-test [ size [ iterations [ thread count ]]]  
00010  *
00011  */
00012 
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015 #include <sys/time.h>
00016 #include <unistd.h>
00017 
00018 #include <pthread.h>
00019 
00020 #define USECSPERSEC 1000000
00021 #define pthread_attr_default NULL
00022 #define MAX_THREADS 50
00023 
00024 void * run_test (void *);
00025 void *dummy (unsigned);
00026 
00027 static unsigned size = 512;
00028 static unsigned iteration_count = 1000000;
00029 
00030 int 
00031 main (int argc, char *argv[])
00032 {
00033   unsigned i;
00034   unsigned thread_count = 1;
00035   pthread_t thread[MAX_THREADS];
00036 
00037   /*          * Parse our arguments          */
00038   switch (argc)
00039     {
00040     case 4:                     /* size, iteration count, and thread count were specified */
00041       thread_count = atoi (argv[3]);
00042       if (thread_count > MAX_THREADS)
00043         thread_count = MAX_THREADS;
00044     case 3:                     /* size and iteration count were specified; others default */
00045       iteration_count = atoi (argv[2]);
00046     case 2:                     /* size was specified; others default */
00047       size = atoi (argv[1]);
00048 
00049     case 1:                     /* use default values */
00050       break;
00051     default:
00052       printf ("Unrecognized arguments.\n");
00053       exit (1);
00054     }
00055 
00056 /*          * Invoke the tests          */
00057   printf ("Starting test...\n");
00058   for (i = 1; i <= thread_count; i++) {
00059     pthread_attr_t attr;
00060     pthread_attr_init (&attr);
00061 #ifdef PTHREAD_SCOPE_SYSTEM
00062     pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM); /* bound behavior */
00063 #endif
00064     if (pthread_create (&(thread[i]), &attr, &run_test, (void *) NULL))
00065       printf ("failed.\n");
00066   }
00067 
00068 /*          * Wait for tests to finish          */
00069   for (i = 1; i <= thread_count; i++)
00070     pthread_join (thread[i], NULL);
00071 
00072   exit (0);
00073 }
00074 
00075 void * 
00076 run_test (void * arg)
00077 {
00078   register unsigned int i;
00079   register unsigned request_size = size;
00080   register unsigned total_iterations = iteration_count;
00081   struct timeval start, end, null, elapsed, adjusted;
00082 
00083 /*          * Time a null loop.  We'll subtract this from the final          * malloc loop results to get a more accurate value.          */ gettimeofday (&start, NULL);
00084 
00085   for (i = 0; i < total_iterations; i++)
00086     {
00087       register void *buf;
00088       buf = dummy (i);
00089       buf = dummy (i);
00090     }
00091 
00092   gettimeofday (&end, NULL);
00093   null.tv_sec = end.tv_sec - start.tv_sec;
00094   null.tv_usec = end.tv_usec - start.tv_usec;
00095   if (null.tv_usec < 0)
00096     {
00097       null.tv_sec--;
00098       null.tv_usec += USECSPERSEC;
00099     }
00100 
00101 /*          * Run the real malloc test          */ gettimeofday (&start, NULL);
00102 
00103   for (i = 0; i < total_iterations; i++)
00104     {
00105       register void *buf;
00106 
00107       buf = malloc (request_size);
00108       free (buf);
00109     }
00110 
00111   gettimeofday (&end, NULL);
00112   elapsed.tv_sec = end.tv_sec - start.tv_sec;
00113   elapsed.tv_usec = end.tv_usec - start.tv_usec;
00114   if (elapsed.tv_usec < 0)
00115     {
00116       elapsed.tv_sec--;
00117       elapsed.tv_usec += USECSPERSEC;
00118     }
00119 
00120 /*          * Adjust elapsed time by null loop time          */
00121   adjusted.tv_sec = elapsed.tv_sec - null.tv_sec;
00122   adjusted.tv_usec = elapsed.tv_usec - null.tv_usec;
00123   if (adjusted.tv_usec < 0)
00124     {
00125       adjusted.tv_sec--;
00126       adjusted.tv_usec += USECSPERSEC;
00127     }
00128   printf ("Thread %d adjusted timing: %d.%06d seconds for %d requests" " of %d bytes.\n", pthread_self (), adjusted.tv_sec, adjusted.tv_usec, total_iterations, request_size);
00129 
00130   return (NULL);
00131 }
00132 
00133 void *
00134 dummy (unsigned i)
00135 {
00136   return NULL;
00137 }

Generated on Sat Apr 23 11:43:35 2011 for Mnemosyne by  doxygen 1.4.7