| The Gaudi Framework v29r1 (5cd95fa2)

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

- File List
- File Members

StalledEventMonitor.cpp
Go to the documentation of this file.
1 /*
2 * StalledEventMonitor.cpp
3 *
4 * Created on: Apr 19, 2010
5 * Author: Marco Clemencic
6 */
7
8 // Include files
9 #include "StalledEventMonitor.h"
10
11 #include "GaudiKernel/IIncidentSvc.h"
12 #include "GaudiKernel/Memory.h"
13 #include "GaudiKernel/WatchdogThread.h"
14
15 #include "TSystem.h"
16
17 #include <csignal>
18
19 namespace
20 {
22 class EventWatchdog : public WatchdogThread
23 {
24 public:
25 EventWatchdog( const SmartIF<IMessageSvc>& msgSvc, const std::string& name,
26 boost::posix_time::time_duration timeout, bool stackTrace = false, long maxCount = 0,
27 bool autostart = false )
28 : WatchdogThread( timeout, autostart ), log( msgSvc, name ), m_maxCount( maxCount ), m_stackTrace( stackTrace )
29 {
30 }
31 ~EventWatchdog() override = default;
32
33 private:
35 MsgStream log;
37 long m_counter = 0;
39 long m_maxCount = 0;
41 bool m_stackTrace = false;
43 void action() override
44 {
45 if ( !m_counter ) {
46 log << MSG::WARNING << "More than " << getTimeout().total_seconds() << "s since the last "
47 << IncidentType::BeginEvent << endmsg;
48 } else {
49 log << MSG::WARNING << "Other " << getTimeout().total_seconds() << "s passed" << endmsg;
50 }
51 log << MSG::INFO << "Current memory usage is"
52 " virtual size = "
53 << System::virtualMemory() / 1024. << " MB"
54 ", resident set size = "
55 << System::pagedMemory() / 1024. << " MB" << endmsg;
56 if ( m_stackTrace && gSystem ) {
57 // TSystem::StackTrace() prints on the standard error, so we
58 std::cerr << "=== Stalled event: current stack trace ===" << std::endl;
59 gSystem->StackTrace();
60 }
61 ++m_counter;
62 if ( m_maxCount > 0 && m_counter >= m_maxCount ) {
63 log << MSG::FATAL << "too much time on a single event: aborting process" << endmsg;
64 std::raise( SIGABRT );
65 }
66 }
67 void onPing() override
68 {
69 if ( m_counter ) {
70 if ( m_counter >= 3 )
71 log << MSG::INFO << "Starting a new event after ~" << m_counter * getTimeout().total_seconds() << "s"
72 << endmsg;
73 m_counter = 0;
74 }
75 }
76 void onStop() override
77 {
78 if ( m_counter >= 3 )
79 log << MSG::INFO << "The last event took ~" << m_counter * getTimeout().total_seconds() << "s" << endmsg;
80 }
81 };
82 }
83
84 // Constructor
85 StalledEventMonitor::StalledEventMonitor( const std::string& name, ISvcLocator* svcLoc ) : base_class( name, svcLoc ) {}
86
87 // Initialization of the service.
88 StatusCode StalledEventMonitor::initialize()
89 {
90 StatusCode sc = base_class::initialize();
91 if ( sc.isFailure() ) return sc;
92
93 if ( m_eventTimeout ) {
94 // create the watchdog thread
95 m_watchdog.reset( new EventWatchdog( msgSvc(), "EventWatchdog", boost::posix_time::seconds( m_eventTimeout ),
96 m_stackTrace, m_maxTimeoutCount ) );
97
98 // register to the incident service
99 static const std::string serviceName = "IncidentSvc";
100 m_incidentSvc = serviceLocator()->service( serviceName );
101 if ( !m_incidentSvc ) {
102 error() << "Cannot retrieve " << serviceName << endmsg;
103 return StatusCode::FAILURE;
104 }
105 debug() << "Register to the IncidentSvc" << endmsg;
106 m_incidentSvc->addListener( this, IncidentType::BeginEvent );
107 } else {
108 warning() << "StalledEventMonitor/" << name() << " instantiated with 0 time-out: no monitoring performed" << endmsg;
109 }
110
111 return StatusCode::SUCCESS;
112 }
113
114 // Start the monitoring.
115 StatusCode StalledEventMonitor::start()
116 {
117 if ( m_watchdog ) m_watchdog->start();
118 return StatusCode::SUCCESS;
119 }
120
121 // Notify the watchdog that a new event has been started
122 void StalledEventMonitor::handle( const Incident& /* incident */ )
123 {
124 if ( m_watchdog ) m_watchdog->ping();
125 }
126
127 // Start the monitoring.
128 StatusCode StalledEventMonitor::stop()
129 {
130 if ( m_watchdog ) m_watchdog->stop();
131 return StatusCode::SUCCESS;
132 }
133
134 // Finalization of the service.
135 StatusCode StalledEventMonitor::finalize()
136 {
137 // destroy the watchdog thread (if any)
138 m_watchdog.reset();
139 // unregistering from the IncidentSvc
140 m_incidentSvc->removeListener( this, IncidentType::BeginEvent );
141 m_incidentSvc.reset();
142 return base_class::finalize();
143 }
144
145 // Declaration of the factory
146 DECLARE_COMPONENT( StalledEventMonitor )
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode::SUCCESS
Definition: StatusCode.h:31
SmartIF< IMessageSvc >
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
StalledEventMonitor::stop
StatusCode stop() override
Stop the watchdog thread (after the event loop).
Definition: StalledEventMonitor.cpp:128
Service::name
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:289
WatchdogThread::getTimeout
boost::posix_time::time_duration getTimeout() const
Get the current time-out value.
Definition: WatchdogThread.h:59
WatchdogThread::start
void start()
Start the watchdog thread.
Definition: WatchdogThread.cpp:25
WatchdogThread::onStop
virtual void onStop()
User implemented function that will be called when stopping.
Definition: WatchdogThread.cpp:93
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:32
StatusCode::FAILURE
Definition: StatusCode.h:31
StalledEventMonitor::m_eventTimeout
Gaudi::Property< unsigned int > m_eventTimeout
Definition: StalledEventMonitor.h:55
StalledEventMonitor::m_watchdog
std::unique_ptr< WatchdogThread > m_watchdog
Pointer to the watchdog thread that checks for the event timeout.
Definition: StalledEventMonitor.h:62
StalledEventMonitor::handle
void handle(const Incident &) override
Notify the watchdog thread for a new event.
Definition: StalledEventMonitor.cpp:122
std::endl
T endl(T...args)
StalledEventMonitor::start
StatusCode start() override
Start the watchdog thread (before entering the event loop).
Definition: StalledEventMonitor.cpp:115
WatchdogThread::ping
void ping()
Function to call to notify the watchdog thread that we are still alive.
Definition: WatchdogThread.h:48
StatusCode::isFailure
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:33
std::string
STL class.
MSG::FATAL
Definition: IMessageSvc.h:16
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
StalledEventMonitor
Service that monitor the time taken by processing of single events using a separate thread...
Definition: StalledEventMonitor.h:29
StalledEventMonitor::m_incidentSvc
SmartIF< IIncidentSvc > m_incidentSvc
Pointer to the incident service.
Definition: StalledEventMonitor.h:65
CommonMessagingBase::warning
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
Definition: CommonMessaging.h:109
IIncidentSvc.h
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
ForwardSchedulerSimpleTest.msgSvc
msgSvc
Definition: ForwardSchedulerSimpleTest.py:112
std::unique_ptr::reset
T reset(T...args)
CommonMessagingBase::msgSvc
const SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
Definition: CommonMessaging.h:75
std::raise
T raise(T...args)
StalledEventMonitor::StalledEventMonitor
StalledEventMonitor(const std::string &name, ISvcLocator *svcLoc)
Constructor.
Definition: StalledEventMonitor.cpp:85
WatchdogThread::onPing
virtual void onPing()
User implemented function that will be called when ping is called.
Definition: WatchdogThread.cpp:87
StalledEventMonitor::initialize
StatusCode initialize() override
Initialization of the service.
Definition: StalledEventMonitor.cpp:88
Memory.h
WatchdogThread
Simple class for asynchronous check of time-out.
Definition: WatchdogThread.h:28
CommonMessagingBase::debug
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Definition: CommonMessaging.h:115
WatchdogThread::stop
void stop()
Signal the watchdog thread to stop and wait for it.
Definition: WatchdogThread.cpp:38
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.
CommonMessaging
Definition: CommonMessaging.h:62
System::virtualMemory
GAUDI_API long virtualMemory(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: priority boost.
Definition: Memory.cpp:239
StalledEventMonitor::finalize
StatusCode finalize() override
Finalization of the service.
Definition: StalledEventMonitor.cpp:135
MSG::WARNING
Definition: IMessageSvc.h:16
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.
WatchdogThread::action
virtual void action()
User implemented function that will be called if the time-out is reached.
Definition: WatchdogThread.cpp:84
StalledEventMonitor.h
MSG::INFO
Definition: IMessageSvc.h:16
std::cerr
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:292
System::pagedMemory
GAUDI_API long pagedMemory(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: Amount of paged memory currently occupied by the process &#39;pid&#39;...
Definition: Memory.cpp:141
WatchdogThread.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
Gaudi.Configuration.log
log
Definition: Configuration.py:13
StalledEventMonitor::m_maxTimeoutCount
Gaudi::Property< int > m_maxTimeoutCount
Definition: StalledEventMonitor.h:57