SoDaRadio-5.0.3-master:8901fb5
ProcInfo.cxx
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 #include "ProcInfo.hxx"
30 
31 void kb1vc::ProcInfo::openFiles(const std::string & of_name)
32 {
34  if(f_statm.bad()) {
35  std::cerr << boost::format("ProcInfo could not open input file %s for object %s\n")
37  stat_file_ok = false;
38  }
39  else {
40  stat_file_ok = true;
41  }
42 
43  f_report.open(of_name);
44  if(f_report.bad()) {
45  std::cerr << boost::format("ProcInfo could not open output report file %s for object %s\n")
46  % of_name % unit_name;
47  report_file_ok = false;
48  }
49  else {
50  report_file_ok = true;
51  }
52 }
53 
60 {
61  if(stat_file_ok) {
62  f_statm.seekg(0);
63 
64  unsigned long n_vm_size, n_resident_pages,
65  n_shared_pages, n_text_pages,
66  n_lib_pages, n_data_stack_pages, n_dirty_pages;
67 
68  f_statm >> n_vm_size
69  >> n_resident_pages
70  >> n_shared_pages
71  >> n_text_pages
72  >> n_lib_pages
73  >> n_data_stack_pages
74  >> n_dirty_pages;
75 
76  stats_changed = false;
77  if(n_vm_size != vm_size) {
78  stats_changed = true;
79  vm_size = n_vm_size;
80  }
81  if(n_resident_pages != resident_pages) {
82  stats_changed = true;
83  resident_pages = n_resident_pages;
84  }
85  if(n_shared_pages != shared_pages) {
86  stats_changed = true;
87  shared_pages = n_shared_pages;
88  }
89  if(n_text_pages != text_pages) {
90  stats_changed = true;
91  text_pages = n_text_pages;
92  }
93  if(n_lib_pages != lib_pages) {
94  stats_changed = true;
95  lib_pages = n_lib_pages;
96  }
97  if(n_data_stack_pages != data_stack_pages) {
98  stats_changed = true;
99  data_stack_pages = n_data_stack_pages;
100  }
101  if(n_dirty_pages != dirty_pages) {
102  stats_changed = true;
103  dirty_pages = n_dirty_pages;
104  }
105 
107  }
108 
109  return stats_changed;
110 }
111 
112 
113 void kb1vc::ProcInfo::printInfo(std::ostream & out)
114 {
115  out << boost::format("%s %s %ld vm_size %d rss %d sh %d txt %d lib %d dss %d dirty %d\n")
118  out.flush();
119 }
120 
121 
123 {
124  boost::posix_time::ptime t1;
125  t1 = boost::posix_time::second_clock::local_time();
126  return to_simple_string(t1);
127 }
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
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
unsigned long text_pages
Definition: ProcInfo.hxx:74
unsigned long shared_pages
Definition: ProcInfo.hxx:74
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
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