Skip to content

Commit 3b27272

Browse files
committed
Initial work on memory pressure handling. See #904.
1 parent 6b0a06e commit 3b27272

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

scopehal/AcceleratorBuffer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/***********************************************************************************************************************
22
* *
3-
* libscopehal v0.1 *
3+
* libscopehal *
44
* *
5-
* Copyright (c) 2012-2022 Andrew D. Zonenberg and contributors *
5+
* Copyright (c) 2012-2024 Andrew D. Zonenberg and contributors *
66
* All rights reserved. *
77
* *
88
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *

scopehal/scopehal.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,3 +987,15 @@ const char* ScopehalGetVersion()
987987
{
988988
return SCOPEHAL_VERSION;
989989
}
990+
991+
/**
992+
@brief Called when we run low on memory
993+
994+
@param level Indicates if this is a soft or hard memory exhaustion condition
995+
@param type Indicates if we are low on CPU or GPU memory
996+
*/
997+
void OnMemoryPressure(MemoryPressureLevel level, MemoryPressureType type)
998+
{
999+
for(auto handler : g_memoryPressureHandlers)
1000+
handler(level, type);
1001+
}

scopehal/scopehal.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,39 @@ uint32_t ColorFromString(const std::string& str, unsigned int alpha = 255);
272272

273273
const char* ScopehalGetVersion();
274274

275+
///@brief Levels of memory pressure
276+
enum class MemoryPressureLevel
277+
{
278+
///@brief A memory allocation has failed and we need to free memory immediately to continue execution
279+
Hard,
280+
281+
/**
282+
@brief Free memory has reached a warning threshold.
283+
284+
We should trim caches or otherwise try to make space but don't need to be too aggressive about it.
285+
286+
This level is only available if we have VK_EXT_memory_budget; without this extension we do not know about
287+
memory pressure until a hard allocation failure occurs.
288+
*/
289+
Soft
290+
};
291+
292+
///@brief Types of memory pressure
293+
enum class MemoryPressureType
294+
{
295+
///@brief Pinned CPU-side memory
296+
Host,
297+
298+
///@brief GPU-side memory
299+
Device
300+
};
301+
302+
///@brief Memory pressure handler type, called when free memory reaches a warning level or a Vulkan allocation fails
303+
typedef void (*MemoryPressureHandler)(MemoryPressureLevel level, MemoryPressureType type);
304+
305+
///@brief List of handlers for low memory registered by various subsystems
306+
std::set<MemoryPressureHandler> g_memoryPressureHandlers;
307+
308+
void OnMemoryPressure(MemoryPressureLevel level, MemoryPressureType type);
309+
275310
#endif

0 commit comments

Comments
 (0)