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 // $Id: ProcInfoFetcher.cc,v 1.1 2012/05/06 19:11:56 chrjones Exp $
12 //
13 
14 // system include files
15 
16 // user include files
17 
18 #include <cstring>
19 #include <cassert>
20 #include <iostream>
21 #ifdef __linux__
22 #include <malloc.h>
23 #endif
24 #include <sstream>
25 //#include <stdio.h>
26 #include <string>
27 #include <boost/lexical_cast.hpp>
28 
29 #include <fcntl.h>
30 #include <unistd.h>
31 
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_(0),
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_ = 0; // Null for subsequent strtok_r calls.
135  return item;
136  }
137  char* buffer_;
138  char* save_;
139  char const* const delims_;
140  };
141 
142  Fetcher& operator>>(Fetcher& iFetch, int& oValue) {
143  oValue = iFetch.getInt();
144  return iFetch;
145  }
146  Fetcher& operator>>(Fetcher& iFetch, long& oValue) {
147  oValue = iFetch.getLong();
148  return iFetch;
149  }
150  Fetcher& operator>>(Fetcher& iFetch, unsigned int& oValue) {
151  oValue = iFetch.getUInt();
152  return iFetch;
153  }
154  Fetcher& operator>>(Fetcher& iFetch, unsigned long& oValue) {
155  oValue = iFetch.getULong();
156  return iFetch;
157  }
158  Fetcher& operator>>(Fetcher& iFetch, unsigned long long& oValue) {
159  oValue = iFetch.getULongLong();
160  return iFetch;
161  }
162  Fetcher& operator>>(Fetcher& iFetch, char& oValue) {
163  oValue = iFetch.getChar();
164  return iFetch;
165  }
166  Fetcher& operator>>(Fetcher& iFetch, std::string& oValue) {
167  oValue = iFetch.getString();
168  return iFetch;
169  }
170 }
171 
172 namespace edm {
173  namespace service {
174 
176  pg_size_(sysconf(_SC_PAGESIZE)) {
177 #ifdef __linux__
178  std::ostringstream ost;
179  ost << "/proc/" << getpid() << "/stat";
180 
181  if((fd_ = open(ost.str().c_str(), O_RDONLY)) < 0) {
183  << "Failed to open " << ost.str() << std::endl;
184  }
185 #endif
186  }
188 #ifdef LINUX
189  close(fd_);
190 #endif
191  }
193  ProcInfo ret;
194 
195 #ifdef __linux__
196  double pr_size = 0.0, pr_rssize = 0.0;
197 
198  linux_proc pinfo;
199  int cnt;
200 
201  lseek(fd_, 0, SEEK_SET);
202 
203  if((cnt = read(fd_, buf_, sizeof(buf_) - 1)) < 0) {
204  perror("Read of Proc file failed:");
205  return ProcInfo();
206  }
207 
208  if(cnt > 0) {
209  buf_[cnt] = '\0';
210 
211 
212  try {
213  Fetcher fetcher(buf_);
214  fetcher >> pinfo.pid
215  >> pinfo.comm
216  >> pinfo.state
217  >> pinfo.ppid
218  >> pinfo.pgrp
219  >> pinfo.session
220  >> pinfo.tty
221  >> pinfo.tpgid
222  >> pinfo.flags
223  >> pinfo.minflt
224  >> pinfo.cminflt
225  >> pinfo.majflt
226  >> pinfo.cmajflt
227  >> pinfo.utime
228  >> pinfo.stime
229  >> pinfo.cutime
230  >> pinfo.cstime
231  >> pinfo.priority
232  >> pinfo.nice
233  >> pinfo.num_threads
234  >> pinfo.itrealvalue
235  >> pinfo.starttime
236  >> pinfo.vsize
237  >> pinfo.rss
238  >> pinfo.rlim
239  >> pinfo.startcode
240  >> pinfo.endcode
241  >> pinfo.startstack
242  >> pinfo.kstkesp
243  >> pinfo.kstkeip
244  >> pinfo.signal
245  >> pinfo.blocked
246  >> pinfo.sigignore
247  >> pinfo.sigcatch
248  >> pinfo.wchan;
249  } catch (boost::bad_lexical_cast& iE) {
250  LogWarning("ProcInfoFetcher")<<"Parsing of Prof file failed:"<<iE.what()<<std::endl;
251  return ProcInfo();
252  }
253 
254  // resident set size in pages
255  pr_size = (double)pinfo.vsize;
256  pr_rssize = (double)pinfo.rss;
257 
258  ret.vsize = pr_size / (1024.0*1024.0);
259  ret.rss = (pr_rssize * pg_size_) / (1024.0*1024.0);
260  }
261 #else
262  ret.vsize = 0;
263  ret.rss = 0;
264 #endif
265  return ret;
266  }
267  }
268 }
num priority
Definition: procUtils.cc:93
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
int getInt(ResultSet *rset, int ipar)
tuple starttime
Definition: lumiContext.py:322
num cutime
Definition: procUtils.cc:91
num cstime
Definition: procUtils.cc:92
num sigcatch
Definition: procUtils.cc:112
char state
Definition: procUtils.cc:75
std::istream & operator>>(std::istream &input, CLHEP::HepGenMatrix &matrix)
Definition: matrixSaver.cc:111
num num_threads
Definition: procUtils.cc:95