CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
34 
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) :
94  buffer_(buffer),
95  save_(0),
96  delims_(" \t\n\f\v\r") {
97  }
98  private:
99  int getInt() {
100  const char* t = getItem();
101  //std::cout <<"int '"<<t <<"'"<<std::endl;
102  return boost::lexical_cast<int>(t);
103  }
104  long getLong() {
105  const char* t = getItem();
106  //std::cout <<"long '"<<t <<"'"<<std::endl;
107  return boost::lexical_cast<long>(t);
108  }
109  unsigned int getUInt() {
110  const char* t = getItem();
111  //std::cout <<"uint '"<<t <<"'"<<std::endl;
112  return boost::lexical_cast<unsigned int>(t);
113  }
114  unsigned long getULong() {
115  const char* t = getItem();
116  //std::cout <<"ulong '"<<t <<"'"<<std::endl;
117  return boost::lexical_cast<unsigned long>(t);
118  }
119  unsigned long long getULongLong() {
120  const char* t = getItem();
121  //std::cout <<"ulong '"<<t <<"'"<<std::endl;
122  return boost::lexical_cast<unsigned long long>(t);
123  }
124  char getChar() {
125  return *getItem();
126  }
127  std::string getString() {
128  return std::string(getItem());
129  }
130  char* getItem() {
131  char* item = strtok_r(buffer_, delims_, &save_);
132  assert(item);
133  buffer_ = 0; // Null for subsequent strtok_r calls.
134  return item;
135  }
136  char* buffer_;
137  char* save_;
138  char const* const delims_;
139  };
140 
141  Fetcher& operator>>(Fetcher& iFetch, int& oValue) {
142  oValue = iFetch.getInt();
143  return iFetch;
144  }
145  Fetcher& operator>>(Fetcher& iFetch, long& oValue) {
146  oValue = iFetch.getLong();
147  return iFetch;
148  }
149  Fetcher& operator>>(Fetcher& iFetch, unsigned int& oValue) {
150  oValue = iFetch.getUInt();
151  return iFetch;
152  }
153  Fetcher& operator>>(Fetcher& iFetch, unsigned long& oValue) {
154  oValue = iFetch.getULong();
155  return iFetch;
156  }
157  Fetcher& operator>>(Fetcher& iFetch, unsigned long long& oValue) {
158  oValue = iFetch.getULongLong();
159  return iFetch;
160  }
161  Fetcher& operator>>(Fetcher& iFetch, char& oValue) {
162  oValue = iFetch.getChar();
163  return iFetch;
164  }
165  Fetcher& operator>>(Fetcher& iFetch, std::string& oValue) {
166  oValue = iFetch.getString();
167  return iFetch;
168  }
169 }
170 
171 namespace edm {
172  namespace service {
173 
175  pg_size_(sysconf(_SC_PAGESIZE)) {
176 #ifdef __linux__
177  std::ostringstream ost;
178  ost << "/proc/" << getpid() << "/stat";
179 
180  if((fd_ = open(ost.str().c_str(), O_RDONLY)) < 0) {
182  << "Failed to open " << ost.str() << std::endl;
183  }
184 #endif
185  }
187 #ifdef LINUX
188  close(fd_);
189 #endif
190  }
192  ProcInfo ret;
193 
194 #ifdef __linux__
195  double pr_size = 0.0, pr_rssize = 0.0;
196 
197  linux_proc pinfo;
198  int cnt;
199 
200  lseek(fd_, 0, SEEK_SET);
201 
202  if((cnt = read(fd_, buf_, sizeof(buf_) - 1)) < 0) {
203  perror("Read of Proc file failed:");
204  return ProcInfo();
205  }
206 
207  if(cnt > 0) {
208  buf_[cnt] = '\0';
209 
210 
211  try {
212  Fetcher fetcher(buf_);
213  fetcher >> pinfo.pid
214  >> pinfo.comm
215  >> pinfo.state
216  >> pinfo.ppid
217  >> pinfo.pgrp
218  >> pinfo.session
219  >> pinfo.tty
220  >> pinfo.tpgid
221  >> pinfo.flags
222  >> pinfo.minflt
223  >> pinfo.cminflt
224  >> pinfo.majflt
225  >> pinfo.cmajflt
226  >> pinfo.utime
227  >> pinfo.stime
228  >> pinfo.cutime
229  >> pinfo.cstime
230  >> pinfo.priority
231  >> pinfo.nice
232  >> pinfo.num_threads
233  >> pinfo.itrealvalue
234  >> pinfo.starttime
235  >> pinfo.vsize
236  >> pinfo.rss
237  >> pinfo.rlim
238  >> pinfo.startcode
239  >> pinfo.endcode
240  >> pinfo.startstack
241  >> pinfo.kstkesp
242  >> pinfo.kstkeip
243  >> pinfo.signal
244  >> pinfo.blocked
245  >> pinfo.sigignore
246  >> pinfo.sigcatch
247  >> pinfo.wchan;
248  } catch (boost::bad_lexical_cast& iE) {
249  LogWarning("ProcInfoFetcher")<<"Parsing of Prof file failed:"<<iE.what()<<std::endl;
250  return ProcInfo();
251  }
252 
253  // resident set size in pages
254  pr_size = (double)pinfo.vsize;
255  pr_rssize = (double)pinfo.rss;
256 
257  ret.vsize = pr_size / (1024.0*1024.0);
258  ret.rss = (pr_rssize * pg_size_) / (1024.0*1024.0);
259  }
260 #else
261  ret.vsize = 0;
262  ret.rss = 0;
263 #endif
264  return ret;
265  }
266  }
267 }
assert(m_qm.get())
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
int getInt(ResultSet *rset, int ipar)
tuple starttime
Definition: lumiContext.py:322
tuple pid
Definition: sysUtil.py:22
std::istream & operator>>(std::istream &input, CLHEP::HepGenMatrix &matrix)
Definition: matrixSaver.cc:111