Project
Loading...
Searching...
No Matches
BoundedAllocator.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
13
14#include <cstdio>
15#include <format>
16
17#include "GPUCommonLogger.h"
18
19namespace o2::gtrk
20{
21namespace
22{
23constexpr double kGigabyte = 1024. * 1024. * 1024.;
24}
25
27{
28 char buf[256];
29 if (attempted != 0) {
30 (void)snprintf(buf, sizeof(buf), "Reached set memory limit (attempted: %zu, used: %zu, max: %zu)", attempted, used, max);
31 } else {
32 (void)snprintf(buf, sizeof(buf), "New set maximum below current used (newMax: %zu, used: %zu)", max, used);
33 }
34 mMsg = buf;
35}
36
38{
39 return mMsg.c_str();
40}
41
43{
44 static std::pmr::synchronized_pool_resource pool{std::pmr::get_default_resource()};
45 return &pool;
46}
47
49 : mMaxMemory(maxBytes), mUpstream(upstream != nullptr ? upstream : cachingUpstream())
50{
51}
52
53BoundedMemoryResource::BoundedMemoryResource(std::unique_ptr<std::pmr::memory_resource> upstream, size_t maxBytes)
54 : mMaxMemory(maxBytes), mOwnedUpstream(std::move(upstream)), mUpstream(mOwnedUpstream.get())
55{
56}
57
58void* BoundedMemoryResource::do_allocate(size_t bytes, size_t alignment)
59{
60 size_t newUsed{0};
61 size_t currentUsed{mUsedMemory.load(std::memory_order_relaxed)};
62 for (;;) {
63 const size_t limit = mMaxMemory.load(std::memory_order_relaxed);
64 // Test before adding, so even SIZE_MAX requests cannot wrap under budget.
65 if (currentUsed > limit || bytes > limit - currentUsed) {
66 const size_t attempted = bytes > std::numeric_limits<size_t>::max() - currentUsed ? std::numeric_limits<size_t>::max() : currentUsed + bytes;
67 mCountThrow.fetch_add(1, std::memory_order_relaxed);
68 throw MemoryLimitExceeded(attempted, currentUsed, limit);
69 }
70 newUsed = currentUsed + bytes;
71 if (mUsedMemory.compare_exchange_weak(currentUsed, newUsed, std::memory_order_acq_rel, std::memory_order_relaxed)) {
72 break;
73 }
74 }
75
76 void* p{nullptr};
77 try {
78 p = mUpstream->allocate(bytes, alignment);
79 } catch (...) {
80 mUsedMemory.fetch_sub(bytes, std::memory_order_relaxed);
81#ifdef BOUNDED_MR_STATS
82 mStats.upstreamFailures.fetch_add(1, std::memory_order_relaxed);
83#endif
84 throw;
85 }
86
87 mAllocations.fetch_add(1, std::memory_order_relaxed);
88 mAllocatedBytes.fetch_add(bytes, std::memory_order_relaxed);
89
90 size_t peak = mPeakUsedMemory.load(std::memory_order_relaxed);
91 while (newUsed > peak && !mPeakUsedMemory.compare_exchange_weak(peak, newUsed, std::memory_order_relaxed)) {
92 }
93
94#ifdef BOUNDED_MR_STATS
95 size_t statsPeak = mStats.peak.load(std::memory_order_relaxed);
96 while (newUsed > statsPeak && !mStats.peak.compare_exchange_weak(statsPeak, newUsed, std::memory_order_relaxed)) {
97 }
98 mStats.live.fetch_add(1, std::memory_order_relaxed);
99 mStats.nAlloc.fetch_add(1, std::memory_order_relaxed);
100 mStats.totalAlloc.fetch_add(bytes, std::memory_order_relaxed);
101
102 size_t maxAlignment = mStats.maxAlign.load(std::memory_order_relaxed);
103 while (alignment > maxAlignment && !mStats.maxAlign.compare_exchange_weak(maxAlignment, alignment, std::memory_order_relaxed)) {
104 }
105#endif
106 return p;
107}
108
109void BoundedMemoryResource::do_deallocate(void* p, size_t bytes, size_t alignment)
110{
111 mUpstream->deallocate(p, bytes, alignment);
112 mUsedMemory.fetch_sub(bytes, std::memory_order_relaxed);
113#ifdef BOUNDED_MR_STATS
114 mStats.live.fetch_sub(1, std::memory_order_relaxed);
115 mStats.nFree.fetch_add(1, std::memory_order_relaxed);
116 mStats.totalFreed.fetch_add(bytes, std::memory_order_relaxed);
117#endif
118}
119
120bool BoundedMemoryResource::do_is_equal(const std::pmr::memory_resource& other) const noexcept
121{
122 return this == &other;
123}
124
126{
127 return mUsedMemory.load(std::memory_order_relaxed);
128}
129
131{
132 return mMaxMemory.load(std::memory_order_relaxed);
133}
134
136{
137 return mCountThrow.load(std::memory_order_relaxed);
138}
139
141{
142 return mPeakUsedMemory.load(std::memory_order_relaxed);
143}
144
146{
147 const size_t peak = mPeakUsedMemory.load(std::memory_order_relaxed);
148 const size_t baseline = mPeakBaselineMemory.load(std::memory_order_relaxed);
149 return peak > baseline ? peak - baseline : 0;
150}
151
153{
154 const size_t used = mUsedMemory.load(std::memory_order_acquire);
155 mPeakBaselineMemory.store(used, std::memory_order_release);
156 return mPeakUsedMemory.exchange(used, std::memory_order_acq_rel);
157}
158
160{
161 size_t current = mPeakUsedMemory.load(std::memory_order_relaxed);
162 while (peak > current && !mPeakUsedMemory.compare_exchange_weak(current, peak, std::memory_order_relaxed)) {
163 }
164}
165
167{
168 size_t current = mMaxMemory.load(std::memory_order_relaxed);
169 if (max == current) {
170 return;
171 }
172 for (;;) {
173 const size_t used = mUsedMemory.load(std::memory_order_acquire);
174 if (used > max) {
175 mCountThrow.fetch_add(1, std::memory_order_relaxed);
176 throw MemoryLimitExceeded(0, used, max);
177 }
178 if (mMaxMemory.compare_exchange_weak(current, max, std::memory_order_release, std::memory_order_relaxed)) {
179 return;
180 }
181 if (current == max) {
182 return;
183 }
184 }
185}
186
188{
189 const auto throwCount = mCountThrow.load(std::memory_order_relaxed);
190 const auto used = static_cast<double>(mUsedMemory.load(std::memory_order_relaxed));
191 const auto peak = static_cast<double>(mPeakUsedMemory.load(std::memory_order_relaxed));
192 const auto peakDelta = static_cast<double>(getPeakMemoryDelta());
193 const auto maxMemory = mMaxMemory.load(std::memory_order_relaxed);
194 std::string result;
195 if (maxMemory == std::numeric_limits<size_t>::max()) {
196 result += std::format("maxthrow={} maxmem=unbounded used={:.2f} GB stagepeak={:.2f} GB stagealloc={:.2f} GB", throwCount, used / kGigabyte, peak / kGigabyte, peakDelta / kGigabyte);
197 } else {
198 result += std::format("maxthrow={} maxmem={:.2f} GB used={:.2f} GB ({:.2f}%) stagepeak={:.2f} GB stagealloc={:.2f} GB", throwCount, static_cast<double>(maxMemory) / kGigabyte, used / kGigabyte, 100.0 * used / static_cast<double>(maxMemory), peak / kGigabyte, peakDelta / kGigabyte);
199 }
200#ifdef BOUNDED_MR_STATS
201 result += std::format(" peak={:.2f} GB live={} nAlloc={} nFree={} totalAlloc={:.2f} GB totalFreed={:.2f} GB maxAlign={} upstreamFail={}",
202 static_cast<float>(mStats.peak.load(std::memory_order_relaxed)) / kGigabyte,
203 mStats.live.load(std::memory_order_relaxed),
204 mStats.nAlloc.load(std::memory_order_relaxed),
205 mStats.nFree.load(std::memory_order_relaxed),
206 static_cast<float>(mStats.totalAlloc.load(std::memory_order_relaxed)) / kGigabyte,
207 static_cast<float>(mStats.totalFreed.load(std::memory_order_relaxed)) / kGigabyte,
208 mStats.maxAlign.load(std::memory_order_relaxed),
209 mStats.upstreamFailures.load(std::memory_order_relaxed));
210#endif
211 return result;
212}
213
215{
216 LOGP(info, "{}", asString());
217}
218
219} // namespace o2::gtrk
configStandalone Reconstruction rec_tpc multiplier for the combined cluster track error during track following area in cm for the search of for z only used if decrease removal by when above this local occupancy rowx Shift of TPC revert cluster flag edge bit distance to edge exceeds this ZOffset for secondary multiple of radius max P of electron K BB bands crossing max P of electron p BB bands crossing max P of electron d BB bands crossing max P of electron t BB bands crossing min accepted dEdxMaxTPC of the track Additive extra cluster error for Y2 if edge flag set Additive extra cluster error for Y2 if single set Additive extra cluster error for Y2 if or single set Additive extra cluster error for Z2 if or single set Extra errors to add to track when crossing depending on addErrorsCECrossing Inner portion of y range in sector that is not used in searching for extrapolated track candidates factor2YS for track merging factor2K for track merging Margin in cm of Y position when rejecting edge clusters based on uncorrected track Y Max fraction of shared clusters for tracklet Except pad from cluster finding if total number of charges in a fragment is above this baseline(disable=0)") AddOptionRTC(maxConsecTimeBinAboveThreshold
Shared budgeted host allocation and bounded-vector utilities.
size_t bytes
MemoryLimitExceeded(size_t attempted, size_t used, size_t max)
static std::pmr::memory_resource * cachingUpstream()
size_t getThrowCount() const noexcept
size_t getMaxMemory() const noexcept
void raisePeakMemory(size_t peak) noexcept
size_t getUsedMemory() const noexcept
BoundedMemoryResource(size_t maxBytes=std::numeric_limits< size_t >::max(), std::pmr::memory_resource *upstream=nullptr)
size_t getPeakMemoryDelta() const noexcept
size_t getPeakMemory() const noexcept
GLuint64EXT * result
Definition glcorearb.h:5662
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition glcorearb.h:2514
constexpr size_t max
VectorOfTObjectPtrs other