Files
IfcOpenShell/src/ifcviewer/GpuBudget.cpp
T
Dion Moult 777b728205 ifcviewer: move the live budget only on sustained device readings
A 66-model session oscillated with a ~4 s period — 298 releases in one
log: the pool grew to its ceiling, the next report read ~83 MB free, the
budget dropped and the pool shrank, the reading rebounded, the budget
rose and the pool re-grew, reloading the same chunks each time. Objects
flickered on and off continuously.

The report includes transients the viewer itself creates: the upload
staging behind a burst of chunk loads (~170 MB in that session) and a
released sub-buffer the driver has not yet reclaimed. A budget that
followed every reading fed those straight back into growth decisions.

GpuBudget::update now bounds the cache outright on the first device
report and afterwards moves only on sustained readings: lower when free
memory is below half the margin on two consecutive scheduled reports,
raise when it is above 1.5× the margin on two, and nothing in between.
Transients drain well within a poll interval, so a momentary low never
reaches the pool, while a process that really took memory still does a
second later. A refused allocation (onPressure) is never deferred.

Verified in the saturated regime (working set ~990 MB against a 683 MB
budget, continuous streaming): zero releases over 75 s.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-25 07:42:13 +10:00

87 lines
4.2 KiB
C++

/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
#include "GpuBudget.h"
#include <algorithm>
void GpuBudget::bound(std::uint64_t budget) {
if (hard_cap_ > 0) budget = std::min(budget, hard_cap_);
bounded_ = true;
budget_ = std::max(budget, kMinCacheBudgetBytes);
}
void GpuBudget::setHardCap(std::uint64_t hard_cap_bytes) {
hard_cap_ = hard_cap_bytes;
if (hard_cap_ > 0) bound(bounded_ ? budget_ : hard_cap_);
}
void GpuBudget::update(std::uint64_t device_free_bytes,
std::uint64_t cache_capacity_bytes) {
if (device_free_bytes == 0) return;
const std::uint64_t available = cache_capacity_bytes + device_free_bytes;
const std::uint64_t margin = margin_bytes();
const std::uint64_t reading = available > margin ? available - margin : 0;
if (!had_device_report_) {
had_device_report_ = true;
bound(reading);
return;
}
const bool tight = device_free_bytes < margin / 2;
const bool roomy = device_free_bytes > margin + margin / 2 && reading > budget_;
low_reports_ = tight ? low_reports_ + 1 : 0;
high_reports_ = roomy ? high_reports_ + 1 : 0;
if (low_reports_ >= kConfirmReports) {
bound(std::min(budget_, reading));
low_reports_ = 0;
} else if (high_reports_ >= kConfirmReports) {
bound(reading);
high_reports_ = 0;
}
}
bool GpuBudget::onPressure(std::uint64_t cache_capacity_bytes,
std::uint64_t bytes_needed,
std::uint64_t device_free_bytes) {
++pressure_events_;
// The driver refused bytes_needed while reporting device_free_bytes
// free, so at least (free - needed) of what it reports is not really
// available. Remember that so update() stops short of it next time.
if (device_free_bytes > bytes_needed) {
learned_margin_ = std::max(learned_margin_,
device_free_bytes - bytes_needed + kPressureSlackBytes);
}
// What the cache may keep once the failed allocation and its slack
// have been carved out of what it holds right now. The pool's actual
// capacity, not the previous budget, is the honest baseline: the
// budget may never have been reached (unbounded, or growth refused
// earlier by the driver), and lowering a number the pool never hit
// would free nothing.
const std::uint64_t carve = bytes_needed + kPressureSlackBytes;
const std::uint64_t target = cache_capacity_bytes > carve
? cache_capacity_bytes - carve
: 0;
const std::uint64_t lowered = std::max(target, kMinCacheBudgetBytes);
if (bounded_ && lowered >= budget_) return false;
bound(lowered);
low_reports_ = high_reports_ = 0;
return true;
}