Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RPagePool.hxx
Go to the documentation of this file.
1/// \file ROOT/RPagePool.hxx
2/// \author Jakob Blomer <jblomer@cern.ch>
3/// \date 2018-10-09
4
5/*************************************************************************
6 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 *************************************************************************/
12
13#ifndef ROOT_RPagePool
14#define ROOT_RPagePool
15
16#include <ROOT/RPage.hxx>
19#include <ROOT/RNTupleTypes.hxx>
20#include <ROOT/RSpan.hxx>
21
22#include <cstddef>
23#include <map>
24#include <mutex>
25#include <typeindex>
26#include <typeinfo>
27#include <unordered_map>
28#include <unordered_set>
29#include <utility>
30#include <vector>
31
32namespace ROOT {
33namespace Internal {
34
35class RPageSource;
36
37// clang-format off
38/**
39\class ROOT::Internal::RPagePool
40\ingroup NTuple
41\brief A thread-safe cache of pages loaded from the page source.
42
43The page pool is used as a cache for pages loaded from a page source.
44In this way, identical page needed at the same time, only need to be loaded once.
45Page sources also use the page pool to stage (preload) pages unsealed by IMT tasks.
46*/
47// clang-format on
48class RPagePool {
49 friend class RPageRef;
50
51public:
52 // Search key for a set of pages covering the same column and in-memory target type.
53 // Within the set of pages, one needs to find the page of a given index.
54 struct RKey {
56 std::type_index fInMemoryType = std::type_index(typeid(void));
57
58 bool operator==(const RKey &other) const
59 {
60 return this->fColumnId == other.fColumnId && this->fInMemoryType == other.fInMemoryType;
61 }
62
63 bool operator!=(const RKey &other) const { return !(*this == other); }
64 };
65
66private:
67 /// Hash function to be used in the unordered map fLookupByKey
68 struct RKeyHasher {
69 /// Like boost::hash_combine
70 std::size_t operator()(const RKey &k) const
71 {
72 auto seed = std::hash<ROOT::DescriptorId_t>()(k.fColumnId);
73 return seed ^ (std::hash<std::type_index>()(k.fInMemoryType) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
74 }
75 };
76
77 /// Every page in the page pool is annotated with a search key and a reference counter.
78 struct REntry {
81 std::int64_t fRefCounter = 0;
82 };
83
84 /// Used in fLookupByKey to store both the absolute and the cluster-local page index of the referenced page.
85 /// This allows to do binary search for one or the other. Note that elements in fLookupByKey must have
86 /// _both_ values to be valid. If RPagePosition is used as a search key, only one of the two needs to be set.
118
119 /// Performance counters that get registered in fMetrics
123 std::unique_ptr<RCounters> fCounters;
124
125 /// Every page pool is associated to exactly one page source. The page source is queried for pinned cluster
126 /// when pages are released.
128 std::vector<REntry> fEntries; ///< All cached pages in the page pool
129 /// Used in ReleasePage() to find the page index in fPages
130 std::unordered_map<void *, std::size_t> fLookupByBuffer;
131 /// Used in GetPage() to find the right page in fEntries. Lookup for the key (pair of on-disk and in-memory type)
132 /// takes place in O(1). The selected pages are identified by index into the fEntries vector (map's value)
133 /// and sorted by the position of the page in the column (map's key). Thus, access to pages of the page set
134 /// has logarithmic complexity.
135 std::unordered_map<RKey, std::map<RPagePosition, std::size_t>, RKeyHasher> fLookupByKey;
136 /// Remembers pages with reference counter 0, organized by the page's cluster id. The pages are identified
137 /// by their page buffer address. The fLookupByBuffer map can be used to resolve the address to a page.
138 /// Once a page gets used, it is removed from the unused pages list. Evict will remove all unused pages
139 /// from a given cluster id.
140 std::unordered_map<ROOT::DescriptorId_t, std::unordered_set<void *>> fUnusedPages;
141 std::mutex fLock; ///< The page pool is accessed concurrently due to parallel decompression
142
143 /// The page pool counters are observed by the page source
145
146 /// Add a new page to the fLookupByBuffer and fLookupByKey data structures.
147 REntry &AddPage(RPage page, const RKey &key, std::int64_t initialRefCounter);
148
149 /// Give back a page to the pool and decrease the reference counter. There must not be any pointers anymore into
150 /// this page. If the reference counter drops to zero, the page pool might decide to call the deleter given in
151 /// during registration. Pages of pinned clusters are given back to the "unused pages" pool and are not immedately
152 /// evicted. Called by the RPageRef destructor.
153 void ReleasePage(const RPage &page);
154
155 /// Called by PreloadPage() if the page at hand is new and thus added with ref counter 0
156 void AddToUnusedPages(const RPage &page);
157 /// Called by GetPage(), when the reference counter increases from zero to one
158 void RemoveFromUnusedPages(const RPage &page);
159
160 /// Called both by ReleasePage() and by Evict() to remove an unused page from the pool
161 void ErasePage(std::size_t entryIdx, decltype(fLookupByBuffer)::iterator lookupByBufferItr);
162
163public:
164 explicit RPagePool(RPageSource &pageSource);
165 RPagePool(const RPagePool&) = delete;
166 RPagePool& operator =(const RPagePool&) = delete;
167 ~RPagePool() = default;
168
169 /// Adds a new page to the pool. Upon registration, the page pool takes ownership of the page's memory.
170 /// The new page has its reference counter set to 1.
171 RPageRef RegisterPage(RPage page, RKey key);
172 /// Like RegisterPage() but the reference counter is initialized to 0. In addition, the page is added
173 /// to the set of unused pages of the page's cluster (see Evict()).
174 void PreloadPage(RPage page, RKey key);
175 void PreloadPageV(std::span<std::pair<RPage, RKey>> pages);
176 /// Removes unused pages (pages with reference counter 0) from the page pool. Users of PreloadPage() should
177 /// use Evict() appropriately to avoid accumulation of unused pages.
178 void Evict(ROOT::DescriptorId_t clusterId);
179 /// Tries to find the page corresponding to column and index in the cache. If the page is found, its reference
180 /// counter is increased
181 RPageRef GetPage(RKey key, ROOT::NTupleSize_t globalIndex);
182 RPageRef GetPage(RKey key, RNTupleLocalIndex localIndex);
183
185};
186
187// clang-format off
188/**
189\class ROOT::Internal::RPageRef
190\ingroup NTuple
191\brief Reference to a page stored in the page pool
192
193The referenced page knows about its page pool and decreases the reference counter on destruction.
194*/
195// clang-format on
196class RPageRef {
197 friend class RPagePool;
198
201
202 // Called as delegated constructor and directly by the page pool
203 RPageRef(const RPage &page, RPagePool *pagePool) : fPagePool(pagePool)
204 {
205 // We leave the fPage::fPageAllocator member unset (nullptr), since fPage is a non-owning view on the page
206 fPage.fBuffer = page.fBuffer;
212 }
213
214public:
215 RPageRef() = default;
216 RPageRef(const RPageRef &other) = delete;
217 RPageRef &operator=(const RPageRef &other) = delete;
218
219 RPageRef(RPageRef &&other) noexcept : RPageRef(other.fPage, other.fPagePool) { other.fPagePool = nullptr; }
220
221 RPageRef &operator=(RPageRef &&other) noexcept
222 {
223 if (this != &other) {
224 std::swap(fPage, other.fPage);
225 std::swap(fPagePool, other.fPagePool);
226 }
227 return *this;
228 }
229
231 {
232 if (fPagePool)
234 }
235
236 const RPage &Get() const { return fPage; }
237};
238
239} // namespace Internal
240} // namespace ROOT
241
242#endif
A thread-safe integral performance counter.
A collection of Counter objects with a name, a unit, and a description.
A thread-safe cache of pages loaded from the page source.
Definition RPagePool.hxx:48
std::unique_ptr< RCounters > fCounters
void ErasePage(std::size_t entryIdx, decltype(fLookupByBuffer)::iterator lookupByBufferItr)
Called both by ReleasePage() and by Evict() to remove an unused page from the pool.
Definition RPagePool.cxx:79
ROOT::Experimental::Detail::RNTupleMetrics fMetrics
The page pool counters are observed by the page source.
void Evict(ROOT::DescriptorId_t clusterId)
Removes unused pages (pages with reference counter 0) from the page pool.
REntry & AddPage(RPage page, const RKey &key, std::int64_t initialRefCounter)
Add a new page to the fLookupByBuffer and fLookupByKey data structures.
Definition RPagePool.cxx:31
std::unordered_map< ROOT::DescriptorId_t, std::unordered_set< void * > > fUnusedPages
Remembers pages with reference counter 0, organized by the page's cluster id.
RPagePool(const RPagePool &)=delete
std::mutex fLock
The page pool is accessed concurrently due to parallel decompression.
RPageRef GetPage(RKey key, ROOT::NTupleSize_t globalIndex)
Tries to find the page corresponding to column and index in the cache.
RPagePool(RPageSource &pageSource)
Definition RPagePool.cxx:23
ROOT::Experimental::Detail::RNTupleMetrics & GetMetrics()
std::unordered_map< RKey, std::map< RPagePosition, std::size_t >, RKeyHasher > fLookupByKey
Used in GetPage() to find the right page in fEntries.
void AddToUnusedPages(const RPage &page)
Called by PreloadPage() if the page at hand is new and thus added with ref counter 0.
void PreloadPage(RPage page, RKey key)
Like RegisterPage() but the reference counter is initialized to 0.
Definition RPagePool.cxx:63
RPageRef RegisterPage(RPage page, RKey key)
Adds a new page to the pool.
Definition RPagePool.cxx:57
RPagePool & operator=(const RPagePool &)=delete
void RemoveFromUnusedPages(const RPage &page)
Called by GetPage(), when the reference counter increases from zero to one.
RPageSource & fPageSource
Every page pool is associated to exactly one page source.
std::unordered_map< void *, std::size_t > fLookupByBuffer
Used in ReleasePage() to find the page index in fPages.
std::vector< REntry > fEntries
All cached pages in the page pool.
void ReleasePage(const RPage &page)
Give back a page to the pool and decrease the reference counter.
void PreloadPageV(std::span< std::pair< RPage, RKey > > pages)
Definition RPagePool.cxx:69
Reference to a page stored in the page pool.
RPageRef & operator=(const RPageRef &other)=delete
RPageRef(const RPage &page, RPagePool *pagePool)
RPageRef & operator=(RPageRef &&other) noexcept
const RPage & Get() const
RPageRef(const RPageRef &other)=delete
RPageRef(RPageRef &&other) noexcept
Abstract interface to read data from an ntuple.
ROOT::DescriptorId_t GetId() const
Definition RPage.hxx:62
A page is a slice of a column that is mapped into memory.
Definition RPage.hxx:43
RClusterInfo fClusterInfo
Definition RPage.hxx:75
std::uint32_t fNElements
Definition RPage.hxx:71
std::uint32_t fMaxElements
The capacity of the page in number of elements.
Definition RPage.hxx:73
ROOT::NTupleSize_t fRangeFirst
Definition RPage.hxx:74
std::uint32_t fElementSize
Definition RPage.hxx:70
const RClusterInfo & GetClusterInfo() const
Definition RPage.hxx:126
ROOT::NTupleSize_t GetLocalRangeFirst() const
Definition RPage.hxx:124
Addresses a column element or field item relative to a particular cluster, instead of a global NTuple...
ROOT::NTupleSize_t GetIndexInCluster() const
ROOT::DescriptorId_t GetClusterId() const
std::uint64_t DescriptorId_t
Distriniguishes elements of the same type within a descriptor, e.g. different fields.
constexpr NTupleSize_t kInvalidNTupleIndex
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
constexpr DescriptorId_t kInvalidDescriptorId
Performance counters that get registered in fMetrics.
ROOT::Experimental::Detail::RNTupleAtomicCounter & fNPage
Every page in the page pool is annotated with a search key and a reference counter.
Definition RPagePool.hxx:78
Hash function to be used in the unordered map fLookupByKey.
Definition RPagePool.hxx:68
std::size_t operator()(const RKey &k) const
Like boost::hash_combine.
Definition RPagePool.hxx:70
ROOT::DescriptorId_t fColumnId
Definition RPagePool.hxx:55
bool operator!=(const RKey &other) const
Definition RPagePool.hxx:63
bool operator==(const RKey &other) const
Definition RPagePool.hxx:58
Used in fLookupByKey to store both the absolute and the cluster-local page index of the referenced pa...
Definition RPagePool.hxx:87
bool operator<(const RPagePosition &other) const
Definition RPagePool.hxx:91
RPagePosition(ROOT::NTupleSize_t globalIndex)
RPagePosition(RNTupleLocalIndex localIndex)