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 long flags; // %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  int starttime; // %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&, 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  char getChar() {
120  return *getItem();
121  }
122  std::string getString() {
123  return std::string(getItem());
124  }
125  char* getItem() {
126  char* item = strtok_r(buffer_, delims_, &save_);
127  assert(item);
128  buffer_ = 0; // Null for subsequent strtok_r calls.
129  return item;
130  }
131  char* buffer_;
132  char* save_;
133  char const* const delims_;
134  };
135 
136  Fetcher& operator>>(Fetcher& iFetch, int& oValue) {
137  oValue = iFetch.getInt();
138  return iFetch;
139  }
140  Fetcher& operator>>(Fetcher& iFetch, long& oValue) {
141  oValue = iFetch.getLong();
142  return iFetch;
143  }
144  Fetcher& operator>>(Fetcher& iFetch, unsigned int& oValue) {
145  oValue = iFetch.getUInt();
146  return iFetch;
147  }
148  Fetcher& operator>>(Fetcher& iFetch, unsigned long& oValue) {
149  oValue = iFetch.getULong();
150  return iFetch;
151  }
152  Fetcher& operator>>(Fetcher& iFetch, char& oValue) {
153  oValue = iFetch.getChar();
154  return iFetch;
155  }
156  Fetcher& operator>>(Fetcher& iFetch, std::string& oValue) {
157  oValue = iFetch.getString();
158  return iFetch;
159  }
160 }
161 
162 namespace edm {
163  namespace service {
164 
166  pg_size_(sysconf(_SC_PAGESIZE)) {
167 #ifdef __linux__
168  std::ostringstream ost;
169  ost << "/proc/" << getpid() << "/stat";
170 
171  if((fd_ = open(ost.str().c_str(), O_RDONLY)) < 0) {
173  << "Failed to open " << ost.str() << std::endl;
174  }
175 #endif
176  }
178 #ifdef LINUX
179  close(fd_);
180 #endif
181  }
183  ProcInfo ret;
184 
185 #ifdef __linux__
186  double pr_size = 0.0, pr_rssize = 0.0;
187 
188  linux_proc pinfo;
189  int cnt;
190 
191  lseek(fd_, 0, SEEK_SET);
192 
193  if((cnt = read(fd_, buf_, sizeof(buf_) - 1)) < 0) {
194  perror("Read of Proc file failed:");
195  return ProcInfo();
196  }
197 
198  if(cnt > 0) {
199  buf_[cnt] = '\0';
200 
201 
202  try {
203  Fetcher fetcher(buf_);
204  fetcher >> pinfo.pid
205  >> pinfo.comm
206  >> pinfo.state
207  >> pinfo.ppid
208  >> pinfo.pgrp
209  >> pinfo.session
210  >> pinfo.tty
211  >> pinfo.tpgid
212  >> pinfo.flags
213  >> pinfo.minflt
214  >> pinfo.cminflt
215  >> pinfo.majflt
216  >> pinfo.cmajflt
217  >> pinfo.utime
218  >> pinfo.stime
219  >> pinfo.cutime
220  >> pinfo.cstime
221  >> pinfo.priority
222  >> pinfo.nice
223  >> pinfo.num_threads
224  >> pinfo.itrealvalue
225  >> pinfo.starttime
226  >> pinfo.vsize
227  >> pinfo.rss
228  >> pinfo.rlim
229  >> pinfo.startcode
230  >> pinfo.endcode
231  >> pinfo.startstack
232  >> pinfo.kstkesp
233  >> pinfo.kstkeip
234  >> pinfo.signal
235  >> pinfo.blocked
236  >> pinfo.sigignore
237  >> pinfo.sigcatch
238  >> pinfo.wchan;
239  } catch (boost::bad_lexical_cast& iE) {
240  LogWarning("ProcInfoFetcher")<<"Parsing of Prof file failed:"<<iE.what()<<std::endl;
241  return ProcInfo();
242  }
243 
244  // resident set size in pages
245  pr_size = (double)pinfo.vsize;
246  pr_rssize = (double)pinfo.rss;
247 
248  ret.vsize = pr_size / (1024.0*1024.0);
249  ret.rss = (pr_rssize * pg_size_) / (1024.0*1024.0);
250  }
251 #else
252  ret.vsize = 0;
253  ret.rss = 0;
254 #endif
255  return ret;
256  }
257  }
258 }
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