| The Gaudi Framework v27r1

- 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/Service.h"
3 #include "GaudiKernel/IIncidentListener.h"
4 #include "GaudiKernel/IIncidentSvc.h"
5 #include <iostream>
6 #include <climits>
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 {
16 int mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
17 }
18
19 //-----------------------------------------------------------------------------
20 // Implementation file for class : JemallocProfileSvc
21 //
22 // 2016-01-11 : Ben Couturier
23 //-----------------------------------------------------------------------------
24
25 //=============================================================================
26 // Standard constructor, initializes variables
27 //=============================================================================
28 JemallocProfileSvc::JemallocProfileSvc(const std::string& name, ISvcLocator* svcLoc):
29 base_class(name, svcLoc), m_eventNumber(0), m_startFromIncidents({}),
30 m_stopAtIncidents({}), m_hasStartIncident(false), m_hasStopIncident(false),
31 m_profiling(false) {
32
33 declareProperty("StartFromEventN", m_nStartFromEvent = 0,
34 "After what event we start profiling. "
35 );
36
37 declareProperty("StartFromIncidents", m_startFromIncidents = {},
38 "Incidents that trigger profiling start"
39 );
40
41 declareProperty("StopAtEventN", m_nStopAtEvent = 0,
42 "After what event we stop profiling. "
43 "If 0 than we also profile finalization stage. Default = 0."
44 );
45
46 declareProperty("StopAtIncidents", m_stopAtIncidents = {},
47 "Incidents that trigger profiling start"
48 );
49
50 declareProperty("DumpPeriod", m_dumpPeriod = 100,
51 "Period for dumping head to a file. Default=100"
52 );
53
54 }
55
56 //=============================================================================
57 // Initializer
58 //=============================================================================
59 StatusCode JemallocProfileSvc::initialize() {
60 StatusCode sc = base_class::initialize();
61 if (sc.isFailure()) return sc;
62
63 // register to the incident service
64 static const std::string serviceName = "IncidentSvc";
65 m_incidentSvc = serviceLocator()->service(serviceName);
66 if ( ! m_incidentSvc ) {
67 error() << "Cannot retrieve " << serviceName << endmsg;
68 return StatusCode::FAILURE;
69 }
70
71 debug() << "Register to the IncidentSvc" << endmsg;
72 m_incidentSvc->addListener(this, IncidentType::BeginEvent);
73 m_incidentSvc->addListener(this, IncidentType::EndEvent);
74 for (std::string incident: m_startFromIncidents)
75 {
76 m_incidentSvc->addListener(this, incident);
77 }
78 for (std::string incident: m_stopAtIncidents)
79 {
80 m_incidentSvc->addListener(this, incident);
81 }
82
83 // Resetting the event counter
84 m_eventNumber = 0;
85 m_profiling = false;
86
87 // Cache whether we have start/stop incidents
88 m_hasStartIncident = (m_startFromIncidents.size() > 0);
89 m_hasStopIncident = (m_stopAtIncidents.size() > 0);
90
91 // Checking the consistency of the start/stop events
92 if (m_nStartFromEvent == 0 && !m_hasStartIncident
93 && (m_hasStopIncident || m_nStopAtEvent > 0))
94 {
95 info() << "Stop profiling trigger was specified but no start. Defaulting to first events" << endmsg;
96 m_nStartFromEvent = 1;
97 }
98
99
100
101 return StatusCode::SUCCESS;
102 }
103
104 // Finalization of the service.
105 StatusCode JemallocProfileSvc::finalize() {
106
107 // unregistering from the IncidentSvc
108 m_incidentSvc->removeListener(this, IncidentType::BeginEvent);
109 m_incidentSvc->removeListener(this, IncidentType::EndEvent);
110 m_incidentSvc.reset();
111 return base_class::finalize();
112 }
113
114 //=============================================================================
115 // Event handling methods
116
117 //=============================================================================
118
119 // Handler for incidents
120 void JemallocProfileSvc::handle(const Incident& incident)
121 {
122 if (IncidentType::BeginEvent == incident.type())
123 {
124 handleBegin();
125 } else if (IncidentType::EndEvent == incident.type())
126 {
127 handleEnd();
128 }
129
130 // If already processing we can ignore the incidents for start
131 if (!m_profiling && m_hasStartIncident)
132 {
133 for(std::string startincident: m_startFromIncidents)
134 {
135 if (startincident == incident.type())
136 {
137 info() << "Starting Jemalloc profile at incident "
138 << incident.type() << endmsg;
139 startProfiling();
140 break;
141 }
142 } // Loop on incidents
143 } // If checking incidents to start
144
145 // If already processing we can ignore the incidents for start
146 if (m_profiling && m_hasStopIncident)
147 {
148 for(std::string stopincident: m_stopAtIncidents)
149 {
150 if (stopincident == incident.type())
151 {
152 info() << "Stopping Jemalloc profile at incident "
153 << incident.type() << endmsg;
154 stopProfiling();
155 break;
156 }
157 } // Loop on incidents
158 } // If checking incidents to stop
159
160
161 }
162
163 // Handler for incidents
164 // Called on at begin events
165 inline void JemallocProfileSvc::handleBegin()
166 {
167 m_eventNumber += 1;
168
169 if (m_eventNumber == m_nStartFromEvent)
170 {
171 startProfiling();
172 }
173 }
174
175 // Handler for incidents
176 // Called on at End events
177 inline void JemallocProfileSvc::handleEnd()
178 {
179 if (m_profiling
180 && m_eventNumber != m_nStartFromEvent
181 && ((m_eventNumber - m_nStartFromEvent) % m_dumpPeriod == 0))
182 {
183 dumpProfile();
184 }
185
186 if (m_eventNumber == m_nStopAtEvent)
187 {
188 stopProfiling();
189 }
190 }
191
192 //=============================================================================
193 // Utilities calling mallctl
194
195 //=============================================================================
196
200 inline void JemallocProfileSvc::startProfiling()
201 {
202 m_profiling = true;
203 info() << "Starting Jemalloc profile at event "
204 << m_eventNumber << endmsg;
205 mallctl("prof.dump", NULL, NULL, NULL, 0);
206 }
207
211 inline void JemallocProfileSvc::stopProfiling()
212 {
213 m_profiling = false;
214 dumpProfile();
215 }
216
220 inline void JemallocProfileSvc::dumpProfile()
221 {
222 info() << "Dumping Jemalloc profile at event "
223 << m_eventNumber << endmsg;
224 mallctl("prof.dump", NULL, NULL, NULL, 0);
225
226 }
227
228
229 //=============================================================================
230 // Destructor
231
232 //=============================================================================
233 JemallocProfileSvc::~JemallocProfileSvc() {}
234
235 //=============================================================================
236 // Declaration of the factory
237 DECLARE_COMPONENT(JemallocProfileSvc)
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:324
JemallocProfileSvc::initialize
StatusCode initialize()
Initializer.
Definition: JemallocProfileSvc.cpp:59
AnalysisTest.initialize
def initialize()
Definition: AnalysisTest.py:12
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
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:200
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:28
CommonMessagingBase::info
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:124
JemallocProfileSvc::finalize
StatusCode finalize()
Finalizer.
Definition: JemallocProfileSvc.cpp:105
JemallocProfileSvc
Service that enables the Jemalloc profiler on demand.
Definition: JemallocProfileSvc.h:18
JemallocProfileSvc::m_stopAtIncidents
std::vector< std::string > m_stopAtIncidents
Definition: JemallocProfileSvc.h:52
JemallocProfileSvc::~JemallocProfileSvc
virtual ~JemallocProfileSvc()
Definition: JemallocProfileSvc.cpp:233
JemallocProfileSvc::m_hasStopIncident
bool m_hasStopIncident
Definition: JemallocProfileSvc.h:54
JemallocProfileSvc::handleBegin
void handleBegin()
Definition: JemallocProfileSvc.cpp:165
merge_files.sc
int sc
Definition: merge_files.py:167
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:36
std::string
STL class.
JemallocProfileSvc::m_eventNumber
int m_eventNumber
Definition: JemallocProfileSvc.h:50
Service.h
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:78
CommonMessagingBase::error
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:118
StatusCode::FAILURE
Definition: StatusCode.h:29
IIncidentSvc.h
StatusCode::SUCCESS
Definition: StatusCode.h:30
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
JemallocProfileSvc::m_nStopAtEvent
int m_nStopAtEvent
Definition: JemallocProfileSvc.h:48
JemallocProfileSvc::stopProfiling
void stopProfiling()
Utility method to stop profiling with jemalloc.
Definition: JemallocProfileSvc.cpp:211
JemallocProfileSvc::m_hasStartIncident
bool m_hasStartIncident
Definition: JemallocProfileSvc.h:53
JemallocProfileSvc::m_dumpPeriod
int m_dumpPeriod
Definition: JemallocProfileSvc.h:49
JemallocProfileSvc::m_startFromIncidents
std::vector< std::string > m_startFromIncidents
Definition: JemallocProfileSvc.h:51
CommonMessagingBase::debug
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Definition: CommonMessaging.h:127
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:220
CommonMessaging
Definition: CommonMessaging.h:58
JemallocProfileSvc::handleEnd
void handleEnd()
Definition: JemallocProfileSvc.cpp:177
JemallocProfileSvc::m_incidentSvc
SmartIF< IIncidentSvc > m_incidentSvc
Pointer to the incident service.
Definition: JemallocProfileSvc.h:61
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:88
IIncidentSvc::removeListener
virtual void removeListener(IIncidentListener *lis, const std::string &type="")=0
Remove listener.
JemallocProfileSvc::JemallocProfileSvc
JemallocProfileSvc(const std::string &name, ISvcLocator *svcLoc)
Standard constructor.
Definition: JemallocProfileSvc.cpp:28
JemallocProfileSvc::m_nStartFromEvent
int m_nStartFromEvent
Start, End event and counter.
Definition: JemallocProfileSvc.h:47
JemallocProfileSvc::handle
void handle(const Incident &incident)
Destructor.
Definition: JemallocProfileSvc.cpp:120
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
JemallocProfileSvc::m_profiling
bool m_profiling
Status of the profiling.
Definition: JemallocProfileSvc.h:58