ATLAS Offline Software
Loading...
Searching...
No Matches
HiveExSvc.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "HiveExSvc.h"
6#include "GaudiKernel/ISvcLocator.h"
7#include "GaudiKernel/ConcurrencyFlags.h"
8#include "GaudiKernel/EventContext.h"
9
10#include <map>
11
12/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13
14HiveExSvc::HiveExSvc(const std::string& name, ISvcLocator* svc)
15 : base_class( name, svc ) {}
16
17/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
18
19HiveExSvc::~HiveExSvc() = default;
20
21/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22
23StatusCode
25
26 size_t nslots = Gaudi::Concurrency::ConcurrencyFlags::numConcurrentEvents();
27 if (nslots == 0) {
28 nslots = 1;
29 }
30
31 ATH_MSG_INFO("initialize structures of size " << nslots);
32
33 // initialize the structures with the number of concurrent events
34 m_times.resize( nslots );
35 m_locks.resize( nslots );
36 for ( auto & m : m_locks ) {
37 m = std::make_unique<std::mutex>();
38 }
39
40 return StatusCode::SUCCESS;
41}
42
43/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
44
45StatusCode
47 ATH_MSG_INFO("finalize");
48
49 // calculate the standard deviation of the sleep times
50 std::map<std::string,unsigned int> sum;
51 std::map<std::string,unsigned int> sumSq;
52 std::map<std::string,unsigned int> num;
53
54 for (const auto &ve : m_times) {
55 for (const auto &e : ve) {
56 sum[e.algName] += e.sleep_time;
57 sumSq[e.algName] += e.sleep_time * e.sleep_time;
58 num[e.algName] ++;
59 }
60 }
61
62 info() << "listing timing by alg:";
63 for (const auto &s : sum) {
64 float avg = static_cast<float>(s.second)/num[s.first];
65 float sig = sqrt( ( sumSq[s.first] - 2*s.second*avg + num[s.first]*avg*avg )/(num[s.first]) );
66 info() << "\n " << s.first
67 << " avg: " << avg << " sig: " << sig;
68 }
69 info() << endmsg;
70
71 return StatusCode::SUCCESS;
72}
73
74/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
75
76void
77HiveExSvc::add(const std::string& algName, const unsigned int& time) {
78
79 // even though we have separate data structures per concurrent
80 // event (slot), we can't be sure that multiple threads aren't
81 // processing the same slot at the same time, so need to lock the mutex.
82 // this reduces the waiting for a lock, compared with locking the
83 // whole structure, as it's unlikely that we'll have the same Alg in
84 // different concurrent events executing at the same time.
85
86 // for a true lock-free design, we would need a separate container for
87 // each Algorithm in each slot
88
89 EventContext::ContextID_t slot = Gaudi::Hive::currentContextId();
90 std::lock_guard<std::mutex> lock ( *m_locks[slot] );
91 m_times[slot].push_back( tDat(algName, time) );
92}
93
#define endmsg
#define ATH_MSG_INFO(x)
Simple service that accumulates timings for Algorithms by name.
virtual ~HiveExSvc()
std::vector< std::unique_ptr< std::mutex > > m_locks
Definition HiveExSvc.h:49
std::vector< std::list< tDat > > m_times
Definition HiveExSvc.h:45
HiveExSvc(const std::string &name, ISvcLocator *svc)
Definition HiveExSvc.cxx:14
virtual StatusCode finalize() override
Definition HiveExSvc.cxx:46
virtual StatusCode initialize() override
Definition HiveExSvc.cxx:24
virtual void add(const std::string &, const unsigned int &) override
Definition HiveExSvc.cxx:77