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 //
38 // constants, enums and typedefs
39 //
40 
41 //
42 // static data member definitions
43 //
44 
45 namespace {
46  struct linux_proc {
47  int pid; // %d
48  std::string comm;
49  char state; // %c
50  int ppid; // %d
51  int pgrp; // %d
52  int session; // %d
53  int tty; // %d
54  int tpgid; // %d
55  unsigned int flags; // %u [before linux 2.6 %lu]
56  unsigned long minflt; // %lu
57  unsigned long cminflt; // %lu
58  unsigned long majflt; // %lu
59  unsigned long cmajflt; // %lu
60  unsigned long utime; // %lu
61  unsigned long stime; // %lu
62  long cutime; // %ld
63  long cstime; // %ld
64  long priority; // %ld
65  long nice; // %ld
66  long num_threads; // %ld
67  long itrealvalue; // %ld
68  unsigned long long starttime; // %llu [before linux 2.6 %d]
69  unsigned long vsize; // %lu
70  long rss; // %ld
71  unsigned long rlim; // %lu
72  unsigned long startcode; // %lu
73  unsigned long endcode; // %lu
74  unsigned long startstack; // %lu
75  unsigned long kstkesp; // %lu
76  unsigned long kstkeip; // %lu
77  unsigned long signal; // %lu
78  unsigned long blocked; // %lu
79  unsigned long sigignore; // %lu
80  unsigned long sigcatch; // %lu
81  unsigned long wchan; // %lu
82  };
83 
84  class Fetcher {
85  public:
86  friend Fetcher& operator>>(Fetcher&, int&);
87  friend Fetcher& operator>>(Fetcher&, long&);
88  friend Fetcher& operator>>(Fetcher&, unsigned int&);
89  friend Fetcher& operator>>(Fetcher&, unsigned long&);
90  friend Fetcher& operator>>(Fetcher&, unsigned long long&);
91  friend Fetcher& operator>>(Fetcher&, char&);
92  friend Fetcher& operator>>(Fetcher&, std::string&);
93 
94  explicit Fetcher(char* buffer) :
95  buffer_(buffer),
96  save_(nullptr),
97  delims_(" \t\n\f\v\r") {
98  }
99  private:
100  int getInt() {
101  const char* t = getItem();
102  //std::cout <<"int '"<<t <<"'"<<std::endl;
103  return boost::lexical_cast<int>(t);
104  }
105  long getLong() {
106  const char* t = getItem();
107  //std::cout <<"long '"<<t <<"'"<<std::endl;
108  return boost::lexical_cast<long>(t);
109  }
110  unsigned int getUInt() {
111  const char* t = getItem();
112  //std::cout <<"uint '"<<t <<"'"<<std::endl;
113  return boost::lexical_cast<unsigned int>(t);
114  }
115  unsigned long getULong() {
116  const char* t = getItem();
117  //std::cout <<"ulong '"<<t <<"'"<<std::endl;
118  return boost::lexical_cast<unsigned long>(t);
119  }
120  unsigned long long getULongLong() {
121  const char* t = getItem();
122  //std::cout <<"ulong '"<<t <<"'"<<std::endl;
123  return boost::lexical_cast<unsigned long long>(t);
124  }
125  char getChar() {
126  return *getItem();
127  }
128  std::string getString() {
129  return std::string(getItem());
130  }
131  char* getItem() {
132  char* item = strtok_r(buffer_, delims_, &save());
133  assert(item);
134  buffer_ = nullptr; // Null for subsequent strtok_r calls.
135  return item;
136  }
137 
138  char const* save() const {return get_underlying_safe(save_);}
139  char*& save() {return get_underlying_safe(save_);}
140 
143  char const* const delims_;
144  };
145 
146  Fetcher& operator>>(Fetcher& iFetch, int& oValue) {
147  oValue = iFetch.getInt();
148  return iFetch;
149  }
150  Fetcher& operator>>(Fetcher& iFetch, long& oValue) {
151  oValue = iFetch.getLong();
152  return iFetch;
153  }
154  Fetcher& operator>>(Fetcher& iFetch, unsigned int& oValue) {
155  oValue = iFetch.getUInt();
156  return iFetch;
157  }
158  Fetcher& operator>>(Fetcher& iFetch, unsigned long& oValue) {
159  oValue = iFetch.getULong();
160  return iFetch;
161  }
162  Fetcher& operator>>(Fetcher& iFetch, unsigned long long& oValue) {
163  oValue = iFetch.getULongLong();
164  return iFetch;
165  }
166  Fetcher& operator>>(Fetcher& iFetch, char& oValue) {
167  oValue = iFetch.getChar();
168  return iFetch;
169  }
170  Fetcher& operator>>(Fetcher& iFetch, std::string& oValue) {
171  oValue = iFetch.getString();
172  return iFetch;
173  }
174 }
175 
176 namespace edm {
177  namespace service {
178 
179  ProcInfoFetcher::ProcInfoFetcher():
180  pg_size_(sysconf(_SC_PAGESIZE)) {
181 #ifdef __linux__
182  std::ostringstream ost;
183  ost << "/proc/" << getpid() << "/stat";
184 
185  if((fd_ = open(ost.str().c_str(), O_RDONLY)) < 0) {
187  << "Failed to open " << ost.str() << std::endl;
188  }
189 #endif
190  }
192 #ifdef LINUX
193  close(fd_);
194 #endif
195  }
197  ProcInfo ret;
198 
199 #ifdef __linux__
200  double pr_size = 0.0, pr_rssize = 0.0;
201 
202  linux_proc pinfo;
203  int cnt;
204 
205  lseek(fd_, 0, SEEK_SET);
206 
207  if((cnt = read(fd_, buf_, sizeof(buf_) - 1)) < 0) {
208  perror("Read of Proc file failed:");
209  return ProcInfo();
210  }
211 
212  if(cnt > 0) {
213  buf_[cnt] = '\0';
214 
215 
216  try {
217  Fetcher fetcher(buf_);
218  fetcher >> pinfo.pid
219  >> pinfo.comm
220  >> pinfo.state
221  >> pinfo.ppid
222  >> pinfo.pgrp
223  >> pinfo.session
224  >> pinfo.tty
225  >> pinfo.tpgid
226  >> pinfo.flags
227  >> pinfo.minflt
228  >> pinfo.cminflt
229  >> pinfo.majflt
230  >> pinfo.cmajflt
231  >> pinfo.utime
232  >> pinfo.stime
233  >> pinfo.cutime
234  >> pinfo.cstime
235  >> pinfo.priority
236  >> pinfo.nice
237  >> pinfo.num_threads
238  >> pinfo.itrealvalue
239  >> pinfo.starttime
240  >> pinfo.vsize
241  >> pinfo.rss
242  >> pinfo.rlim
243  >> pinfo.startcode
244  >> pinfo.endcode
245  >> pinfo.startstack
246  >> pinfo.kstkesp
247  >> pinfo.kstkeip
248  >> pinfo.signal
249  >> pinfo.blocked
250  >> pinfo.sigignore
251  >> pinfo.sigcatch
252  >> pinfo.wchan;
253  } catch (boost::bad_lexical_cast& iE) {
254  LogWarning("ProcInfoFetcher")<<"Parsing of Prof file failed:"<<iE.what()<<std::endl;
255  return ProcInfo();
256  }
257 
258  // resident set size in pages
259  pr_size = (double)pinfo.vsize;
260  pr_rssize = (double)pinfo.rss;
261 
262  ret.vsize = pr_size / (1024.0*1024.0);
263  ret.rss = (pr_rssize * pg_size_) / (1024.0*1024.0);
264  }
265 #else
266  ret.vsize = 0;
267  ret.rss = 0;
268 #endif
269  return ret;
270  }
271  }
272 }
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
int getInt(ResultSet *rset, int ipar)
#define nullptr
std::shared_ptr< T > & get_underlying_safe(propagate_const< std::shared_ptr< T >> &iP)
std::istream & operator>>(std::istream &input, CLHEP::HepGenMatrix &matrix)
Definition: matrixSaver.cc:111
HLT enums.
save
Definition: cuy.py:1164