Advertisement
Bkmz

Untitled

Oct 6th, 2011
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <stdbool.h>
  5.  
  6. // for memset
  7. #include <string.h>
  8. #include <proc/sysinfo.h>
  9. #include <proc/readproc.h>
  10.  
  11.  
  12. #include <sys/time.h>
  13. #include <sys/resource.h>
  14.  
  15. #include <errno.h>
  16.  
  17. #include <unistd.h>
  18.  
  19. //#include <statgrab.h>
  20.  
  21. #include <time.h>
  22.  
  23. #define STEP 5
  24. #define CHUNK (STEP * 1024)
  25. #define PAGE_SIZE sysconf(_SC_PAGESIZE)
  26.  
  27. //#define CHUNK (32)
  28.  
  29. static int64_t __rss;
  30.  
  31. int64_t getTotalSystemMemory()
  32. {
  33.     // return only availble mem. Cache+buffers are includeing =(
  34. //    return (int64_t) sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE);
  35.     return (int64_t) sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
  36. }
  37.  
  38.  
  39. void * allocate_memory (size_t size) {
  40.  
  41.   void *p = malloc(size);
  42.   if (p != NULL)
  43.   {
  44.       memset(p, 0, size);
  45.   }
  46.   return p;
  47. }
  48.  
  49. struct rusage r;
  50.  
  51. bool is_swapped()
  52. {
  53.     getrusage(RUSAGE_SELF, &r);
  54.     if (__rss != r.ru_maxrss)
  55.     {
  56.         __rss = r.ru_maxrss;
  57. //        printf("rss:%lld\n", r.ru_maxrss);
  58.         return false;
  59.     }else {
  60. //        printf("last rss check. Same.  %lld\n", r.ru_maxrss*PAGE_SIZE);
  61.         return true;
  62.     }
  63.  
  64.  
  65. }
  66.  
  67.  
  68. int main()
  69. {
  70.     int SELF_PID = getpid();
  71.  
  72.     __rss = r.ru_maxrss;
  73.  
  74.     int64_t phys_mem = getTotalSystemMemory();
  75.     printf("phys_mem: %lld Bytes\n", phys_mem);
  76.  
  77.  
  78.     struct proc_t p;
  79.     look_up_our_self(&p);
  80.  
  81.     long unsigned int rss_rlim = p.rss_rlim;
  82.     printf("rss_rlim: %u Bytes\n", rss_rlim);
  83.  
  84.     int64_t total_mem;
  85.  
  86.     if (rss_rlim > phys_mem)
  87.     {
  88.         total_mem = phys_mem;
  89.     }else {
  90.         total_mem = rss_rlim;
  91.     }
  92.  
  93. //    total_mem = 1094967295;
  94.  
  95.     printf("total_available_mem: %lld Bytes\n", total_mem);
  96.  
  97.     int64_t amount_chunks = total_mem / CHUNK;
  98.  
  99.     printf("Available chunks %u\n", amount_chunks);
  100.  
  101.     void **chunks = malloc(sizeof(void *) * amount_chunks );
  102.  
  103.     int64_t last_chunk;
  104.  
  105.     int64_t i;
  106.  
  107.     clock_t start;
  108.     clock_t diff;
  109.     clock_t end;
  110.  
  111.     start= clock();
  112.  
  113.     for (i=0;i<amount_chunks;i++)
  114.     {
  115.         chunks[i] = allocate_memory(CHUNK);
  116.         last_chunk = i;
  117.  
  118.         if (chunks[i] == NULL)
  119.         {
  120.             printf("Last malloc return NULL pointer\n");
  121.             printf("Allocated bytes: %lld\n", last_chunk*CHUNK);
  122.             break;
  123.         }
  124.  
  125.         if ((i % (24*STEP) == 0) && is_swapped())
  126.         {
  127.             break;
  128. ;
  129.         }
  130.  
  131. //        usleep(50000);
  132.  
  133.     }
  134.  
  135.     end = clock();
  136.     diff = end - start;
  137.  
  138.     printf("Time of malloc'ing %u ms\n", (diff/1000)/1);
  139.  
  140.  
  141. //    sleep(60);
  142.  
  143.     printf("Allocated bytes %lld\n", last_chunk*CHUNK);
  144.     printf("Used %u chunks\n", last_chunk);
  145.  
  146. //    system();
  147.     sleep(12000);
  148.  
  149.     /*function executiontime()
  150.     {
  151.     clock_t start;
  152.     clock_t diff;
  153.     clock_t end;
  154.  
  155.     start= clock();
  156.     various algorithm;
  157.     end = clock();
  158.  
  159.     diff = end - start;
  160.     print diff;
  161.  
  162.     }*/
  163.  
  164.  
  165.     return 0;
  166. }
  167.  
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement