ATLAS Offline Software
Loading...
Searching...
No Matches
SGHiveMgrSvc.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 "GaudiKernel/ConcurrencyFlags.h"
12
13using namespace SG;
14
16
17HiveMgrSvc::HiveMgrSvc(const std::string& name,
18 ISvcLocator* svc) : base_class(name, svc),
19 m_hiveStore(StoreID::storeName(StoreID::EVENT_STORE), name),
20 m_nSlots(1)
21{
22 declareProperty("HiveStoreSvc", m_hiveStore);
23 declareProperty("NSlots", m_nSlots, "number of event slots");
24}
25
26
35void HiveMgrSvc::setNumProcs(size_t numProcs)
36{
37 Gaudi::Concurrency::ConcurrencyFlags::setNumProcs(numProcs);
38}
39
46StatusCode HiveMgrSvc::selectStore(size_t slotIndex) {
47 s_current = &m_slots[slotIndex];
49 return StatusCode::SUCCESS;
50}
51
57StatusCode HiveMgrSvc::clearStore(size_t slotIndex) {
58 StatusCode rc(StatusCode::FAILURE);
59 if (slotIndex < m_nSlots) {
60 rc=m_slots[slotIndex].pEvtStore->clearStore();
61 if (rc.isSuccess()) debug() << "cleared store " << slotIndex << endmsg;
62 }
63 if (!rc.isSuccess()) error() << "could not clear store " << slotIndex << endmsg;
64 return rc;
65}
66
72StatusCode HiveMgrSvc::setNumberOfStores(size_t slots) {
73 //FIXME what if running?
74 if(FSMState() == Gaudi::StateMachine::INITIALIZED) {
75 fatal() << "Too late to change the number of slots!" << endmsg;
76 return StatusCode::FAILURE;
77 } else {
78 m_slots.resize(slots);
79 m_nSlots = slots;
80 m_freeSlots.store(slots);
81 Gaudi::Concurrency::ConcurrencyFlags::setNumConcEvents( slots );
82 return StatusCode::SUCCESS;
83 }
84}
85
87 return m_nSlots;
88}
89
90
91
98size_t HiveMgrSvc::allocateStore( int evtNumber ) {
99 if (m_freeSlots == 0) {
100 error() << "No slots available for event number " << evtNumber << endmsg;
101 return std::string::npos;
102 }
103 std::scoped_lock lock{m_mutex};
104 for (size_t index=0; index<m_nSlots; ++index) {
105 if( m_slots[index].eventNumber == evtNumber) {
106 error() << "Attempt to allocate an event slot for an event that is still active: event number " << evtNumber << endmsg;
107 return std::string::npos;
108 } else if (m_slots[index].eventNumber == -1) {
109 m_slots[index].eventNumber = evtNumber;
110 debug() << "Slot " << index
111 << " allocated to event number "<< evtNumber << endmsg;
112 m_freeSlots--;
113 return index;
114 }
115 }
116 error() << "No slots available for event number " << evtNumber << endmsg;
117 return std::string::npos;
118}
119
125StatusCode HiveMgrSvc::freeStore( size_t slotIndex ) {
126 if (slotIndex < m_nSlots) {
127 std::scoped_lock lock{m_mutex};
128 if (m_slots[slotIndex].eventNumber == -1) {
129 debug() << "Slot " << slotIndex << " is already free" << endmsg;
130 }
131 else {
132 m_slots[slotIndex].eventNumber = -1;
133 m_freeSlots++;
134 debug() << "Freed slot " << slotIndex << endmsg;
135 }
136 return StatusCode::SUCCESS;
137 } else {
138 error() << "no slot at " << slotIndex << endmsg;
139 return StatusCode::FAILURE;
140 }
141}
142
143
149size_t HiveMgrSvc::getPartitionNumber(int evtNumber) const {
150 std::scoped_lock lock{m_mutex};
151 for (size_t index=0; index<m_nSlots; ++index) {
152 if( m_slots[index].eventNumber == evtNumber) return index;
153 }
154 return std::string::npos;
155}
156
158 return m_freeSlots;
159}
160
161bool HiveMgrSvc::exists( const DataObjID& id) {
162 // this should only get called in error situations, so we
163 // don't care if it's slow
164 std::string key = id.key();
165 key.erase(0,key.find('+')+1);
166
167 if (id.clid() == 0) {
168 // this is an ugly hack in case the DataObjID gets munged
169 // upstream, and we have to re-separate it into (class,key)
170 // from "class/key"
171 std::string cl = id.fullKey();
172 cl.erase(cl.find('/'),cl.length());
173
174 DataObjID d2(std::move(cl),key);
175 return m_hiveStore->transientContains(d2.clid(), key);
176 } else {
177 return m_hiveStore->transientContains(id.clid(), key);
178 }
179
180}
181
183 verbose() << "Initializing " << name() << endmsg;
184
185 if ( !(Service::initialize().isSuccess()) ) {
186 fatal() << "Unable to initialize base class" << endmsg;
187 return StatusCode::FAILURE;
188 }
189 //this sets the hiveStore pointer to StoreGateSvc.defaultStore
190 if (!(m_hiveStore.retrieve()).isSuccess()) {
191 fatal() << "Unable to get hive event store" << endmsg;
192 return StatusCode::FAILURE;
193 }
194
195 //use hiveStore default impl store as prototype
196 Service* child(0);
197 SGImplSvc* pSG(0);
198
199 for( size_t i = 0; i< m_nSlots; ++i) {
200 std::ostringstream oss;
201 oss << i << '_' << m_hiveStore->currentStore()->name();
202 if (CloneService::clone(m_hiveStore->currentStore(), oss.str(), child).isSuccess() &&
203 child->initialize().isSuccess() &&
204 0 != (pSG = dynamic_cast<SGImplSvc*>(child)) )
205 {
206 pSG->setSlotNumber (i, m_nSlots);
207 m_slots.push_back(SG::HiveEventSlot(pSG));
208 } else {
209 fatal() << "Unable to clone event store " << oss.str() << endmsg;
210 return StatusCode::FAILURE;
211 }
212 }
213
214 m_freeSlots.store( m_nSlots );
215 Gaudi::Concurrency::ConcurrencyFlags::setNumConcEvents( m_nSlots );
216
217 return selectStore(0);
218}
219
221 info() << "Finalizing " << name() << endmsg;
222
223 for (SG::HiveEventSlot& s : m_slots) {
224 // The impl services are not set to active, so ServiceManager
225 // won't finalize them.
226 CHECK( s.pEvtStore->finalize() );
227 s.pEvtStore->release();
228 }
229
230 return StatusCode::SUCCESS;
231}
232
233
235{
236 // On a start transition, merge the string pool from the default store
237 // into that of each store, so that they will know about any explicit
238 // registrations that were done during initialize().
239 // See ATEAM-846.
240 for (SG::HiveEventSlot& slot : m_slots) {
241 slot.pEvtStore->mergeStringPool (*m_hiveStore->currentStore());
242 }
243 return StatusCode::SUCCESS;
244}
#define endmsg
Helpers for checking error return status codes and reporting errors.
#define CHECK(...)
Evaluate an expression and check for errors.
static Double_t rc
const bool debug
__thread HiveEventSlot * s_current(0)
The Athena Transient Store API.
Definition SGImplSvc.h:110
void setSlotNumber(int slot, int numSlots)
Set the Hive slot number for this store.
virtual StatusCode start() override
virtual size_t allocateStore(int evtnumber) override
Allocate a store slot for new event.
HiveMgrSvc(const std::string &name, ISvcLocator *svc)
Standard Service Constructor. sets active store to default event store.
virtual size_t getNumberOfStores() const override
Get the number of 'slots'.
static void setNumProcs(size_t numProcs)
Set number of concurrent processes.
virtual bool exists(const DataObjID &) override
Check if a data object exists in store.
std::atomic< size_t > m_freeSlots
virtual size_t freeSlots() override
Get free slots number.
virtual size_t getPartitionNumber(int eventnumber) const override
Get the slot number corresponding to a given event.
virtual StatusCode selectStore(size_t slotIndex) override
Activate an given 'slot' for all subsequent calls within the same thread id.
ServiceHandle< StoreGateSvc > m_hiveStore
std::mutex m_mutex
virtual StatusCode clearStore(size_t slotIndex) override
Clear a given 'slot'.
virtual StatusCode setNumberOfStores(size_t slots) override
Set the number of 'slots'.
std::vector< SG::HiveEventSlot > m_slots
virtual StatusCode initialize() override
virtual StatusCode freeStore(size_t slotIndex) override
Free a store slot.
virtual StatusCode finalize() override
static void setSlot(SG::HiveEventSlot *pSlot)
set the hive event slot pointer: used by the event loop mgrs
defines an enum used by address providers to decide what kind of StoreGateSvc they are providing addr...
Definition StoreID.h:18
bool verbose
Definition hcg.cxx:73
StatusCode clone(const IService *parent, const std::string &childName, Service *&child)
given a reference to a parent svc sets a reference to a cloned child
Forward declaration.
Definition index.py:1