MarlinMT  0.1.0
MemoryMonitorProcessor.cc
Go to the documentation of this file.
1 
2 // -- marlinmt headers
3 #include <marlinmt/Processor.h>
5 
6 // -- std headers
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #ifdef __APPLE__
12 #include <sys/sysctl.h>
13 #include <mach/mach.h>
14 #else
15 #include <sys/sysinfo.h>
16 #endif
17 
18 namespace marlinmt {
19 
32  public:
37 
38  // from Processor
39  void init() override ;
40  void processEvent( EventStore * evt ) override ;
41 
42  protected:
43  UIntParameter _howOften {*this, "howOften",
44  "Print event number every N events", 1 } ;
45 
47  unsigned int _eventNumber {0} ;
48  };
49 
50  //--------------------------------------------------------------------------
51  //--------------------------------------------------------------------------
52 
54  Processor("MemoryMonitor") {
55  // modify processor description
56  setDescription( "Simple processor to print out the memory consumption at defined intervals" ) ;
57  // It doesn't make sense to create clones of this processor in MT environement
58  // So we don't duplicate it per thread. To make it thread-safe we thus need to lock
59  // make it thread-safe by using a lock
61  // don't duplicate opening/writing of output file
63  }
64 
65  //--------------------------------------------------------------------------
66 
68  // Print the initial parameters
69  printParameters() ;
70  }
71 
72  //--------------------------------------------------------------------------
73 
75  if (_eventNumber % _howOften == 0) {
76 
77 #ifdef __APPLE__
78  struct task_basic_info t_info;
79  mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
80 
81  if (KERN_SUCCESS != task_info(mach_task_self(),
82  TASK_BASIC_INFO, (task_info_t)&t_info,
83  &t_info_count)) {
84  log<ERROR>() << "Problem accessing system information from MacOS X!"<< std::endl ;
85  }
86  log<MESSAGE>() << " Processed event " << _eventNumber
87  << " Resident size is: "<< t_info.resident_size<<" virtual size: "<<t_info.virtual_size
88  << std::endl ;
89 #else
90  struct sysinfo memInfo;
91  sysinfo (&memInfo);
92  unsigned long physMemUsed = memInfo.totalram - memInfo.freeram;
93  log<MESSAGE>() << " Processed event " << _eventNumber
94  << " Physical memory in use: " << physMemUsed
95  << std::endl ;
96 #endif
97  }
98  _eventNumber++ ;
99  }
100 
101  // plugin declaration
103 }
void setRuntimeOption(ERuntimeOption option, bool value)
Force the runtime option to a given boolean value.
Definition: Processor.cc:37
void init() override
Initialize the processor.
MemoryMonitorProcessor is a memory monitoring application for Marlin.
UIntParameter _howOften
Event counter.
#define MARLINMT_DECLARE_PROCESSOR(Class)
Definition: PluginManager.h:34
void setDescription(const std::string &desc)
Set the component description.
Definition: Component.cc:41
Whether the processor has to be executed in a critical section.
Processor class.
Definition: Processor.h:43
void printParameters() const
Print the component parameters.
Definition: Component.cc:118
void processEvent(EventStore *evt) override
Process an input event.
EventStore class.
Definition: EventStore.h:17