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