The Gaudi Framework  master (08f81203)
Loading...
Searching...
No Matches
EvtStoreSvc.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2026 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\***********************************************************************************/
11#include <Gaudi/Accumulators.h>
20#include <GaudiKernel/Service.h>
21#include <GaudiKernel/System.h>
22#include <tbb/concurrent_queue.h>
23
24#include <ThreadLocalStorage.h>
25
26#include <algorithm>
27#include <iomanip>
28#include <iterator>
29#include <map>
30#include <mutex>
31#include <stdexcept>
32#include <type_traits>
33#include <unordered_map>
34#include <utility>
35#include <vector>
36
37namespace {
38 using LocalArena = Gaudi::Arena::Monotonic<>;
39
40 template <typename T>
41 using LocalAlloc = Gaudi::Allocator::MonotonicArena<T>;
42
43 using pool_string = std::basic_string<char, std::char_traits<char>, LocalAlloc<char>>;
44
45 class Entry final : public IRegistry {
46 std::unique_ptr<DataObject> m_data;
47 std::unique_ptr<IOpaqueAddress> m_addr;
48 pool_string m_identifierStorage;
49 mutable std::optional<std::string> m_identifier;
50 static IDataProviderSvc* s_svc;
51
52 public:
53 using allocator_type = LocalAlloc<char>;
54 static void setDataProviderSvc( IDataProviderSvc* p ) { s_svc = p; }
55
56 Entry( std::string_view id, std::unique_ptr<DataObject> data, std::unique_ptr<IOpaqueAddress> addr,
57 allocator_type alloc ) noexcept
58 : m_data{ std::move( data ) }, m_addr{ std::move( addr ) }, m_identifierStorage{ id, alloc } {
59 if ( m_data ) m_data->setRegistry( this );
60 if ( m_addr ) m_addr->setRegistry( this );
61 }
62 Entry( const Entry& ) = delete;
63 Entry& operator=( const Entry& rhs ) = delete;
64 Entry( Entry&& rhs ) = delete;
65 Entry& operator=( Entry&& rhs ) = delete;
66
67 // required by IRegistry...
68 unsigned long addRef() override { return -1; }
69 unsigned long release() override { return -1; }
70 const name_type& name() const override {
71 // should really be from last '/' onward...
72 if ( !m_identifier ) m_identifier.emplace( m_identifierStorage );
73 return *m_identifier;
74 }
75 const id_type& identifier() const override {
76 if ( !m_identifier ) m_identifier.emplace( m_identifierStorage );
77 return *m_identifier;
78 }
79 std::string_view identifierView() const { return m_identifierStorage; }
80 IDataProviderSvc* dataSvc() const override { return s_svc; }
81 DataObject* object() const override { return const_cast<DataObject*>( m_data.get() ); }
82 IOpaqueAddress* address() const override { return m_addr.get(); }
83 void setAddress( IOpaqueAddress* iAddr ) override {
84 m_addr.reset( iAddr );
85 if ( m_addr ) m_addr->setRegistry( this );
86 }
87 };
88 IDataProviderSvc* Entry::s_svc = nullptr;
89
90 using UnorderedMap =
91 std::unordered_map<std::string_view, Entry, std::hash<std::string_view>, std::equal_to<std::string_view>,
92 LocalAlloc<std::pair<const std::string_view, Entry>>>;
93 using OrderedMap = std::map<std::string_view, Entry>;
94
95 template <typename Map = UnorderedMap>
96 class Store {
97 LocalArena m_resource;
98 std::size_t m_est_size;
99 // Optional purely to make [re]construction simpler, should "always" be valid
100 std::optional<Map> m_store{ std::in_place, &m_resource };
101 static_assert( std::is_same_v<typename Map::key_type, std::string_view> );
102
103 const auto& emplace( std::string_view k, std::unique_ptr<DataObject> d, std::unique_ptr<IOpaqueAddress> a = {} ) {
104 // tricky way to insert a string_view key which points to the
105 // string contained in the mapped type...
106 auto [i, b] = m_store->try_emplace( k, k, std::move( d ), std::move( a ), &m_resource );
107 if ( !b ) throw std::runtime_error( "failed to insert " + std::string{ k } );
108 auto nh = m_store->extract( i );
109 nh.key() = nh.mapped().identifierView(); // "re-point" key to the string contained in the Entry
110 auto r = m_store->insert( std::move( nh ) );
111 if ( !r.inserted ) throw std::runtime_error( "failed to insert " + std::string{ k } );
112 return r.position->second;
113 }
114
115 public:
116 Store( std::size_t est_size, std::size_t pool_size ) : m_resource{ pool_size }, m_est_size{ est_size } {}
117 [[nodiscard]] bool empty() const { return m_store->empty(); }
118 [[nodiscard]] std::size_t size() const { return m_store->size(); }
119 [[nodiscard]] std::size_t used_bytes() const noexcept { return m_resource.size(); }
120 [[nodiscard]] std::size_t used_blocks() const noexcept { return m_resource.num_blocks(); }
121 [[nodiscard]] std::size_t used_buckets() const { return m_store->bucket_count(); }
122 [[nodiscard]] std::size_t num_allocations() const noexcept { return m_resource.num_allocations(); }
123
124 void reset() {
125 m_store.reset(); // kill the old map
126 m_resource.reset(); // tell the memory pool it can start re-using its resources
127 m_store.emplace( m_est_size, &m_resource ); // initialise the new map with a sane number of buckets
128 }
129
130 const DataObject* put( std::string_view k, std::unique_ptr<DataObject> data,
131 std::unique_ptr<IOpaqueAddress> addr = {} ) {
132 return emplace( k, std::move( data ), std::move( addr ) ).object();
133 }
134 const DataObject* get( std::string_view k ) const noexcept {
135 const Entry* d = find( k );
136 return d ? d->object() : nullptr;
137 }
138 const Entry* find( std::string_view k ) const noexcept {
139 auto i = m_store->find( k );
140 return i != m_store->end() ? &( i->second ) : nullptr;
141 }
142
143 [[nodiscard]] auto begin() const noexcept { return m_store->begin(); }
144 [[nodiscard]] auto end() const noexcept { return m_store->end(); }
145 void clear() noexcept { m_store->clear(); }
146 auto erase( std::string_view k ) { return m_store->erase( k ); }
147 template <typename Predicate>
148 void erase_if( Predicate p ) {
149 auto i = m_store->begin();
150 auto end = m_store->end();
151 while ( i != end ) {
152 if ( std::invoke( p, std::as_const( *i ) ) )
153 i = m_store->erase( i );
154 else
155 ++i;
156 }
157 }
158 };
159
160 StatusCode dummy( const std::string& s ) {
161 std::string trace;
162 System::backTrace( trace, 6, 2 );
163 throw std::logic_error{ "Unsupported Function Called: " + s + "\n" + trace };
164 return StatusCode::FAILURE;
165 }
166
167 std::string_view normalize_path( std::string_view path, std::string_view prefix ) {
168 if ( path.size() >= prefix.size() && std::equal( prefix.begin(), prefix.end(), path.begin() ) )
169 path.remove_prefix( prefix.size() );
170 if ( !path.empty() && path.front() == '/' ) path.remove_prefix( 1 );
171 return path;
172 }
173
174 std::unique_ptr<DataObject> createObj( IConversionSvc& cnv, IOpaqueAddress& addr ) {
175 DataObject* pObject = nullptr;
176 auto status = cnv.createObj( &addr, pObject ); // Call data loader
177 auto object = std::unique_ptr<DataObject>( pObject );
178 if ( status.isFailure() ) object.reset();
179 return object;
180 }
181
182 // HiveWhiteBoard helpers
183 struct Partition final {
184 // Use optional to allow re-constructing in-place without an ugly
185 // exception-unsafe placement-new conconction, and also to make it easier
186 // to pass constructor arguments to Store<>.
187 std::optional<Store<>> store;
188 int eventNumber = -1;
189 };
190
191 template <typename T, typename Mutex = std::recursive_mutex, typename ReadLock = std::scoped_lock<Mutex>,
192 typename WriteLock = ReadLock>
193 class Synced {
194 T m_obj;
195 mutable Mutex m_mtx;
196
197 public:
198 template <typename F>
199 decltype( auto ) with_lock( F&& f ) {
200 WriteLock lock{ m_mtx };
201 return f( m_obj );
202 }
203 template <typename F>
204 decltype( auto ) with_lock( F&& f ) const {
205 ReadLock lock{ m_mtx };
206 return f( m_obj );
207 }
208 };
209 // transform an f(T) into an f(Synced<T>)
210 template <typename Fun>
211 auto with_lock( Fun&& f ) {
212 return [f = std::forward<Fun>( f )]( auto& p ) -> decltype( auto ) { return p.with_lock( f ); };
213 }
214
215 TTHREAD_TLS( Synced<Partition>* ) s_current = nullptr;
216
217 template <typename Fun>
218 StatusCode fwd( Fun&& f ) {
219 return s_current ? s_current->with_lock( std::forward<Fun>( f ) )
221 }
222
223} // namespace
224
235class GAUDI_API EvtStoreSvc : public extends<Service, IDataProviderSvc, IDataManagerSvc, IHiveWhiteBoard> {
236 Gaudi::Property<CLID> m_rootCLID{ this, "RootCLID", 110 /*CLID_Event*/, "CLID of root entry" };
237 Gaudi::Property<std::string> m_rootName{ this, "RootName", "/Event", "name of root entry" };
238 Gaudi::Property<bool> m_forceLeaves{ this, "ForceLeaves", false,
239 "force creation of default leaves on registerObject" };
240 Gaudi::Property<std::string> m_loader{ this, "DataLoader", "EventPersistencySvc" };
241 Gaudi::Property<size_t> m_slots{ this, "EventSlots", 1, "number of event slots" };
242 Gaudi::Property<bool> m_printPoolStats{ this, "PrintPoolStats", false, "Print memory pool statistics" };
243 Gaudi::Property<std::size_t> m_poolSize{ this, "PoolSize", 1024, "Initial per-event memory pool size [KiB]" };
245 "Estimated number of buckets in the store" };
248
249 // Convert to bytes
250 std::size_t poolSize() const { return m_poolSize * 1024; }
251
252 void fillStats( Partition& p ) const {
253 if ( !m_printPoolStats ) return;
254 auto n_allocs = p.store->num_allocations();
255 if ( n_allocs ) {
256 m_storeEntries += p.store->size();
257 m_usedPoolSize += p.store->used_bytes();
258 m_storeBuckets += p.store->used_buckets();
259 m_usedPoolAllocations += p.store->used_blocks();
260 m_servedPoolAllocations += n_allocs;
261 }
262 }
263
264 void initStore( Partition& p ) const {
265 if ( p.store ) {
266 // re-use the existing memory pool
267 p.store->reset();
268 } else {
269 p.store.emplace( m_estStoreBuckets, poolSize() );
270 }
271 }
272
274
276 std::vector<DataStoreItem> m_preLoads;
277
279 std::vector<Synced<Partition>> m_partitions;
280
281 tbb::concurrent_queue<size_t> m_freeSlots;
282
284 this,
285 "InhibitedPathPrefixes",
286 {},
287 "Prefixes of TES locations that will not be loaded by the persistency service " };
289 this, "FollowLinksToAncestors", true,
290 "Load objects which reside in files other than the one corresponding to the root of the event store" };
291 std::string_view m_onlyThisID; // let's be a bit risky... we 'know' when the underlying string goes out of scope...
292
293public:
294 using extends::extends;
295
296 CLID rootCLID() const override;
297 const std::string& rootName() const override;
299
300 size_t allocateStore( int evtnumber ) override;
301 StatusCode freeStore( size_t partition ) override;
302 size_t freeSlots() override { return m_freeSlots.unsafe_size(); }
303 StatusCode selectStore( size_t partition ) override;
304 StatusCode clearStore() override;
305 StatusCode clearStore( size_t partition ) override;
306 StatusCode setNumberOfStores( size_t slots ) override;
307 size_t getNumberOfStores() const override { return m_slots; }
308 size_t getPartitionNumber( int eventnumber ) const override;
309 bool exists( const DataObjID& id ) override {
310 DataObject* pObject{ nullptr };
311 return findObject( id.fullKey(), pObject ).isSuccess();
312 }
313
314 StatusCode objectParent( const DataObject*, IRegistry*& ) override { return dummy( __FUNCTION__ ); }
315 StatusCode objectParent( const IRegistry*, IRegistry*& ) override { return dummy( __FUNCTION__ ); }
316 // Objects have no leaves, so just return success
317 StatusCode objectLeaves( const DataObject*, std::vector<IRegistry*>& ) override { return StatusCode::SUCCESS; }
318 StatusCode objectLeaves( const IRegistry*, std::vector<IRegistry*>& ) override { return StatusCode::SUCCESS; }
319
320 StatusCode clearSubTree( std::string_view ) override;
322 return obj && obj->registry() ? clearSubTree( obj->registry()->identifier() ) : StatusCode::FAILURE;
323 }
324
325 StatusCode traverseSubTree( std::string_view, IDataStoreAgent* ) override;
327 return ( obj && obj->registry() ) ? traverseSubTree( obj->registry()->identifier(), pAgent ) : StatusCode::FAILURE;
328 }
329 StatusCode traverseTree( IDataStoreAgent* pAgent ) override { return traverseSubTree( std::string_view{}, pAgent ); }
330
331 StatusCode setRoot( std::string root_name, DataObject* pObject ) override;
332 StatusCode setRoot( std::string root_path, IOpaqueAddress* pRootAddr ) override;
333
334 StatusCode unregisterAddress( std::string_view ) override { return dummy( __FUNCTION__ ); };
335 StatusCode unregisterAddress( IRegistry*, std::string_view ) override { return dummy( __FUNCTION__ ); };
336
337 StatusCode registerAddress( std::string_view fullPath, IOpaqueAddress* pAddress ) override;
338 StatusCode registerAddress( IRegistry* parentObj, std::string_view objectPath, IOpaqueAddress* pAddress ) override;
339 StatusCode registerObject( std::string_view parentPath, std::string_view objectPath, DataObject* pObject ) override;
340 StatusCode registerObject( DataObject* parentObj, std::string_view objectPath, DataObject* pObject ) override;
341
342 StatusCode unregisterObject( std::string_view ) override;
344 return ( obj && obj->registry() ) ? unregisterObject( obj->registry()->identifier() ) : StatusCode::FAILURE;
345 }
346 StatusCode unregisterObject( DataObject* obj, std::string_view sr ) override {
347 return !obj ? unregisterObject( sr )
348 : obj->registry() ? unregisterObject( ( obj->registry()->identifier() + '/' ).append( sr ) )
350 };
351
352 StatusCode retrieveObject( IRegistry* pDirectory, std::string_view path, DataObject*& pObject ) override;
353
354 StatusCode findObject( IRegistry* pDirectory, std::string_view path, DataObject*& pObject ) override;
355 StatusCode findObject( std::string_view fullPath, DataObject*& pObject ) override;
356
357 StatusCode updateObject( IRegistry* ) override { return dummy( __FUNCTION__ ); }
358 StatusCode updateObject( DataObject* ) override { return dummy( __FUNCTION__ ); }
359
360 StatusCode addPreLoadItem( const DataStoreItem& ) override;
361 StatusCode removePreLoadItem( const DataStoreItem& ) override;
363 m_preLoads.clear();
364 return StatusCode::SUCCESS;
365 }
366 StatusCode preLoad() override;
367
368 StatusCode linkObject( IRegistry*, std::string_view, DataObject* ) override { return dummy( __FUNCTION__ ); }
369 StatusCode linkObject( std::string_view, DataObject* ) override { return dummy( __FUNCTION__ ); }
370 StatusCode unlinkObject( IRegistry*, std::string_view ) override { return dummy( __FUNCTION__ ); }
371 StatusCode unlinkObject( DataObject*, std::string_view ) override { return dummy( __FUNCTION__ ); }
372 StatusCode unlinkObject( std::string_view ) override { return dummy( __FUNCTION__ ); }
373
375 Entry::setDataProviderSvc( this );
376 extends::initialize().ignore();
377 if ( !setNumberOfStores( m_slots ).isSuccess() ) {
378 error() << "Cannot set number of slots" << endmsg;
379 return StatusCode::FAILURE;
380 }
381 m_partitions = std::vector<Synced<Partition>>( m_slots );
382 // m_partitions is now full of empty std::optionals, fill them now.
383 for ( auto& synced_p : m_partitions ) {
384 synced_p.with_lock( [this]( Partition& p ) { initStore( p ); } );
385 }
386 for ( size_t i = 0; i < m_slots; i++ ) { m_freeSlots.push( i ); }
387 selectStore( 0 ).ignore();
388
389 auto loader = serviceLocator()->service( m_loader ).as<IConversionSvc>().get();
390 if ( !loader ) {
391 error() << "Cannot get IConversionSvc " << m_loader.value() << endmsg;
392 return StatusCode::FAILURE;
393 }
394 return setDataLoader( loader, nullptr );
395 }
396 StatusCode finalize() override {
397 if ( m_printPoolStats ) {
398 info() << "Mean memory pool usage: " << float( 1e-3f * float( m_usedPoolSize.mean() ) ) << " KiB serving "
399 << float( m_servedPoolAllocations.mean() ) << " allocations from " << float( m_usedPoolAllocations.mean() )
400 << " to produce " << float( m_storeEntries.mean() ) << " entries in " << float( m_storeBuckets.mean() )
401 << " buckets" << endmsg;
402 }
403 setDataLoader( nullptr, nullptr ).ignore(); // release
404 return extends::finalize();
405 }
406};
407
408// Instantiation of a static factory class used by clients to create
409// instances of this service
411
413const std::string& EvtStoreSvc::rootName() const { return m_rootName; }
415 m_dataLoader = pDataLoader;
416 if ( m_dataLoader ) m_dataLoader->setDataProvider( dpsvc ? dpsvc : this ).ignore();
417 return StatusCode::SUCCESS;
418}
419
420size_t EvtStoreSvc::allocateStore( int evtnumber ) {
421 // take next free slot in the list
422 size_t slot = std::string::npos;
423 if ( m_freeSlots.try_pop( slot ) ) {
424 assert( slot != std::string::npos );
425 assert( slot < m_partitions.size() );
426 [[maybe_unused]] auto prev = m_partitions[slot].with_lock(
427 [evtnumber]( Partition& p ) { return std::exchange( p.eventNumber, evtnumber ); } );
428 assert( prev == -1 ); // or whatever value represents 'free'
429 }
430 return slot;
431}
432
434 if ( slots < size_t{ 1 } ) {
435 error() << "Invalid number of slots (" << slots << ")" << endmsg;
436 return StatusCode::FAILURE;
437 }
439 error() << "Too late to change the number of slots!" << endmsg;
440 return StatusCode::FAILURE;
441 }
442 m_slots = slots;
444 return StatusCode::SUCCESS;
445}
446
447size_t EvtStoreSvc::getPartitionNumber( int eventnumber ) const {
448 auto i = std::find_if( begin( m_partitions ), end( m_partitions ),
449 with_lock( [eventnumber]( const Partition& p ) { return p.eventNumber == eventnumber; } ) );
450 return i != end( m_partitions ) ? std::distance( begin( m_partitions ), i ) : std::string::npos;
451}
452
454 s_current = &m_partitions[partition];
455 return StatusCode::SUCCESS;
456}
457
459 assert( partition < m_partitions.size() );
460 auto prev = m_partitions[partition].with_lock( []( Partition& p ) { return std::exchange( p.eventNumber, -1 ); } );
461 if ( prev == -1 ) return StatusCode::FAILURE; // double free -- should never happen!
462 m_freeSlots.push( partition );
463 return StatusCode::SUCCESS;
464}
465
467 m_onlyThisID = {};
468 return m_partitions[partition].with_lock( [this]( Partition& p ) {
469 fillStats( p );
470 initStore( p ); // replace with a clean store
471 return StatusCode::SUCCESS;
472 } );
473}
474StatusCode EvtStoreSvc::clearSubTree( std::string_view top ) {
475 top = normalize_path( top, rootName() );
476 return fwd( [&]( Partition& p ) {
477 p.store->erase_if( [top]( const auto& value ) { return value.first.starts_with( top ); } );
478 return StatusCode::SUCCESS;
479 } );
480}
482 m_onlyThisID = {};
483 return fwd( [this]( Partition& p ) {
484 fillStats( p );
485 initStore( p ); // replace with a clean store
486 return StatusCode::SUCCESS;
487 } );
488}
490 return fwd( [&]( Partition& p ) {
491 top = normalize_path( top, rootName() );
492 unsigned int nbSlashesInRootName = std::count( rootName().begin(), rootName().end(), '/' );
493 auto cmp = []( const Entry* lhs, const Entry* rhs ) { return lhs->identifier() < rhs->identifier(); };
494 std::set<const Entry*, decltype( cmp )> keys{ std::move( cmp ) };
495 for ( const auto& v : *p.store ) {
496 if ( v.second.identifier().starts_with( top ) ) keys.insert( &v.second );
497 }
498 auto k = keys.begin();
499 while ( k != keys.end() ) {
500 const auto& id = ( *k )->identifier();
501 int level = std::count( id.begin(), id.end(), '/' ) + nbSlashesInRootName;
502 bool accept = pAgent->analyse( const_cast<Entry*>( *( k++ ) ), level );
503 if ( !accept ) {
504 k = std::find_if_not( k, keys.end(), [&id]( const auto& e ) { return e->identifier().starts_with( id ); } );
505 }
506 }
507 return StatusCode::SUCCESS;
508 } );
509}
510StatusCode EvtStoreSvc::setRoot( std::string root_path, DataObject* pObject ) {
511 if ( msgLevel( MSG::DEBUG ) ) {
512 debug() << "setRoot( " << root_path << ", (DataObject*)" << (void*)pObject << " )" << endmsg;
513 }
514 if ( !fwd( []( Partition& p ) {
515 return p.store->empty() ? StatusCode::SUCCESS : StatusCode::FAILURE;
516 } ).isSuccess() ) {
517 throw GaudiException{ "setRoot called with non-empty store", "EvtStoreSvc", StatusCode::FAILURE };
518 }
519 return registerObject( nullptr, root_path, pObject );
520}
521StatusCode EvtStoreSvc::setRoot( std::string root_path, IOpaqueAddress* pRootAddr ) {
522 auto rootAddr = std::unique_ptr<IOpaqueAddress>( pRootAddr );
523 if ( msgLevel( MSG::DEBUG ) ) {
524 debug() << "setRoot( " << root_path << ", (IOpaqueAddress*)" << rootAddr.get();
525 if ( rootAddr ) debug() << "[ " << rootAddr->par()[0] << ", " << rootAddr->par()[1] << " ]";
526 debug() << " )" << endmsg;
527 }
528 if ( !fwd( []( Partition& p ) {
529 return p.store->empty() ? StatusCode::SUCCESS : StatusCode::FAILURE;
530 } ).isSuccess() ) {
531 throw GaudiException{ "setRoot called with non-empty store", "EvtStoreSvc", StatusCode::FAILURE };
532 }
533 if ( !rootAddr ) return Status::INVALID_OBJ_ADDR; // Precondition: Address must be valid
534 if ( !m_followLinksToAncestors ) m_onlyThisID = rootAddr->par()[0];
535 auto object = createObj( *m_dataLoader, *rootAddr ); // Call data loader
536 if ( !object ) return Status::INVALID_OBJECT;
537 if ( msgLevel( MSG::DEBUG ) ) { debug() << "Root Object " << root_path << " created " << endmsg; }
538 LocalArena dummy_arena{ root_path.size() + 1 };
539 auto dummy = Entry{ root_path, {}, {}, &dummy_arena };
540 object->setRegistry( &dummy );
541 rootAddr->setRegistry( &dummy );
542 auto status = m_dataLoader->fillObjRefs( rootAddr.get(), object.get() );
543 if ( status.isSuccess() ) {
544 auto pObject = object.get();
545 status = registerObject( nullptr, root_path, object.release() );
546 if ( status.isSuccess() ) pObject->registry()->setAddress( rootAddr.release() );
547 }
548 return status;
549}
551 return registerAddress( nullptr, path, pAddr );
552}
553StatusCode EvtStoreSvc::registerAddress( IRegistry* pReg, std::string_view path, IOpaqueAddress* pAddr ) {
554 auto addr = std::unique_ptr<IOpaqueAddress>( pAddr );
555 if ( !addr ) return Status::INVALID_OBJ_ADDR; // Precondition: Address must be valid
556 if ( msgLevel( MSG::DEBUG ) ) {
557 debug() << "registerAddress( (IRegistry*)" << (void*)pReg << ", " << path << ", (IOpaqueAddress*)" << addr.get()
558 << "[ " << addr->par()[0] << ", " << addr->par()[1] << " ]"
559 << " )" << endmsg;
560 }
561 if ( path.empty() || path[0] != '/' ) return StatusCode::FAILURE;
562 if ( !m_onlyThisID.empty() && addr->par()[0] != m_onlyThisID ) {
563 if ( msgLevel( MSG::DEBUG ) )
564 debug() << "Attempt to load " << addr->par()[1] << " from file " << addr->par()[0] << " blocked -- different file"
565 << endmsg;
566 return StatusCode::SUCCESS;
567 }
568 if ( std::any_of(
570 [addrPath = addr->par()[1]]( std::string_view prefix ) { return addrPath.starts_with( prefix ); } ) ) {
571 if ( msgLevel( MSG::DEBUG ) )
572 debug() << "Attempt to load " << addr->par()[1] << " from file " << addr->par()[0] << " blocked -- path inhibited"
573 << endmsg;
574 return StatusCode::SUCCESS;
575 }
576
577 auto object = createObj( *m_dataLoader, *addr ); // Call data loader
578 if ( !object ) return Status::INVALID_OBJECT;
579 auto fullpath = ( pReg ? pReg->identifier() : m_rootName.value() ) + std::string{ path };
580 // the data loader expects the path _including_ the root
581 LocalArena dummy_arena{ fullpath.size() + 1 };
582 auto dummy = Entry{ fullpath, {}, {}, &dummy_arena };
583 object->setRegistry( &dummy );
584 addr->setRegistry( &dummy );
585 auto status = m_dataLoader->fillObjRefs( addr.get(), object.get() );
586 if ( !status.isSuccess() ) return status;
587 // note: put will overwrite the registry in pObject to point at the
588 // one actually used -- so we do not dangle, pointing at dummy beyond its
589 // lifetime
590 if ( msgLevel( MSG::DEBUG ) ) {
591 auto ptr = object.get();
592 debug() << "registerAddress: " << std::quoted( normalize_path( fullpath, rootName() ) ) << " (DataObject*)"
593 << static_cast<void*>( ptr ) << ( ptr ? " -> " + System::typeinfoName( typeid( *ptr ) ) : std::string{} )
594 << endmsg;
595 }
596 fwd( [&]( Partition& p ) {
597 p.store->put( normalize_path( fullpath, rootName() ), std::move( object ), std::move( addr ) );
598 return StatusCode::SUCCESS;
599 } ).ignore();
600 return status;
601}
602StatusCode EvtStoreSvc::registerObject( std::string_view parentPath, std::string_view objectPath,
603 DataObject* pObject ) {
604 return parentPath.empty()
605 ? registerObject( nullptr, objectPath, pObject )
606 : registerObject( nullptr, std::string{ parentPath }.append( "/" ).append( objectPath ), pObject );
607}
608StatusCode EvtStoreSvc::registerObject( DataObject* parentObj, std::string_view path, DataObject* pObject ) {
609 if ( parentObj ) return StatusCode::FAILURE;
610 return fwd( [&, object = std::unique_ptr<DataObject>( pObject ),
611 path = normalize_path( path, rootName() )]( Partition& p ) mutable {
612 if ( m_forceLeaves ) {
613 auto dir = path;
614 for ( auto i = dir.rfind( '/' ); i != std::string_view::npos; i = dir.rfind( '/' ) ) {
615 dir = dir.substr( 0, i );
616 if ( !p.store->find( dir ) ) {
617 if ( msgLevel( MSG::DEBUG ) ) {
618 debug() << "registerObject: adding directory " << std::quoted( dir ) << endmsg;
619 }
620 p.store->put( dir, std::unique_ptr<DataObject>{} );
621 }
622 }
623 }
624 if ( msgLevel( MSG::DEBUG ) ) {
625 auto ptr = object.get();
626 debug() << "registerObject: " << std::quoted( path ) << " (DataObject*)" << static_cast<void*>( ptr )
627 << ( ptr ? " -> " + System::typeinfoName( typeid( *ptr ) ) : std::string{} ) << endmsg;
628 }
629 p.store->put( path, std::move( object ) );
630 return StatusCode::SUCCESS;
631 } );
632}
633StatusCode EvtStoreSvc::retrieveObject( IRegistry* pDirectory, std::string_view path, DataObject*& pObject ) {
634 if ( pDirectory ) return StatusCode::FAILURE;
635 return fwd( [&]( Partition& p ) {
636 path = normalize_path( path, rootName() );
637 pObject = const_cast<DataObject*>( p.store->get( path ) );
638 if ( msgLevel( MSG::DEBUG ) ) {
639 debug() << "retrieveObject: " << std::quoted( path ) << " (DataObject*)" << (void*)pObject
640 << ( pObject ? " -> " + System::typeinfoName( typeid( *pObject ) ) : std::string{} ) << endmsg;
641 }
642 return pObject ? StatusCode::SUCCESS : StatusCode::FAILURE;
643 } );
644}
645StatusCode EvtStoreSvc::findObject( IRegistry* pDirectory, std::string_view path, DataObject*& pObject ) {
646 return retrieveObject( pDirectory, path, pObject );
647}
648StatusCode EvtStoreSvc::findObject( std::string_view fullPath, DataObject*& pObject ) {
649 return retrieveObject( nullptr, fullPath, pObject );
650}
652 sr = normalize_path( sr, rootName() );
653 return fwd( [&]( Partition& p ) { return p.store->erase( sr ) != 0 ? StatusCode::SUCCESS : StatusCode::FAILURE; } );
654}
656 auto i = std::find( m_preLoads.begin(), m_preLoads.end(), item );
657 if ( i == m_preLoads.end() ) m_preLoads.push_back( item );
658 return StatusCode::SUCCESS;
659}
661 auto i = std::remove( m_preLoads.begin(), m_preLoads.begin(), item );
662 m_preLoads.erase( i, m_preLoads.end() );
663 return StatusCode::SUCCESS;
664}
666 for ( const auto& i : m_preLoads ) {
667 DataObject* pObj;
668 if ( msgLevel( MSG::DEBUG ) ) debug() << "Preloading " << i.path() << endmsg;
669 retrieveObject( nullptr, i.path(), pObj ).ignore();
670 }
671 return StatusCode::SUCCESS;
672}
unsigned int CLID
Class ID definition.
Definition ClassID.h:16
TTHREAD_TLS(Synced< Partition > *) s_current
#define GAUDI_API
Definition Kernel.h:49
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
A DataObject is the base class of any identifiable object on any data store.
Definition DataObject.h:37
IRegistry * registry() const
Get pointer to Registry.
Definition DataObject.h:79
Description of the DataStoreItem class.
Use a minimal event store implementation, and adds everything required to satisfy the IDataProviderSv...
Gaudi::Property< std::size_t > m_poolSize
Gaudi::Accumulators::AveragingCounter< std::size_t > m_usedPoolAllocations
StatusCode updateObject(IRegistry *) override
bool exists(const DataObjID &id) override
StatusCode unlinkObject(IRegistry *, std::string_view) override
Gaudi::Property< std::vector< std::string > > m_inhibitPrefixes
StatusCode objectLeaves(const IRegistry *, std::vector< IRegistry * > &) override
StatusCode setDataLoader(IConversionSvc *svc, IDataProviderSvc *dpsvc) override
StatusCode finalize() override
SmartIF< IConversionSvc > m_dataLoader
StatusCode traverseSubTree(std::string_view, IDataStoreAgent *) override
StatusCode preLoad() override
StatusCode addPreLoadItem(const DataStoreItem &) override
StatusCode retrieveObject(IRegistry *pDirectory, std::string_view path, DataObject *&pObject) override
std::size_t poolSize() const
StatusCode unregisterAddress(IRegistry *, std::string_view) override
Gaudi::Property< bool > m_followLinksToAncestors
StatusCode updateObject(DataObject *) override
Gaudi::Accumulators::AveragingCounter< std::size_t > m_storeBuckets
StatusCode findObject(IRegistry *pDirectory, std::string_view path, DataObject *&pObject) override
StatusCode removePreLoadItem(const DataStoreItem &) override
StatusCode unregisterObject(DataObject *obj) override
size_t freeSlots() override
StatusCode clearSubTree(std::string_view) override
Gaudi::Property< size_t > m_slots
StatusCode traverseTree(IDataStoreAgent *pAgent) override
StatusCode selectStore(size_t partition) override
Activate a partition object. The identifies the partition uniquely.
Gaudi::Property< CLID > m_rootCLID
StatusCode initialize() override
StatusCode unregisterAddress(std::string_view) override
StatusCode traverseSubTree(DataObject *obj, IDataStoreAgent *pAgent) override
StatusCode registerAddress(std::string_view fullPath, IOpaqueAddress *pAddress) override
Gaudi::Property< std::string > m_loader
Gaudi::Accumulators::AveragingCounter< std::size_t > m_servedPoolAllocations
StatusCode unregisterObject(std::string_view) override
Gaudi::Property< bool > m_printPoolStats
std::vector< Synced< Partition > > m_partitions
The actual store(s).
void fillStats(Partition &p) const
StatusCode setNumberOfStores(size_t slots) override
Set the number of event slots (copies of DataSvc objects).
StatusCode objectParent(const IRegistry *, IRegistry *&) override
StatusCode objectParent(const DataObject *, IRegistry *&) override
StatusCode unregisterObject(DataObject *obj, std::string_view sr) override
void initStore(Partition &p) const
size_t getPartitionNumber(int eventnumber) const override
Get the partition number corresponding to a given event.
StatusCode setRoot(std::string root_name, DataObject *pObject) override
StatusCode clearSubTree(DataObject *obj) override
size_t getNumberOfStores() const override
StatusCode registerObject(std::string_view parentPath, std::string_view objectPath, DataObject *pObject) override
Gaudi::Accumulators::AveragingCounter< std::size_t > m_usedPoolSize
StatusCode resetPreLoad() override
tbb::concurrent_queue< size_t > m_freeSlots
StatusCode linkObject(IRegistry *, std::string_view, DataObject *) override
Gaudi::Property< bool > m_forceLeaves
Gaudi::Accumulators::AveragingCounter< std::size_t > m_storeEntries
StatusCode freeStore(size_t partition) override
Free a store partition.
size_t allocateStore(int evtnumber) override
Allocate a store partition for a given event number.
const std::string & rootName() const override
Gaudi::Property< std::size_t > m_estStoreBuckets
Gaudi::Property< std::string > m_rootName
std::string_view m_onlyThisID
CLID rootCLID() const override
StatusCode objectLeaves(const DataObject *, std::vector< IRegistry * > &) override
StatusCode linkObject(std::string_view, DataObject *) override
StatusCode clearStore() override
StatusCode unlinkObject(std::string_view) override
StatusCode unlinkObject(DataObject *, std::string_view) override
std::vector< DataStoreItem > m_preLoads
Items to be pre-loaded.
A fast memory arena that does not track deallocations.
Definition Monotonic.h:46
static GAUDI_API void setNumConcEvents(const std::size_t &nE)
Implementation of property with value of concrete type.
Definition Property.h:35
Define general base for Gaudi exception.
virtual StatusCode createObj(IOpaqueAddress *pAddress, DataObject *&refpObject)=0
Create the transient representation of an object.
Data provider interface definition.
@ INVALID_ROOT
Invalid root path object cannot be retrieved or stored.
Generic data agent interface.
virtual bool analyse(IRegistry *pObject, int level)=0
Analyse the data object.
Opaque address interface definition.
virtual void setRegistry(IRegistry *r)=0
Update directory pointer.
virtual const std::string * par() const =0
Retrieve String parameters.
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition IRegistry.h:29
virtual const id_type & identifier() const =0
Full identifier (or key).
virtual void setAddress(IOpaqueAddress *pAddress)=0
Set/Update Opaque storage address.
Gaudi::StateMachine::State FSMState() const override
Definition Service.h:55
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition Service.cpp:336
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
void ignore() const
Allow discarding a StatusCode without warning.
Definition StatusCode.h:128
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
Base class used to extend a class implementing other interfaces.
Definition extends.h:19
::Gaudi::Allocator::Arena<::Gaudi::Arena::Monotonic< Alignment, UpstreamAllocator >, T, DefaultResource > MonotonicArena
Definition Monotonic.h:170
auto put(const DataObjectHandle< Out1 > &out_handle, Out2 &&out)
Definition details.h:103
GAUDI_API std::string path(const AIDA::IBaseHistogram *aida)
get the path in THS for AIDA histogram
AttribStringParser::Iterator begin(const AttribStringParser &parser)
auto with_lock(Fun &&f)
get
decorate the vector of properties
Definition decorators.py:94
@ DEBUG
Definition IMessageSvc.h:22
GAUDI_API int backTrace(void **addresses, const int depth)
Definition System.cpp:357
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition System.cpp:254
str release
Definition conf.py:27
constexpr auto size(const T &, Args &&...) noexcept
str prefix
Definition gaudirun.py:361
A counter aiming at computing sum and average.