openPMD-api
MemoryProfiler.hpp
1 /* Copyright 2020 Junmin Gu
2  *
3  * This file is part of openPMD-api.
4  *
5  * openPMD-api is free software: you can redistribute it and/or modify
6  * it under the terms of of either the GNU General Public License or
7  * the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * openPMD-api is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License and the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * and the GNU Lesser General Public License along with openPMD-api.
19  * If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #pragma once
23 
24 #include <chrono>
25 #include <fstream>
26 #include <iostream>
27 
28 
29 namespace openPMD
30 {
31 namespace benchmark
32 {
38  {
39  public:
45  MemoryProfiler( int rank, const std::string& tag )
46  : m_Rank( rank ), m_Name( "" )
47  {
48 #if defined(__linux)
49  //m_Name = "/proc/meminfo";
50  m_Name = "/proc/self/status";
51  Display( tag );
52 #endif
53  }
54 
62  void Display(const std::string& tag){
63  if (0 == m_Name.size())
64  return;
65 
66  if (m_Rank > 0)
67  return;
68 
69  std::cout<<" memory at: "<<tag;
70  std::ifstream input(m_Name.c_str());
71 
72  if (input.is_open()) {
73  for (std::string line; getline(input, line);)
74  {
75  if (line.find("VmRSS") == 0)
76  std::cout<<line<<" ";
77  if (line.find("VmSize") == 0)
78  std::cout<<line<<" ";
79  if (line.find("VmSwap") == 0)
80  std::cout<<line;
81  }
82  std::cout<<std::endl;
83  input.close();
84  }
85  }
86  private:
87  int m_Rank;
88  std::string m_Name;
89  };
90 }
91 }
MemoryProfiler(int rank, const std::string &tag)
Simple Memory profiler for linux.
Definition: MemoryProfiler.hpp:45
void Display(const std::string &tag)
Display virtual memory info.
Definition: MemoryProfiler.hpp:62
Public definitions of openPMD-api.
Definition: Date.cpp:29
The Memory profiler class for profiling purpose.
Definition: MemoryProfiler.hpp:37