Advertisement
Bkmz

Untitled

Oct 20th, 2011
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.35 KB | None | 0 0
  1. #include <stdlib.h>
  2.  
  3. #include <stdbool.h>
  4.  
  5. // for memset
  6. #include <string.h>
  7. #include <proc/readproc.h>
  8.  
  9.  
  10. #include <sys/resource.h>
  11.  
  12. #include <time.h>
  13.  
  14. // for malloc_trim
  15. #include <malloc.h>
  16.  
  17. #define STEP 6
  18. #define CHUNK (STEP * 1024)
  19. #define PAGE_SIZE sysconf(_SC_PAGESIZE)
  20.  
  21.  
  22.  
  23.  
  24. /*
  25. * Platform pecific function. Return amount of pysical memory on this system;
  26. */
  27. int64_t getTotalSystemMemory()
  28. {
  29.     return (int64_t) sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
  30. }
  31.  
  32. /*
  33.  *  Function write to allocated memory block Zeros.
  34.  *  Return pointer to allocated block.
  35.  */
  36. void *allocate_memory (size_t size) {
  37.  
  38.   void *p = malloc(size);
  39.   if (p != NULL)
  40.   {
  41.       memset(p, 0, size);
  42.   }
  43.   return p;
  44. }
  45.  
  46. /*
  47.  * Function checking if process is swapped.
  48.  * Must run less then each iteration of malloc'ing to check it.
  49.  */
  50.  
  51. bool is_swapped()
  52. {
  53.     getrusage(RUSAGE_SELF, &r);
  54.     if (__rss != r.ru_maxrss)
  55.     {
  56.         __rss = r.ru_maxrss;
  57.         return false;
  58.     }else {
  59.         return true;
  60.     }
  61. }
  62.  
  63.  
  64. static int64_t __rss;
  65. struct rusage r;
  66.  
  67.  
  68. int64_t last_chunk;
  69.  
  70. int64_t i;
  71.  
  72. clock_t start;
  73. clock_t malloc_time,free_time;
  74.  
  75.  
  76. int main()
  77. {
  78.     __rss = r.ru_maxrss;
  79.  
  80.     int64_t phys_mem = getTotalSystemMemory();
  81.     printf("Total installed memory:\t%lld bytes\n", phys_mem);
  82.  
  83.  
  84.     struct proc_t p;
  85.     look_up_our_self(&p);
  86.  
  87.     long unsigned int rss_rlim = p.rss_rlim;
  88.  
  89.     int64_t total_mem;
  90.  
  91.     // geting the must available memory limit, to avoid
  92.     // segfault in memset, when using PAE.
  93.     if (rss_rlim > phys_mem)
  94.     {
  95.         total_mem = phys_mem;
  96.     }else {
  97.         total_mem = rss_rlim;
  98.     }
  99.  
  100.     int64_t amount_chunks = total_mem / CHUNK;
  101.  
  102.     printf("total_available_mem:\t%lld bytes\n", total_mem);
  103.     printf("Calculated chunks:\t%u pcs\n", amount_chunks);
  104.  
  105.     // allocating memory for array with pointers of memory
  106.     void **chunks = malloc(sizeof(void *) * amount_chunks );
  107.  
  108.  
  109.     printf("Start grabbing\n");
  110.  
  111.     // timer
  112.     start = clock();
  113.  
  114.     for (i=0;i<amount_chunks;i++)
  115.     {
  116.         // write pointer of allocated memory chunk to array
  117.         chunks[i] = allocate_memory(CHUNK);
  118.         last_chunk = i;
  119.  
  120.         // checking, if malloc return NULL pointer, after that we can't allocate more memory. Stoping
  121.         if (chunks[i] == NULL)
  122.         {
  123.             break;
  124.         }
  125.  
  126.  
  127.         // Linux specific swap checker
  128.         if ((i % (24*STEP) == 0) && is_swapped())
  129.         {
  130.             break;
  131.         }
  132.     }
  133.  
  134.     // timer
  135.     malloc_time = clock() - start;
  136.  
  137.  
  138.     printf("Time of malloc'ing %u ms\n", (malloc_time/1000)/1);
  139.     printf("-----------------------------------------------\n");
  140.  
  141.  
  142.  
  143.  
  144.     printf("Allocated bytes:\t%lld\n", last_chunk*CHUNK);
  145.     printf("Used chunks:\t\t%u\n", last_chunk);
  146.  
  147.     printf("* Check the current state of allocated memory\n");
  148.  
  149.  
  150.  
  151.     printf("Start free'ing\n");
  152.     // timer
  153.     start = clock();
  154.  
  155.     for (i=0;i<amount_chunks;i++)
  156.     {
  157.         free(chunks[i]);
  158.     }
  159.  
  160.     // using this system-call to release the "garbage" memory from process to Operating System
  161.     malloc_trim(0);
  162.  
  163.     // timer
  164.     free_time = clock() - start;
  165.  
  166.  
  167.  
  168.  
  169.     printf("Time of free'ing   %u ms\n", (free_time/1000)/1);
  170.  
  171.     printf("* Check the current state of allocated memory\n");
  172.     sleep(5);
  173.  
  174.     return 0;
  175. }
  176.  
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement