All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
JemallocProfile.cpp
Go to the documentation of this file.
1 // Include files
2 
3 // local
4 #include "JemallocProfile.h"
5 
6 // including jemmalloc.h is difficult as the malloc signature is not exactly identical
7 // to the system one (issue with throw).
8 // We therefore declare mallctl here.
9 
10 extern "C"
11 {
12  int mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
13 }
14 
15 //-----------------------------------------------------------------------------
16 // Implementation file for class : JemallocProfile
17 //
18 // 2015-06-09 : Ben Couturier
19 //-----------------------------------------------------------------------------
20 
21 // Declaration of the Algorithm Factory
23 
24 
25 //=============================================================================
26 // Standard constructor, initializes variables
27 //=============================================================================
29  ISvcLocator* pSvcLocator)
30 : GaudiAlgorithm ( name , pSvcLocator ), m_profiling(false), m_eventNumber (0)
31 {
32 
33  declareProperty("StartFromEventN", m_nStartFromEvent = 1,
34  "After what event we start profiling. "
35  );
36 
37  declareProperty("StopAtEventN", m_nStopAtEvent = 0,
38  "After what event we stop profiling. "
39  "If 0 than we also profile finalization stage. Default = 0."
40  );
41 
42  declareProperty("DumpPeriod", m_dumpPeriod = 100,
43  "Period for dumping head to a file. Default=100"
44  );
45 
46 }
47 //=============================================================================
48 // Destructor
49 //=============================================================================
51 
52 //=============================================================================
53 // Initialization
54 //=============================================================================
56  StatusCode sc = GaudiAlgorithm::initialize(); // must be executed first
57  if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
58 
59  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Initialize" << endmsg;
60  bool active = true;
61  int res = mallctl("prof.active", NULL, NULL, &active, sizeof(active));
62  if (res != 0)
63  {
64  return StatusCode::FAILURE;
65  }
66  return StatusCode::SUCCESS;
67 }
68 
69 //=============================================================================
70 // Main execution
71 //=============================================================================
73 
74  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Execute" << endmsg;
75 
76  // Increase event number
77  m_eventNumber += 1;
78 
80  {
81  m_profiling = true;
82  info() << "Starting Jemalloc profile at event "
83  << m_eventNumber << endmsg;
84  }
85 
87  {
88  info() << "Jemalloc Dumping heap at event "
89  << m_eventNumber << endmsg;
90  mallctl("prof.dump", NULL, NULL, NULL, 0);
91  }
92 
94  {
95  m_profiling = false;
96  info() << "Stopping Jemalloc profile at event "
97  << m_eventNumber << endmsg;
98  mallctl("prof.dump", NULL, NULL, NULL, 0);
99  }
100  return StatusCode::SUCCESS;
101 }
102 
103 //=============================================================================
104 // Finalize
105 //=============================================================================
107 
108  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Finalize" << endmsg;
109  return GaudiAlgorithm::finalize(); // must be called after all other actions
110 }
111 
112 //=============================================================================
#define DECLARE_ALGORITHM_FACTORY(x)
Definition: Algorithm.h:946
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
virtual StatusCode finalize()
Algorithm finalization.
StatusCode initialize() override
standard initialization method
STL namespace.
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
virtual StatusCode execute()
Algorithm execution.
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
StatusCode finalize() override
standard finalization method
The useful base class for data processing algorithms.
virtual StatusCode initialize()
Algorithm initialization.
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
virtual ~JemallocProfile()
Destructor.
Algorithm to enable/disable the profiling of the head by Jemalloc.
int mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen)
MSG::Level msgLevel() const
get the output level from the embedded MsgStream