![]() |
|
|||
File indexing completed on 2025-09-18 09:32:01
0001 /// \file ROOT/RClusterPool.hxx 0002 /// \ingroup NTuple 0003 /// \author Jakob Blomer <jblomer@cern.ch> 0004 /// \date 2020-03-11 0005 /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback 0006 /// is welcome! 0007 0008 /************************************************************************* 0009 * Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. * 0010 * All rights reserved. * 0011 * * 0012 * For the licensing terms see $ROOTSYS/LICENSE. * 0013 * For the list of contributors see $ROOTSYS/README/CREDITS. * 0014 *************************************************************************/ 0015 0016 #ifndef ROOT_RClusterPool 0017 #define ROOT_RClusterPool 0018 0019 #include <ROOT/RCluster.hxx> 0020 #include <ROOT/RNTupleUtil.hxx> 0021 0022 #include <condition_variable> 0023 #include <deque> 0024 #include <memory> 0025 #include <mutex> 0026 #include <future> 0027 #include <thread> 0028 #include <set> 0029 #include <vector> 0030 0031 namespace ROOT { 0032 namespace Internal { 0033 class RPageSource; 0034 } 0035 0036 namespace Internal { 0037 0038 // clang-format off 0039 /** 0040 \class ROOT::Internal::RClusterPool 0041 \ingroup NTuple 0042 \brief Managed a set of clusters containing compressed and packed pages 0043 0044 The cluster pool steers the preloading of (partial) clusters. There is a two-step pipeline: in a first step, 0045 compressed pages are read from clusters into a memory buffer. The second pipeline step decompresses the pages 0046 and pushes them into the page pool. The actual logic of reading and unzipping is implemented by the page source. 0047 The cluster pool only orchestrates the work queues for reading and unzipping. It uses one extra I/O thread for 0048 reading waits for data from storage and generates no CPU load. 0049 0050 The unzipping step of the pipeline therefore behaves differently depending on whether or not implicit multi-threading 0051 is turned on. If it is turned off, i.e. in a single-threaded environment, the cluster pool will only read the 0052 compressed pages and the page source has to uncompresses pages at a later point when data from the page is requested. 0053 */ 0054 // clang-format on 0055 class RClusterPool { 0056 private: 0057 /// Request to load a subset of the columns of a particular cluster. 0058 /// Work items come in groups and are executed by the page source. 0059 struct RReadItem { 0060 /// Items with different bunch ids are scheduled for different vector reads 0061 std::int64_t fBunchId = -1; 0062 std::promise<std::unique_ptr<RCluster>> fPromise; 0063 RCluster::RKey fClusterKey; 0064 }; 0065 0066 /// Clusters that are currently being processed by the pipeline. Every in-flight cluster has a corresponding 0067 /// work item, first a read item and then an unzip item. 0068 struct RInFlightCluster { 0069 std::future<std::unique_ptr<RCluster>> fFuture; 0070 RCluster::RKey fClusterKey; 0071 0072 bool operator ==(const RInFlightCluster &other) const { 0073 return (fClusterKey.fClusterId == other.fClusterKey.fClusterId) && 0074 (fClusterKey.fPhysicalColumnSet == other.fClusterKey.fPhysicalColumnSet); 0075 } 0076 bool operator !=(const RInFlightCluster &other) const { return !(*this == other); } 0077 /// First order by cluster id, then by number of columns, than by the column ids in fColumns 0078 bool operator <(const RInFlightCluster &other) const; 0079 }; 0080 0081 /// Every cluster pool is responsible for exactly one page source that triggers loading of the clusters 0082 /// (GetCluster()) and is used for implementing the I/O and cluster memory allocation (PageSource::LoadClusters()). 0083 ROOT::Internal::RPageSource &fPageSource; 0084 /// The number of clusters before the currently active cluster that should stay in the pool if present 0085 /// Reserved for later use. 0086 unsigned int fWindowPre = 0; 0087 /// The number of clusters that are being read in a single vector read. 0088 unsigned int fClusterBunchSize; 0089 /// Used as an ever-growing counter in GetCluster() to separate bunches of clusters from each other 0090 std::int64_t fBunchId = 0; 0091 /// The cache of clusters around the currently active cluster 0092 std::vector<std::unique_ptr<RCluster>> fPool; 0093 0094 /// Protects the shared state between the main thread and the I/O thread, namely the work queue and the in-flight 0095 /// clusters vector 0096 std::mutex fLockWorkQueue; 0097 /// The clusters that were handed off to the I/O thread 0098 std::vector<RInFlightCluster> fInFlightClusters; 0099 /// Signals a non-empty I/O work queue 0100 std::condition_variable fCvHasReadWork; 0101 /// The communication channel to the I/O thread 0102 std::deque<RReadItem> fReadQueue; 0103 0104 /// The I/O thread calls RPageSource::LoadClusters() asynchronously. The thread is mostly waiting for the 0105 /// data to arrive (blocked by the kernel) and therefore can safely run in addition to the application 0106 /// main threads. 0107 std::thread fThreadIo; 0108 0109 /// Every cluster id has at most one corresponding RCluster pointer in the pool 0110 RCluster *FindInPool(ROOT::DescriptorId_t clusterId) const; 0111 /// Returns an index of an unused element in fPool; callers of this function (GetCluster() and WaitFor()) 0112 /// make sure that a free slot actually exists 0113 size_t FindFreeSlot() const; 0114 /// The I/O thread routine, there is exactly one I/O thread in-flight for every cluster pool 0115 void ExecReadClusters(); 0116 /// Returns the given cluster from the pool, which needs to contain at least the columns `physicalColumns`. 0117 /// Executed at the end of GetCluster when all missing data pieces have been sent to the load queue. 0118 /// Ideally, the function returns without blocking if the cluster is already in the pool. 0119 RCluster *WaitFor(ROOT::DescriptorId_t clusterId, const RCluster::ColumnSet_t &physicalColumns); 0120 0121 public: 0122 static constexpr unsigned int kDefaultClusterBunchSize = 1; 0123 RClusterPool(ROOT::Internal::RPageSource &pageSource, unsigned int clusterBunchSize); 0124 explicit RClusterPool(ROOT::Internal::RPageSource &pageSource) : RClusterPool(pageSource, kDefaultClusterBunchSize) 0125 { 0126 } 0127 RClusterPool(const RClusterPool &other) = delete; 0128 RClusterPool &operator =(const RClusterPool &other) = delete; 0129 ~RClusterPool(); 0130 0131 /// Returns the requested cluster either from the pool or, in case of a cache miss, lets the I/O thread load 0132 /// the cluster in the pool, blocks until done, and then returns it. Triggers along the way the background loading 0133 /// of the following fWindowPost number of clusters. The returned cluster has at least all the pages of 0134 /// `physicalColumns` and possibly pages of other columns, too. If implicit multi-threading is turned on, the 0135 /// uncompressed pages of the returned cluster are already pushed into the page pool associated with the page source 0136 /// upon return. The cluster remains valid until the next call to GetCluster(). 0137 RCluster *GetCluster(ROOT::DescriptorId_t clusterId, const RCluster::ColumnSet_t &physicalColumns); 0138 0139 /// Used by the unit tests to drain the queue of clusters to be preloaded 0140 void WaitForInFlightClusters(); 0141 }; // class RClusterPool 0142 0143 } // namespace Internal 0144 } // namespace ROOT 0145 0146 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |