Skip to content

[Lilit summer student project] Add memory measurements to interpreted PyROOT #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions include/rootbench/MemoryMeasurement.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
///\file Memory measurement.

#include <string>

//running usr/bin/time to collect measurements in perffile
std::string getMemoryStats(const std::string& dir, const std::string& filename, std::string& rootInvocation, const std::string& rootexe, const std::string& endInvoc);

//getting Maximum resident set size form perffile and storing in peakSize
int getMemoryMeasurement(std::string& perftutorial);

//extracting filename from given path
std::string getFileName(std::string& path);
3 changes: 2 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
RB_ADD_LIBRARY(RBSupport
ErrorHandling.cxx
)
MemoryMeasurement.cxx
)
target_include_directories(RBSupport PUBLIC ${PROJECT_BINARY_DIR}/include ${PROJECT_SOURCE_DIR}/include)
73 changes: 73 additions & 0 deletions lib/MemoryMeasurement.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
///\file Contains utilities to measure memory usage.
#include "rootbench/MemoryMeasurement.h"
#include "rootbench/RBConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <unistd.h>
#include <cstdlib>
#include <string>
#include <fstream>
#include <iostream>

// runs the given command and returns the output
std::string GetStdoutFromCommand(std::string cmd)
{
std::string data;
FILE *stream;
const int max_buffer = 256;
char buffer[max_buffer];
cmd.append(" 2>&1");
stream = popen(cmd.c_str(), "r");
if (stream) {
while (!feof(stream))
if (fgets(buffer, max_buffer, stream) != NULL)
data.append(buffer);
pclose(stream);
}
return data;
}

// gets file name from path
std::string getFileName(std::string path)
{
std::string base_fn = path.substr(path.find_last_of("/\\") + 1);
return base_fn.substr(0, base_fn.find_last_of('.'));
}

std::string getMemoryStats(const std::string &dir, const std::string &filename, std::string &rootInvocation,
const std::string &rootexe, const std::string &endInvoc)
{
std::string perffile = "";
std::string timePathLong = GetStdoutFromCommand("which time");
std::string timePath = timePathLong.substr(0, timePathLong.size() - 1);
if (access(timePath.c_str(), X_OK) == 0) {
std::string rootsys = RB::GetRootSys();
std::string thisroot = rootsys + "/bin/thisroot.sh";
perffile = getFileName(filename) + "_perfile.txt";
if (!filename.empty()) {
std::string fullpath = rootsys + "/" + dir + filename;
// FIXME: no source in /usr/dash
// We are writing /usr/bin/time -v output in file to get maximum resident memory for the benchmark
rootInvocation =
"source \"" + thisroot + "\" && " + timePath + " -v -o \"" + perffile + rootexe + fullpath + endInvoc;
} else {
rootInvocation = "source \"" + thisroot + "\" && " + timePath + " -v -o \"" + perffile +
"\" root.exe -l -q -b "; // starter for ROOT
}
} else {
std::cout << "Cannot find usr/bin/time" << std::endl;
}
return perffile;
}

int getMemoryMeasurement(std::string &perftutorial)
{
int peakSize = 0;
std::string memorytutorial =
"cat \"" + perftutorial + "\"| grep 'Maximum resident set size' | awk '{print $6}' > tmp_mem_file";
int res = std::system(memorytutorial.c_str());
(void)res;
std::ifstream("tmp_mem_file") >> peakSize;
return peakSize;
}
2 changes: 1 addition & 1 deletion root/interpreter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ RB_ADD_GBENCHMARK(InterpreterBenchmarks

RB_ADD_GBENCHMARK(InterpreterBenchmarksNoPCH
RunInterpreterNoPCH.cxx
LABEL short)
LABEL short)
47 changes: 14 additions & 33 deletions root/interpreter/InterpreterTest.h
Original file line number Diff line number Diff line change
@@ -1,49 +1,30 @@
#include <iostream>
#include "rootbench/MemoryMeasurement.h"
#include "benchmark/benchmark.h"
#include <chrono>
#include <unistd.h>
#include <cstdlib>
#include <string>
#include <cstdlib>
#include <fstream>
#include <iostream>

#include <unistd.h>

#include "benchmark/benchmark.h"

#include <chrono>

#include "rootbench/RBConfig.h"

