Skip to content

Commit e1ad48c

Browse files
Pass device handles to sysman power module
Signed-off-by: Mayank Raghuwanshi <[email protected]>
1 parent b5e0d32 commit e1ad48c

File tree

18 files changed

+433
-187
lines changed

18 files changed

+433
-187
lines changed

level_zero/tools/source/sysman/power/linux/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ set(L0_SRCS_TOOLS_SYSMAN_POWER_LINUX
88
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
99
${CMAKE_CURRENT_SOURCE_DIR}/os_power_imp.cpp
1010
${CMAKE_CURRENT_SOURCE_DIR}/os_power_imp.h
11+
${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/os_power_helper.cpp
1112
)
1213

1314
if(UNIX)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (C) 2021 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "level_zero/tools/source/sysman/power/linux/os_power_imp.h"
9+
10+
namespace L0 {
11+
12+
bool LinuxPowerImp::isEnergyHwmonDir(std::string name) {
13+
if (isSubdevice == false && (name == i915)) {
14+
return true;
15+
}
16+
return false;
17+
}
18+
19+
} // namespace L0

level_zero/tools/source/sysman/power/linux/os_power_imp.cpp

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ void powerGetTimestamp(uint64_t &timestamp) {
3232
}
3333

3434
ze_result_t LinuxPowerImp::getProperties(zes_power_properties_t *pProperties) {
35-
pProperties->onSubdevice = false;
36-
pProperties->subdeviceId = 0;
35+
pProperties->onSubdevice = isSubdevice;
36+
pProperties->subdeviceId = subdeviceId;
3737
pProperties->canControl = canControl;
3838
pProperties->isEnergyThresholdSupported = false;
3939
pProperties->defaultLimit = -1;
@@ -58,16 +58,25 @@ ze_result_t LinuxPowerImp::getProperties(zes_power_properties_t *pProperties) {
5858
return ZE_RESULT_SUCCESS;
5959
}
6060

61+
ze_result_t LinuxPowerImp::getPmtEnergyCounter(zes_power_energy_counter_t *pEnergy) {
62+
const std::string key("PACKAGE_ENERGY");
63+
uint64_t energy = 0;
64+
ze_result_t result = pPmt->readValue(key, energy);
65+
// PMT will return energy counter in Q20 format(fixed point representation) where first 20 bits(from LSB) represent decimal part and remaining integral part which is converted into joule by division with 1048576(2^20) and then converted into microjoules
66+
pEnergy->energy = (energy / 1048576) * convertJouleToMicroJoule;
67+
return result;
68+
}
6169
ze_result_t LinuxPowerImp::getEnergyCounter(zes_power_energy_counter_t *pEnergy) {
62-
ze_result_t result = pSysfsAccess->read(i915HwmonDir + "/" + energyCounterNode, pEnergy->energy);
70+
powerGetTimestamp(pEnergy->timestamp);
71+
ze_result_t result = pSysfsAccess->read(energyHwmonDir + "/" + energyCounterNode, pEnergy->energy);
6372
if (result != ZE_RESULT_SUCCESS) {
64-
const std::string key("PACKAGE_ENERGY");
65-
uint64_t energy = 0;
66-
result = pPmt->readValue(key, energy);
67-
// PMT will return energy counter in Q20 format(fixed point representation) where first 20 bits(from LSB) represent decimal part and remaining integral part which is converted into joule by division with 1048576(2^20) and then converted into microjoules
68-
pEnergy->energy = (energy / 1048576) * convertJouleToMicroJoule;
73+
if (pPmt != nullptr) {
74+
return getPmtEnergyCounter(pEnergy);
75+
}
76+
}
77+
if (result != ZE_RESULT_SUCCESS) {
78+
return getErrorCode(result);
6979
}
70-
powerGetTimestamp(pEnergy->timestamp);
7180
return result;
7281
}
7382

@@ -131,6 +140,13 @@ ze_result_t LinuxPowerImp::setLimits(const zes_power_sustained_limit_t *pSustain
131140
if (ZE_RESULT_SUCCESS != result) {
132141
return getErrorCode(result);
133142
}
143+
if (isSustainedPowerLimitEnabled != static_cast<uint64_t>(pSustained->enabled)) {
144+
result = pSysfsAccess->write(i915HwmonDir + "/" + sustainedPowerLimitEnabled, static_cast<int>(pSustained->enabled));
145+
if (ZE_RESULT_SUCCESS != result) {
146+
return getErrorCode(result);
147+
}
148+
isSustainedPowerLimitEnabled = static_cast<uint64_t>(pSustained->enabled);
149+
}
134150

135151
if (isSustainedPowerLimitEnabled) {
136152
val = static_cast<uint32_t>(pSustained->power) * milliFactor; // Convert milliWatts to microwatts
@@ -144,6 +160,7 @@ ze_result_t LinuxPowerImp::setLimits(const zes_power_sustained_limit_t *pSustain
144160
return getErrorCode(result);
145161
}
146162
}
163+
result = ZE_RESULT_SUCCESS;
147164
}
148165
if (pBurst != nullptr) {
149166
result = pSysfsAccess->write(i915HwmonDir + "/" + burstPowerLimitEnabled, static_cast<int>(pBurst->enabled));
@@ -159,7 +176,6 @@ ze_result_t LinuxPowerImp::setLimits(const zes_power_sustained_limit_t *pSustain
159176
}
160177
}
161178
}
162-
163179
return result;
164180
}
165181

@@ -187,7 +203,9 @@ bool LinuxPowerImp::isPowerModuleSupported() {
187203
i915HwmonDir = hwmonDir + "/" + tempHwmonDirEntry;
188204
hwmonDirExists = true;
189205
canControl = true;
190-
break;
206+
}
207+
if (isEnergyHwmonDir(name) == true) {
208+
energyHwmonDir = hwmonDir + "/" + tempHwmonDirEntry;
191209
}
192210
}
193211
if (hwmonDirExists == false) {
@@ -196,16 +214,14 @@ bool LinuxPowerImp::isPowerModuleSupported() {
196214
return true;
197215
}
198216

199-
LinuxPowerImp::LinuxPowerImp(OsSysman *pOsSysman) {
217+
LinuxPowerImp::LinuxPowerImp(OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId) : isSubdevice(onSubdevice), subdeviceId(subdeviceId) {
200218
LinuxSysmanImp *pLinuxSysmanImp = static_cast<LinuxSysmanImp *>(pOsSysman);
201-
// Lets hardcode subDeviceId to 0, as we are expecting this code to execute on device without subdevice
202-
uint32_t subDeviceId = 0;
203-
pPmt = pLinuxSysmanImp->getPlatformMonitoringTechAccess(subDeviceId);
219+
pPmt = pLinuxSysmanImp->getPlatformMonitoringTechAccess(subdeviceId);
204220
pSysfsAccess = &pLinuxSysmanImp->getSysfsAccess();
205221
}
206222

207-
OsPower *OsPower::create(OsSysman *pOsSysman) {
208-
LinuxPowerImp *pLinuxPowerImp = new LinuxPowerImp(pOsSysman);
223+
OsPower *OsPower::create(OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId) {
224+
LinuxPowerImp *pLinuxPowerImp = new LinuxPowerImp(pOsSysman, onSubdevice, subdeviceId);
209225
return static_cast<OsPower *>(pLinuxPowerImp);
210226
}
211227

level_zero/tools/source/sysman/power/linux/os_power_imp.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ class LinuxPowerImp : public OsPower, NEO::NonCopyableOrMovableClass {
2727
ze_result_t setEnergyThreshold(double threshold) override;
2828

2929
bool isPowerModuleSupported() override;
30-
LinuxPowerImp(OsSysman *pOsSysman);
30+
bool isEnergyHwmonDir(std::string name);
31+
ze_result_t getPmtEnergyCounter(zes_power_energy_counter_t *pEnergy);
32+
LinuxPowerImp(OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId);
3133
LinuxPowerImp() = default;
3234
~LinuxPowerImp() override = default;
3335

@@ -37,6 +39,7 @@ class LinuxPowerImp : public OsPower, NEO::NonCopyableOrMovableClass {
3739

3840
private:
3941
std::string i915HwmonDir;
42+
std::string energyHwmonDir;
4043
static const std::string hwmonDir;
4144
static const std::string i915;
4245
static const std::string sustainedPowerLimitEnabled;
@@ -49,6 +52,8 @@ class LinuxPowerImp : public OsPower, NEO::NonCopyableOrMovableClass {
4952
static const std::string minPowerLimit;
5053
static const std::string maxPowerLimit;
5154
bool canControl = false;
55+
bool isSubdevice = false;
56+
uint32_t subdeviceId = 0;
5257

5358
ze_result_t getErrorCode(ze_result_t result) {
5459
if (result == ZE_RESULT_ERROR_NOT_AVAILABLE) {

level_zero/tools/source/sysman/power/os_power.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 Intel Corporation
2+
* Copyright (C) 2020-2021 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -22,7 +22,7 @@ class OsPower {
2222
virtual ze_result_t setEnergyThreshold(double threshold) = 0;
2323

2424
virtual bool isPowerModuleSupported() = 0;
25-
static OsPower *create(OsSysman *pOsSysman);
25+
static OsPower *create(OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId);
2626
virtual ~OsPower() = default;
2727
};
2828

level_zero/tools/source/sysman/power/power.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 Intel Corporation
2+
* Copyright (C) 2020-2021 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -19,14 +19,20 @@ PowerHandleContext::~PowerHandleContext() {
1919
}
2020
}
2121

22-
void PowerHandleContext::init() {
23-
Power *pPower = new PowerImp(pOsSysman);
22+
void PowerHandleContext::createHandle(ze_device_handle_t deviceHandle) {
23+
Power *pPower = new PowerImp(pOsSysman, deviceHandle);
2424
if (pPower->initSuccess == true) {
2525
handleList.push_back(pPower);
2626
} else {
2727
delete pPower;
2828
}
2929
}
30+
ze_result_t PowerHandleContext::init(std::vector<ze_device_handle_t> &deviceHandles) {
31+
for (const auto &deviceHandle : deviceHandles) {
32+
createHandle(deviceHandle);
33+
}
34+
return ZE_RESULT_SUCCESS;
35+
}
3036

3137
ze_result_t PowerHandleContext::powerGet(uint32_t *pCount, zes_pwr_handle_t *phPower) {
3238
uint32_t handleListSize = static_cast<uint32_t>(handleList.size());

level_zero/tools/source/sysman/power/power.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/*
2-
* Copyright (C) 2020 Intel Corporation
2+
* Copyright (C) 2020-2021 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
66
*/
77

88
#pragma once
9+
#include "level_zero/core/source/device/device.h"
910
#include <level_zero/zes_api.h>
1011

1112
#include <vector>
@@ -41,12 +42,14 @@ struct PowerHandleContext {
4142
PowerHandleContext(OsSysman *pOsSysman) : pOsSysman(pOsSysman){};
4243
~PowerHandleContext();
4344

44-
void init();
45-
45+
ze_result_t init(std::vector<ze_device_handle_t> &deviceHandles);
4646
ze_result_t powerGet(uint32_t *pCount, zes_pwr_handle_t *phPower);
4747

4848
OsSysman *pOsSysman = nullptr;
4949
std::vector<Power *> handleList = {};
50+
51+
private:
52+
void createHandle(ze_device_handle_t deviceHandle);
5053
};
5154

5255
} // namespace L0

level_zero/tools/source/sysman/power/power_imp.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 Intel Corporation
2+
* Copyright (C) 2020-2021 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -35,10 +35,11 @@ ze_result_t PowerImp::powerSetEnergyThreshold(double threshold) {
3535
return pOsPower->setEnergyThreshold(threshold);
3636
}
3737

38-
PowerImp::PowerImp(OsSysman *pOsSysman) {
39-
pOsPower = OsPower::create(pOsSysman);
38+
PowerImp::PowerImp(OsSysman *pOsSysman, ze_device_handle_t handle) : deviceHandle(handle) {
39+
ze_device_properties_t deviceProperties = {};
40+
Device::fromHandle(deviceHandle)->getProperties(&deviceProperties);
41+
pOsPower = OsPower::create(pOsSysman, deviceProperties.flags & ZE_DEVICE_PROPERTY_FLAG_SUBDEVICE, deviceProperties.subdeviceId);
4042
UNRECOVERABLE_IF(nullptr == pOsPower);
41-
4243
init();
4344
}
4445

level_zero/tools/source/sysman/power/power_imp.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 Intel Corporation
2+
* Copyright (C) 2020-2021 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -23,10 +23,13 @@ class PowerImp : public Power, NEO::NonCopyableOrMovableClass {
2323
ze_result_t powerSetEnergyThreshold(double threshold) override;
2424

2525
PowerImp() = default;
26-
PowerImp(OsSysman *pOsSysman);
26+
PowerImp(OsSysman *pOsSysman, ze_device_handle_t device);
2727
~PowerImp() override;
2828

2929
OsPower *pOsPower = nullptr;
3030
void init();
31+
32+
private:
33+
ze_device_handle_t deviceHandle = {};
3134
};
3235
} // namespace L0

level_zero/tools/source/sysman/power/windows/os_power_imp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,13 @@ bool WddmPowerImp::isPowerModuleSupported() {
330330
return ((status == ZE_RESULT_SUCCESS) && (enabled));
331331
}
332332

333-
WddmPowerImp::WddmPowerImp(OsSysman *pOsSysman) {
333+
WddmPowerImp::WddmPowerImp(OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId) {
334334
WddmSysmanImp *pWddmSysmanImp = static_cast<WddmSysmanImp *>(pOsSysman);
335335
pKmdSysManager = &pWddmSysmanImp->getKmdSysManager();
336336
}
337337

338-
OsPower *OsPower::create(OsSysman *pOsSysman) {
339-
WddmPowerImp *pWddmPowerImp = new WddmPowerImp(pOsSysman);
338+
OsPower *OsPower::create(OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId) {
339+
WddmPowerImp *pWddmPowerImp = new WddmPowerImp(pOsSysman, onSubdevice, subdeviceId);
340340
return static_cast<OsPower *>(pWddmPowerImp);
341341
}
342342

0 commit comments

Comments
 (0)