Project
Loading...
Searching...
No Matches
ReadPlan.cxx
Go to the documentation of this file.
1// Copyright 2019-2026 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#include "AODNative/ReadPlan.h"
13#include <algorithm>
14#include <cstring>
15#include <new>
16#include <sys/mman.h>
17
18namespace o2::aod::native
19{
20
22{
23 std::lock_guard lock{mMutex};
24 auto best = mFree.end();
25 for (auto it = mFree.begin(); it != mFree.end(); ++it) {
26 if (it->capacity >= size && (best == mFree.end() || it->capacity < best->capacity)) {
27 best = it;
28 }
29 }
30 if (best != mFree.end()) {
31 auto buffer = std::move(*best);
32 mFree.erase(best);
33 return buffer;
34 }
35 // All the free buffers are too small: give them back rather than keep them
36 // resident next to the new one. Some headroom, so that the next DF, a bit
37 // larger, still fits.
38 mFree.clear();
39 auto capacity = std::max<size_t>(size + size / 8, 1);
40 void* p = mmap(nullptr, capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
41 if (p == MAP_FAILED) {
42 throw std::bad_alloc();
43 }
44 return {std::unique_ptr<uint8_t[], Unmap>((uint8_t*)p, Unmap{capacity}), capacity};
45}
46
48{
49 munmap(p, size);
50}
51
53{
54 std::lock_guard lock{mMutex};
55 mFree.push_back(std::move(buffer));
56 if (mMaxFreeBytes == 0 && mFree.size() > kMaxFree) {
57 mFree.erase(std::ranges::min_element(mFree, {}, &Buffer::capacity));
58 }
59 auto freeBytes = [this] {
60 size_t total = 0;
61 for (auto const& b : mFree) {
62 total += b.capacity;
63 }
64 return total;
65 };
66 // The same headroom as get() allocates, so that a buffer for a group just
67 // under the budget can still be kept.
68 while (mMaxFreeBytes > 0 && !mFree.empty() && freeBytes() > mMaxFreeBytes + mMaxFreeBytes / 8) {
69 mFree.erase(std::ranges::max_element(mFree, {}, &Buffer::capacity));
70 }
71}
72
73ReadPlan::ReadPlan(std::shared_ptr<ByteSource> source, Options const& options, std::shared_ptr<ReadBufferPool> pool)
74 : mSource(std::move(source)), mPool(pool ? std::move(pool) : std::make_shared<ReadBufferPool>()), mCoalesceGap(options.coalesceGap)
75{
76}
77
79{
80 for (auto& buffer : mBuffers) {
81 mPool->put(std::move(buffer));
82 }
83}
84
86{
87 if (length == 0 || offset > mSource->size() || length > mSource->size() - offset) {
88 // Left to the decoder, which reports it with the branch it belongs to.
89 return;
90 }
91 std::lock_guard lock{mMutex};
92 mPending.emplace_back(offset, length);
93}
94
96{
97 for (auto const& branch : tree.branches) {
98 for (auto const& basket : branch.baskets) {
99 if (!basket.embedded) {
100 add(basket.seek, basket.nbytes);
101 }
102 }
103 }
104}
105
107{
108 uint64_t total = 0;
109 for (auto const& branch : tree.branches) {
110 for (auto const& basket : branch.baskets) {
111 if (!basket.embedded) {
112 total += basket.nbytes;
113 }
114 }
115 }
116 return total;
117}
118
119std::span<const uint8_t> ReadPlan::lookup(uint64_t offset, size_t length) const
120{
121 auto it = std::upper_bound(mChunks.begin(), mChunks.end(), offset, [](uint64_t off, Chunk const& c) { return off < c.offset; });
122 if (it == mChunks.begin()) {
123 return {};
124 }
125 --it;
126 if (offset - it->offset + length > it->size) {
127 return {};
128 }
129 return {it->data + (offset - it->offset), length};
130}
131
132void ReadPlan::fetchPending()
133{
134 std::ranges::sort(mPending);
135 std::vector<Chunk> fetched;
136 for (auto [offset, length] : mPending) {
137 if (!lookup(offset, length).empty()) {
138 continue;
139 }
140 auto end = offset + length;
141 if (!fetched.empty()) {
142 auto& last = fetched.back();
143 auto lastEnd = last.offset + last.size;
144 auto next = std::lower_bound(mChunks.begin(), mChunks.end(), lastEnd, [](Chunk const& c, uint64_t off) { return c.offset < off; });
145 bool bridgesFetched = next != mChunks.end() && next->offset < offset;
146 if (offset <= lastEnd + mCoalesceGap && !bridgesFetched) {
147 if (end > lastEnd) {
148 last.size = end - last.offset;
149 }
150 continue;
151 }
152 }
153 fetched.push_back({offset, nullptr, length});
154 }
155 mPending.clear();
156 if (fetched.empty()) {
157 return;
158 }
159 // One buffer for all the chunks of this fetch. Earlier buffers stay where
160 // they are, so spans handed out before remain valid.
161 size_t total = 0;
162 for (auto& chunk : fetched) {
163 total += chunk.size;
164 }
165 auto buffer = mPool->get(total);
166 std::vector<ReadRequest> requests;
167 auto* at = buffer.data.get();
168 for (auto& chunk : fetched) {
169 chunk.data = at;
170 at += chunk.size;
171 requests.push_back({chunk.offset, chunk.size, chunk.data});
172 }
173 mBuffers.push_back(std::move(buffer));
174 mSource->readvInto(requests);
175 mChunks.insert(mChunks.end(), fetched.begin(), fetched.end());
176 std::ranges::sort(mChunks, {}, &Chunk::offset);
177}
178
179std::span<const uint8_t> ReadPlan::find(uint64_t offset, size_t length)
180{
181 std::lock_guard lock{mMutex};
182 auto bytes = lookup(offset, length);
183 if (bytes.empty() && !mPending.empty()) {
184 fetchPending();
185 bytes = lookup(offset, length);
186 }
187 return bytes;
188}
189
191{
192 std::lock_guard lock{mMutex};
193 if (!mPending.empty()) {
194 fetchPending();
195 }
196}
197
199{
200 std::lock_guard lock{mMutex};
201 size_t total = 0;
202 for (auto const& chunk : mChunks) {
203 total += chunk.size;
204 }
205 return total;
206}
207
208PlannedByteSource::PlannedByteSource(std::shared_ptr<ByteSource> source, std::shared_ptr<ReadPlan> plan)
209 : mSource(std::move(source)), mPlan(std::move(plan))
210{
211}
212
213std::span<const uint8_t> PlannedByteSource::tryMap(uint64_t off, size_t len)
214{
215 auto bytes = mSource->tryMap(off, len);
216 return bytes.empty() ? mPlan->find(off, len) : bytes;
217}
218
220{
221 auto bytes = mPlan->find(off, len);
222 if (bytes.empty()) {
223 mSource->readInto(off, len, dst);
224 return;
225 }
226 std::memcpy(dst, bytes.data(), len);
227}
228
229void PlannedByteSource::readvInto(std::span<ReadRequest> requests)
230{
231 mSource->readvInto(requests);
232}
233
234} // namespace o2::aod::native
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char Directory with events to Events directory is and not inside events Show standalone event Run event generator maximum time bin of continuous for triggered for automatic continuous for automatic continuous triggered uint64_t
uint32_t c
Definition RawData.h:2
size_t bytes
void readInto(uint64_t off, size_t len, void *dst) override
Throws IoError, or CorruptData when the range is past the end of the file.
Definition ReadPlan.cxx:219
void readvInto(std::span< ReadRequest > requests) override
Definition ReadPlan.cxx:229
PlannedByteSource(std::shared_ptr< ByteSource > source, std::shared_ptr< ReadPlan > plan)
Definition ReadPlan.cxx:208
std::span< const uint8_t > tryMap(uint64_t off, size_t len) override
A view of the bytes without copying them, if the backend can provide it.
Definition ReadPlan.cxx:213
void put(Buffer buffer)
Definition ReadPlan.cxx:52
Buffer get(size_t size)
The smallest free buffer of at least size bytes, or a new one.
Definition ReadPlan.cxx:21
void fetch()
Fetches the pending ranges now, rather than on the first find().
Definition ReadPlan.cxx:190
static uint64_t bytesOf(TreeInfo const &tree)
The bytes add(tree) registers.
Definition ReadPlan.cxx:106
std::span< const uint8_t > find(uint64_t offset, size_t length)
Definition ReadPlan.cxx:179
ReadPlan(std::shared_ptr< ByteSource > source, Options const &options, std::shared_ptr< ReadBufferPool > pool=nullptr)
Without a pool, the plan allocates its own buffers.
Definition ReadPlan.cxx:73
void add(uint64_t offset, size_t length)
Definition ReadPlan.cxx:85
size_t bytesHeld() const
Definition ReadPlan.cxx:198
GLuint buffer
Definition glcorearb.h:655
GLsizeiptr size
Definition glcorearb.h:659
GLuint GLuint end
Definition glcorearb.h:469
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLsizei GLsizei GLchar * source
Definition glcorearb.h:798
GLenum GLenum dst
Definition glcorearb.h:1767
GLintptr offset
Definition glcorearb.h:660
GLuint GLsizei GLsizei * length
Definition glcorearb.h:790
GLenum GLenum GLsizei len
Definition glcorearb.h:4232
void empty(int)
void operator()(uint8_t *p) const
Definition ReadPlan.cxx:47
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))