Unravel Engine C++ Reference
Loading...
Searching...
No Matches
eviction.h
Go to the documentation of this file.
1#pragma once
2
3#include "evictable.h"
4#include "graphics.h"
5
6#include <memory>
7#include <vector>
8
15namespace gfx::eviction
16{
17
18using backing_buffer = std::shared_ptr<std::vector<std::uint8_t>>;
19
20inline auto make_backing(const void* data, std::uint32_t size) -> backing_buffer
21{
22 const auto* bytes = static_cast<const std::uint8_t*>(data);
23 return std::make_shared<std::vector<std::uint8_t>>(bytes, bytes + size);
24}
25
26inline void release_backing_ref(void* /*data*/, void* user_data)
27{
28 delete static_cast<backing_buffer*>(user_data);
29}
30
31inline auto make_backing_ref(const backing_buffer& backing) -> const memory_view*
32{
33 auto* bgfx_ref = new backing_buffer(backing);
34 return gfx::make_ref((*bgfx_ref)->data(),
35 static_cast<std::uint32_t>((*bgfx_ref)->size()),
37 bgfx_ref);
38}
39
41enum class init_status : std::uint8_t
42{
43 ok,
46 failed,
47};
48
50enum class strategy : std::uint8_t
51{
52 lru,
53 lfu,
55 age_ttl,
56};
57
62enum class reclaim_result : std::uint8_t
63{
64 headroom,
65 reclaimed,
67};
68
70constexpr auto to_string(reclaim_result r) -> const char*
71{
72 switch(r)
73 {
74 case reclaim_result::headroom: return "headroom";
75 case reclaim_result::reclaimed: return "reclaimed";
76 case reclaim_result::insufficient: return "insufficient";
77 }
78 return "unknown";
79}
80
82enum class reclaim_kind : std::uint8_t
83{
90};
91
105{
106 std::uint64_t hard_limit_bytes = 0;
107 std::uint64_t soft_budget_bytes = 0;
108 std::uint64_t target_bytes = 0;
109 std::uint64_t safety_margin_bytes = 0;
110};
111
113struct config
114{
119 std::uint64_t budget_bytes = 0;
121 std::uint64_t target_bytes = 0;
123 std::uint32_t min_age_frames = 60;
125 std::uint32_t max_idle_frames = 0;
127 std::uint32_t max_evictions = 0;
128};
129
131struct stats
132{
133 std::uint64_t resident_count = 0;
134 std::uint64_t resident_bytes = 0;
135 std::uint64_t evicted_count = 0;
136 std::uint64_t evicted_bytes = 0;
137 std::uint64_t registered_count = 0;
138 std::uint64_t total_evictions = 0;
139 std::uint64_t total_restores = 0;
140 std::uint64_t total_bytes_evicted = 0;
141 std::uint64_t total_bytes_restored = 0;
142 std::uint64_t failed_restores = 0;
143 std::uint64_t thrash_events = 0;
144 std::uint64_t budget_bytes = 0;
145 std::uint64_t target_bytes = 0;
146 std::uint64_t budget_used_bytes = 0;
147 std::uint64_t last_pass_scanned = 0;
148 std::uint64_t last_pass_evicted = 0;
149 std::uint64_t last_pass_freed_bytes = 0;
150 std::uint64_t pending_release_bytes = 0;
151 double last_pass_ms = 0.0;
152 double last_restore_ms = 0.0;
153};
154
159auto init(const config& cfg = {}) -> init_status;
160
163auto is_supported() -> bool;
164
167void shutdown();
168
171auto evict(const config& cfg) -> stats;
172
174auto evict() -> stats;
175
181auto evict_bytes(std::uint64_t free_bytes,
182 strategy strat = strategy::lru,
183 std::uint32_t min_age_frames = 0,
184 std::uint32_t max_evictions = 0) -> stats;
185
188auto evict_all() -> stats;
189
199
202auto would_allocation_fit(std::uint64_t bytes) -> bool;
203
208auto restore_all() -> stats;
209
214void set_budget(const budget_state& budget, std::uint64_t used_bytes);
215
218auto current_budget() -> budget_state;
219
224auto peek_queued_bytes() -> std::uint64_t;
225
228auto peek_pending_release_bytes() -> std::uint64_t;
229
232auto peek_external_queued_bytes() -> std::uint64_t;
233
238void note_pending_allocation(std::uint64_t bytes);
239
243
245auto get_stats() -> stats;
246
248void set_frame(std::uint64_t frame);
249
251void advance_frame();
252
253// --- Test / diagnostics ---------------------------------------------------------------------
254
262auto debug_consume_memory(std::uint64_t bytes) -> std::uint64_t;
263
270auto debug_simulate_budget(std::uint64_t target_free_bytes) -> std::uint64_t;
271
275
277auto debug_consumed_bytes() -> std::uint64_t;
278
279// --- Internal surface used by gfx::handle_impl ----------------------------------------------
280
282void register_resource(ievictable* resource);
283
287
289void unregister_resource(ievictable* resource);
290
293void restore_resource(ievictable* resource);
294
295} // namespace gfx::eviction
std::uint64_t bytes
Definition eviction.cpp:823
uint32_t frame
Definition graphics.cpp:23
void note_pending_allocation(std::uint64_t bytes)
Definition eviction.cpp:806
auto is_supported() -> bool
Definition eviction.cpp:650
void restore_resource(ievictable *resource)
Definition eviction.cpp:987
auto make_backing_ref(const backing_buffer &backing) -> const memory_view *
Definition eviction.h:31
auto current_budget() -> budget_state
Definition eviction.cpp:948
auto evict() -> stats
Run a single eviction pass using the config supplied to init.
Definition eviction.cpp:669
auto get_stats() -> stats
Snapshot the current statistics.
Definition eviction.cpp:953
auto restore_all() -> stats
Definition eviction.cpp:923
auto debug_simulate_budget(std::uint64_t target_free_bytes) -> std::uint64_t
Definition eviction.cpp:875
std::shared_ptr< std::vector< std::uint8_t > > backing_buffer
Definition eviction.h:18
auto debug_consume_memory(std::uint64_t bytes) -> std::uint64_t
Definition eviction.cpp:852
auto evict_bytes(std::uint64_t free_bytes, strategy strat, std::uint32_t min_age_frames, std::uint32_t max_evictions) -> stats
Definition eviction.cpp:674
constexpr auto to_string(reclaim_result r) -> const char *
Human-readable name for reclaim_result (for diagnostics / logging).
Definition eviction.h:70
auto evict_all() -> stats
Definition eviction.cpp:680
void set_budget(const budget_state &budget, std::uint64_t used_bytes)
Definition eviction.cpp:943
void register_resource(ievictable *resource)
Begin tracking a resource (assumed resident). Called from handle_impl::make_evictable.
Definition eviction.cpp:972
auto would_allocation_fit(std::uint64_t bytes) -> bool
Definition eviction.cpp:723
void register_evicted_resource(ievictable *resource)
Definition eviction.cpp:977
auto peek_pending_release_bytes() -> std::uint64_t
Definition eviction.cpp:796
auto make_backing(const void *data, std::uint32_t size) -> backing_buffer
Definition eviction.h:20
void clear_queued_allocations()
Definition eviction.cpp:811
auto debug_consumed_bytes() -> std::uint64_t
Total bytes currently reserved by debug_consume_memory.
Definition eviction.cpp:918
auto init(const config &cfg) -> init_status
Definition eviction.cpp:645
auto peek_external_queued_bytes() -> std::uint64_t
Definition eviction.cpp:801
void shutdown()
Definition eviction.cpp:655
strategy
Selection policy used by a sweep to choose eviction victims.
Definition eviction.h:51
@ lfu
Least frequently used first (smallest use count).
@ lru
Least recently used first (smallest last-use frame).
@ age_ttl
Every resource idle for longer than config::max_idle_frames.
@ largest_first
Largest resources first (fastest headroom recovery).
void release_backing_ref(void *, void *user_data)
Definition eviction.h:26
reclaim_kind
Controls whether reclaim_for pumps the GPU command buffer after evicting.
Definition eviction.h:83
void debug_release_memory()
Definition eviction.cpp:903
init_status
Result of init. Determines whether the system tracks resources at all.
Definition eviction.h:42
@ failed
Initialization failed (graphics not ready).
@ ok
Initialized and active.
@ unsupported
Backend does not report a GPU memory budget; eviction is disabled.
@ unnecessary
Eviction is not needed for this backend (driver manages residency).
auto peek_queued_bytes() -> std::uint64_t
Definition eviction.cpp:791
void set_frame(std::uint64_t frame)
Set the global frame counter (call once per presented frame).
Definition eviction.cpp:958
void advance_frame()
Advance the global frame counter by one.
Definition eviction.cpp:966
auto reclaim_for(std::uint64_t bytes, reclaim_kind kind) -> reclaim_result
Definition eviction.cpp:737
void unregister_resource(ievictable *resource)
Stop tracking a resource. Called from handle_impl's destructor.
Definition eviction.cpp:982
@ insufficient
Eviction could not free enough room; the allocation may exhaust device memory.
@ headroom
Already enough headroom; nothing was done.
@ reclaimed
Eviction (and possibly a command-buffer pump) freed enough room.
bgfx::Stats stats
Definition graphics.h:31
const memory_view * make_ref(const void *_data, uint32_t _size, release_fn _releaseFn, void *_userData)
Definition graphics.cpp:465
bgfx::Memory memory_view
Definition graphics.h:23
std::uint64_t soft_budget_bytes
Definition eviction.h:107
std::uint64_t hard_limit_bytes
Definition eviction.h:106
std::uint64_t safety_margin_bytes
Definition eviction.h:109
std::uint64_t target_bytes
Definition eviction.h:108
Configuration for a single eviction pass.
Definition eviction.h:114
std::uint64_t target_bytes
Evict down to this watermark (<= budget) to provide hysteresis. 0 falls back to budget_bytes.
Definition eviction.h:121
strategy strat
Victim selection policy.
Definition eviction.h:116
std::uint64_t budget_bytes
Definition eviction.h:119
std::uint32_t max_idle_frames
For strategy::age_ttl: evict anything idle for more than this many frames.
Definition eviction.h:125
std::uint32_t min_age_frames
Never evict a resource used within this many frames (anti-thrash protection).
Definition eviction.h:123
std::uint32_t max_evictions
Maximum number of resources to evict in a single pass (bounds latency). 0 means unlimited.
Definition eviction.h:127
Cumulative and last-pass statistics reported by the eviction system.
Definition eviction.h:132
double last_pass_ms
Duration of the most recent sweep in milliseconds.
Definition eviction.h:151
std::uint64_t last_pass_evicted
Resources evicted by the most recent sweep.
Definition eviction.h:148
std::uint64_t evicted_bytes
GPU bytes currently reclaimed.
Definition eviction.h:136
std::uint64_t last_pass_scanned
Candidates considered by the most recent sweep.
Definition eviction.h:147
std::uint64_t budget_used_bytes
Metric compared against the budget (e.g. GPU mem used).
Definition eviction.h:146
std::uint64_t target_bytes
Watermark the driver evicts down to.
Definition eviction.h:145
std::uint64_t resident_bytes
GPU bytes currently resident.
Definition eviction.h:134
double last_restore_ms
Duration of the most recent restore in milliseconds.
Definition eviction.h:152
std::uint64_t total_restores
Lifetime number of restores.
Definition eviction.h:139
std::uint64_t total_evictions
Lifetime number of evictions.
Definition eviction.h:138
std::uint64_t total_bytes_evicted
Lifetime GPU bytes evicted.
Definition eviction.h:140
std::uint64_t last_pass_freed_bytes
GPU bytes reclaimed by the most recent sweep.
Definition eviction.h:149
std::uint64_t evicted_count
Resources currently evicted.
Definition eviction.h:135
std::uint64_t failed_restores
Restores that failed to recreate a handle.
Definition eviction.h:142
std::uint64_t pending_release_bytes
Destroys queued in bgfx but not yet reflected in gpuMemoryUsed.
Definition eviction.h:150
std::uint64_t total_bytes_restored
Lifetime GPU bytes restored.
Definition eviction.h:141
std::uint64_t resident_count
Resources currently resident on the GPU.
Definition eviction.h:133
std::uint64_t budget_bytes
Active budget reported by the driver (0 = none).
Definition eviction.h:144
std::uint64_t thrash_events
Restores within min_age_frames of their eviction.
Definition eviction.h:143
std::uint64_t registered_count
Total tracked resources (resident + evicted).
Definition eviction.h:137
float size