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