Last active
August 12, 2018 11:08
-
-
Save clevertension/0f6ee7e0a122ed8b7b9b to your computer and use it in GitHub Desktop.
get the peak vm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <vector> | |
#include <utility> | |
#include <iostream> | |
#include <fstream> | |
#include <sstream> | |
//getpid | |
#include <unistd.h> | |
////mkdir | |
#include <sys/stat.h> | |
////setvbuf | |
#include <cstdio> | |
////ptrace | |
#include <sys/ptrace.h> | |
////rusage | |
#include <sys/resource.h> | |
// | |
////wait4 | |
#include <sys/types.h> | |
#include <sys/time.h> | |
#include <sys/resource.h> | |
#include <sys/wait.h> | |
#include <sys/user.h> | |
using namespace std; | |
int get_memory_usage(pid_t processPid) { | |
char buffer[64]; | |
sprintf(buffer, "/proc/%d/status", processPid); | |
FILE* fproc = fopen(buffer, "r"); | |
if (fproc == NULL) { | |
return -1; | |
} | |
int vmPeak = 0; | |
while (fgets(buffer, 32, fproc)) { | |
if (!strncmp(buffer, "VmPeak:", 7)) { | |
sscanf(buffer + 7, "%d", &vmPeak); | |
} | |
} | |
fclose(fproc); | |
return vmPeak; | |
} | |
int main() { | |
cout<<"vmpeak (KB) "<<get_memory_usage(getpid())<<endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment