|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +#include "shared/source/debug_settings/debug_settings_manager.h" |
| 9 | +#include "shared/source/helpers/constants.h" |
| 10 | +#include "shared/source/os_interface/linux/drm_neo.h" |
| 11 | +#include "shared/source/os_interface/linux/ioctl_helper.h" |
| 12 | +#include "shared/source/os_interface/linux/sys_calls.h" |
| 13 | +#include "shared/source/os_interface/os_interface.h" |
| 14 | +#include "shared/source/xe_hpc_core/hw_cmds_base.h" |
| 15 | + |
| 16 | +#include "level_zero/core/source/device/device.h" |
| 17 | +#include "level_zero/core/source/device/device_imp.h" |
| 18 | +#include "level_zero/tools/source/metrics/os_metric_ip_sampling.h" |
| 19 | + |
| 20 | +#include <algorithm> |
| 21 | + |
| 22 | +namespace L0 { |
| 23 | + |
| 24 | +constexpr uint32_t maxDssBufferSize = 512 * KB; |
| 25 | +constexpr uint32_t defaultPollPeriodNs = 10000000u; |
| 26 | +constexpr uint32_t unitReportSize = 64u; |
| 27 | + |
| 28 | +class MetricIpSamplingLinuxImp : public MetricIpSamplingOsInterface { |
| 29 | + public: |
| 30 | + MetricIpSamplingLinuxImp(Device &device); |
| 31 | + ~MetricIpSamplingLinuxImp() override = default; |
| 32 | + ze_result_t startMeasurement(uint32_t ¬ifyEveryNReports, uint32_t &samplingPeriodNs) override; |
| 33 | + ze_result_t stopMeasurement() override; |
| 34 | + ze_result_t readData(uint8_t *pRawData, size_t *pRawDataSize) override; |
| 35 | + uint32_t getRequiredBufferSize(const uint32_t maxReportCount) override; |
| 36 | + uint32_t getUnitReportSize() override; |
| 37 | + bool isNReportsAvailable() override; |
| 38 | + bool isDependencyAvailable() override; |
| 39 | + |
| 40 | + private: |
| 41 | + int32_t stream = -1; |
| 42 | + Device &device; |
| 43 | + |
| 44 | + ze_result_t getNearestSupportedSamplingUnit(uint32_t &samplingPeriodNs, uint32_t &samplingRate); |
| 45 | +}; |
| 46 | + |
| 47 | +MetricIpSamplingLinuxImp::MetricIpSamplingLinuxImp(Device &device) : device(device) {} |
| 48 | + |
| 49 | +ze_result_t MetricIpSamplingLinuxImp::getNearestSupportedSamplingUnit(uint32_t &samplingPeriodNs, uint32_t &samplingUnit) { |
| 50 | + |
| 51 | + static constexpr uint64_t nsecPerSec = 1000000000ull; |
| 52 | + static constexpr uint32_t samplingClockGranularity = 251u; |
| 53 | + static constexpr uint32_t minSamplingUnit = 1u; |
| 54 | + static constexpr uint32_t maxSamplingUnit = 7u; |
| 55 | + const auto drm = device.getOsInterface().getDriverModel()->as<NEO::Drm>(); |
| 56 | + int32_t gpuTimeStampfrequency = 0; |
| 57 | + int32_t ret = drm->getTimestampFrequency(gpuTimeStampfrequency); |
| 58 | + if (ret < 0) { |
| 59 | + return ZE_RESULT_ERROR_UNKNOWN; |
| 60 | + } |
| 61 | + |
| 62 | + uint64_t gpuClockPeriodNs = nsecPerSec / static_cast<uint64_t>(gpuTimeStampfrequency); |
| 63 | + uint64_t numberOfClocks = samplingPeriodNs / gpuClockPeriodNs; |
| 64 | + |
| 65 | + samplingUnit = std::clamp(static_cast<uint32_t>(numberOfClocks / samplingClockGranularity), minSamplingUnit, maxSamplingUnit); |
| 66 | + samplingPeriodNs = samplingUnit * samplingClockGranularity * static_cast<uint32_t>(gpuClockPeriodNs); |
| 67 | + return ZE_RESULT_SUCCESS; |
| 68 | +} |
| 69 | + |
| 70 | +ze_result_t MetricIpSamplingLinuxImp::startMeasurement(uint32_t ¬ifyEveryNReports, uint32_t &samplingPeriodNs) { |
| 71 | + |
| 72 | + const auto drm = device.getOsInterface().getDriverModel()->as<NEO::Drm>(); |
| 73 | + |
| 74 | + uint32_t samplingUnit = 0; |
| 75 | + if (getNearestSupportedSamplingUnit(samplingPeriodNs, samplingUnit) != ZE_RESULT_SUCCESS) { |
| 76 | + return ZE_RESULT_ERROR_UNKNOWN; |
| 77 | + } |
| 78 | + |
| 79 | + DeviceImp &deviceImp = static_cast<DeviceImp &>(device); |
| 80 | + |
| 81 | + auto ioctlHelper = drm->getIoctlHelper(); |
| 82 | + uint32_t euStallFdParameter = ioctlHelper->getEuStallFdParameter(); |
| 83 | + std::array<uint64_t, 10u> properties; |
| 84 | + if (!ioctlHelper->getEuStallProperties(properties, maxDssBufferSize, samplingUnit, defaultPollPeriodNs, deviceImp.getPhysicalSubDeviceId())) { |
| 85 | + return ZE_RESULT_ERROR_UNKNOWN; |
| 86 | + } |
| 87 | + |
| 88 | + struct drm_i915_perf_open_param param = { |
| 89 | + .flags = I915_PERF_FLAG_FD_CLOEXEC | |
| 90 | + euStallFdParameter | |
| 91 | + I915_PERF_FLAG_FD_NONBLOCK, |
| 92 | + .num_properties = sizeof(properties) / 16, |
| 93 | + .properties_ptr = reinterpret_cast<uintptr_t>(properties.data()), |
| 94 | + }; |
| 95 | + |
| 96 | + stream = drm->ioctl(DRM_IOCTL_I915_PERF_OPEN, ¶m); |
| 97 | + if (stream < 0) { |
| 98 | + return ZE_RESULT_ERROR_UNKNOWN; |
| 99 | + } |
| 100 | + |
| 101 | + int32_t ret = NEO::SysCalls::ioctl(stream, I915_PERF_IOCTL_ENABLE, 0); |
| 102 | + PRINT_DEBUG_STRING(NEO::DebugManager.flags.PrintDebugMessages.get() && (ret < 0), stderr, |
| 103 | + "PRELIM_I915_PERF_IOCTL_ENABLE failed errno = %d | ret = %d \n", errno, ret); |
| 104 | + |
| 105 | + return (ret == 0) ? ZE_RESULT_SUCCESS : ZE_RESULT_ERROR_UNKNOWN; |
| 106 | +} |
| 107 | + |
| 108 | +ze_result_t MetricIpSamplingLinuxImp::stopMeasurement() { |
| 109 | + |
| 110 | + int32_t ret = NEO::SysCalls::close(stream); |
| 111 | + PRINT_DEBUG_STRING(NEO::DebugManager.flags.PrintDebugMessages.get() && (ret < 0), stderr, "close() failed errno = %d | ret = %d \n", |
| 112 | + errno, ret); |
| 113 | + stream = -1; |
| 114 | + |
| 115 | + return (ret == 0) ? ZE_RESULT_SUCCESS : ZE_RESULT_ERROR_UNKNOWN; |
| 116 | +} |
| 117 | + |
| 118 | +ze_result_t MetricIpSamplingLinuxImp::readData(uint8_t *pRawData, size_t *pRawDataSize) { |
| 119 | + |
| 120 | + ssize_t ret = NEO::SysCalls::pread(stream, pRawData, *pRawDataSize, 0u); |
| 121 | + PRINT_DEBUG_STRING(NEO::DebugManager.flags.PrintDebugMessages.get() && (ret < 0), stderr, "pread() failed errno = %d | ret = %d \n", |
| 122 | + errno, ret); |
| 123 | + |
| 124 | + if (ret >= 0) { |
| 125 | + *pRawDataSize = ret; |
| 126 | + return ZE_RESULT_SUCCESS; |
| 127 | + } |
| 128 | + |
| 129 | + *pRawDataSize = 0; |
| 130 | + return ZE_RESULT_ERROR_UNKNOWN; |
| 131 | +} |
| 132 | + |
| 133 | +uint32_t MetricIpSamplingLinuxImp::getRequiredBufferSize(const uint32_t maxReportCount) { |
| 134 | + |
| 135 | + uint32_t requiredBufferSize = getUnitReportSize() * maxReportCount; |
| 136 | + const auto hwInfo = device.getNEODevice()->getHardwareInfo(); |
| 137 | + return std::min(requiredBufferSize, maxDssBufferSize * hwInfo.gtSystemInfo.MaxDualSubSlicesSupported); |
| 138 | +} |
| 139 | + |
| 140 | +uint32_t MetricIpSamplingLinuxImp::getUnitReportSize() { |
| 141 | + return unitReportSize; |
| 142 | +} |
| 143 | + |
| 144 | +bool MetricIpSamplingLinuxImp::isNReportsAvailable() { |
| 145 | + struct pollfd pollParams; |
| 146 | + memset(&pollParams, 0, sizeof(pollParams)); |
| 147 | + |
| 148 | + DEBUG_BREAK_IF(stream == -1); |
| 149 | + |
| 150 | + pollParams.fd = stream; |
| 151 | + pollParams.revents = 0; |
| 152 | + pollParams.events = POLLIN; |
| 153 | + |
| 154 | + int32_t pollResult = NEO::SysCalls::poll(&pollParams, 1, 0u); |
| 155 | + PRINT_DEBUG_STRING(NEO::DebugManager.flags.PrintDebugMessages.get() && (pollResult < 0), stderr, "poll() failed errno = %d | pollResult = %d \n", |
| 156 | + errno, pollResult); |
| 157 | + |
| 158 | + if (pollResult > 0) { |
| 159 | + return true; |
| 160 | + } |
| 161 | + return false; |
| 162 | +} |
| 163 | + |
| 164 | +bool MetricIpSamplingLinuxImp::isDependencyAvailable() { |
| 165 | + |
| 166 | + auto hwInfo = device.getNEODevice()->getHardwareInfo(); |
| 167 | + auto isSupportedDevice = false; |
| 168 | + |
| 169 | + if (hwInfo.platform.eProductFamily == IGFX_PVC) { |
| 170 | + if (hwInfo.platform.usDeviceID == NEO::XE_HPC_CORE::pvcXtDeviceIds[0] || |
| 171 | + hwInfo.platform.usDeviceID == NEO::XE_HPC_CORE::pvcXtDeviceIds[1] || |
| 172 | + hwInfo.platform.usDeviceID == NEO::XE_HPC_CORE::pvcXtDeviceIds[2] || |
| 173 | + hwInfo.platform.usDeviceID == NEO::XE_HPC_CORE::pvcXtDeviceIds[3] || |
| 174 | + hwInfo.platform.usDeviceID == NEO::XE_HPC_CORE::pvcXtDeviceIds[4]) { |
| 175 | + isSupportedDevice = true; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + if (!isSupportedDevice) { |
| 180 | + return false; |
| 181 | + } |
| 182 | + |
| 183 | + uint32_t notifyEveryNReports = 1u; |
| 184 | + uint32_t samplingPeriod = 100; |
| 185 | + |
| 186 | + ze_result_t status = startMeasurement(notifyEveryNReports, samplingPeriod); |
| 187 | + if (stream != -1) { |
| 188 | + stopMeasurement(); |
| 189 | + } |
| 190 | + return status == ZE_RESULT_SUCCESS ? true : false; |
| 191 | +} |
| 192 | + |
| 193 | +std::unique_ptr<MetricIpSamplingOsInterface> MetricIpSamplingOsInterface::create(Device &device) { |
| 194 | + return std::make_unique<MetricIpSamplingLinuxImp>(device); |
| 195 | +} |
| 196 | + |
| 197 | +} // namespace L0 |
0 commit comments