Skip to content

Commit 8668744

Browse files
feature(sysman): Add support for Memory properties & state for iGPU's in Windows
Related-To: NEO-14197 Signed-off-by: Vishnu Khanth <[email protected]>
1 parent 75e313c commit 8668744

File tree

3 files changed

+135
-16
lines changed

3 files changed

+135
-16
lines changed

level_zero/tools/source/sysman/memory/windows/os_memory_imp.cpp

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
/*
2-
* Copyright (C) 2020-2023 Intel Corporation
2+
* Copyright (C) 2020-2025 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
66
*/
77

88
#include "level_zero/tools/source/sysman/memory/windows/os_memory_imp.h"
99

10+
#include "shared/source/helpers/hw_info.h"
1011
#include "shared/source/os_interface/windows/wddm/wddm.h"
1112

1213
#include "level_zero/tools/source/sysman/sysman.h"
1314
#include "level_zero/tools/source/sysman/windows/kmd_sys_manager.h"
1415

16+
#include <Windows.h>
17+
1518
template <typename I>
1619
std::string intToHex(I w, size_t hexLength = sizeof(I) << 1) {
1720
static const char *digits = "0123456789ABCDEF";
@@ -47,6 +50,8 @@ bool WddmMemoryImp::isMemoryModuleSupported() {
4750
KmdSysman::RequestProperty request;
4851
KmdSysman::ResponseProperty response;
4952

53+
bool isIntegratedDevice = pDevice->getHwInfo().capabilityTable.isIntegratedDevice;
54+
5055
request.commandId = KmdSysman::Command::Get;
5156
request.componentId = KmdSysman::Component::MemoryComponent;
5257
request.requestId = KmdSysman::Requests::Memory::NumMemoryDomains;
@@ -57,6 +62,10 @@ bool WddmMemoryImp::isMemoryModuleSupported() {
5762

5863
memcpy_s(&value, sizeof(uint32_t), response.dataBuffer, sizeof(uint32_t));
5964

65+
if (!value && isIntegratedDevice) {
66+
return true;
67+
}
68+
6069
return (value > 0);
6170
}
6271
ze_result_t WddmMemoryImp::getProperties(zes_mem_properties_t *pProperties) {
@@ -68,6 +77,21 @@ ze_result_t WddmMemoryImp::getProperties(zes_mem_properties_t *pProperties) {
6877

6978
pProperties->onSubdevice = isSubdevice;
7079
pProperties->subdeviceId = subdeviceId;
80+
pProperties->type = ZES_MEM_TYPE_FORCE_UINT32;
81+
pProperties->physicalSize = 0;
82+
pProperties->numChannels = -1;
83+
pProperties->busWidth = -1;
84+
pProperties->location = ZES_MEM_LOC_FORCE_UINT32;
85+
86+
bool isIntegratedDevice = pDevice->getHwInfo().capabilityTable.isIntegratedDevice;
87+
if (isIntegratedDevice) {
88+
pProperties->location = ZES_MEM_LOC_SYSTEM;
89+
ULONGLONG installedMemoryKb = 0;
90+
if (GetPhysicallyInstalledSystemMemory(&installedMemoryKb)) {
91+
pProperties->physicalSize = installedMemoryKb * 1024;
92+
}
93+
return ZE_RESULT_SUCCESS;
94+
}
7195

7296
request.commandId = KmdSysman::Command::Get;
7397
request.componentId = KmdSysman::Component::MemoryComponent;
@@ -93,7 +117,6 @@ ze_result_t WddmMemoryImp::getProperties(zes_mem_properties_t *pProperties) {
93117
return status;
94118
}
95119

96-
pProperties->type = ZES_MEM_TYPE_FORCE_UINT32;
97120
if (vResponses[0].returnCode == KmdSysman::Success) {
98121
memcpy_s(&retValu32, sizeof(uint32_t), vResponses[0].dataBuffer, sizeof(uint32_t));
99122
switch (retValu32) {
@@ -139,33 +162,26 @@ ze_result_t WddmMemoryImp::getProperties(zes_mem_properties_t *pProperties) {
139162
}
140163
}
141164

142-
pProperties->physicalSize = 0;
143165
if (vResponses[1].returnCode == KmdSysman::Success) {
144166
memcpy_s(&retValu64, sizeof(uint64_t), vResponses[1].dataBuffer, sizeof(uint64_t));
145167
pProperties->physicalSize = retValu64;
146168
}
147169

148-
pProperties->numChannels = -1;
149170
if (vResponses[2].returnCode == KmdSysman::Success) {
150171
memcpy_s(&retValu32, sizeof(uint32_t), vResponses[2].dataBuffer, sizeof(uint32_t));
151172
pProperties->numChannels = retValu32;
152173
}
153174

154-
pProperties->location = ZES_MEM_LOC_FORCE_UINT32;
155175
if (vResponses[3].returnCode == KmdSysman::Success) {
156176
memcpy_s(&retValu32, sizeof(uint32_t), vResponses[3].dataBuffer, sizeof(uint32_t));
157177
pProperties->location = static_cast<zes_mem_loc_t>(retValu32);
158178
}
159179

160-
pProperties->busWidth = -1;
161180
if (vResponses[4].returnCode == KmdSysman::Success) {
162181
memcpy_s(&retValu32, sizeof(uint32_t), vResponses[4].dataBuffer, sizeof(uint32_t));
163182
pProperties->busWidth = retValu32;
164183
}
165184

166-
pProperties->subdeviceId = 0;
167-
pProperties->onSubdevice = false;
168-
169185
return ZE_RESULT_SUCCESS;
170186
}
171187

@@ -229,14 +245,21 @@ ze_result_t WddmMemoryImp::getState(zes_mem_state_t *pState) {
229245
request.componentId = KmdSysman::Component::MemoryComponent;
230246
request.requestId = KmdSysman::Requests::Memory::CurrentTotalAllocableMem;
231247

248+
bool isIntegratedDevice = pDevice->getHwInfo().capabilityTable.isIntegratedDevice;
249+
232250
status = pKmdSysManager->requestSingle(request, response);
233251

234-
if (status != ZE_RESULT_SUCCESS) {
252+
if (status != ZE_RESULT_SUCCESS && !isIntegratedDevice) {
235253
return status;
236254
}
237255

238256
memcpy_s(&retValu64, sizeof(uint64_t), response.dataBuffer, sizeof(uint64_t));
239-
pState->size = retValu64;
257+
258+
if (isIntegratedDevice && pKmdSysManager->GetWddmAccess()) {
259+
pState->size = pKmdSysManager->GetWddmAccess()->getSystemSharedMemory();
260+
} else {
261+
pState->size = retValu64;
262+
}
240263

241264
if (!pdhInitialized) {
242265
if (pdhOpenQuery && pdhOpenQuery(NULL, NULL, &gpuQuery) == ERROR_SUCCESS) {
@@ -245,14 +268,19 @@ ze_result_t WddmMemoryImp::getState(zes_mem_state_t *pState) {
245268
}
246269

247270
if (!pdhCounterAdded && pdhAddEnglishCounterW && pKmdSysManager->GetWddmAccess()) {
248-
std::wstring counterStr = constructCounterStr(L"GPU Adapter Memory", L"Dedicated Usage", pKmdSysManager->GetWddmAccess()->getAdapterLuid(), 0);
249-
pdhCounterAdded = (pdhAddEnglishCounterW(gpuQuery, counterStr.c_str(), NULL, &dedicatedUsage) == ERROR_SUCCESS);
271+
if (isIntegratedDevice) {
272+
std::wstring counterStr = constructCounterStr(L"GPU Adapter Memory", L"Shared Usage", pKmdSysManager->GetWddmAccess()->getAdapterLuid(), 0);
273+
pdhCounterAdded = (pdhAddEnglishCounterW(gpuQuery, counterStr.c_str(), NULL, &usage) == ERROR_SUCCESS);
274+
} else {
275+
std::wstring counterStr = constructCounterStr(L"GPU Adapter Memory", L"Dedicated Usage", pKmdSysManager->GetWddmAccess()->getAdapterLuid(), 0);
276+
pdhCounterAdded = (pdhAddEnglishCounterW(gpuQuery, counterStr.c_str(), NULL, &usage) == ERROR_SUCCESS);
277+
}
250278
}
251279

252280
if (pdhCounterAdded && pdhCollectQueryData && pdhGetFormattedCounterValue) {
253281
PDH_FMT_COUNTERVALUE counterVal;
254282
pdhCollectQueryData(gpuQuery);
255-
pdhGetFormattedCounterValue(dedicatedUsage, PDH_FMT_LARGE, NULL, &counterVal);
283+
pdhGetFormattedCounterValue(usage, PDH_FMT_LARGE, NULL, &counterVal);
256284
retValu64 = counterVal.largeValue;
257285
pState->free = pState->size - retValu64;
258286
}

level_zero/tools/source/sysman/memory/windows/os_memory_imp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class WddmMemoryImp : public OsMemory, NEO::NonCopyableAndNonMovableClass {
6161
bool pdhInitialized = false;
6262
bool pdhCounterAdded = false;
6363
PDH_HQUERY gpuQuery = nullptr;
64-
PDH_HCOUNTER dedicatedUsage = nullptr;
64+
PDH_HCOUNTER usage = nullptr;
6565
HINSTANCE hGetProcPDH = nullptr;
6666
};
6767

level_zero/tools/test/unit_tests/sources/sysman/memory/windows/test_zes_memory.cpp

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2023 Intel Corporation
2+
* Copyright (C) 2020-2025 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -72,6 +72,20 @@ class SysmanDeviceMemoryFixture : public SysmanDeviceFixture {
7272
pSysmanDeviceImp->pMemoryHandleContext->init(deviceHandles);
7373
}
7474

75+
void clearMemHandleListAndReinit() {
76+
pSysmanDeviceImp->pMemoryHandleContext->handleList.clear();
77+
uint32_t subDeviceCount = 0;
78+
std::vector<ze_device_handle_t> deviceHandles{};
79+
device->getSubDevices(&subDeviceCount, nullptr);
80+
if (subDeviceCount == 0) {
81+
deviceHandles.resize(1, device->toHandle());
82+
} else {
83+
deviceHandles.resize(subDeviceCount, nullptr);
84+
device->getSubDevices(&subDeviceCount, deviceHandles.data());
85+
}
86+
pSysmanDeviceImp->pMemoryHandleContext->init(deviceHandles);
87+
}
88+
7589
std::vector<zes_mem_handle_t> getMemoryHandles(uint32_t count) {
7690
std::vector<zes_mem_handle_t> handles(count, nullptr);
7791
EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS);
@@ -145,6 +159,38 @@ TEST_F(SysmanDeviceMemoryFixture, DISABLED_GivenValidMemoryHandleWhenCallingGett
145159
}
146160
}
147161

162+
TEST_F(SysmanDeviceMemoryFixture, GivenValidMemoryHandleWhenCallingGettingPropertiesThenCallSucceeds) {
163+
pKmdSysManager->mockMemoryDomains = 1;
164+
clearMemHandleListAndReinit();
165+
166+
uint32_t count = 0;
167+
EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS);
168+
EXPECT_EQ(count, 1u);
169+
170+
std::vector<zes_mem_handle_t> handles(count, nullptr);
171+
EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS);
172+
173+
for (auto handle : handles) {
174+
EXPECT_NE(handle, nullptr);
175+
zes_mem_properties_t properties{};
176+
EXPECT_EQ(zesMemoryGetProperties(handle, &properties), ZE_RESULT_SUCCESS);
177+
EXPECT_FALSE(properties.onSubdevice);
178+
EXPECT_EQ(properties.subdeviceId, 0u);
179+
if (defaultHwInfo->capabilityTable.isIntegratedDevice) {
180+
EXPECT_EQ(properties.location, ZES_MEM_LOC_SYSTEM);
181+
EXPECT_GT(properties.physicalSize, 0u);
182+
EXPECT_EQ(properties.numChannels, -1);
183+
EXPECT_EQ(properties.busWidth, -1);
184+
} else {
185+
EXPECT_EQ(properties.type, ZES_MEM_TYPE_GDDR6);
186+
EXPECT_EQ(properties.location, ZES_MEM_LOC_DEVICE);
187+
EXPECT_EQ(properties.physicalSize, pKmdSysManager->mockMemoryPhysicalSize);
188+
EXPECT_EQ(static_cast<uint32_t>(properties.numChannels), pKmdSysManager->mockMemoryChannels);
189+
EXPECT_EQ(static_cast<uint32_t>(properties.busWidth), pKmdSysManager->mockMemoryBus);
190+
}
191+
}
192+
}
193+
148194
TEST_F(SysmanDeviceMemoryFixture, DISABLED_GivenValidMemoryHandleWhenGettingStateThenCallSucceeds) {
149195
setLocalSupportedAndReinit(true);
150196
auto handles = getMemoryHandles(memoryHandleComponentCount);
@@ -193,5 +239,50 @@ TEST_F(SysmanDeviceMemoryFixture, DISABLED_GivenValidMemoryHandleWhenGettingBand
193239
}
194240
}
195241

242+
TEST_F(SysmanDeviceMemoryFixture, GivenMockedComponentCountZeroWhenEnumeratingMemoryModulesThenExpectNonZeroCountAndValidHandlesForIntegratedPlatforms) {
243+
pKmdSysManager->mockMemoryDomains = 0;
244+
clearMemHandleListAndReinit();
245+
246+
uint32_t count = 0;
247+
EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS);
248+
249+
if (defaultHwInfo->capabilityTable.isIntegratedDevice) {
250+
EXPECT_EQ(count, 1u);
251+
std::vector<zes_mem_handle_t> handles(count, nullptr);
252+
EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS);
253+
for (auto handle : handles) {
254+
EXPECT_NE(handle, nullptr);
255+
}
256+
} else {
257+
EXPECT_EQ(count, 0u);
258+
}
259+
}
260+
261+
TEST_F(SysmanDeviceMemoryFixture, GivenMockedComponentCountZeroWhenEnumeratingMemoryModulesThenExpectNonZeroCountAndValidPropertiesForIntegratedPlatforms) {
262+
pKmdSysManager->mockMemoryDomains = 0;
263+
clearMemHandleListAndReinit();
264+
265+
uint32_t count = 0;
266+
EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, nullptr), ZE_RESULT_SUCCESS);
267+
268+
if (defaultHwInfo->capabilityTable.isIntegratedDevice) {
269+
EXPECT_EQ(count, 1u);
270+
std::vector<zes_mem_handle_t> handles(count, nullptr);
271+
EXPECT_EQ(zesDeviceEnumMemoryModules(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS);
272+
for (auto handle : handles) {
273+
EXPECT_NE(handle, nullptr);
274+
zes_mem_properties_t properties{};
275+
EXPECT_EQ(zesMemoryGetProperties(handle, &properties), ZE_RESULT_SUCCESS);
276+
277+
EXPECT_EQ(properties.location, ZES_MEM_LOC_SYSTEM);
278+
EXPECT_FALSE(properties.onSubdevice);
279+
EXPECT_EQ(properties.subdeviceId, 0u);
280+
EXPECT_GT(properties.physicalSize, 0u);
281+
}
282+
} else {
283+
EXPECT_EQ(count, 0u);
284+
}
285+
}
286+
196287
} // namespace ult
197288
} // namespace L0

0 commit comments

Comments
 (0)