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