The Gaudi Framework  master (37c0b60a)
PerfProfile.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2023 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 \***********************************************************************************/
12 #include <fcntl.h>
13 #include <string_view>
14 #include <sys/stat.h>
15 
26 struct PerfProfile final : Gaudi::Functional::Consumer<void()> {
27  using Consumer::Consumer;
28 
29  StatusCode initialize() override {
30  return Consumer::initialize().andThen( [this]() {
31  m_fifo = ::open( m_fifoPath.value().c_str(), O_WRONLY | O_NONBLOCK );
32  if ( m_fifo == -1 ) {
33  fatal() << "open(\"" << m_fifoPath.value() << "\"): " << ::strerror( errno ) << endmsg;
34  return StatusCode::FAILURE;
35  }
36  return StatusCode::SUCCESS;
37  } );
38  }
39 
40  StatusCode finalize() override {
41  if ( m_fifo != -1 ) { ::close( m_fifo ); }
42  return Consumer::finalize();
43  }
44 
45  void operator()() const override {
46  // We could use EventContext::evt(), however it is not always valid, as is the case with EvtSel="NONE". Instead, use
47  // an atomic counter.
48  auto eventNumber = m_eventNumber++;
49  if ( eventNumber == m_nStartFromEvent.value() ) {
50  warning() << "Starting perf profile at event " << eventNumber << endmsg;
51  fifo_write( "enable\n" );
52  }
53 
54  if ( m_nStopAtEvent > 0 && eventNumber == m_nStopAtEvent.value() ) {
55  warning() << "Stopping perf profile at event " << eventNumber << endmsg;
56  fifo_write( "disable\n" );
57  }
58  }
59 
60 private:
61  void fifo_write( std::string_view s ) const {
62  if ( ::write( m_fifo, s.data(), s.size() ) < ssize_t( s.size() ) ) {
63  error() << "Write of \"" << s << "\" to FIFO failed: " << ::strerror( errno ) << endmsg;
64  }
65  }
66 
68  int m_fifo = -1;
69 
70  Gaudi::Property<std::string> m_fifoPath{ this, "FIFOPath", "GaudiPerfProfile.fifo", "Path to perf control FIFO." };
72  "After what event we start profiling." };
74  this, "StopAtEventN", 0,
75  "After what event we stop profiling. If 0 than we also profile finalization stage. Default = 0." };
76 };
77 
Gaudi::Functional::Consumer
details::Consumer< Signature, Traits_, details::isLegacy< Traits_ > > Consumer
Definition: Consumer.h:69
StatusCode::andThen
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
Definition: StatusCode.h:163
gaudirun.s
string s
Definition: gaudirun.py:346
PerfProfile::m_fifo
int m_fifo
Definition: PerfProfile.cpp:68
PerfProfile::m_fifoPath
Gaudi::Property< std::string > m_fifoPath
Definition: PerfProfile.cpp:70
Gaudi::Functional::details::Consumer
Definition: Consumer.h:24
PerfProfile::m_nStopAtEvent
Gaudi::Property< unsigned long > m_nStopAtEvent
Definition: PerfProfile.cpp:73
PerfProfile::finalize
StatusCode finalize() override
Definition: PerfProfile.cpp:40
StatusCode
Definition: StatusCode.h:65
Consumer.h
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:237
PerfProfile::initialize
StatusCode initialize() override
Definition: PerfProfile.cpp:29
PerfProfile::operator()
void operator()() const override
Definition: PerfProfile.cpp:45
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
std::atomic< long unsigned >
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
PerfProfile::m_eventNumber
std::atomic< long unsigned > m_eventNumber
Definition: PerfProfile.cpp:67
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
PerfProfile
Algorithm to enable/disable profiling with Linux perf at given events.
Definition: PerfProfile.cpp:26
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
PerfProfile::m_nStartFromEvent
Gaudi::Property< unsigned long > m_nStartFromEvent
Definition: PerfProfile.cpp:71
PerfProfile::fifo_write
void fifo_write(std::string_view s) const
Definition: PerfProfile.cpp:61
Gaudi::Property< std::string >