Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- // for memset
- #include <string.h>
- #include <proc/sysinfo.h>
- #include <proc/readproc.h>
- #include <sys/time.h>
- #include <sys/resource.h>
- #include <errno.h>
- #include <unistd.h>
- //#include <statgrab.h>
- #include <time.h>
- #define STEP 5
- #define CHUNK (STEP * 1024)
- #define PAGE_SIZE sysconf(_SC_PAGESIZE)
- //#define CHUNK (32)
- static int64_t __rss;
- int64_t getTotalSystemMemory()
- {
- // return only availble mem. Cache+buffers are includeing =(
- // return (int64_t) sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE);
- return (int64_t) sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
- }
- void * allocate_memory (size_t size) {
- void *p = malloc(size);
- if (p != NULL)
- {
- memset(p, 0, size);
- }
- return p;
- }
- struct rusage r;
- bool is_swapped()
- {
- getrusage(RUSAGE_SELF, &r);
- if (__rss != r.ru_maxrss)
- {
- __rss = r.ru_maxrss;
- // printf("rss:%lld\n", r.ru_maxrss);
- return false;
- }else {
- // printf("last rss check. Same. %lld\n", r.ru_maxrss*PAGE_SIZE);
- return true;
- }
- }
- int main()
- {
- int SELF_PID = getpid();
- __rss = r.ru_maxrss;
- int64_t phys_mem = getTotalSystemMemory();
- printf("phys_mem: %lld Bytes\n", phys_mem);
- struct proc_t p;
- look_up_our_self(&p);
- long unsigned int rss_rlim = p.rss_rlim;
- printf("rss_rlim: %u Bytes\n", rss_rlim);
- int64_t total_mem;
- if (rss_rlim > phys_mem)
- {
- total_mem = phys_mem;
- }else {
- total_mem = rss_rlim;
- }
- // total_mem = 1094967295;
- printf("total_available_mem: %lld Bytes\n", total_mem);
- int64_t amount_chunks = total_mem / CHUNK;
- printf("Available chunks %u\n", amount_chunks);
- void **chunks = malloc(sizeof(void *) * amount_chunks );
- int64_t last_chunk;
- int64_t i;
- clock_t start;
- clock_t diff;
- clock_t end;
- start= clock();
- for (i=0;i<amount_chunks;i++)
- {
- chunks[i] = allocate_memory(CHUNK);
- last_chunk = i;
- if (chunks[i] == NULL)
- {
- printf("Last malloc return NULL pointer\n");
- printf("Allocated bytes: %lld\n", last_chunk*CHUNK);
- break;
- }
- if ((i % (24*STEP) == 0) && is_swapped())
- {
- break;
- ;
- }
- // usleep(50000);
- }
- end = clock();
- diff = end - start;
- printf("Time of malloc'ing %u ms\n", (diff/1000)/1);
- // sleep(60);
- printf("Allocated bytes %lld\n", last_chunk*CHUNK);
- printf("Used %u chunks\n", last_chunk);
- // system();
- sleep(12000);
- /*function executiontime()
- {
- clock_t start;
- clock_t diff;
- clock_t end;
- start= clock();
- various algorithm;
- end = clock();
- diff = end - start;
- print diff;
- }*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement