We are trying to measure the amount of resources needed to run our program, written in C. Looking here and there, I found getrusage (2). So, yesterday I tried to write a simple code:
#include <stdio.h>
#include <errno.h>
#include <sys/resource.h>
int main(){
int y[100][100];
struct rusage *resource;
int x;
x = getrusage(0, resource);
printf("x = %d\n", x);
printf("errno = %d\n", errno);
return(0);
}
The result was not great. getrusage returned -1. According to the man page, non zero return value means it fails. The value of errno is 14.
What went wrong? Anybody?
Is there a better way to measure the amount of resources (memory, CPU, time) needed for a program? [Right now, I am looking at dtrace. Would it help?]


September 10th, 2009 at 9:20 am
buset mainannya C “butut” euy …
September 11th, 2009 at 5:56 am
langsung dibaca di procfs
September 11th, 2009 at 8:26 am
gak bisa via procfs karena ini di mac os x. gak ada procfs
September 11th, 2009 at 10:25 am
who (0=RUSAGE_SELF) sudah valid.
errno-14 EFAULT, pak. The address specified by the r_usage argument is not in a valid portion of the process’ address space. Pointer struktur resource belum teralokasi?
Bagaimana jika:
struct rusage resource;
…
x = getrusage(0, &resource);
atau
#include
struct rusage *resource;
resource = malloc(sizeof(resource)); /*Allocate memory*/
…
free(resource); /*free resource*/
Jalan di Linux saya (return 0, untuk solusi 1). Mudah2 di MacOS juga.
September 11th, 2009 at 10:26 am
ketinggalan: stdlib.h diinclude di solusi 2.
September 11th, 2009 at 4:42 pm
salah lagi:
resource = malloc(sizeof(resource)); /*Allocate memory*/
harusnya:
resource = malloc(sizeof(struct rusage)); /*Allocate memory*/