DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4DetectorConstructionResources.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // \author Markus Frank
11 // \date 2015-11-09
12 //
13 //==========================================================================
14 
15 // Framework include files
17 #include <unistd.h>
18 
19 // C/C++ include files
20 
22 namespace dd4hep {
23 
25  namespace sim {
26 
28 
36  public:
38 
43  class __attribute__((__packed__)) StatusProcess {
44  public:
45  char comm[399];
46  char state;
47  int umask;
48  int tgid;
49  int ngid;
50  int pid;
51  int ppid;
52  int uid;
53  int gid;
54  int utrace;
55  int fdSize;
56  long vmPeak;
57  long vmSize;
58  long vmLock;
59  long vmPin;
60  long vmHWM;
61  long vmRSS;
62  long vmRSSano;
63  long vmRSSfil;
64  long vmRSSshm;
65  long vmData;
66  long vmStack;
67  long vmExe;
68  long vmLib;
69  long vmPTE;
70  long vmSwap;
72  StatusProcess() {}
73  };
74  std::unique_ptr<StatusProcess> snapshot;
75  std::string when { "geometry|sensitives" };
76 
77  void print_status(const char* tag, const StatusProcess& sp) const;
78 
79  public:
81  Geant4DetectorConstructionResources(Geant4Context* ctxt, const std::string& nam);
85  virtual void constructGeo(Geant4DetectorConstructionContext*) override;
87  virtual void constructSensitives(Geant4DetectorConstructionContext* ctxt) override;
88  };
89  } // End namespace sim
90 } // End namespace dd4hep
91 
92 
93 // Framework include files
94 #include <DD4hep/InstanceCount.h>
95 #include <DD4hep/Printout.h>
96 #include <DD4hep/Plugins.h>
97 #include <DD4hep/Detector.h>
98 
99 #include <DDG4/Geant4Mapping.h>
100 #include <DDG4/Geant4Kernel.h>
101 #include <DDG4/Factories.h>
102 
103 #include <stdexcept>
104 #include <sys/types.h>
105 #include <sys/stat.h>
106 #include <fcntl.h>
107 
108 using namespace dd4hep::sim;
109 
111 
112 namespace {
113 
114  class SysFile {
115  public:
116  class FileDescriptor {
118  int m_fd;
119 
120  public:
122  FileDescriptor(int value) : m_fd(value) {}
124  ~FileDescriptor();
126  int get() const { return m_fd; }
127  };
128 
129 
130  public:
132  std::string m_name;
134  SysFile(const char* name) : m_name(name) {}
136  SysFile(const std::string& name) : m_name(name) {}
138  ~SysFile() {}
140  int read(char* buffer, size_t len) const;
141  };
142 
144  SysFile::FileDescriptor::~FileDescriptor() {
145  if (m_fd > 0) ::close(m_fd);
146  m_fd = 0;
147  }
148 
150  int SysFile::read(char* buf, size_t siz) const {
151  FileDescriptor fd(::open(m_name.c_str(),O_RDONLY));
152  if( fd.get() < 0 ) {
153  std::string err = "Failed to open "+m_name+" ";
154  throw std::runtime_error(err+std::make_error_code(std::errc(errno)).message());
155  }
156  std::size_t tmp = 0;
157  while ( tmp < siz ) {
158  int sc = ::read(fd.get(),buf+tmp,siz-tmp);
159  if ( sc > 0 ) {
160  tmp += sc;
161  }
162  else if ( sc == 0 ) {
163  buf[tmp] = 0;
164  return tmp;
165  }
166  else if ( errno == EINTR ) {
167  printf("EINTR~!!!!\n");
168  continue;
169  }
170  else {
171  break;
172  }
173  }
174  if ( tmp != siz ) {
175  std::string err = "Read of system file "+m_name+" failed:";
176  throw std::runtime_error(err+std::make_error_code(std::errc(errno)).message());
177  }
178  return tmp;
179  }
180 
181  int read_info(Geant4DetectorConstructionResources::StatusProcess& proc, int proc_id) {
182  char buff[2048], *ptr=buff;
183  std::string fn = "/proc/"+std::to_string(proc_id)+"/status";
184  int nitem=0, cnt=SysFile(fn.c_str()).read(buff,sizeof(buff));
185  if(cnt>0) {
186  int ival;
187  long int lval;
188 
189  while(ptr && ptr<(buff+cnt)) {
190  char* p = ::strchr(ptr,'\t');
191  char* end = ::strchr(ptr,'\n');
192 
193  ptr = (end) ? end+1 : 0;
194  if ( 0 == p ) continue;
195  ++p;
196  switch(++nitem) {
197  case 1: ::sscanf(p,"%s",proc.comm); break;
198  case 2: ::sscanf(p,"%d",&ival); proc.umask = ival; break;
199  case 3: ::sscanf(p,"%c",&proc.state); break;
200  case 4: ::sscanf(p,"%d",&ival); proc.tgid = ival; break;
201  case 5: ::sscanf(p,"%d",&ival); proc.ngid = ival; break;
202  case 6: ::sscanf(p,"%d",&ival); proc.pid = ival; break;
203  case 7: ::sscanf(p,"%d",&ival); proc.ppid = ival; break;
204  case 8: ::sscanf(p,"%d",&ival); proc.utrace = ival; break;
205  case 9: ::sscanf(p,"%d",&ival); proc.uid = ival; break;
206  case 10: ::sscanf(p,"%d",&ival); proc.gid = ival; break;
207  case 11: ::sscanf(p,"%d",&ival); proc.fdSize = ival; break;
208  case 17: ::sscanf(p,"%ld",&lval); proc.vmPeak = lval; break;
209  case 18: ::sscanf(p,"%ld",&lval); proc.vmSize = lval; break;
210  case 19: ::sscanf(p,"%ld",&lval); proc.vmLock = lval; break;
211  case 20: ::sscanf(p,"%ld",&lval); proc.vmPin = lval; break;
212  case 21: ::sscanf(p,"%ld",&lval); proc.vmHWM = lval; break;
213  case 22: ::sscanf(p,"%ld",&lval); proc.vmRSS = lval; break;
214  case 23: ::sscanf(p,"%ld",&lval); proc.vmRSSano= lval; break;
215  case 24: ::sscanf(p,"%ld",&lval); proc.vmRSSfil= lval; break;
216  case 25: ::sscanf(p,"%ld",&lval); proc.vmRSSshm= lval; break;
217  case 26: ::sscanf(p,"%ld",&lval); proc.vmData = lval; break;
218  case 27: ::sscanf(p,"%ld",&lval); proc.vmStack = lval; break;
219  case 28: ::sscanf(p,"%ld",&lval); proc.vmExe = lval; break;
220  case 29: ::sscanf(p,"%ld",&lval); proc.vmLib = lval; break;
221  case 30: ::sscanf(p,"%ld",&lval); proc.vmPTE = lval; break;
222  case 31: ::sscanf(p,"%ld",&lval); proc.vmSwap = lval; break;
223  case 32: return 1;
224  default: break;
225  }
226  }
227  return 1;
228  }
229  return 0;
230  }
231 }
232 
235 : Geant4DetectorConstruction(ctxt,nam)
236 {
237  declareProperty("When", this->when);
239 }
240 
244 }
245 
248  if ( this->when.find("geometry") != std::string::npos ) {
249  this->snapshot = std::make_unique<StatusProcess>();
250  read_info(*this->snapshot, ::getpid());
251  this->print_status("ConstructGeo: ", *this->snapshot);
252  }
253 }
254 
255 void Geant4DetectorConstructionResources::print_status(const char* tag, const StatusProcess& sp) const {
256  this->always("%s Name: \t%s", tag, sp.comm);
257  this->always("%s State: \t%c", tag, sp.state);
258  this->always("%s Umask: \t%8d", tag, sp.umask);
259  this->always("%s Tgid: \t%8d", tag, sp.tgid);
260  this->always("%s Pid: \t%8d", tag, sp.pid);
261  this->always("%s PPid: \t%8d", tag, sp.ppid);
262  this->always("%s utrace: \t%8d", tag, sp.utrace);
263  this->always("%s Uid: \t%8d", tag, sp.uid);
264  this->always("%s Gid: \t%8d", tag, sp.gid);
265  this->always("%s FDSize: \t%8d", tag, sp.fdSize);
266  this->always("%s VmPeak: \t%8ld kB", tag, sp.vmPeak);
267  this->always("%s VmSize: \t%8ld kB", tag, sp.vmSize);
268  this->always("%s VmLck: \t%8ld kB", tag, sp.vmLock);
269  this->always("%s VmHWM: \t%8ld kB", tag, sp.vmHWM);
270  this->always("%s VmRSS: \t%8ld kB", tag, sp.vmRSS);
271  this->always("%s VmRSS anon: \t%8ld kB", tag, sp.vmRSSano);
272  this->always("%s VmRSS file: \t%8ld kB", tag, sp.vmRSSfil);
273  this->always("%s VmRSS shm: \t%8ld kB", tag, sp.vmRSSshm);
274  this->always("%s VmData: \t%8ld kB", tag, sp.vmData);
275  this->always("%s VmStk: \t%8ld kB", tag, sp.vmStack);
276  this->always("%s VmExe: \t%8ld kB", tag, sp.vmExe);
277  this->always("%s VmLib: \t%8ld kB", tag, sp.vmLib);
278  this->always("%s VmPTE: \t%8ld kB", tag, sp.vmPTE);
279 }
280 
283  if ( this->when.find("sensitives") != std::string::npos ) {
284  StatusProcess rd;
285  read_info(rd, ::getpid());
286  this->print_status("ConstructSD: ", rd);
287 #if 0
288  if ( snapshot ) {
289  const auto& snap = *this->snapshot;
290  this->always(" --> DIFFERENCE: FDSize: \t%8d", rd.fdSize - snap.fdSize);
291  this->always(" --> DIFFERENCE: VmPeak: \t%8ld kB", rd.vmPeak - snap.vmPeak);
292  this->always(" --> DIFFERENCE: VmSize: \t%8ld kB", rd.vmSize - snap.vmSize);
293  this->always(" --> DIFFERENCE: VmLck: \t%8ld kB", rd.vmLock - snap.vmLock);
294  this->always(" --> DIFFERENCE: VmHWM: \t%8ld kB", rd.vmHWM - snap.vmHWM);
295  this->always(" --> DIFFERENCE: VmRSS: \t%8ld kB", rd.vmRSS - snap.vmRSS);
296  this->always(" --> DIFFERENCE: VmRSS anon: \t%8ld kB", rd.vmRSSano - snap.vmRSSano);
297  this->always(" --> DIFFERENCE: VmRSS file: \t%8ld kB", rd.vmRSSfil - snap.vmRSSfil);
298  this->always(" --> DIFFERENCE: VmRSS shm: \t%8ld kB", rd.vmRSSshm - snap.vmRSSshm);
299  this->always(" --> DIFFERENCE: VmData: \t%8ld kB", rd.vmData - snap.vmData);
300  this->always(" --> DIFFERENCE: VmStk: \t%8ld kB", rd.vmStack - snap.vmStack);
301  this->always(" --> DIFFERENCE: VmExe: \t%8ld kB", rd.vmExe - snap.vmExe);
302  this->always(" --> DIFFERENCE: VmLib: \t%8ld kB", rd.vmLib - snap.vmLib);
303  this->always(" --> DIFFERENCE: VmPTE: \t%8ld kB", rd.vmPTE - snap.vmPTE);
304  }
305 #endif
306  }
307  snapshot.reset();
308 }
Geant4DetectorConstruction.h
dd4hep::sim::Geant4DetectorConstructionResources::print_status
void print_status(const char *tag, const StatusProcess &sp) const
Definition: Geant4DetectorConstructionResources.cpp:255
dd4hep::sim::Geant4DetectorConstructionResources
Debug class to dump resources usage during detector construction.
Definition: Geant4DetectorConstructionResources.cpp:35
Geant4Mapping.h
Detector.h
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
DECLARE_GEANT4ACTION
#define DECLARE_GEANT4ACTION(name)
Plugin defintion to create Geant4Action objects.
Definition: Factories.h:210
dd4hep::sim::Geant4DetectorConstructionContext
Geant4 detector construction context definition.
Definition: Geant4DetectorConstruction.h:61
dd4hep::sim::Geant4DetectorConstructionResources::__attribute__
class __attribute__((__packed__)) StatusProcess
Class to store the status information of a process from /proc/<pid>/status.
Definition: Geant4DetectorConstructionResources.cpp:43
dd4hep::sim::Geant4DetectorConstructionResources::snapshot
std::unique_ptr< StatusProcess > snapshot
Definition: Geant4DetectorConstructionResources.cpp:73
dd4hep::sim::Geant4DetectorConstructionResources::when
std::string when
Definition: Geant4DetectorConstructionResources.cpp:75
dd4hep::sim::Geant4Action::declareProperty
Geant4Action & declareProperty(const std::string &nam, T &val)
Declare property.
Definition: Geant4Action.h:366
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
dd4hep::sim::Geant4DetectorConstructionResources::Geant4DetectorConstructionResources
Geant4DetectorConstructionResources(Geant4Context *ctxt, const std::string &nam)
Initializing constructor for DDG4.
Definition: Geant4DetectorConstructionResources.cpp:234
Plugins.h
dd4hep::sim::Geant4DetectorConstructionResources::~Geant4DetectorConstructionResources
virtual ~Geant4DetectorConstructionResources()
Default destructor.
Definition: Geant4DetectorConstructionResources.cpp:242
Factories.h
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: Geant4Output2EDM4hep.cpp:49
dd4hep::sim::Geant4DetectorConstructionResources::constructSensitives
virtual void constructSensitives(Geant4DetectorConstructionContext *ctxt) override
Sensitives construction callback. Called at "ConstructSDandField()".
Definition: Geant4DetectorConstructionResources.cpp:282
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::sim::Geant4Action::always
void always(const char *fmt,...) const
Support of always printed messages.
Definition: Geant4Action.cpp:199
Geant4Kernel.h
InstanceCount.h
dd4hep::sim::Geant4DetectorConstruction
Basic implementation of the Geant4 detector construction action.
Definition: Geant4DetectorConstruction.h:102
Printout.h
dd4hep::sim::Geant4Context
Generic context to extend user, run and event information.
Definition: Geant4Context.h:201
dd4hep::sim::Geant4DetectorConstructionResources::constructGeo
virtual void constructGeo(Geant4DetectorConstructionContext *) override
Sensitive detector construction callback. Called at "Construct()".
Definition: Geant4DetectorConstructionResources.cpp:247