Skip to content

Commit 421a7bd

Browse files
refactor: Move all constants to L0 namespace in level_zero/tools
Related-To: NEO-12769 - Use inline constexpr for declaring global variables - Avoid using Macros - Use string_view type instead of std::string Signed-off-by: Harini Kumaran <[email protected]>
1 parent f5c433c commit 421a7bd

File tree

11 files changed

+77
-73
lines changed

11 files changed

+77
-73
lines changed

level_zero/tools/source/sysman/firmware/linux/os_firmware_imp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2023 Intel Corporation
2+
* Copyright (C) 2020-2024 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -45,7 +45,7 @@ ze_result_t OsFirmware::getSupportedFwTypes(std::vector<std::string> &supportedF
4545

4646
void LinuxFirmwareImp::osGetFwProperties(zes_firmware_properties_t *pProperties) {
4747
if (ZE_RESULT_SUCCESS != getFirmwareVersion(osFwType, pProperties)) {
48-
strncpy_s(static_cast<char *>(pProperties->version), ZES_STRING_PROPERTY_SIZE, unknown.c_str(), ZES_STRING_PROPERTY_SIZE - 1);
48+
strncpy_s(static_cast<char *>(pProperties->version), ZES_STRING_PROPERTY_SIZE, unknown.data(), ZES_STRING_PROPERTY_SIZE - 1);
4949
}
5050
pProperties->canControl = true; // Assuming that user has permission to flash the firmware
5151
}

level_zero/tools/source/sysman/firmware/windows/os_firmware_imp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2023 Intel Corporation
2+
* Copyright (C) 2020-2024 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -25,7 +25,7 @@ ze_result_t WddmFirmwareImp::getFirmwareVersion(std::string fwType, zes_firmware
2525

2626
void WddmFirmwareImp::osGetFwProperties(zes_firmware_properties_t *pProperties) {
2727
if (ZE_RESULT_SUCCESS != getFirmwareVersion(osFwType, pProperties)) {
28-
strncpy_s(static_cast<char *>(pProperties->version), ZES_STRING_PROPERTY_SIZE, unknown.c_str(), ZES_STRING_PROPERTY_SIZE);
28+
strncpy_s(static_cast<char *>(pProperties->version), ZES_STRING_PROPERTY_SIZE, unknown.data(), ZES_STRING_PROPERTY_SIZE);
2929
}
3030
pProperties->canControl = true; // Assuming that user has permission to flash the firmware
3131
}

level_zero/tools/source/sysman/global_operations/global_operations_imp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2023 Intel Corporation
2+
* Copyright (C) 2020-2024 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -81,11 +81,11 @@ void GlobalOperationsImp::init() {
8181
pOsGlobalOperations->getBrandName(sysmanProperties.brandName);
8282
memset(sysmanProperties.boardNumber, 0, ZES_STRING_PROPERTY_SIZE);
8383
if (!pOsGlobalOperations->getBoardNumber(sysmanProperties.boardNumber)) {
84-
memcpy_s(sysmanProperties.boardNumber, ZES_STRING_PROPERTY_SIZE, unknown.c_str(), unknown.length() + 1);
84+
memcpy_s(sysmanProperties.boardNumber, ZES_STRING_PROPERTY_SIZE, unknown.data(), unknown.length() + 1);
8585
}
8686
memset(sysmanProperties.serialNumber, 0, ZES_STRING_PROPERTY_SIZE);
8787
if (!pOsGlobalOperations->getSerialNumber(sysmanProperties.serialNumber)) {
88-
memcpy_s(sysmanProperties.serialNumber, ZES_STRING_PROPERTY_SIZE, unknown.c_str(), unknown.length() + 1);
88+
memcpy_s(sysmanProperties.serialNumber, ZES_STRING_PROPERTY_SIZE, unknown.data(), unknown.length() + 1);
8989
}
9090
}
9191
void GlobalOperationsImp::initGlobalOperations() {

level_zero/tools/source/sysman/global_operations/linux/os_global_operations_imp.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ void LinuxGlobalOperationsImp::getBrandName(char (&brandName)[ZES_STRING_PROPERT
146146
std::string strVal;
147147
ze_result_t result = pSysfsAccess->read(subsystemVendorFile, strVal);
148148
if (ZE_RESULT_SUCCESS != result) {
149-
std::strncpy(brandName, unknown.c_str(), ZES_STRING_PROPERTY_SIZE);
149+
std::strncpy(brandName, unknown.data(), ZES_STRING_PROPERTY_SIZE);
150150
return;
151151
}
152152
if (strVal.compare(intelPciId) == 0) {
153-
std::strncpy(brandName, vendorIntel.c_str(), ZES_STRING_PROPERTY_SIZE);
153+
std::strncpy(brandName, vendorIntel.data(), ZES_STRING_PROPERTY_SIZE);
154154
} else {
155-
std::strncpy(brandName, unknown.c_str(), ZES_STRING_PROPERTY_SIZE);
155+
std::strncpy(brandName, unknown.data(), ZES_STRING_PROPERTY_SIZE);
156156
}
157157
}
158158

@@ -168,15 +168,15 @@ void LinuxGlobalOperationsImp::getVendorName(char (&vendorName)[ZES_STRING_PROPE
168168
std::stringstream pciId;
169169
pciId << std::hex << coreDeviceProperties.vendorId;
170170
if (("0x" + pciId.str()).compare(intelPciId) == 0) {
171-
std::strncpy(vendorName, vendorIntel.c_str(), ZES_STRING_PROPERTY_SIZE);
171+
std::strncpy(vendorName, vendorIntel.data(), ZES_STRING_PROPERTY_SIZE);
172172
} else {
173-
std::strncpy(vendorName, unknown.c_str(), ZES_STRING_PROPERTY_SIZE);
173+
std::strncpy(vendorName, unknown.data(), ZES_STRING_PROPERTY_SIZE);
174174
}
175175
}
176176

177177
void LinuxGlobalOperationsImp::getDriverVersion(char (&driverVersion)[ZES_STRING_PROPERTY_SIZE]) {
178178
std::string strVal;
179-
std::strncpy(driverVersion, unknown.c_str(), ZES_STRING_PROPERTY_SIZE);
179+
std::strncpy(driverVersion, unknown.data(), ZES_STRING_PROPERTY_SIZE);
180180
ze_result_t result = pFsAccess->read(agamaVersionFile, strVal);
181181
if (ZE_RESULT_SUCCESS != result) {
182182
if (ZE_RESULT_ERROR_NOT_AVAILABLE != result) {

level_zero/tools/source/sysman/pci/linux/os_pci_imp.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2023 Intel Corporation
2+
* Copyright (C) 2020-2024 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -73,8 +73,8 @@ void LinuxPciImp::getMaxLinkCaps(double &maxLinkSpeed, int32_t &maxLinkWidth) {
7373
}
7474

7575
uint16_t linkCaps = L0::PciUtil::getWordFromConfig(linkCapPos, configMemory.data());
76-
maxLinkSpeed = convertPciGenToLinkSpeed(BITS(linkCaps, 0, 4));
77-
maxLinkWidth = BITS(linkCaps, 4, 6);
76+
maxLinkSpeed = convertPciGenToLinkSpeed(bits(linkCaps, 0, 4));
77+
maxLinkWidth = bits(linkCaps, 4, 6);
7878

7979
return;
8080
}
@@ -181,7 +181,7 @@ uint16_t LinuxPciImp::getLinkCapabilityPos(uint8_t *configMem) {
181181
id = L0::PciUtil::getByteFromConfig(pos + PCI_CAP_LIST_ID, configMem);
182182
if (id == PCI_CAP_ID_EXP) {
183183
capRegister = L0::PciUtil::getWordFromConfig(pos + PCI_CAP_FLAGS, configMem);
184-
type = BITS(capRegister, 4, 4);
184+
type = bits(capRegister, 4, 4);
185185

186186
// Root Complex Integrated end point and
187187
// Root Complex Event collector will not implement link capabilities
@@ -250,11 +250,11 @@ bool LinuxPciImp::resizableBarEnabled(uint32_t barIndex) {
250250

251251
// Only first Control register(at offset 008h, as shown above), could tell about number of resizable Bars
252252
controlRegister = L0::PciUtil::getDwordFromConfig(rebarCapabilityPos + PCI_REBAR_CTRL, configMemory.data());
253-
nBars = BITS(controlRegister, 5, 3); // control register's bits 5,6 and 7 contain number of resizable bars information
253+
nBars = bits(controlRegister, 5, 3); // control register's bits 5,6 and 7 contain number of resizable bars information
254254
for (auto barNumber = 0u; barNumber < nBars; barNumber++) {
255255
uint32_t barId = 0;
256256
controlRegister = L0::PciUtil::getDwordFromConfig(rebarCapabilityPos + PCI_REBAR_CTRL, configMemory.data());
257-
barId = BITS(controlRegister, 0, 3); // Control register's bit 0,1,2 tells the index of bar
257+
barId = bits(controlRegister, 0, 3); // Control register's bit 0,1,2 tells the index of bar
258258
if (barId == barIndex) {
259259
isBarResizable = true;
260260
break;
@@ -278,7 +278,7 @@ bool LinuxPciImp::resizableBarEnabled(uint32_t barIndex) {
278278

279279
// Control register's bit 8 to 13 indicates current BAR size in encoded form.
280280
// Example, real value of current size could be 2^currentSize MB
281-
auto currentSize = BITS(controlRegister, 8, 6);
281+
auto currentSize = bits(controlRegister, 8, 6);
282282

283283
// If current size is equal to larget possible BAR size, it indicates resizable BAR is enabled.
284284
return (currentSize == largestPossibleBarSize);

level_zero/tools/source/sysman/pci/pci_imp.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2023 Intel Corporation
2+
* Copyright (C) 2020-2024 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -48,19 +48,19 @@ int64_t convertPcieSpeedFromGTsToBs(double maxLinkSpeedInGt) {
4848

4949
double convertPciGenToLinkSpeed(uint32_t gen) {
5050
switch (gen) {
51-
case PciGenerations::PciGen1: {
51+
case static_cast<uint32_t>(PciGenerations::pciGen1): {
5252
return PciLinkSpeeds::pci2Dot5GigaTransfersPerSecond;
5353
} break;
54-
case PciGenerations::PciGen2: {
54+
case static_cast<uint32_t>(PciGenerations::pciGen2): {
5555
return PciLinkSpeeds::pci5GigaTransfersPerSecond;
5656
} break;
57-
case PciGenerations::PciGen3: {
57+
case static_cast<uint32_t>(PciGenerations::pciGen3): {
5858
return PciLinkSpeeds::pci8GigaTransfersPerSecond;
5959
} break;
60-
case PciGenerations::PciGen4: {
60+
case static_cast<uint32_t>(PciGenerations::pciGen4): {
6161
return PciLinkSpeeds::pci16GigaTransfersPerSecond;
6262
} break;
63-
case PciGenerations::PciGen5: {
63+
case static_cast<uint32_t>(PciGenerations::pciGen5): {
6464
return PciLinkSpeeds::pci32GigaTransfersPerSecond;
6565
} break;
6666
default: {
@@ -71,15 +71,15 @@ double convertPciGenToLinkSpeed(uint32_t gen) {
7171

7272
int32_t convertLinkSpeedToPciGen(double speed) {
7373
if (speed == PciLinkSpeeds::pci2Dot5GigaTransfersPerSecond) {
74-
return PciGenerations::PciGen1;
74+
return static_cast<int32_t>(PciGenerations::pciGen1);
7575
} else if (speed == PciLinkSpeeds::pci5GigaTransfersPerSecond) {
76-
return PciGenerations::PciGen2;
76+
return static_cast<int32_t>(PciGenerations::pciGen2);
7777
} else if (speed == PciLinkSpeeds::pci8GigaTransfersPerSecond) {
78-
return PciGenerations::PciGen3;
78+
return static_cast<int32_t>(PciGenerations::pciGen3);
7979
} else if (speed == PciLinkSpeeds::pci16GigaTransfersPerSecond) {
80-
return PciGenerations::PciGen4;
80+
return static_cast<int32_t>(PciGenerations::pciGen4);
8181
} else if (speed == PciLinkSpeeds::pci32GigaTransfersPerSecond) {
82-
return PciGenerations::PciGen5;
82+
return static_cast<int32_t>(PciGenerations::pciGen5);
8383
} else {
8484
return -1;
8585
}

level_zero/tools/source/sysman/scheduler/linux/os_scheduler_imp_prelim.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022-2023 Intel Corporation
2+
* Copyright (C) 2022-2024 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -23,7 +23,6 @@ const std::string LinuxSchedulerImp::heartbeatIntervalMilliSecs("heartbeat_inter
2323
const std::string LinuxSchedulerImp::defaultHeartbeatIntervalMilliSecs(".defaults/heartbeat_interval_ms");
2424
const std::string LinuxSchedulerImp::engineDir("engine");
2525
const std::string LinuxSchedulerImp::enableEuDebug("prelim_enable_eu_debug");
26-
constexpr uint16_t milliSecsToMicroSecs = 1000;
2726

2827
static const std::map<__u16, std::string> i915EngineClassToSysfsEngineMap = {
2928
{drm_i915_gem_engine_class::I915_ENGINE_CLASS_RENDER, "rcs"},

level_zero/tools/source/sysman/sysman_const.h

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77

88
#pragma once
99
#include <chrono>
10-
#include <string>
11-
const std::string vendorIntel("Intel(R) Corporation");
12-
const std::string unknown("unknown");
13-
const std::string intelPciId("0x8086");
14-
const std::string guid64BitMemoryCounters("0xb15a0ede");
15-
constexpr uint32_t mbpsToBytesPerSecond = 125000;
16-
constexpr double milliVoltsFactor = 1000.0;
17-
constexpr uint32_t maxRasErrorCategoryCount = 7;
18-
constexpr uint32_t maxRasErrorCategoryExpCount = 10;
10+
#include <string_view>
11+
1912
namespace L0 {
13+
14+
inline constexpr std::string_view vendorIntel("Intel(R) Corporation");
15+
inline constexpr std::string_view unknown("unknown");
16+
inline constexpr std::string_view intelPciId("0x8086");
17+
inline constexpr std::string_view guid64BitMemoryCounters("0xb15a0ede");
18+
inline constexpr uint32_t mbpsToBytesPerSecond = 125000;
19+
inline constexpr double milliVoltsFactor = 1000.0;
20+
inline constexpr uint32_t maxRasErrorCategoryCount = 7;
21+
inline constexpr uint32_t maxRasErrorCategoryExpCount = 10;
22+
2023
struct SteadyClock {
2124
typedef std::chrono::duration<uint64_t, std::milli> duration;
2225
typedef duration::rep rep;
@@ -28,39 +31,41 @@ struct SteadyClock {
2831
}
2932
};
3033

31-
} // namespace L0
32-
3334
namespace PciLinkSpeeds {
34-
constexpr double pci2Dot5GigaTransfersPerSecond = 2.5;
35-
constexpr double pci5GigaTransfersPerSecond = 5.0;
36-
constexpr double pci8GigaTransfersPerSecond = 8.0;
37-
constexpr double pci16GigaTransfersPerSecond = 16.0;
38-
constexpr double pci32GigaTransfersPerSecond = 32.0;
35+
inline constexpr double pci2Dot5GigaTransfersPerSecond = 2.5;
36+
inline constexpr double pci5GigaTransfersPerSecond = 5.0;
37+
inline constexpr double pci8GigaTransfersPerSecond = 8.0;
38+
inline constexpr double pci16GigaTransfersPerSecond = 16.0;
39+
inline constexpr double pci32GigaTransfersPerSecond = 32.0;
3940

4041
} // namespace PciLinkSpeeds
41-
enum PciGenerations {
42-
PciGen1 = 1,
43-
PciGen2,
44-
PciGen3,
45-
PciGen4,
46-
PciGen5,
42+
enum class PciGenerations {
43+
pciGen1 = 1,
44+
pciGen2,
45+
pciGen3,
46+
pciGen4,
47+
pciGen5,
4748
};
4849

49-
constexpr uint8_t maxPciBars = 6;
50+
inline constexpr uint8_t maxPciBars = 6;
5051
// Linux kernel would report 255 link width, as an indication of unknown.
51-
constexpr uint32_t unknownPcieLinkWidth = 255u;
52+
inline constexpr uint32_t unknownPcieLinkWidth = 255u;
53+
54+
inline constexpr uint32_t microSecondsToNanoSeconds = 1000u;
5255

53-
constexpr uint32_t microSecondsToNanoSeconds = 1000u;
56+
inline constexpr uint64_t convertJouleToMicroJoule = 1000000u;
57+
inline constexpr uint64_t minTimeoutModeHeartbeat = 5000u;
58+
inline constexpr uint64_t minTimeoutInMicroSeconds = 1000u;
59+
inline constexpr uint16_t milliSecsToMicroSecs = 1000;
60+
inline constexpr uint32_t milliFactor = 1000u;
61+
inline constexpr uint32_t microFacor = milliFactor * milliFactor;
62+
inline constexpr uint64_t gigaUnitTransferToUnitTransfer = 1000 * 1000 * 1000;
5463

55-
constexpr uint64_t convertJouleToMicroJoule = 1000000u;
56-
constexpr uint64_t minTimeoutModeHeartbeat = 5000u;
57-
constexpr uint64_t minTimeoutInMicroSeconds = 1000u;
58-
constexpr uint16_t milliSecsToMicroSecs = 1000;
59-
constexpr uint32_t milliFactor = 1000u;
60-
constexpr uint32_t microFacor = milliFactor * milliFactor;
61-
constexpr uint64_t gigaUnitTransferToUnitTransfer = 1000 * 1000 * 1000;
64+
inline constexpr int32_t memoryBusWidth = 128; // bus width in bytes
65+
inline constexpr int32_t numMemoryChannels = 8;
66+
inline constexpr uint32_t unknownMemoryType = UINT32_MAX;
6267

63-
constexpr int32_t memoryBusWidth = 128; // bus width in bytes
64-
constexpr int32_t numMemoryChannels = 8;
65-
constexpr uint32_t unknownMemoryType = UINT32_MAX;
66-
#define BITS(x, at, width) (((x) >> (at)) & ((1 << (width)) - 1))
68+
inline constexpr uint32_t bits(uint32_t x, uint32_t at, uint32_t width) {
69+
return (((x) >> (at)) & ((1 << (width)) - 1));
70+
}
71+
} // namespace L0

level_zero/tools/test/unit_tests/sources/sysman/memory/linux/mock_memory_prelim.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ struct MockMemoryPmt : public PlatformMonitoringTech {
253253
bool mockVfid1Status = false;
254254
bool isRepeated = false;
255255

256-
void setGuid(std::string guid) {
256+
void setGuid(std::string_view guid) {
257257
this->guid = guid;
258258
}
259259

level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_zes_pci.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2023 Intel Corporation
2+
* Copyright (C) 2020-2024 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -620,7 +620,7 @@ TEST_F(ZesPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetStatsThenV
620620
}
621621

622622
TEST_F(ZesPciFixture, WhenConvertingLinkSpeedThenResultIsCorrect) {
623-
for (int32_t i = PciGenerations::PciGen1; i <= PciGenerations::PciGen5; i++) {
623+
for (int32_t i = static_cast<int32_t>(PciGenerations::pciGen1); i <= static_cast<int32_t>(PciGenerations::pciGen5); i++) {
624624
double speed = convertPciGenToLinkSpeed(i);
625625
int32_t gen = convertLinkSpeedToPciGen(speed);
626626
EXPECT_EQ(i, gen);

0 commit comments

Comments
 (0)