static int runTutorial(const std::string& dir, const std::string& filename, const std::string& perffile) {
std::string rootsys = RB::GetRootSys();
static int runTutorial(const std::string& dir, const std::string& filename, std::string& perftutorial) {
std::string rootInvocation;
std::string thisroot = rootsys + "/bin/thisroot.sh";
if (!filename.empty()) {
std::string fullpath = rootsys + "/" + dir + "/" + filename;
// FIXME: no source in /usr/dash
// We are writing /usr/bin/time -v output in file to get maximum resident memory for the benchmark
rootInvocation = "source \"" + thisroot + "\" && /usr/bin/time -v -o \"" + perffile + "\" root.exe -l -q -b -n -x \"" + fullpath + "\" -e return ";
} else {
rootInvocation = "source \"" + thisroot + "\" && /usr/bin/time -v -o \"" + perffile + "\" root.exe -l -q -b ";
}

return std::system(rootInvocation.c_str());
std::string rootexe = "\" root.exe -l -q -b -n -x \"";
std::string endInvoc = "\" -e return ";
perftutorial = getMemoryStats(dir,filename,rootInvocation, rootexe, endInvoc);
return std::system(rootInvocation.c_str());
}

static void TestTutorial(benchmark::State &state, const char *dir, const char *tutorial) {
int peakSize = 0;
std::string perftutorial ("perfile.txt");
for(auto _ : state){
auto start = std::chrono::high_resolution_clock::now();
runTutorial(dir, tutorial,perftutorial);
auto end = std::chrono::high_resolution_clock::now();
auto elapsed_seconds =
std::chrono::duration_cast<std::chrono::duration<double>>(
end - start);
std::string perftutorial="";
runTutorial(dir, tutorial, perftutorial);
auto end = std::chrono::high_resolution_clock::now();
auto elapsed_seconds = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
state.SetIterationTime(elapsed_seconds.count());
std::string memorytutorial = "cat \"" + perftutorial + "\"| grep 'Maximum resident set size' | awk '{print $6}' > tmp_mem_file";
int res = std::system(memorytutorial.c_str());
(void) res;
std::ifstream("tmp_mem_file") >> peakSize;
peakSize = getMemoryMeasurement(perftutorial);
}
state.counters.insert({{"RSS", peakSize}});
}
35 changes: 15 additions & 20 deletions root/pyroot/PyROOTTest.h
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
#include <iostream>
#include "rootbench/MemoryMeasurement.h"
#include "benchmark/benchmark.h"
#include <chrono>
#include <unistd.h>
#include <cstdlib>
#include <string>
#include <cstdlib>
#include <fstream>
#include <iostream>

#include <unistd.h>

#include "benchmark/benchmark.h"

#include <chrono>

#include "rootbench/RBConfig.h"

static int runTutorial(const std::string& dir, const std::string& filename) {
std::string rootsys = RB::GetRootSys();
std::string fullpath = rootsys + "/" + dir + "/" + filename;
std::string rootInvocation = "root -l -b -q -e 'TPython::Exec(\" exec( open(\"" + fullpath + "\").read())\")'";
return std::system(rootInvocation.c_str());
static int runTutorial(const std::string& dir, const std::string& filename, std::string& perftutorial) {
std::string rootInvocation;
std::string rootexe = "\" root -l -b -q -e 'TPython::Exec(\" exec( open('";
std::string endInvoc = "').read())\")'";
perftutorial = getMemoryStats(dir,filename,rootInvocation, rootexe, endInvoc);
return std::system(rootInvocation.c_str());
}

static void TestTutorial(benchmark::State &state, const char *dir, const char *tutorial) {
int peakSize = 0;
for(auto _ : state){
auto start = std::chrono::high_resolution_clock::now();
runTutorial(dir, tutorial);
auto end = std::chrono::high_resolution_clock::now();
auto elapsed_seconds =
std::chrono::duration_cast<std::chrono::duration<double>>(
end - start);
std::string perftutorial="";
runTutorial(dir, tutorial, perftutorial);
auto end = std::chrono::high_resolution_clock::now();
auto elapsed_seconds = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
state.SetIterationTime(elapsed_seconds.count());
peakSize = getMemoryMeasurement(perftutorial);
}
state.counters.insert({{"RSS", peakSize}});
}