| The Gaudi Framework v29r1 (5cd95fa2)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

JemallocProfileSvc.cpp
Go to the documentation of this file.
1 // Include files
2 #include "GaudiKernel/IIncidentListener.h"
3 #include "GaudiKernel/IIncidentSvc.h"
4 #include "GaudiKernel/Service.h"
5 #include <climits>
6 #include <iostream>
7
8 // local
9 #include "JemallocProfileSvc.h"
10
11 // including jemmalloc.h is difficult as the malloc signature is not exactly identical
12 // to the system one (issue with throw).
13 // We therefore declare mallctl here.
14 extern "C" {
15 int mallctl( const char* name, void* oldp, size_t* oldlenp, void* newp, size_t newlen );
16 }
17
18 //-----------------------------------------------------------------------------
19 // Implementation file for class : JemallocProfileSvc
20 //
21 // 2016-01-11 : Ben Couturier
22 //-----------------------------------------------------------------------------
23
24 //=============================================================================
25 // Initializer
26 //=============================================================================
27 StatusCode JemallocProfileSvc::initialize()
28 {
29 StatusCode sc = base_class::initialize();
30 if ( sc.isFailure() ) return sc;
31
32 // register to the incident service
33 static const std::string serviceName = "IncidentSvc";
34 m_incidentSvc = serviceLocator()->service( serviceName );
35 if ( !m_incidentSvc ) {
36 error() << "Cannot retrieve " << serviceName << endmsg;
37 return StatusCode::FAILURE;
38 }
39
40 debug() << "Register to the IncidentSvc" << endmsg;
41 m_incidentSvc->addListener( this, IncidentType::BeginEvent );
42 m_incidentSvc->addListener( this, IncidentType::EndEvent );
43 for ( std::string incident : m_startFromIncidents ) {
44 m_incidentSvc->addListener( this, incident );
45 }
46 for ( std::string incident : m_stopAtIncidents ) {
47 m_incidentSvc->addListener( this, incident );
48 }
49
50 // Resetting the event counter
51 m_eventNumber = 0;
52 m_profiling = false;
53
54 // Cache whether we have start/stop incidents
55 m_hasStartIncident = ( m_startFromIncidents.size() > 0 );
56 m_hasStopIncident = ( m_stopAtIncidents.size() > 0 );
57
58 // Checking the consistency of the start/stop events
59 if ( m_nStartFromEvent == 0 && !m_hasStartIncident && ( m_hasStopIncident || m_nStopAtEvent > 0 ) ) {
60 info() << "Stop profiling trigger was specified but no start. Defaulting to first events" << endmsg;
61 m_nStartFromEvent = 1;
62 }
63
64 return StatusCode::SUCCESS;
65 }
66
67 // Finalization of the service.
68 StatusCode JemallocProfileSvc::finalize()
69 {
70
71 // unregistering from the IncidentSvc
72 m_incidentSvc->removeListener( this, IncidentType::BeginEvent );
73 m_incidentSvc->removeListener( this, IncidentType::EndEvent );
74 m_incidentSvc.reset();
75 return base_class::finalize();
76 }
77
78 //=============================================================================
79 // Event handling methods
80
81 //=============================================================================
82
83 // Handler for incidents
84 void JemallocProfileSvc::handle( const Incident& incident )
85 {
86 if ( IncidentType::BeginEvent == incident.type() ) {
87 handleBegin();
88 } else if ( IncidentType::EndEvent == incident.type() ) {
89 handleEnd();
90 }
91
92 // If already processing we can ignore the incidents for start
93 if ( !m_profiling && m_hasStartIncident ) {
94 for ( std::string startincident : m_startFromIncidents ) {
95 if ( startincident == incident.type() ) {
96 info() << "Starting Jemalloc profile at incident " << incident.type() << endmsg;
97 startProfiling();
98 break;
99 }
100 } // Loop on incidents
101 } // If checking incidents to start
102
103 // If already processing we can ignore the incidents for start
104 if ( m_profiling && m_hasStopIncident ) {
105 for ( std::string stopincident : m_stopAtIncidents ) {
106 if ( stopincident == incident.type() ) {
107 info() << "Stopping Jemalloc profile at incident " << incident.type() << endmsg;
108 stopProfiling();
109 break;
110 }
111 } // Loop on incidents
112 } // If checking incidents to stop
113 }
114
115 // Handler for incidents
116 // Called on at begin events
117 inline void JemallocProfileSvc::handleBegin()
118 {
119 m_eventNumber += 1;
120
121 if ( m_eventNumber == m_nStartFromEvent ) {
122 startProfiling();
123 }
124 }
125
126 // Handler for incidents
127 // Called on at End events
128 inline void JemallocProfileSvc::handleEnd()
129 {
130 if ( m_profiling && m_eventNumber != m_nStartFromEvent &&
131 ( ( m_eventNumber - m_nStartFromEvent ) % m_dumpPeriod == 0 ) ) {
132 dumpProfile();
133 }
134
135 if ( m_eventNumber == m_nStopAtEvent ) {
136 stopProfiling();
137 }
138 }
139
140 //=============================================================================
141 // Utilities calling mallctl
142
143 //=============================================================================
144
148 inline void JemallocProfileSvc::startProfiling()
149 {
150 m_profiling = true;
151 info() << "Starting Jemalloc profile at event " << m_eventNumber << endmsg;
152 mallctl( "prof.dump", NULL, NULL, NULL, 0 );
153 }
154
158 inline void JemallocProfileSvc::stopProfiling()
159 {
160 m_profiling = false;
161 dumpProfile();
162 }
163
167 inline void JemallocProfileSvc::dumpProfile()
168 {
169 info() << "Dumping Jemalloc profile at event " << m_eventNumber << endmsg;
170 mallctl( "prof.dump", NULL, NULL, NULL, 0 );
171 }
172
173 //=============================================================================
174 // Declaration of the factory
175 DECLARE_COMPONENT( JemallocProfileSvc )
StatusCode::SUCCESS
Definition: StatusCode.h:31
Incident::type
const std::string & type() const
Access to the incident type.
Definition: Incident.h:41
JemallocProfileSvc::startProfiling
void startProfiling()
Utility method to start profiling with jemalloc.
Definition: JemallocProfileSvc.cpp:148
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:32
StatusCode::FAILURE
Definition: StatusCode.h:31
CommonMessagingBase::info
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:112
JemallocProfileSvc::finalize
StatusCode finalize() override
Finalizer.
Definition: JemallocProfileSvc.cpp:68
JemallocProfileSvc::m_dumpPeriod
Gaudi::Property< int > m_dumpPeriod
Definition: JemallocProfileSvc.h:53
JemallocProfileSvc
Service that enables the Jemalloc profiler on demand.
Definition: JemallocProfileSvc.h:18
JemallocProfileSvc::m_hasStopIncident
bool m_hasStopIncident
Definition: JemallocProfileSvc.h:59
JemallocProfileSvc::handle
void handle(const Incident &incident) override
Destructor.
Definition: JemallocProfileSvc.cpp:84
JemallocProfileSvc::handleBegin
void handleBegin()
Definition: JemallocProfileSvc.cpp:117
StatusCode::isFailure
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
mallctl
int mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen)
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:33
std::string
STL class.
JemallocProfileSvc::m_eventNumber
int m_eventNumber
Current event number.
Definition: JemallocProfileSvc.h:56
Service.h
JemallocProfileSvc::initialize
StatusCode initialize() override
Initializer.
Definition: JemallocProfileSvc.cpp:27
ISvcLocator::service
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:79
CommonMessagingBase::error
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:106
IIncidentSvc.h
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
JemallocProfileSvc::m_nStopAtEvent
Gaudi::Property< int > m_nStopAtEvent
Definition: JemallocProfileSvc.h:48
JemallocProfileSvc::stopProfiling
void stopProfiling()
Utility method to stop profiling with jemalloc.
Definition: JemallocProfileSvc.cpp:158
JemallocProfileSvc::m_hasStartIncident
bool m_hasStartIncident
Definition: JemallocProfileSvc.h:58
JemallocProfileSvc::m_nStartFromEvent
Gaudi::Property< int > m_nStartFromEvent
Definition: JemallocProfileSvc.h:45
JemallocProfileSvc::m_stopAtIncidents
Gaudi::Property< std::vector< std::string > > m_stopAtIncidents
Definition: JemallocProfileSvc.h:51
CommonMessagingBase::debug
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Definition: CommonMessaging.h:115
Incident
Base class for all Incidents (computing events).
Definition: Incident.h:17
IIncidentSvc::addListener
virtual void addListener(IIncidentListener *lis, const std::string &type="", long priority=0, bool rethrow=false, bool singleShot=false)=0
Add listener.
JemallocProfileSvc::dumpProfile
void dumpProfile()
Utility method to dump profile with jemalloc.
Definition: JemallocProfileSvc.cpp:167
JemallocProfileSvc::handleEnd
void handleEnd()
Definition: JemallocProfileSvc.cpp:128
JemallocProfileSvc::m_incidentSvc
SmartIF< IIncidentSvc > m_incidentSvc
Pointer to the incident service.
Definition: JemallocProfileSvc.h:65
IIncidentListener.h
JemallocProfileSvc.h
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:92
IIncidentSvc::removeListener
virtual void removeListener(IIncidentListener *lis, const std::string &type="")=0
Remove listener.
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:292
JemallocProfileSvc::m_startFromIncidents
Gaudi::Property< std::vector< std::string > > m_startFromIncidents
Definition: JemallocProfileSvc.h:46
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
JemallocProfileSvc::m_profiling
bool m_profiling
Status of the profiling.
Definition: JemallocProfileSvc.h:62