Commit 1d77dd6d authored by Dong Hung Pham's avatar Dong Hung Pham 🤡
Browse files

add p5

parent 392a9c8b
#include <string>
#include <iostream>
#include "stdlib.h"
#include "stdio.h"
#include <cstring>
#include <unistd.h>
#include "sys/types.h"
#include "sys/sysinfo.h"
#include "sys/times.h"
#include "sys/vtimes.h"
static clock_t lastCPU, lastSysCPU, lastUserCPU;
static int numProcessors;
void init(){
FILE* file;
struct tms timeSample;
char line[128];
lastCPU = times(&timeSample);
lastSysCPU = timeSample.tms_stime; //This is the processor time the system has used on behalf of the calling process.
lastUserCPU = timeSample.tms_utime;//This is the total processor time the calling process has used in executing the instructions of its program.
file = fopen("/proc/cpuinfo", "r");
numProcessors = 0;
while(fgets(line, 128, file) != NULL){
if (strncmp(line, "processor", 9) == 0) numProcessors++;
}
fclose(file);
}
double getCurrentValue(){
struct tms timeSample;
clock_t now;
double percent;
now = times(&timeSample);
if (now <= lastCPU || timeSample.tms_stime < lastSysCPU ||
timeSample.tms_utime < lastUserCPU){
//Overflow detection. Just skip this value.
percent = -1.0;
}
else{
percent = (timeSample.tms_stime - lastSysCPU) +
(timeSample.tms_utime - lastUserCPU);
percent /= (now - lastCPU);
percent /= numProcessors;
percent *= 100;
}
lastCPU = now;
lastSysCPU = timeSample.tms_stime;
lastUserCPU = timeSample.tms_utime;
return percent;
}
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 getValue(){ //Note: this value is in KB!
FILE* file = fopen("/proc/self/status", "r");
int result = -1;
char line[128];
while (fgets(line, 128, file) != NULL){
if (strncmp(line, "VmRSS:", 6) == 0){
result = parseLine(line);
break;
}
}
fclose(file);
return result;
}
int main()
{
//sleep(15);
init();
struct sysinfo memInfo;
sysinfo (&memInfo);
long long totalPhysMem = memInfo.totalram;
long long physMemUsed = memInfo.totalram - memInfo.freeram;
for (int j = 0; j < 200;j++){ //200mb
char *data;
int bytes = (1000*1000);
data = (char *) malloc(bytes);
for(int i=0;i<bytes;i++){
data[i] = (char) rand();
}
}
std::cout << "RAM in MB :" ;
int res = getValue()/1000;
std::cout << res << std::endl;
std::cout << "CPU:" ;
res = getCurrentValue();
std::cout << res << std::endl;
return 0;
}
// to test : sudo docker run --cpus=".5" b2726c539605 . wie viel wird cpu verbraucht !!! Ans: 49*0.5 = 25%, ANs : nur 0.5 cpu
// sudo docker run --cpu-shares="80" b2726c539605
// sudo docker run --cpu-shares="262100" b2726c539605.
//sudo docker run --memory="90mb" b2726c539605. // why 100mb can still run but not be killed !!!!!!
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment