Posted in Programming

Resource Usage when allocating RAM

[root@serv-1 dev]# g++ getrusage.c -o get_resource_usage
[root@serv-1 dev]# time ./get_resource_usage
RAM size[MB]: 12
        User CPU: 0.005881
        System CPU: 0.011762
RAM size[MB]: 22
        User CPU: 0.008497
        System CPU: 0.01605
RAM size[MB]: 32
        User CPU: 0.008659
        System CPU: 0.022129
RAM size[MB]: 42
        User CPU: 0.010456
        System CPU: 0.026617
RAM size[MB]: 52
        User CPU: 0.011487
        System CPU: 0.032548
RAM size[MB]: 62
        User CPU: 0.013506
        System CPU: 0.035694
RAM size[MB]: 72
        User CPU: 0.013506
        System CPU: 0.039341
RAM size[MB]: 82
        User CPU: 0.014366
        System CPU: 0.041182
RAM size[MB]: 92
        User CPU: 0.014366
        System CPU: 0.043871
RAM size[MB]: 102
        User CPU: 0.015425
        System CPU: 0.045312

real    0m0.069s
user    0m0.015s
sys     0m0.051s
[root@serv-1 dev]# cat getrusage.c
#include <sys/resource.h>
#include <sys/time.h>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <unistd.h>

#define TO_SEC 1000000

int main() {
  int i = 0;
  struct rusage r_usage;
  struct timeval t_utime, t_stime ;
  while (++i <= 10) {
    void *m = malloc(10*1024*1024);
    memset(m,0,10*1024*1024);
//    usleep(2000000);
    getrusage(RUSAGE_SELF,&r_usage);
    std::cout << "RAM size[MB]: " << r_usage.ru_maxrss/1024 << std::endl ;
    t_utime = r_usage.ru_utime ;
    t_stime = r_usage.ru_stime ;
    std::cout << "\tUser CPU: "
        << (float) t_utime.tv_usec/TO_SEC
        << "\n"
        << "\tSystem CPU: "
        << (float) t_stime.tv_usec/TO_SEC
        << std::endl ;
  }
  return 0;
}

Advertisements

Author:

Database administrator who loves to work with Oracle software. (Sometimes not)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s