Go to the documentation of this file.00001
00059
00060
00061
00062
00063
00064
00065
00066 #ifndef MEMORY_H_
00067 #define MEMORY_H_
00068
00069 #include <sys/resource.h>
00070
00071 namespace measurement_tools {
00072
00073 namespace hidden {
00074 int parseLine(char* line){
00075 int i = strlen(line);
00076 while (*line < '0' || *line > '9') line++;
00077 line[i-3] = '\0';
00078 i = atoi(line);
00079 return i;
00080 }
00081
00082
00083 int getValue(FILE *file){
00084 int resultA = -1;
00085 int resultB = -1;
00086 char line[128];
00087
00088
00089 while (fgets(line, 128, file) != NULL){
00090 if (strncmp(line, "VmSize:", 7) == 0) resultA = parseLine(line);
00091 else if (strncmp(line, "VmRSS:", 6) == 0) resultB = parseLine(line);
00092 }
00093 if(resultA<0||resultB<0)
00094 return -1;
00095
00096 return resultA+resultB;
00097 }
00098
00099 }
00100
00102 int getMemoryUsage(int pid=getpid())
00103 {
00104 char buffer[256];
00105 sprintf(buffer,"/proc/%d/status", pid);
00106 FILE* status = fopen( buffer, "r" );
00107 int used = hidden::getValue(status);
00108 fclose(status);
00109
00110 return used;
00111 }
00112
00117 class MemoryUsage {
00118 int pid,start;
00119 public:
00120 MemoryUsage(int pid=getpid()):
00121 pid(pid),
00122 start(getMemoryUsage(pid))
00123 {}
00124
00125 int getKB() {
00126 return getMemoryUsage(pid)-start;
00127 }
00128 };
00129 }
00130
00131
00132 #endif