Skip to content

Commit

Permalink
Merge pull request #356 from cniethammer/refactor-MemoryProfiler
Browse files Browse the repository at this point in the history
Refactor MemoryProfiler
  • Loading branch information
cniethammer authored Jan 7, 2025
2 parents 2a9cd42 + 13ad942 commit b2c7c3d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 67 deletions.
102 changes: 36 additions & 66 deletions src/io/MemoryProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,38 @@

#include "MemoryProfiler.h"

#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <memory>
#include <regex>
#include <sstream>
#include <string>
#include <cstring>
#include "sys/types.h"
#include "sys/sysinfo.h"

#ifndef _SX
#include <sys/sysinfo.h>
#endif
#include "utils/Logger.h"

MemoryProfiler::MemoryProfiler() :
_list(), _hugePageSize(0) {

const size_t MAXLEN = 1024;
FILE *fp;
char buf[MAXLEN];
fp = fopen("/proc/meminfo", "r");
char *p1;
while (fgets(buf, MAXLEN, fp)) {
p1 = strstr(buf, "Hugepagesize:");
if (p1 != NULL) {
int colon = ':';
p1 = strchr(buf, colon) + 1;
_hugePageSize = atoi(p1);
std::ifstream meminfo_file("/proc/meminfo");
std::string token;
while(meminfo_file >> token) {
if (token == "Hugepagesize:") {
meminfo_file >> _hugePageSize;
break;
}
}
if (std::getenv("HUGETLB_DEFAULT_PAGE_SIZE")!=NULL){
std::string hugepsString = std::getenv("HUGETLB_DEFAULT_PAGE_SIZE");
if (const char *env_p = std::getenv("HUGETLB_DEFAULT_PAGE_SIZE")) {
std::string hugepsString(env_p);
_hugePageSize = std::stoi(hugepsString);
if( hugepsString.find("G") != std::string::npos or hugepsString.find("g") != std::string::npos ){
_hugePageSize *= 1024 * 1024;
} else if( hugepsString.find("M") != std::string::npos or hugepsString.find("m") != std::string::npos ){
_hugePageSize *= 1024;
}
}
fclose(fp);
}

void MemoryProfiler::registerObject(MemoryProfilable** object) {
Expand All @@ -67,72 +63,46 @@ void MemoryProfiler::doOutput(const std::string& myString) {

unsigned long long MemoryProfiler::getCachedSize() {
unsigned long long cached_size = 0;
const size_t MAXLEN = 1024;
FILE *fp;
char buf[MAXLEN];
fp = fopen("/proc/meminfo", "r");
while (fgets(buf, MAXLEN, fp)) {
char *p1 = strstr(buf, "Cached:");
if (p1 != NULL) {
int colon = ':';
p1 = strchr(buf, colon) + 1;
cached_size = strtoull(p1, NULL, 10);
std::ifstream meminfo_file("/proc/meminfo");
std::string token;
while(meminfo_file >> token) {
if (token == "Cached:") {
meminfo_file >> cached_size;
break;
}
}
fclose(fp);
return cached_size;
}

int MemoryProfiler::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 MemoryProfiler::countHugePages() {
FILE* file = fopen("/proc/self/numa_maps", "r");
if (!file) {
return 0;
}
std::ifstream numa_maps_file("/proc/self/numa_maps");

int hugepagecount = 0;
char line[1024];
while (fgets(line, 1024, file) != NULL) {
std::string line;
while(std::getline(numa_maps_file, line)) {
//if (/huge.*dirty=(\d+)/) {
std::string linestring(line);
if (linestring.find("huge") != std::string::npos) {
auto pos = linestring.find("dirty");
if (pos != std::string::npos) {
linestring = linestring.substr(pos);
auto eqpos = linestring.find("=");
auto spacepos = linestring.find(" ");
linestring = linestring.substr(eqpos + 1, spacepos - eqpos - 1);
hugepagecount += std::stoi(linestring);
if (line.find("huge") != std::string::npos) {
const std::regex re("dirty=(\\d+) ");
std::smatch match;
if (std::regex_match(line, match, re)) {
hugepagecount += std::stoi(match[1].str());
}
}
}
fclose(file);
return hugepagecount;
}

int MemoryProfiler::getOwnMemory() { //Note: this value is in KB!
FILE* file = fopen("/proc/self/status", "r");
int result = -1;
char line[1024];

while (fgets(line, 128, file) != NULL) {
if (strncmp(line, "VmRSS:", 6) == 0) {
result = parseLine(line);
int vmrss = 0;
std::ifstream self_status_file("/proc/self/status");
std::string token;
while(self_status_file >> token) {
if (token == "VmRSS:") {
self_status_file >> vmrss;
break;
}
}
fclose(file);
return result;
return vmrss;
}

void MemoryProfiler::printGeneralInfo(const std::string& myString) {
Expand Down
3 changes: 2 additions & 1 deletion src/io/MemoryProfiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#pragma once

#include <cstddef>
#include <vector>
#include <string>

Expand All @@ -22,7 +23,7 @@ class MemoryProfilable {
class MemoryProfiler {
private:
std::vector<MemoryProfilable **> _list;
int _hugePageSize; // in kB
int _hugePageSize; // Hugepagesize in kB
public:
MemoryProfiler();

Expand Down

0 comments on commit b2c7c3d

Please sign in to comment.