@@ -24,14 +24,36 @@ ProcessMonitor::ProcessMonitor()
2424 mPid = static_cast <unsigned int >(::getpid ());
2525 getrusage (RUSAGE_SELF , &mPreviousGetrUsage );
2626 mTimeLastRun = std::chrono::high_resolution_clock::now ();
27+ #ifdef _OS_LINUX
28+ setTotalMemory ();
29+ #endif
30+ }
31+
32+ void ProcessMonitor::setTotalMemory ()
33+ {
34+ std::ifstream memInfo (" /proc/meminfo" );
35+ std::string totalString;
36+ std::getline (memInfo, totalString);
37+ std::istringstream iss (totalString);
38+ std::vector<std::string> tokens{std::istream_iterator<std::string>{iss},
39+ std::istream_iterator<std::string>{}};
40+ mTotalMemory = std::stoi (tokens[1 ]);
2741}
2842
2943Metric ProcessMonitor::getMemoryUsage ()
3044{
31- std::string command = " ps --no-headers -o pmem --pid " + std::to_string (mPid );
32- std::string output = exec (command.c_str ());
33- boost::trim (output);
34- return Metric{std::stod (output), " memoryUsagePercentage" };
45+ std::ifstream statusStream (" /proc/self/status" );
46+ std::string rssString;
47+ rssString.reserve (50 );
48+
49+ // Scan for VmRSS
50+ for (int i = 0 ; i < 18 ; i++) {
51+ std::getline (statusStream, rssString);
52+ }
53+ std::istringstream iss (rssString);
54+ std::vector<std::string> tokens{std::istream_iterator<std::string>{iss},
55+ std::istream_iterator<std::string>{}};
56+ return Metric{(std::stod (tokens[1 ])*100 )/mTotalMemory , " memoryUsagePercentage" };
3557}
3658
3759std::vector<Metric> ProcessMonitor::getCpuAndContexts () {
@@ -85,20 +107,5 @@ std::vector<Metric> ProcessMonitor::getNetworkUsage()
85107return metrics;
86108}
87109
88- std::string ProcessMonitor::exec (const char * cmd)
89- {
90- char buffer[128 ];
91- std::string result = " " ;
92- std::shared_ptr<FILE > pipe (popen (cmd, " r" ), pclose);
93- if (!pipe) {
94- throw MonitoringInternalException (" Process Monitor exec" , " Issue encountered when running 'ps' (popen)" );
95- }
96- while (!feof (pipe.get ())) {
97- if (fgets (buffer, 128 , pipe.get ()) != NULL )
98- result += buffer;
99- }
100- return result;
101- }
102-
103110} // namespace monitoring
104111} // namespace o2
0 commit comments