CMS 3D CMS Logo

ProcInfoFetcher.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Services
4 // Class : ProcInfoFetcher
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Chris Jones
10 // Created: Sun May 6 11:14:31 CDT 2012
11 //
12 
13 // system include files
14 
15 // user include files
16 
17 #include <cstring>
18 #include <cassert>
19 #include <iostream>
20 #ifdef __linux__
21 #include <malloc.h>
22 #endif
23 #include <sstream>
24 //#include <stdio.h>
25 #include <string>
26 #include <boost/lexical_cast.hpp>
27 
28 #include <fcntl.h>
29 #include <unistd.h>
30 
35 
36 //
37 // constants, enums and typedefs
38 //
39 
40 //
41 // static data member definitions
42 //
43 
44 namespace {
45  struct linux_proc {
46  int pid; // %d
47  std::string comm;
48  char state; // %c
49  int ppid; // %d
50  int pgrp; // %d
51  int session; // %d
52  int tty; // %d
53  int tpgid; // %d
54  unsigned int flags; // %u [before linux 2.6 %lu]
55  unsigned long minflt; // %lu
56  unsigned long cminflt; // %lu
57  unsigned long majflt; // %lu
58  unsigned long cmajflt; // %lu
59  unsigned long utime; // %lu
60  unsigned long stime; // %lu
61  long cutime; // %ld
62  long cstime; // %ld
63  long priority; // %ld
64  long nice; // %ld
65  long num_threads; // %ld
66  long itrealvalue; // %ld
67  unsigned long long starttime; // %llu [before linux 2.6 %d]
68  unsigned long vsize; // %lu
69  long rss; // %ld
70  unsigned long rlim; // %lu
71  unsigned long startcode; // %lu
72  unsigned long endcode; // %lu
73  unsigned long startstack; // %lu
74  unsigned long kstkesp; // %lu
75  unsigned long kstkeip; // %lu
76  unsigned long signal; // %lu
77  unsigned long blocked; // %lu
78  unsigned long sigignore; // %lu
79  unsigned long sigcatch; // %lu
80  unsigned long wchan; // %lu
81  };
82 
83  class Fetcher {
84  public:
85  friend Fetcher& operator>>(Fetcher&, int&);
86  friend Fetcher& operator>>(Fetcher&, long&);
87  friend Fetcher& operator>>(Fetcher&, unsigned int&);
88  friend Fetcher& operator>>(Fetcher&, unsigned long&);
89  friend Fetcher& operator>>(Fetcher&, unsigned long long&);
90  friend Fetcher& operator>>(Fetcher&, char&);
91  friend Fetcher& operator>>(Fetcher&, std::string&);
92 
93  explicit Fetcher(char* buffer) : buffer_(buffer), save_(nullptr), delims_(" \t\n\f\v\r") {}
94 
95  private:
96  int getInt() {
97  const char* t = getItem();
98  //std::cout <<"int '"<<t <<"'"<<std::endl;
99  return boost::lexical_cast<int>(t);
100  }
101  long getLong() {
102  const char* t = getItem();
103  //std::cout <<"long '"<<t <<"'"<<std::endl;
104  return boost::lexical_cast<long>(t);
105  }
106  unsigned int getUInt() {
107  const char* t = getItem();
108  //std::cout <<"uint '"<<t <<"'"<<std::endl;
109  return boost::lexical_cast<unsigned int>(t);
110  }
111  unsigned long getULong() {
112  const char* t = getItem();
113  //std::cout <<"ulong '"<<t <<"'"<<std::endl;
114  return boost::lexical_cast<unsigned long>(t);
115  }
116  unsigned long long getULongLong() {
117  const char* t = getItem();
118  //std::cout <<"ulong '"<<t <<"'"<<std::endl;
119  return boost::lexical_cast<unsigned long long>(t);
120  }
121  char getChar() { return *getItem(); }
122  std::string getString() { return std::string(getItem()); }
123  char* getItem() {
124  char* item = strtok_r(buffer_, delims_, &save());
125  assert(item);
126  buffer_ = nullptr; // Null for subsequent strtok_r calls.
127  return item;
128  }
129 
130  char const* save() const { return get_underlying_safe(save_); }
131  char*& save() { return get_underlying_safe(save_); }
132 
135  char const* const delims_;
136  };
137 
138  Fetcher& operator>>(Fetcher& iFetch, int& oValue) {
139  oValue = iFetch.getInt();
140  return iFetch;
141  }
142  Fetcher& operator>>(Fetcher& iFetch, long& oValue) {
143  oValue = iFetch.getLong();
144  return iFetch;
145  }
146  Fetcher& operator>>(Fetcher& iFetch, unsigned int& oValue) {
147  oValue = iFetch.getUInt();
148  return iFetch;
149  }
150  Fetcher& operator>>(Fetcher& iFetch, unsigned long& oValue) {
151  oValue = iFetch.getULong();
152  return iFetch;
153  }
154  Fetcher& operator>>(Fetcher& iFetch, unsigned long long& oValue) {
155  oValue = iFetch.getULongLong();
156  return iFetch;
157  }
158  Fetcher& operator>>(Fetcher& iFetch, char& oValue) {
159  oValue = iFetch.getChar();
160  return iFetch;
161  }
162  Fetcher& operator>>(Fetcher& iFetch, std::string& oValue) {
163  oValue = iFetch.getString();
164  return iFetch;
165  }
166 } // namespace
167 
168 namespace edm {
169  namespace service {
170 
171  ProcInfoFetcher::ProcInfoFetcher() : pg_size_(sysconf(_SC_PAGESIZE)) {
172 #ifdef __linux__
173  std::ostringstream ost;
174  ost << "/proc/" << getpid() << "/stat";
175 
176  if ((fd_ = open(ost.str().c_str(), O_RDONLY)) < 0) {
177  throw Exception(errors::Configuration) << "Failed to open " << ost.str() << std::endl;
178  }
179 #endif
180  }
182 #ifdef LINUX
183  close(fd_);
184 #endif
185  }
187  ProcInfo ret;
188 
189 #ifdef __linux__
190  double pr_size = 0.0, pr_rssize = 0.0;
191 
192  linux_proc pinfo;
193  int cnt;
194 
195  lseek(fd_, 0, SEEK_SET);
196 
197  if ((cnt = read(fd_, buf_, sizeof(buf_) - 1)) < 0) {
198  perror("Read of Proc file failed:");
199  return ProcInfo();
200  }
201 
202  if (cnt > 0) {
203  buf_[cnt] = '\0';
204 
205  try {
206  Fetcher fetcher(buf_);
207  fetcher >> pinfo.pid >> pinfo.comm >> pinfo.state >> pinfo.ppid >> pinfo.pgrp >> pinfo.session >> pinfo.tty >>
208  pinfo.tpgid >> pinfo.flags >> pinfo.minflt >> pinfo.cminflt >> pinfo.majflt >> pinfo.cmajflt >>
209  pinfo.utime >> pinfo.stime >> pinfo.cutime >> pinfo.cstime >> pinfo.priority >> pinfo.nice >>
210  pinfo.num_threads >> pinfo.itrealvalue >> pinfo.starttime >> pinfo.vsize >> pinfo.rss >> pinfo.rlim >>
211  pinfo.startcode >> pinfo.endcode >> pinfo.startstack >> pinfo.kstkesp >> pinfo.kstkeip >> pinfo.signal >>
212  pinfo.blocked >> pinfo.sigignore >> pinfo.sigcatch >> pinfo.wchan;
213  } catch (boost::bad_lexical_cast& iE) {
214  LogWarning("ProcInfoFetcher") << "Parsing of Prof file failed:" << iE.what() << std::endl;
215  return ProcInfo();
216  }
217 
218  // resident set size in pages
219  pr_size = (double)pinfo.vsize;
220  pr_rssize = (double)pinfo.rss;
221 
222  ret.vsize = pr_size / (1024.0 * 1024.0);
223  ret.rss = (pr_rssize * pg_size_) / (1024.0 * 1024.0);
224  }
225 #else
226  ret.vsize = 0;
227  ret.rss = 0;
228 #endif
229  return ret;
230  }
231  } // namespace service
232 } // namespace edm
runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:542
edm::service::ProcInfoFetcher::ProcInfoFetcher
ProcInfoFetcher()
Definition: ProcInfoFetcher.cc:171
service
Definition: service.py:1
MessageLogger.h
edm
HLT enums.
Definition: AlignableModifier.h:19
getInt
int getInt(ResultSet *rset, int ipar)
Definition: ODFEDAQConfig.cc:26
cms::cuda::assert
assert(be >=bs)
edm::get_underlying_safe
constexpr std::shared_ptr< T > & get_underlying_safe(propagate_const< std::shared_ptr< T >> &iP)
Definition: get_underlying_safe.h:41
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
edmScanValgrind.buffer
buffer
Definition: edmScanValgrind.py:171
EDMException.h
edm::service::ProcInfoFetcher::pg_size_
double pg_size_
Definition: ProcInfoFetcher.h:51
edm::propagate_const< char * >
edm::service::ProcInfoFetcher::~ProcInfoFetcher
~ProcInfoFetcher()
Definition: ProcInfoFetcher.cc:181
edm::service::ProcInfo
Definition: ProcInfoFetcher.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::service::ProcInfoFetcher::fd_
int fd_
Definition: ProcInfoFetcher.h:52
B2GTnPMonitor_cfi.item
item
Definition: B2GTnPMonitor_cfi.py:147
readEcalDQMStatus.read
read
Definition: readEcalDQMStatus.py:38
RunInfoPI::state
state
Definition: RunInfoPayloadInspectoHelper.h:16
edm::service::ProcInfoFetcher::fetch
ProcInfo fetch() const
Definition: ProcInfoFetcher.cc:186
operator>>
std::istream & operator>>(std::istream &input, CLHEP::HepGenMatrix &matrix)
Definition: matrixSaver.cc:80
Exception
Definition: hltDiff.cc:245
edm::service::ProcInfoFetcher::buf_
char buf_[500]
Definition: ProcInfoFetcher.h:53
ProcInfoFetcher.h
cuy.save
save
Definition: cuy.py:1165
HLT_FULL_cff.flags
flags
Definition: HLT_FULL_cff.py:13150
get_underlying_safe.h
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644
edm::errors::Configuration
Definition: EDMException.h:36