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
21#include <cstddef>
22#include <map>
23#include <mutex>
24#include <typeindex>
25#include <typeinfo>
26#include <unordered_map>
27#include <unordered_set>
28#include <vector>
29
30namespace ROOT {
31namespace Internal {
32
33class RPageSource;
34
35// clang-format off
36/**
37\class ROOT::Internal::RPagePool
38\ingroup NTuple
39\brief A thread-safe cache of pages loaded from the page source.
40
41The page pool is used as a cache for pages loaded from a page source.
42In this way, identical page needed at the same time, only need to be loaded once.
43Page sources also use the page pool to stage (preload) pages unsealed by IMT tasks.
44*/
45// clang-format on
46class RPagePool {
47 friend class RPageRef;
48
49public:
50 // Search key for a set of pages covering the same column and in-memory target type.
51 // Within the set of pages, one needs to find the page of a given index.
52 struct RKey {
54 std::type_index fInMemoryType = std::type_index(typeid(void));
55
56 bool operator==(const RKey &other) const
57 {
58 return this->fColumnId == other.fColumnId && this->fInMemoryType == other.fInMemoryType;
59 }
60
61 bool operator!=(const RKey &other) const { return !(*this == other); }
62 };
63
64private:
65 /// Hash function to be used in the unordered map fLookupByKey
66 struct RKeyHasher {
67 /// Like boost::hash_combine
68 std::size_t operator()(const RKey &k) const
69 {
70 auto seed = std::hash<ROOT::DescriptorId_t>()(k.fColumnId);
71 return seed ^ (std::hash<std::type_index>()(k.fInMemoryType) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
72 }
73 };
74
75 /// Every page in the page pool is annotated with a search key and a reference counter.
76 struct REntry {
79 std::int64_t fRefCounter = 0;
80 };
81
82 /// Used in fLookupByKey to store both the absolute and the cluster-local page index of the referenced page.
83 /// This allows to do binary search for one or the other. Note that elements in fLookupByKey must have
84 /// _both_ values to be valid. If RPagePosition is used as a search key, only one of the two needs to be set.
88
104
105 // Constructor used to store a page in fLookupByKey
106 explicit RPagePosition(const RPage &page)
107 : fGlobalFirstElement(page.GetGlobalRangeFirst()),
109 {
110 }
111
112 // Search key constructors
113 explicit RPagePosition(ROOT::NTupleSize_t globalIndex) : fGlobalFirstElement(globalIndex) {}
114 explicit RPagePosition(RNTupleLocalIndex localIndex) : fClusterFirstElement(localIndex) {}
115 };
116
117 /// Performance counters that get registered in fMetrics
121 std::unique_ptr<RCounters> fCounters;
122
123 /// Every page pool is associated to exactly one page source. The page source is queried for pinned cluster
124 /// when pages are released.
126 std::vector<REntry> fEntries; ///< All cached pages in the page pool
127 /// Used in ReleasePage() to find the page index in fPages
128 std::unordered_map<void *, std::size_t> fLookupByBuffer;
129 /// Used in GetPage() to find the right page in fEntries. Lookup for the key (pair of on-disk and in-memory type)
130 /// takes place in O(1). The selected pages are identified by index into the fEntries vector (map's value)
131 /// and sorted by the position of the page in the column (map's key). Thus, access to pages of the page set
132 /// has logarithmic complexity.
133 std::unordered_map<RKey, std::map<RPagePosition, std::size_t>, RKeyHasher> fLookupByKey;
134 /// Remembers pages with reference counter 0, organized by the page's cluster id. The pages are identified
135 /// by their page buffer address. The fLookupByBuffer map can be used to resolve the address to a page.
136 /// Once a page gets used, it is removed from the unused pages list. Evict will remove all unused pages
137 /// from a given cluster id.
138 std::unordered_map<ROOT::DescriptorId_t, std::unordered_set<void *>> fUnusedPages;
139 std::mutex fLock; ///< The page pool is accessed concurrently due to parallel decompression
140
141 /// The page pool counters are observed by the page source
143
144 /// Add a new page to the fLookupByBuffer and fLookupByKey data structures.
145 REntry &AddPage(RPage page, const RKey &key, std::int64_t initialRefCounter);
146
147 /// Give back a page to the pool and decrease the reference counter. There must not be any pointers anymore into
148 /// this page. If the reference counter drops to zero, the page pool might decide to call the deleter given in
149 /// during registration. Pages of pinned clusters are given back to the "unused pages" pool and are not immedately
150 /// evicted. Called by the RPageRef destructor.
151 void ReleasePage(const RPage &page);
152
153 /// Called by PreloadPage() if the page at hand is new and thus added with ref counter 0
154 void AddToUnusedPages(const RPage &page);
155 /// Called by GetPage(), when the reference counter increases from zero to one
156 void RemoveFromUnusedPages(const RPage &page);
157
158 /// Called both by ReleasePage() and by Evict() to remove an unused page from the pool
159 void ErasePage(std::size_t entryIdx, decltype(fLookupByBuffer)::iterator lookupByBufferItr);
160
161public:
162 explicit RPagePool(RPageSource &pageSource);
163 RPagePool(const RPagePool&) = delete;
164 RPagePool& operator =(const RPagePool&) = delete;
165 ~RPagePool() = default;
166
167 /// Adds a new page to the pool. Upon registration, the page pool takes ownership of the page's memory.
168 /// The new page has its reference counter set to 1.
169 RPageRef RegisterPage(RPage page, RKey key);
170 /// Like RegisterPage() but the reference counter is initialized to 0. In addition, the page is added
171 /// to the set of unused pages of the page's cluster (see Evict()).
172 void PreloadPage(RPage page, RKey key);
173 /// Removes unused pages (pages with reference counter 0) from the page pool. Users of PreloadPage() should
174 /// use Evict() appropriately to avoid accumulation of unused pages.
175 void Evict(ROOT::DescriptorId_t clusterId);
176 /// Tries to find the page corresponding to column and index in the cache. If the page is found, its reference
177 /// counter is increased
178 RPageRef GetPage(RKey key, ROOT::NTupleSize_t globalIndex);
179 RPageRef GetPage(RKey key, RNTupleLocalIndex localIndex);
180
182};
183
184// clang-format off
185/**
186\class ROOT::Internal::RPageRef
187\ingroup NTuple
188\brief Reference to a page stored in the page pool
189
190The referenced page knows about its page pool and decreases the reference counter on destruction.
191*/
192// clang-format on
193class RPageRef {
194 friend class RPagePool;
195
198
199 // Called as delegated constructor and directly by the page pool
200 RPageRef(const RPage &page, RPagePool *pagePool) : fPagePool(pagePool)
201 {
202 // We leave the fPage::fPageAllocator member unset (nullptr), since fPage is a non-owning view on the page
203 fPage.fBuffer = page.fBuffer;
204 fPage.fElementSize = page.fElementSize;
205 fPage.fNElements = page.fNElements;
206 fPage.fMaxElements = page.fMaxElements;
207 fPage.fRangeFirst = page.fRangeFirst;
208 fPage.fClusterInfo = page.fClusterInfo;
209 }
210
211public:
212 RPageRef() = default;
213 RPageRef(const RPageRef &other) = delete;
214 RPageRef &operator=(const RPageRef &other) = delete;
215
216 RPageRef(RPageRef &&other) : RPageRef(other.fPage, other.fPagePool) { other.fPagePool = nullptr; }
217
219 {
220 if (this != &other) {
221 std::swap(fPage, other.fPage);
222 std::swap(fPagePool, other.fPagePool);
223 }
224 return *this;
225 }
226
228 {
229 if (fPagePool)
230 fPagePool->ReleasePage(fPage);
231 }
232
233 const RPage &Get() const { return fPage; }
234};
235
236} // namespace Internal
237} // namespace ROOT
238
239#endif
A thread-safe integral performance counter.
A collection of Counter objects with a name, a unit, and a description.
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:71
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.
Definition RPagePool.cxx:97
RPageRef(RPageRef &&other)
RPageRef & operator=(const RPageRef &other)=delete
RPageRef & operator=(RPageRef &&other)
RPageRef(const RPage &page, RPagePool *pagePool)
const RPage & Get() const
RPageRef(const RPageRef &other)=delete
Abstract interface to read data from an ntuple.
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:76
Hash function to be used in the unordered map fLookupByKey.
Definition RPagePool.hxx:66
std::size_t operator()(const RKey &k) const
Like boost::hash_combine.
Definition RPagePool.hxx:68
ROOT::DescriptorId_t fColumnId
Definition RPagePool.hxx:53
bool operator!=(const RKey &other) const
Definition RPagePool.hxx:61
bool operator==(const RKey &other) const
Definition RPagePool.hxx:56
bool operator<(const RPagePosition &other) const
Definition RPagePool.hxx:89
RPagePosition(ROOT::NTupleSize_t globalIndex)
RPagePosition(RNTupleLocalIndex localIndex)