Add memory profiler
parent
2ec7f40125
commit
8970e8927c
@ -0,0 +1,92 @@
|
|||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/sysinfo.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/times.h>
|
||||||
|
#include <sys/vtimes.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "log.h"
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
|
static unsigned long long lastTotalUser, lastTotalUserLow, lastTotalSys, lastTotalIdle;
|
||||||
|
static clock_t lastCPU, lastSysCPU, lastUserCPU;
|
||||||
|
static int numProcessors;
|
||||||
|
|
||||||
|
void getTotalVirtualMem(double* total_ram) {
|
||||||
|
const double megabyte = 1024 * 1024;
|
||||||
|
struct sysinfo si;
|
||||||
|
sysinfo(&si);
|
||||||
|
*total_ram = si.totalram / megabyte;
|
||||||
|
}
|
||||||
|
|
||||||
|
void getVirtualMemUsed(double* used_ram) {
|
||||||
|
const double megabyte = 1024 * 1024;
|
||||||
|
struct sysinfo si;
|
||||||
|
sysinfo(&si);
|
||||||
|
*used_ram = si.freeram / megabyte;
|
||||||
|
}
|
||||||
|
|
||||||
|
int parseLine(char* line) {
|
||||||
|
// This assumes that a digit will be found and the line ends in " Kb".
|
||||||
|
int i = strlen(line);
|
||||||
|
const char* p = line;
|
||||||
|
while (*p <'0' || *p > '9') p++;
|
||||||
|
line[i-3] = '\0';
|
||||||
|
i = atoi(p);
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getVirtualMemUsedByCurrentProcess() {
|
||||||
|
FILE* file = fopen("/proc/self/status", "r");
|
||||||
|
int result = -1;
|
||||||
|
char line[128];
|
||||||
|
|
||||||
|
while (fgets(line, 128, file) != NULL){
|
||||||
|
if (strncmp(line, "VmSize:", 7) == 0){
|
||||||
|
result = parseLine(line);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fclose(file);
|
||||||
|
return result / 1024;
|
||||||
|
}
|
||||||
|
|
||||||
|
void skip_lines(FILE *fp, int numlines) {
|
||||||
|
int cnt = 0;
|
||||||
|
char ch;
|
||||||
|
while ((cnt < numlines) && ((ch = getc(fp)) != EOF)) {
|
||||||
|
if (ch == '\n') cnt++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_stats(struct cpustat *st, int cpunum) {
|
||||||
|
FILE *fp = fopen("/proc/stat", "r");
|
||||||
|
int lskip = cpunum+1;
|
||||||
|
skip_lines(fp, lskip);
|
||||||
|
char cpun[255];
|
||||||
|
fscanf(fp, "%s %d %d %d %d %d %d %d", cpun, &(st->t_user), &(st->t_nice),
|
||||||
|
&(st->t_system), &(st->t_idle), &(st->t_iowait), &(st->t_irq),
|
||||||
|
&(st->t_softirq));
|
||||||
|
fclose(fp);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
double calculate_load(struct cpustat *prev, struct cpustat *cur) {
|
||||||
|
int idle_prev = (prev->t_idle) + (prev->t_iowait);
|
||||||
|
int idle_cur = (cur->t_idle) + (cur->t_iowait);
|
||||||
|
|
||||||
|
int nidle_prev = (prev->t_user) + (prev->t_nice) + (prev->t_system) + (prev->t_irq) + (prev->t_softirq);
|
||||||
|
int nidle_cur = (cur->t_user) + (cur->t_nice) + (cur->t_system) + (cur->t_irq) + (cur->t_softirq);
|
||||||
|
|
||||||
|
int total_prev = idle_prev + nidle_prev;
|
||||||
|
int total_cur = idle_cur + nidle_cur;
|
||||||
|
|
||||||
|
double totald = (double) total_cur - (double) total_prev;
|
||||||
|
|
||||||
|
double idled = (double) idle_cur - (double) idle_prev;
|
||||||
|
|
||||||
|
double cpu_perc = (1000 * (totald - idled) / totald + 1) / 10;
|
||||||
|
|
||||||
|
return cpu_perc;
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
#include "sys/types.h"
|
||||||
|
#include "sys/sysinfo.h"
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "stdio.h"
|
||||||
|
#include "string.h"
|
||||||
|
#include "sys/times.h"
|
||||||
|
#include "sys/vtimes.h"
|
||||||
|
|
||||||
|
void getTotalVirtualMem();
|
||||||
|
void getVirtualMemUsed();
|
||||||
|
int getVirtualMemUsedByCurrentProcess();
|
||||||
|
|
||||||
|
struct cpustat {
|
||||||
|
unsigned long t_user;
|
||||||
|
unsigned long t_nice;
|
||||||
|
unsigned long t_system;
|
||||||
|
unsigned long t_idle;
|
||||||
|
unsigned long t_iowait;
|
||||||
|
unsigned long t_irq;
|
||||||
|
unsigned long t_softirq;
|
||||||
|
};
|
||||||
|
|
||||||
|
void get_stats(struct cpustat *st, int cpunum);
|
||||||
|
double calculate_load(struct cpustat *prev, struct cpustat *cur);
|
@ -1 +1 @@
|
|||||||
python3 test.py 2>&1 | tee log.txt
|
python3 test.py 2>&1 | tee log2.txt
|
Loading…
Reference in New Issue