SoDaRadio-5.0.3-master:8901fb5
ProcInfo.hxx
Go to the documentation of this file.
1 /*
2  Copyright (c) 2017, Matthew H. Reilly (kb1vc)
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8 
9  Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in
13  the documentation and/or other materials provided with the
14  distribution.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef ProcInfo_HDR
30 #define ProcInfo_HDR
31 
32 #include <fstream>
33 #include <string>
34 #include <boost/format.hpp>
35 #include <boost/date_time/posix_time/posix_time.hpp>
36 
37 namespace kb1vc {
41  class ProcInfo {
42  public:
43  ProcInfo(const std::string & outfile,
44  const std::string & _unit_name = std::string("UNKNOWN")) {
45  unit_name = _unit_name;
46  statm_file_name = "/proc/self/statm";
47  init(outfile);
48  }
49 
50  ProcInfo(const std::string & outfile,
51  unsigned int pid,
52  const std::string & _unit_name = std::string("UNKNOWN")) {
53  unit_name = _unit_name;
54  statm_file_name = (boost::format("/proc/%d/statm") % pid).str();
55  init(outfile);
56  }
57 
58  bool getInfo();
59 
60  void reportInfo(bool only_if_changed = false) {
61  getInfo();
62  if(!only_if_changed || stats_changed) {
63  printInfo();
64  }
65  }
66 
67  void printInfo(std::ostream & out);
68 
70 
71 
72  protected:
73  // the stats we gather
74  unsigned long vm_size, resident_pages,
77  // true if things changed
79 
80  void init(const std::string & outfile) {
81  start_seconds = boost::posix_time::second_clock::local_time();
82  openFiles(outfile);
83  clearStats();
84  getInfo();
85  }
86 
87  void openFiles(const std::string & of_name);
88 
89  void clearStats() {
90  vm_size = 0;
91  resident_pages = 0;
92  shared_pages = 0;
93  text_pages = 0;
94  lib_pages = 0;
95  data_stack_pages = 0;
96  dirty_pages = 0;
97  };
98 
99  std::string curDateTime();
100 
101  unsigned long getElapsedTime() {
102  boost::posix_time::ptime now;
103  now = boost::posix_time::second_clock::local_time();
104  boost::posix_time::time_duration duration = now - start_seconds;
105  return duration.total_seconds();
106  }
107 
110 
111  std::string unit_name;
112  std::string statm_file_name;
113  std::ifstream f_statm;
114  std::ofstream f_report;
115 
116  boost::posix_time::ptime start_seconds;
117  unsigned long last_elapsed;
118  };
119 }
120 
121 
122 #endif
bool getInfo()
retrieve current memory usage info from statm
Definition: ProcInfo.cxx:59
std::string unit_name
the name of the unit reporting status
Definition: ProcInfo.hxx:111
boost::posix_time::ptime start_seconds
Definition: ProcInfo.hxx:116
std::ifstream f_statm
Definition: ProcInfo.hxx:113
unsigned long lib_pages
Definition: ProcInfo.hxx:74
unsigned long resident_pages
Definition: ProcInfo.hxx:74
unsigned long data_stack_pages
Definition: ProcInfo.hxx:74
unsigned long dirty_pages
Definition: ProcInfo.hxx:74
std::string curDateTime()
Definition: ProcInfo.cxx:122
ProcInfo(const std::string &outfile, const std::string &_unit_name=std::string("UNKNOWN"))
Definition: ProcInfo.hxx:43
unsigned long text_pages
Definition: ProcInfo.hxx:74
unsigned long shared_pages
Definition: ProcInfo.hxx:74
void clearStats()
Definition: ProcInfo.hxx:89
bool stats_changed
Definition: ProcInfo.hxx:78
unsigned long last_elapsed
Definition: ProcInfo.hxx:117
unsigned long vm_size
Definition: ProcInfo.hxx:74
std::ofstream f_report
Definition: ProcInfo.hxx:114
unsigned long getElapsedTime()
Definition: ProcInfo.hxx:101
ProcInfo(const std::string &outfile, unsigned int pid, const std::string &_unit_name=std::string("UNKNOWN"))
Definition: ProcInfo.hxx:50
void openFiles(const std::string &of_name)
Definition: ProcInfo.cxx:31
std::string statm_file_name
Definition: ProcInfo.hxx:112
void printInfo()
Definition: ProcInfo.hxx:69
A simple base class to provide debug messaging from any derived class.
Definition: ProcInfo.hxx:41
void reportInfo(bool only_if_changed=false)
Definition: ProcInfo.hxx:60
void init(const std::string &outfile)
Definition: ProcInfo.hxx:80