MemUsage.cxx
Go to the documentation of this file.
2 
3 #include <unistd.h>
4 #include <sstream>
5 
6 using namespace std;
7 using namespace WireCell;
8 
9 static double memusage_linux_resident() {
10  int tSize = 0, resident = 0, share = 0;
11  ifstream buffer("/proc/self/statm");
12  buffer >> tSize >> resident >> share;
13  buffer.close();
14 
15  long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
16  double rss = resident * page_size_kb;
17  return rss;
18 }
19 
20 static double memusage_linux_shared() {
21  int tSize = 0, resident = 0, share = 0;
22  ifstream buffer("/proc/self/statm");
23  buffer >> tSize >> resident >> share;
24  buffer.close();
25 
26  long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
27  double shm = share * page_size_kb;
28  return shm;
29 }
30 
31 static double memusage_linux_size() {
32  int tSize = 0, resident = 0, share = 0;
33  ifstream buffer("/proc/self/statm");
34  buffer >> tSize >> resident >> share;
35  buffer.close();
36 
37  long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
38  double siz = tSize * page_size_kb;
39  return siz;
40 }
41 
42 
44 #ifdef __linux__
45  return memusage_linux_resident();
46 #endif
47  return -1;
48 }
49 
51 {
52 #ifdef __linux__
53  return memusage_linux_shared();
54 #endif
55  return -1;
56 }
58 {
59 #ifdef __linux__
60  return memusage_linux_size();
61 #endif
62  return -1;
63 }
64 
65 
66 MemUsage::MemUsage(const std::string& msg)
67 {
68  push(msg);
69 }
70 
71 MemUsage::~MemUsage()
72 {
73 }
74 
76 {
78 }
79 void MemUsage::push(const std::string& msg, MemUsage::memusage mu)
80 {
81  if (mu.first < 0 && mu.second < 0) {
82  mu = current();
83  }
84  m_events.push_back(event(mu,msg));
85 }
86 
87 std::string MemUsage::operator()(std::string msg, MemUsage::memusage mu)
88 {
89  push(msg, mu);
90  return emit(-1);
91 }
92 
93  /// Return event by index.
94 MemUsage::event MemUsage::operator[](int ind) const
95 {
96  while (ind < 0) { ind += m_events.size();}
97 
98  return m_events[ind];
99 }
100 
102 {
103  stringstream ss;
104  for (size_t ind=0; ind<m_events.size(); ++ind) {
105  ss << this->emit(ind) << "\n";
106  }
107  return ss.str();
108 }
109 
110 std::string MemUsage::emit(int ind) const
111 {
112  while (ind < 0) { ind += m_events.size();}
113  int prev_ind = ind-1;
114  if (prev_ind<0) prev_ind=0;
115 
116  const memusage& prev_mem = (*this)[prev_ind].first;
117  const memusage& evt_mem = (*this)[ind].first;
118  const string& evt_msg = (*this)[ind].second;
119 
120  memusage from_prev(evt_mem.first - prev_mem.first, evt_mem.second - prev_mem.second);
121 
122  stringstream ss;
123  ss << "MEM: total: size=" << evt_mem.first << "K, res=" << evt_mem.second << "K "
124  << "increment: size=" << from_prev.first << "K, res=" << from_prev.second << "K "
125  << evt_msg;
126  return ss.str();
127 }
double memusage_size()
Definition: MemUsage.cxx:57
void msg(const char *fmt,...)
Definition: message.cpp:107
std::string string
Definition: nybbler.cc:12
static double memusage_linux_size()
Definition: MemUsage.cxx:31
STL namespace.
internal::basic_buffer< FMT_CHAR(S)> buffer
Definition: printf.h:757
std::pair< memusage, std::string > event
Definition: MemUsage.h:22
static double memusage_linux_shared()
Definition: MemUsage.cxx:20
double memusage_resident()
Definition: MemUsage.cxx:43
static Entry * current
Definition: Main.h:22
std::pair< double, double > memusage
Definition: MemUsage.h:21
static double memusage_linux_resident()
Definition: MemUsage.cxx:9
def summary(store)
Definition: info.py:119
double memusage_shared()
Definition: MemUsage.cxx:50