The Gaudi Framework  master (37c0b60a)
JemallocProfile.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #include "JemallocProfile.h"
12 
13 // including jemmalloc.h is difficult as the malloc signature is not exactly identical
14 // to the system one (issue with throw).
15 // We therefore declare mallctl here.
16 
17 extern "C" {
18 int mallctl( const char* name, void* oldp, size_t* oldlenp, void* newp, size_t newlen );
19 }
20 
21 //-----------------------------------------------------------------------------
22 // Implementation file for class : JemallocProfile
23 //
24 // 2015-06-09 : Ben Couturier
25 //-----------------------------------------------------------------------------
26 
27 // Declaration of the Algorithm Factory
29 
30 //=============================================================================
31 // Initialization
32 //=============================================================================
33 StatusCode JemallocProfile::initialize() {
34  StatusCode sc = Algorithm::initialize(); // must be executed first
35  if ( sc.isFailure() ) return sc; // error printed already by Algorithm
36 
37  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Initialize" << endmsg;
38  bool active = true;
39  int res = mallctl( "prof.active", NULL, NULL, &active, sizeof( active ) );
40  if ( res != 0 ) { return StatusCode::FAILURE; }
41  return StatusCode::SUCCESS;
42 }
43 
44 //=============================================================================
45 // Main execution
46 //=============================================================================
48 
49  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Execute" << endmsg;
50 
51  // Increase event number
52  m_eventNumber += 1;
53 
55  m_profiling = true;
56  info() << "Starting Jemalloc profile at event " << m_eventNumber << endmsg;
57  }
58 
59  if ( m_profiling && ( ( m_eventNumber - m_nStartFromEvent ) % m_dumpPeriod == 0 ) ) {
60  info() << "Jemalloc Dumping heap at event " << m_eventNumber << endmsg;
61  mallctl( "prof.dump", NULL, NULL, NULL, 0 );
62  }
63 
64  if ( m_eventNumber == m_nStopAtEvent ) {
65  m_profiling = false;
66  info() << "Stopping Jemalloc profile at event " << m_eventNumber << endmsg;
67  mallctl( "prof.dump", NULL, NULL, NULL, 0 );
68  }
69  return StatusCode::SUCCESS;
70 }
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
JemallocProfile
Definition: JemallocProfile.h:26
Gaudi::Algorithm::initialize
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition: Algorithm.h:178
JemallocProfile::m_eventNumber
int m_eventNumber
Definition: JemallocProfile.h:42
JemallocProfile::m_nStopAtEvent
Gaudi::Property< int > m_nStopAtEvent
Definition: JemallocProfile.h:36
CommonMessaging< implements< IAlgorithm, IDataHandleHolder, IProperty, IStateful > >::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:148
JemallocProfile::m_dumpPeriod
Gaudi::Property< int > m_dumpPeriod
Definition: JemallocProfile.h:39
StatusCode
Definition: StatusCode.h:65
JemallocProfile.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
JemallocProfile::m_profiling
bool m_profiling
Definition: JemallocProfile.h:41
JemallocProfile::m_nStartFromEvent
Gaudi::Property< int > m_nStartFromEvent
Definition: JemallocProfile.h:35
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
mallctl
int mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen)
JemallocProfile::execute
StatusCode execute() override
Algorithm execution.
Definition: JemallocProfile.cpp:47
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101