The Gaudi Framework  v29r0 (ff2e7097)
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 int mallctl( const char* name, void* oldp, size_t* oldlenp, void* newp, size_t newlen );
12 }
13 
14 //-----------------------------------------------------------------------------
15 // Implementation file for class : JemallocProfile
16 //
17 // 2015-06-09 : Ben Couturier
18 //-----------------------------------------------------------------------------
19 
20 // Declaration of the Algorithm Factory
22 
23 //=============================================================================
24 // Initialization
25 //=============================================================================
27 {
28  StatusCode sc = GaudiAlgorithm::initialize(); // must be executed first
29  if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
30 
31  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Initialize" << endmsg;
32  bool active = true;
33  int res = mallctl( "prof.active", NULL, NULL, &active, sizeof( active ) );
34  if ( res != 0 ) {
35  return StatusCode::FAILURE;
36  }
37  return StatusCode::SUCCESS;
38 }
39 
40 //=============================================================================
41 // Main execution
42 //=============================================================================
44 {
45 
46  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Execute" << endmsg;
47 
48  // Increase event number
49  m_eventNumber += 1;
50 
52  m_profiling = true;
53  info() << "Starting Jemalloc profile at event " << m_eventNumber << endmsg;
54  }
55 
56  if ( m_profiling && ( ( m_eventNumber - m_nStartFromEvent ) % m_dumpPeriod == 0 ) ) {
57  info() << "Jemalloc Dumping heap at event " << m_eventNumber << endmsg;
58  mallctl( "prof.dump", NULL, NULL, NULL, 0 );
59  }
60 
61  if ( m_eventNumber == m_nStopAtEvent ) {
62  m_profiling = false;
63  info() << "Stopping Jemalloc profile at event " << m_eventNumber << endmsg;
64  mallctl( "prof.dump", NULL, NULL, NULL, 0 );
65  }
66  return StatusCode::SUCCESS;
67 }
68 
69 //=============================================================================
70 // Finalize
71 //=============================================================================
73 {
74 
75  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Finalize" << endmsg;
76  return GaudiAlgorithm::finalize(); // must be called after all other actions
77 }
78 
79 //=============================================================================
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
StatusCode initialize() override
standard initialization method
Gaudi::Property< int > m_nStopAtEvent
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
Gaudi::Property< int > m_dumpPeriod
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
StatusCode finalize() override
standard finalization method
#define DECLARE_ALGORITHM_FACTORY(x)
Definition: Algorithm.h:629
StatusCode finalize() override
Algorithm finalization.
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Gaudi::Property< int > m_nStartFromEvent
StatusCode execute() override
Algorithm execution.
MSG::Level msgLevel() const
get the output level from the embedded MsgStream
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
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)