wsl/hsakmt: Introduce WSL_CHECK_AVAIL_SYSRAM environment variable
By default, this variable is 0. Set it to 1 to compare available system memory with the requested amount before allocation. It helps identify system hangs caused by excessive memory allocation. Signed-off-by: lyndonli <Lyndon.Li@amd.com> Reviewed-by: Flora Cui <flora.cui@amd.com> Part-of: <http://10.67.69.192/wsl/libhsakmt/-/merge_requests/32>
This commit is contained in:
@@ -47,5 +47,7 @@ int zfb_support;
|
||||
int vendor_packet_support;
|
||||
/* enable vendor packet in hsa-runtime*/
|
||||
int enable_vendor_packet;
|
||||
/* check available system memory before allocation */
|
||||
bool check_avail_sysram = false;
|
||||
|
||||
size_t max_single_alloc_size = 0;
|
||||
@@ -45,6 +45,7 @@ extern bool is_svm_api_supported;
|
||||
extern int zfb_support;
|
||||
extern int vendor_packet_support;
|
||||
extern int enable_vendor_packet;
|
||||
extern bool check_avail_sysram;
|
||||
extern size_t max_single_alloc_size;
|
||||
|
||||
#undef HSAKMTAPI
|
||||
|
||||
+10
@@ -105,6 +105,13 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtAllocMemory(HSAuint32 PreferredNode,
|
||||
|
||||
#define POWER_OF_2(x) ((x && (!(x & (x - 1)))) ? 1 : 0)
|
||||
|
||||
bool isSystemMemoryAvailable(HSAuint64 SizeInBytes) {
|
||||
struct sysinfo info;
|
||||
if (sysinfo(&info) != 0)
|
||||
return false;
|
||||
return SizeInBytes <= info.freeram;
|
||||
}
|
||||
|
||||
HSAKMT_STATUS HSAKMTAPI hsaKmtAllocMemoryAlign(HSAuint32 PreferredNode,
|
||||
HSAuint64 SizeInBytes,
|
||||
HSAuint64 Alignment,
|
||||
@@ -140,6 +147,9 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtAllocMemoryAlign(HSAuint32 PreferredNode,
|
||||
if (SizeInBytes > max_single_alloc_size)
|
||||
return HSAKMT_STATUS_NO_MEMORY;
|
||||
|
||||
if (check_avail_sysram && !isSystemMemoryAvailable(SizeInBytes))
|
||||
return HSAKMT_STATUS_NO_MEMORY;
|
||||
|
||||
/* If allocate VRAM under ZFB mode */
|
||||
if (zfb_support && MemFlags.ui32.NonPaged == 1)
|
||||
MemFlags.ui32.CoarseGrain = 1;
|
||||
|
||||
@@ -129,6 +129,11 @@ static HSAKMT_STATUS init_vars_from_env(void) {
|
||||
if (envvar)
|
||||
enable_vendor_packet = atoi(envvar);
|
||||
|
||||
/* Decide whether to check available system memory before allocation */
|
||||
envvar = getenv("WSL_CHECK_AVAIL_SYSRAM");
|
||||
if (envvar)
|
||||
check_avail_sysram = !strcmp(envvar, "1");
|
||||
|
||||
return HSAKMT_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user