Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RClusterPool.hxx
Go to the documentation of this file.
1/// \file ROOT/RClusterPool.hxx
2/// \ingroup NTuple
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2020-03-11
5/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
6/// is welcome!
7
8/*************************************************************************
9 * Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. *
10 * All rights reserved. *
11 * *
12 * For the licensing terms see $ROOTSYS/LICENSE. *
13 * For the list of contributors see $ROOTSYS/README/CREDITS. *
14 *************************************************************************/
15
16#ifndef ROOT_RClusterPool
17#define ROOT_RClusterPool
18
19#include <ROOT/RCluster.hxx>
21#include <ROOT/RNTupleTypes.hxx>
22
23#include <condition_variable>
24#include <deque>
25#include <memory>
26#include <mutex>
27#include <future>
28#include <thread>
29#include <unordered_map>
30#include <vector>
31
32namespace ROOT {
33namespace Internal {
34class RPageSource;
35}
36
37namespace Internal {
38
39// clang-format off
40/**
41\class ROOT::Internal::RClusterPool
42\ingroup NTuple
43\brief Managed a set of clusters containing compressed and packed pages
44
45The cluster pool steers the preloading of (partial) clusters. There is a two-step pipeline: in a first step,
46compressed pages are read from clusters into a memory buffer. The second pipeline step decompresses the pages
47and pushes them into the page pool. The actual logic of reading and unzipping is implemented by the page source.
48The cluster pool only orchestrates the work queues for reading and unzipping. It uses one extra I/O thread for
49reading waits for data from storage and generates no CPU load.
50
51The unzipping step of the pipeline therefore behaves differently depending on whether or not implicit multi-threading
52is turned on. If it is turned off, i.e. in a single-threaded environment, the cluster pool will only read the
53compressed pages and the page source has to uncompresses pages at a later point when data from the page is requested.
54*/
55// clang-format on
57private:
58 /// Request to load a subset of the columns of a particular cluster.
59 /// Work items come in groups and are executed by the page source.
60 struct RReadItem {
61 /// Items with different bunch ids are scheduled for different vector reads
62 std::int64_t fBunchId = -1;
63 std::promise<std::unique_ptr<RCluster>> fPromise;
65 };
66
67 /// Clusters that are currently being processed by the pipeline. Every in-flight cluster has a corresponding
68 /// read item.
70 std::future<std::unique_ptr<RCluster>> fFuture;
72
73 bool operator ==(const RInFlightCluster &other) const {
74 return (fClusterKey.fClusterId == other.fClusterKey.fClusterId) &&
75 (fClusterKey.fPhysicalColumnSet == other.fClusterKey.fPhysicalColumnSet);
76 }
77 bool operator !=(const RInFlightCluster &other) const { return !(*this == other); }
78 /// First order by cluster id, then by number of columns, than by the column ids in fColumns
79 bool operator <(const RInFlightCluster &other) const;
80 };
81
82 /// Performance counters that get registered in fMetrics
86 std::unique_ptr<RCounters> fCounters;
87
88 /// Every cluster pool is responsible for exactly one page source that triggers loading of the clusters
89 /// (GetCluster()) and is used for implementing the I/O and cluster memory allocation (PageSource::LoadClusters()).
91 /// The number of clusters that are being read in a single vector read.
92 unsigned int fClusterBunchSize;
93 /// Used as an ever-growing counter in GetCluster() to separate bunches of clusters from each other
94 std::int64_t fBunchId = 0;
95 /// The cache of active clusters and their successors
96 std::unordered_map<ROOT::DescriptorId_t, std::unique_ptr<RCluster>> fPool;
97
98 /// Protects the shared state between the main thread and the I/O thread, namely the work queue and the in-flight
99 /// clusters vector
100 std::mutex fLockWorkQueue;
101 /// The clusters that were handed off to the I/O thread
102 std::vector<RInFlightCluster> fInFlightClusters;
103 /// Signals a non-empty I/O work queue
104 std::condition_variable fCvHasReadWork;
105 /// The communication channel to the I/O thread
106 std::deque<RReadItem> fReadQueue;
107
108 /// The I/O thread calls RPageSource::LoadClusters() asynchronously. The thread is mostly waiting for the
109 /// data to arrive (blocked by the kernel) and therefore can safely run in addition to the application
110 /// main threads.
111 std::thread fThreadIo;
112
113 /// The cluster pool counters are observed by the page source
115
116 /// The I/O thread routine, there is exactly one I/O thread in-flight for every cluster pool
117 void ExecReadClusters();
118 /// Returns the given cluster from the pool, which needs to contain at least the columns `physicalColumns`.
119 /// Executed at the end of GetCluster when all missing data pieces have been sent to the load queue.
120 /// Ideally, the function returns without blocking if the cluster is already in the pool.
122
123public:
124 static constexpr unsigned int kDefaultClusterBunchSize = 1;
132
133 /// Spawn the I/O background thread. No-op if already started.
135
136 /// Stop the I/O background thread. No-op if already stopped. Called by the destructor.
138
139 /// Returns the requested cluster either from the pool or, in case of a cache miss, lets the I/O thread load
140 /// the cluster in the pool, blocks until done, and then returns it. Triggers along the way the background loading
141 /// of the following fClusterBunchSize number of clusters. The returned cluster has at least all the pages of
142 /// `physicalColumns` and possibly pages of other columns, too. If implicit multi-threading is turned on, the
143 /// uncompressed pages of the returned cluster are already pushed into the page pool associated with the page source
144 /// upon return. The cluster remains valid until the next call to GetCluster().
146
147 /// Used by the unit tests to drain the queue of clusters to be preloaded
149
151}; // class RClusterPool
152
153} // namespace Internal
154} // namespace ROOT
155
156#endif
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
A thread-safe integral performance counter.
A collection of Counter objects with a name, a unit, and a description.
Managed a set of clusters containing compressed and packed pages.
RCluster * WaitFor(ROOT::DescriptorId_t clusterId, const RCluster::ColumnSet_t &physicalColumns)
Returns the given cluster from the pool, which needs to contain at least the columns physicalColumns.
std::condition_variable fCvHasReadWork
Signals a non-empty I/O work queue.
ROOT::Experimental::Detail::RNTupleMetrics fMetrics
The cluster pool counters are observed by the page source.
RClusterPool(ROOT::Internal::RPageSource &pageSource)
unsigned int fClusterBunchSize
The number of clusters that are being read in a single vector read.
void WaitForInFlightClusters()
Used by the unit tests to drain the queue of clusters to be preloaded.
std::vector< RInFlightCluster > fInFlightClusters
The clusters that were handed off to the I/O thread.
std::unordered_map< ROOT::DescriptorId_t, std::unique_ptr< RCluster > > fPool
The cache of active clusters and their successors.
std::unique_ptr< RCounters > fCounters
std::deque< RReadItem > fReadQueue
The communication channel to the I/O thread.
void StopBackgroundThread()
Stop the I/O background thread. No-op if already stopped. Called by the destructor.
void ExecReadClusters()
The I/O thread routine, there is exactly one I/O thread in-flight for every cluster pool.
RCluster * GetCluster(ROOT::DescriptorId_t clusterId, const RCluster::ColumnSet_t &physicalColumns)
Returns the requested cluster either from the pool or, in case of a cache miss, lets the I/O thread l...
RClusterPool(ROOT::Internal::RPageSource &pageSource, unsigned int clusterBunchSize)
std::int64_t fBunchId
Used as an ever-growing counter in GetCluster() to separate bunches of clusters from each other.
RClusterPool(const RClusterPool &other)=delete
void StartBackgroundThread()
Spawn the I/O background thread. No-op if already started.
RClusterPool & operator=(const RClusterPool &other)=delete
std::mutex fLockWorkQueue
Protects the shared state between the main thread and the I/O thread, namely the work queue and the i...
ROOT::Internal::RPageSource & fPageSource
Every cluster pool is responsible for exactly one page source that triggers loading of the clusters (...
static constexpr unsigned int kDefaultClusterBunchSize
ROOT::Experimental::Detail::RNTupleMetrics & GetMetrics()
std::thread fThreadIo
The I/O thread calls RPageSource::LoadClusters() asynchronously.
An in-memory subset of the packed and compressed pages of a cluster.
Definition RCluster.hxx:148
std::unordered_set< ROOT::DescriptorId_t > ColumnSet_t
Definition RCluster.hxx:150
Abstract interface to read data from an ntuple.
std::uint64_t DescriptorId_t
Distriniguishes elements of the same type within a descriptor, e.g. different fields.
Performance counters that get registered in fMetrics.
ROOT::Experimental::Detail::RNTupleAtomicCounter & fNCluster
Clusters that are currently being processed by the pipeline.
bool operator!=(const RInFlightCluster &other) const
bool operator<(const RInFlightCluster &other) const
First order by cluster id, then by number of columns, than by the column ids in fColumns.
bool operator==(const RInFlightCluster &other) const
std::future< std::unique_ptr< RCluster > > fFuture
Request to load a subset of the columns of a particular cluster.
std::int64_t fBunchId
Items with different bunch ids are scheduled for different vector reads.
std::promise< std::unique_ptr< RCluster > > fPromise
The identifiers that specifies the content of a (partial) cluster.
Definition RCluster.hxx:152
ROOT::DescriptorId_t fClusterId
Definition RCluster.hxx:153