1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
|
/*
* Copyright (C) 2020-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "level_zero/tools/source/metrics/metric.h"
#include "shared/source/device/sub_device.h"
#include "shared/source/execution_environment/execution_environment.h"
#include "shared/source/execution_environment/root_device_environment.h"
#include "shared/source/helpers/gfx_core_helper.h"
#include "shared/source/helpers/hw_info.h"
#include "level_zero/core/source/device/device.h"
#include "level_zero/core/source/device/device_imp.h"
#include "level_zero/core/source/driver/driver.h"
#include "level_zero/core/source/driver/driver_handle_imp.h"
#include "level_zero/core/source/gfx_core_helpers/l0_gfx_core_helper.h"
#include "level_zero/tools/source/metrics/metric_ip_sampling_source.h"
#include "level_zero/tools/source/metrics/metric_oa_source.h"
#include <map>
#include <utility>
namespace L0 {
void MetricSource::getMetricGroupSourceIdProperty(zet_base_properties_t *property) {
zet_intel_metric_source_id_exp_t *groupProperty = reinterpret_cast<zet_intel_metric_source_id_exp_t *>(property);
groupProperty->sourceId = type;
}
void MetricSource::initComputeMetricScopes(MetricDeviceContext &metricDeviceContext) {
if (metricDeviceContext.isMultiDeviceCapable()) {
// When supported, aggregated scope should be first (ID 0)
auto &l0GfxCoreHelper = metricDeviceContext.getDevice().getNEODevice()->getRootDeviceEnvironment().getHelper<L0GfxCoreHelper>();
if (l0GfxCoreHelper.supportMetricsAggregation()) {
metricDeviceContext.addMetricScope(aggregatedScopeName, aggregatedScopeDescription, 0);
}
auto deviceImp = static_cast<DeviceImp *>(&metricDeviceContext.getDevice());
uint32_t subDeviceCount = deviceImp->numSubDevices;
std::vector<ze_device_handle_t> subDevices(subDeviceCount);
deviceImp->getSubDevices(&subDeviceCount, subDevices.data());
for (auto &subDeviceHandle : subDevices) {
auto neoSubDevice = static_cast<NEO::SubDevice *>(Device::fromHandle(subDeviceHandle)->getNEODevice());
uint32_t subDeviceIndex = neoSubDevice->getSubDeviceIndex();
std::string scopeName = std::string(computeScopeNamePrefix) + std::to_string(subDeviceIndex);
std::string scopeDesc = std::string(computeScopeDescriptionPrefix) + std::to_string(subDeviceIndex);
metricDeviceContext.addMetricScope(scopeName, scopeDesc, subDeviceIndex);
}
} else {
auto subDeviceIndex = metricDeviceContext.getSubDeviceIndex();
std::string scopeName = std::string(computeScopeNamePrefix) + std::to_string(subDeviceIndex);
std::string scopeDesc = std::string(computeScopeDescriptionPrefix) + std::to_string(subDeviceIndex);
metricDeviceContext.addMetricScope(scopeName, scopeDesc, subDeviceIndex);
}
metricDeviceContext.setComputeMetricScopeInitialized();
}
std::optional<zet_intel_metric_hw_buffer_size_exp_desc_t *> MetricSource::getHwBufferSizeDesc(zet_base_desc_t *baseDesc) {
while (baseDesc != nullptr) {
if (baseDesc->stype == ZET_INTEL_STRUCTURE_TYPE_METRIC_HW_BUFFER_SIZE_EXP_DESC) {
return reinterpret_cast<zet_intel_metric_hw_buffer_size_exp_desc_t *>(baseDesc);
}
baseDesc = static_cast<zet_base_desc_t *>(const_cast<void *>(baseDesc->pNext));
}
return std::nullopt;
}
MetricDeviceContext::MetricDeviceContext(Device &inputDevice) : device(inputDevice) {
auto deviceNeo = device.getNEODevice();
std::tuple<uint32_t, uint32_t, uint32_t> subDeviceMap;
uint32_t hwSubDeviceIndex = 0u;
bool requiresSubDeviceHierarchy = false;
if (deviceNeo->getExecutionEnvironment()->getSubDeviceHierarchy(deviceNeo->getRootDeviceIndex(), &subDeviceMap)) {
hwSubDeviceIndex = std::get<1>(subDeviceMap);
requiresSubDeviceHierarchy = true;
}
if (requiresSubDeviceHierarchy) {
subDeviceIndex = hwSubDeviceIndex;
multiDeviceCapable = false;
} else {
bool isSubDevice = deviceNeo->isSubDevice();
subDeviceIndex = isSubDevice
? static_cast<NEO::SubDevice *>(deviceNeo)->getSubDeviceIndex()
: 0;
multiDeviceCapable = !isSubDevice && device.isImplicitScalingCapable();
}
metricSources[MetricSource::metricSourceTypeOa] = OaMetricSourceImp::create(*this);
metricSources[MetricSource::metricSourceTypeIpSampling] = IpSamplingMetricSourceImp::create(*this);
if (NEO::debugManager.flags.DisableProgrammableMetricsSupport.get()) {
isProgrammableMetricsEnabled = false;
}
}
bool MetricDeviceContext::enable() {
bool status = false;
for (auto const &entry : metricSources) {
auto const &metricSource = entry.second;
// Enable only if not already enabled.
if (!isEnableChecked) {
metricSource->enable();
}
status |= metricSource->isAvailable();
}
setMetricsCollectionAllowed(status);
isEnableChecked = true;
return status;
}
bool MetricDeviceContext::canDisable() {
if (isMetricsCollectionAllowed) {
for (auto const &entry : metricSources) {
auto const &metricSource = entry.second;
if (!metricSource->canDisable()) {
return false;
}
}
}
return true;
}
void MetricDeviceContext::disable() {
setMetricsCollectionAllowed(false);
}
ze_result_t MetricDeviceContext::metricGroupGet(uint32_t *pCount, zet_metric_group_handle_t *phMetricGroups) {
ze_result_t result = ZE_RESULT_SUCCESS;
uint32_t availableCount = 0;
uint32_t requestCount = *pCount;
if (!metricScopesInitialized) {
initMetricScopes();
}
for (auto const &entry : metricSources) {
auto const &metricSource = entry.second;
if (!metricSource->isAvailable()) {
continue;
}
result = metricSource->metricGroupGet(&requestCount, phMetricGroups);
if (result == ZE_RESULT_ERROR_UNSUPPORTED_FEATURE) {
result = ZE_RESULT_SUCCESS;
continue;
}
if (result != ZE_RESULT_SUCCESS) {
break;
}
availableCount += requestCount;
if (*pCount == 0) {
requestCount = 0;
} else {
DEBUG_BREAK_IF(availableCount > *pCount);
phMetricGroups += requestCount;
requestCount = *pCount - availableCount;
if (requestCount == 0) {
break;
}
}
}
*pCount = availableCount;
return result;
}
ze_result_t MetricDeviceContext::activateMetricGroupsPreferDeferred(uint32_t count, zet_metric_group_handle_t *phMetricGroups) {
if (!isMetricsCollectionAllowed) {
METRICS_LOG_ERR("%s", "Cannot activate when metrics is disabled");
return ZE_RESULT_ERROR_UNINITIALIZED;
}
// Create a map of metric source types and Metric groups
std::map<uint32_t, std::vector<zet_metric_group_handle_t>> metricGroupsPerMetricSourceMap{};
for (auto index = 0u; index < count; index++) {
auto &metricGroupSource =
static_cast<MetricGroupImp *>(MetricGroup::fromHandle(phMetricGroups[index]))->getMetricSource();
metricGroupsPerMetricSourceMap[metricGroupSource.getType()].push_back(phMetricGroups[index]);
}
for (auto const &entry : metricSources) {
auto const &metricSourceEntry = entry.second;
auto status = ZE_RESULT_SUCCESS;
if (!metricSourceEntry->isAvailable()) {
continue;
}
auto sourceType = metricSourceEntry->getType();
if (metricGroupsPerMetricSourceMap.find(sourceType) == metricGroupsPerMetricSourceMap.end()) {
status = metricSourceEntry->activateMetricGroupsPreferDeferred(0, nullptr);
} else {
auto &metricGroupVec = metricGroupsPerMetricSourceMap[sourceType];
status = metricSourceEntry->activateMetricGroupsPreferDeferred(
static_cast<uint32_t>(metricGroupVec.size()),
metricGroupVec.data());
}
if (status != ZE_RESULT_SUCCESS) {
return status;
}
}
return ZE_RESULT_SUCCESS;
}
ze_result_t MetricDeviceContext::appendMetricMemoryBarrier(CommandList &commandList) {
bool isSuccess = false;
for (auto const &entry : metricSources) {
auto const &metricSource = entry.second;
if (!metricSource->isAvailable()) {
continue;
}
ze_result_t result = metricSource->appendMetricMemoryBarrier(commandList);
if (result == ZE_RESULT_SUCCESS) {
isSuccess = true;
} else if (result != ZE_RESULT_ERROR_UNSUPPORTED_FEATURE) {
return result;
}
}
return isSuccess == false ? ZE_RESULT_ERROR_UNSUPPORTED_FEATURE : ZE_RESULT_SUCCESS;
}
bool MetricDeviceContext::isImplicitScalingCapable() const {
return multiDeviceCapable;
}
ze_result_t MetricDeviceContext::activateMetricGroups() {
for (auto const &entry : metricSources) {
auto const &metricSource = entry.second;
metricSource->activateMetricGroupsAlreadyDeferred();
}
return ZE_RESULT_SUCCESS;
}
void MetricDeviceContext::enableMetricApiForDevice(zet_device_handle_t hDevice, bool &isFailed) {
auto deviceImp = static_cast<DeviceImp *>(L0::Device::fromHandle(hDevice));
std::lock_guard<std::mutex> lock(deviceImp->getMetricDeviceContext().enableMetricsMutex);
// Initialize device.
isFailed |= !deviceImp->metricContext->enable();
// Initialize sub devices if available.
for (uint32_t i = 0; i < deviceImp->numSubDevices; ++i) {
isFailed |= !deviceImp->subDevices[i]->getMetricDeviceContext().enable();
}
}
ze_result_t MetricDeviceContext::disableMetricApiForDevice(zet_device_handle_t hDevice) {
auto deviceImp = static_cast<DeviceImp *>(L0::Device::fromHandle(hDevice));
std::lock_guard<std::mutex> lock(deviceImp->getMetricDeviceContext().enableMetricsMutex);
for (uint32_t i = 0; i < deviceImp->numSubDevices; ++i) {
if (!deviceImp->subDevices[i]->getMetricDeviceContext().canDisable()) {
METRICS_LOG_ERR("%s", "Cannot disable sub device, since metrics resources are still in use.");
return ZE_RESULT_ERROR_HANDLE_OBJECT_IN_USE;
}
}
if (!deviceImp->getMetricDeviceContext().canDisable()) {
METRICS_LOG_ERR("%s", "Cannot disable root device, since metrics resources are still in use.");
return ZE_RESULT_ERROR_HANDLE_OBJECT_IN_USE;
}
for (uint32_t i = 0; i < deviceImp->numSubDevices; ++i) {
deviceImp->subDevices[i]->getMetricDeviceContext().disable();
}
deviceImp->getMetricDeviceContext().disable();
return ZE_RESULT_SUCCESS;
}
ze_result_t MetricDeviceContext::enableMetricApi() {
bool failed = false;
for (auto &globalDriverHandle : *globalDriverHandles) {
auto driverHandle = L0::DriverHandle::fromHandle(globalDriverHandle);
auto rootDevices = std::vector<ze_device_handle_t>();
auto subDevices = std::vector<ze_device_handle_t>();
// Obtain root devices.
uint32_t rootDeviceCount = 0;
driverHandle->getDevice(&rootDeviceCount, nullptr);
rootDevices.resize(rootDeviceCount);
driverHandle->getDevice(&rootDeviceCount, rootDevices.data());
for (auto rootDeviceHandle : rootDevices) {
enableMetricApiForDevice(rootDeviceHandle, failed);
}
if (failed) {
break;
}
}
return failed
? ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE
: ZE_RESULT_SUCCESS;
}
ze_result_t MetricDeviceContext::getConcurrentMetricGroups(uint32_t metricGroupCount,
zet_metric_group_handle_t *phMetricGroups,
uint32_t *pConcurrentGroupCount, uint32_t *pCountPerConcurrentGroup) {
if (!areMetricGroupsFromSameDeviceHierarchy(metricGroupCount, phMetricGroups)) {
METRICS_LOG_ERR("%s", "Mix of root device and sub-device metric group handle is not allowed");
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
std::map<MetricSource *, std::vector<zet_metric_group_handle_t>> metricGroupsPerMetricSourceMap{};
for (auto index = 0u; index < metricGroupCount; index++) {
auto &metricGroupSource =
static_cast<MetricGroupImp *>(MetricGroup::fromHandle(phMetricGroups[index]))->getMetricSource();
metricGroupsPerMetricSourceMap[&metricGroupSource].push_back(phMetricGroups[index]);
}
// Calculate the maximum concurrent group count
uint32_t maxConcurrentGroupCount = 0;
for (auto &[source, metricGroups] : metricGroupsPerMetricSourceMap) {
uint32_t perSourceConcurrentCount = 0;
auto status = source->getConcurrentMetricGroups(metricGroups, &perSourceConcurrentCount, nullptr);
if (status != ZE_RESULT_SUCCESS) {
METRICS_LOG_ERR("Per source concurrent metric group query returned error status %d", status);
*pConcurrentGroupCount = 0;
return status;
}
maxConcurrentGroupCount = std::max(maxConcurrentGroupCount, perSourceConcurrentCount);
}
if (*pConcurrentGroupCount == 0) {
*pConcurrentGroupCount = maxConcurrentGroupCount;
return ZE_RESULT_SUCCESS;
}
if (*pConcurrentGroupCount != maxConcurrentGroupCount) {
METRICS_LOG_ERR("Input Concurrent Group Count %d is not same as expected %d", *pConcurrentGroupCount, maxConcurrentGroupCount);
*pConcurrentGroupCount = 0;
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
std::vector<std::vector<zet_metric_group_handle_t>> concurrentGroups(maxConcurrentGroupCount);
for (auto &entry : metricGroupsPerMetricSourceMap) {
auto source = entry.first;
auto &metricGroups = entry.second;
// Using maximum possible concurrent group count
uint32_t perSourceConcurrentCount = metricGroupCount;
std::vector<uint32_t> countPerConcurrentGroup(perSourceConcurrentCount);
auto status = source->getConcurrentMetricGroups(metricGroups, &perSourceConcurrentCount, countPerConcurrentGroup.data());
if (status != ZE_RESULT_SUCCESS) {
METRICS_LOG_ERR("getConcurrentMetricGroups returned error status %d", status);
*pConcurrentGroupCount = 0;
return status;
}
DEBUG_BREAK_IF(static_cast<uint32_t>(concurrentGroups.size() < perSourceConcurrentCount));
auto metricGroupsStartOffset = metricGroups.begin();
[[maybe_unused]] uint32_t totalMetricGroupCount = 0;
// Copy the handles to appropriate groups
for (uint32_t groupIndex = 0; groupIndex < perSourceConcurrentCount; groupIndex++) {
totalMetricGroupCount += countPerConcurrentGroup[groupIndex];
DEBUG_BREAK_IF(totalMetricGroupCount > static_cast<uint32_t>(metricGroups.size()));
concurrentGroups[groupIndex].insert(concurrentGroups[groupIndex].end(),
metricGroupsStartOffset,
metricGroupsStartOffset + countPerConcurrentGroup[groupIndex]);
metricGroupsStartOffset += countPerConcurrentGroup[groupIndex];
}
}
// Update the concurrent Group count and count per concurrent group
*pConcurrentGroupCount = static_cast<uint32_t>(concurrentGroups.size());
for (uint32_t index = 0u; index < *pConcurrentGroupCount; index++) {
pCountPerConcurrentGroup[index] = static_cast<uint32_t>(concurrentGroups[index].size());
}
// Update the output metric groups
size_t availableSize = metricGroupCount;
for (auto &concurrentGroup : concurrentGroups) {
memcpy_s(phMetricGroups, availableSize * sizeof(zet_metric_group_handle_t), concurrentGroup.data(), concurrentGroup.size() * sizeof(zet_metric_group_handle_t));
availableSize -= concurrentGroup.size();
phMetricGroups += concurrentGroup.size();
}
DEBUG_BREAK_IF(availableSize != 0);
return ZE_RESULT_SUCCESS;
}
ze_result_t MetricDeviceContext::metricProgrammableGet(
uint32_t *pCount, zet_metric_programmable_exp_handle_t *phMetricProgrammables) {
if (!isProgrammableMetricsEnabled) {
*pCount = 0;
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
ze_result_t result = ZE_RESULT_SUCCESS;
uint32_t availableCount = 0;
uint32_t requestCount = *pCount;
for (auto const &entry : metricSources) {
auto const &metricSource = entry.second;
if (!metricSource->isAvailable()) {
continue;
}
result = metricSource->metricProgrammableGet(&requestCount, phMetricProgrammables);
if (result == ZE_RESULT_ERROR_UNSUPPORTED_FEATURE) {
result = ZE_RESULT_SUCCESS;
continue;
}
if (result != ZE_RESULT_SUCCESS) {
// Currently there is no error possible other than unsupported feature
DEBUG_BREAK_IF(true);
break;
}
availableCount += requestCount;
if (*pCount == 0) {
requestCount = 0;
} else {
DEBUG_BREAK_IF(availableCount > *pCount);
phMetricProgrammables += requestCount;
requestCount = *pCount - availableCount;
if (requestCount == 0) {
break;
}
}
}
*pCount = availableCount;
return result;
}
ze_result_t MetricDeviceContext::createMetricGroupsFromMetrics(uint32_t metricCount, zet_metric_handle_t *phMetrics,
const char metricGroupNamePrefix[ZET_INTEL_MAX_METRIC_GROUP_NAME_PREFIX_EXP],
const char description[ZET_MAX_METRIC_GROUP_DESCRIPTION],
uint32_t *pMetricGroupCount,
zet_metric_group_handle_t *phMetricGroups) {
if (!isProgrammableMetricsEnabled) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
if (metricCount == 0) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
zet_metric_group_handle_t *cleanupMetricGroups = phMetricGroups;
// Create a map of metric source types and Metrics
std::map<MetricSource *, std::vector<zet_metric_handle_t>> metricsPerMetricSourceMap{};
for (auto index = 0u; index < metricCount; index++) {
auto metricImp = static_cast<MetricImp *>(Metric::fromHandle(phMetrics[index]));
if (metricImp->isImmutable()) {
*pMetricGroupCount = 0;
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
auto &metricSource = metricImp->getMetricSource();
metricsPerMetricSourceMap[&metricSource].push_back(phMetrics[index]);
}
auto isGetMetricGroupCountPath = *pMetricGroupCount == 0u;
uint32_t remainingMetricGroupCount = *pMetricGroupCount;
auto cleanupApi = [&]() {
if (!isGetMetricGroupCountPath) {
for (uint32_t index = 0; index < (*pMetricGroupCount - remainingMetricGroupCount); index++) {
zetMetricGroupDestroyExp(cleanupMetricGroups[index]);
}
}
};
for (auto &metricSourceEntry : metricsPerMetricSourceMap) {
MetricSource *metricSource = metricSourceEntry.first;
std::vector<zet_metric_group_handle_t> metricGroupList{};
uint32_t metricGroupCountPerSource = remainingMetricGroupCount;
auto status = metricSource->createMetricGroupsFromMetrics(metricSourceEntry.second, metricGroupNamePrefix, description, &metricGroupCountPerSource, metricGroupList);
if (status != ZE_RESULT_SUCCESS) {
if (status == ZE_RESULT_ERROR_UNSUPPORTED_FEATURE) {
continue;
}
cleanupApi();
*pMetricGroupCount = 0;
return status;
}
if (isGetMetricGroupCountPath) {
*pMetricGroupCount += metricGroupCountPerSource;
} else {
memcpy_s(phMetricGroups, remainingMetricGroupCount * sizeof(zet_metric_group_handle_t), metricGroupList.data(), metricGroupCountPerSource * sizeof(metricGroupList[0]));
DEBUG_BREAK_IF(metricGroupCountPerSource > remainingMetricGroupCount);
phMetricGroups += metricGroupCountPerSource;
remainingMetricGroupCount -= metricGroupCountPerSource;
if (!remainingMetricGroupCount) {
break;
}
}
}
*pMetricGroupCount = *pMetricGroupCount - remainingMetricGroupCount;
return ZE_RESULT_SUCCESS;
}
bool MetricDeviceContext::areMetricGroupsFromSameDeviceHierarchy(uint32_t count, zet_metric_group_handle_t *phMetricGroups) {
bool isRootDevice = isImplicitScalingCapable();
// Verify whether metricGroups belong to the device hierarchy
for (uint32_t index = 0; index < count; index++) {
auto metricGroupImp = static_cast<MetricGroupImp *>(MetricGroup::fromHandle(phMetricGroups[index]));
if (isRootDevice != metricGroupImp->isRootDevice()) {
return false;
}
}
return true;
}
bool MetricDeviceContext::areMetricsFromSameDeviceHierarchy(uint32_t count, zet_metric_handle_t *phMetrics) {
bool isRootDevice = isImplicitScalingCapable();
// Verify whether metricGroups belong to the device hierarchy
for (uint32_t index = 0; index < count; index++) {
auto metricImp = static_cast<MetricImp *>(Metric::fromHandle(phMetrics[index]));
if (isRootDevice != metricImp->isRootDevice()) {
return false;
}
}
return true;
}
ze_result_t MetricDeviceContext::metricGroupCreate(const char name[ZET_MAX_METRIC_GROUP_NAME],
const char description[ZET_MAX_METRIC_GROUP_DESCRIPTION],
zet_metric_group_sampling_type_flag_t samplingType,
zet_metric_group_handle_t *pMetricGroupHandle) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
bool MetricDeviceContext::areMetricGroupsFromSameSource(uint32_t count, zet_metric_group_handle_t *phMetricGroups, uint32_t *sourceType) {
DEBUG_BREAK_IF(count == 0);
auto metricGroupImp = static_cast<MetricGroupImp *>(MetricGroup::fromHandle(phMetricGroups[0]));
*sourceType = metricGroupImp->getMetricSource().getType();
// Verify whether all metric groups have the same source type
for (uint32_t index = 1; index < count; index++) {
metricGroupImp = static_cast<MetricGroupImp *>(MetricGroup::fromHandle(phMetricGroups[index]));
if (*sourceType != metricGroupImp->getMetricSource().getType()) {
*sourceType = MetricSource::metricSourceTypeUndefined;
return false;
}
}
return true;
}
bool MetricDeviceContext::areMetricsFromSameSource(uint32_t count, zet_metric_handle_t *phMetrics, uint32_t *sourceType) {
DEBUG_BREAK_IF(count == 0);
auto metricImp = static_cast<MetricImp *>(Metric::fromHandle(phMetrics[0]));
*sourceType = metricImp->getMetricSource().getType();
// Verify whether all metrics have the same source type
for (uint32_t index = 1; index < count; index++) {
auto metricImp = static_cast<MetricImp *>(Metric::fromHandle(phMetrics[index]));
if (*sourceType != metricImp->getMetricSource().getType()) {
*sourceType = MetricSource::metricSourceTypeUndefined;
return false;
}
}
return true;
}
ze_result_t MetricDeviceContext::calcOperationCreate(zet_context_handle_t hContext,
zet_intel_metric_calculation_exp_desc_t *pCalculationDesc,
zet_intel_metric_calculation_operation_exp_handle_t *phCalculationOperation) {
uint32_t metricGroupsSourceType = MetricSource::metricSourceTypeUndefined;
MetricGroupImp *metricGroupImp = nullptr;
if (pCalculationDesc->metricGroupCount > 0) {
if (!areMetricGroupsFromSameSource(pCalculationDesc->metricGroupCount, pCalculationDesc->phMetricGroups, &metricGroupsSourceType)) {
METRICS_LOG_ERR("%s", "Metric groups must be from the same domain");
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
if (!areMetricGroupsFromSameDeviceHierarchy(pCalculationDesc->metricGroupCount, pCalculationDesc->phMetricGroups)) {
METRICS_LOG_ERR("%s", "Mix of root device and sub-device metric group handle is not allowed");
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
metricGroupImp = static_cast<MetricGroupImp *>(MetricGroup::fromHandle(pCalculationDesc->phMetricGroups[0]));
}
uint32_t metricsSourceType = MetricSource::metricSourceTypeUndefined;
MetricImp *metricImp = nullptr;
if (pCalculationDesc->metricCount > 0) {
if (!areMetricsFromSameSource(pCalculationDesc->metricCount, pCalculationDesc->phMetrics, &metricsSourceType)) {
METRICS_LOG_ERR("%s", "Metrics must be from the same domain");
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
metricImp = static_cast<MetricImp *>(Metric::fromHandle(pCalculationDesc->phMetrics[0]));
// IpSampling does not use multi-device metrics
if ((metricImp->getMetricSource().getType() != MetricSource::metricSourceTypeIpSampling) &&
(!areMetricsFromSameDeviceHierarchy(pCalculationDesc->metricCount, pCalculationDesc->phMetrics))) {
METRICS_LOG_ERR("%s", "Mix of root device and sub-device metric handle is not allowed");
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
}
if (pCalculationDesc->metricGroupCount > 0) {
if ((pCalculationDesc->metricCount > 0) && (metricGroupsSourceType != metricsSourceType)) {
METRICS_LOG_ERR("%s", "Metric groups and metrics must be from the same domain");
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
} else if (pCalculationDesc->metricCount == 0) {
METRICS_LOG_ERR("%s", "Must define at least one metric group or metric");
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
if (pCalculationDesc->metricScopesCount == 0) {
METRICS_LOG_ERR("%s", "Must define at least one metric scope");
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
} else if (!isImplicitScalingCapable() && (pCalculationDesc->metricScopesCount > 1)) {
METRICS_LOG_ERR("%s", "Sub-device can only calculate single metric scope");
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
std::vector<MetricScopeImp *> metricScopes;
for (uint32_t i = 0; i < pCalculationDesc->metricScopesCount; i++) {
metricScopes.push_back(static_cast<MetricScopeImp *>(MetricScope::fromHandle(pCalculationDesc->phMetricScopes[i])));
}
// Remove duplicates
std::sort(metricScopes.begin(), metricScopes.end());
metricScopes.erase(std::unique(metricScopes.begin(), metricScopes.end()), metricScopes.end());
// order metricScopes by ID in ascending order
std::stable_sort(metricScopes.begin(), metricScopes.end(),
[](const MetricScopeImp *a, const MetricScopeImp *b) {
return a->getId() < b->getId();
});
MetricSource &metricSource = (metricGroupImp) ? metricGroupImp->getMetricSource() : metricImp->getMetricSource(); // NOLINT(clang-analyzer-core.CallAndMessage)
return metricSource.calcOperationCreate(*this, pCalculationDesc, metricScopes, phCalculationOperation);
}
std::unique_ptr<MetricScopeImp> MetricScopeImp::create(zet_intel_metric_scope_properties_exp_t &scopeProperties, bool aggregated, uint32_t computeSubDeviceIndex) {
return std::make_unique<MetricScopeImp>(scopeProperties, aggregated, computeSubDeviceIndex);
}
void MetricDeviceContext::initMetricScopes() {
for (auto const &entry : metricSources) {
auto const &metricSource = entry.second;
if (!metricSource->isAvailable()) {
continue;
}
metricSource->initMetricScopes(*this);
}
metricScopesInitialized = true;
}
ze_result_t MetricDeviceContext::metricScopesGet(zet_context_handle_t hContext, uint32_t *pMetricScopesCount,
zet_intel_metric_scope_exp_handle_t *phMetricScopes) {
if (!metricScopesInitialized) {
initMetricScopes();
}
if (*pMetricScopesCount == 0) {
*pMetricScopesCount = static_cast<uint32_t>(metricScopes.size());
return ZE_RESULT_SUCCESS;
}
// User is expected to allocate space.
DEBUG_BREAK_IF(phMetricScopes == nullptr);
*pMetricScopesCount = std::min(*pMetricScopesCount, static_cast<uint32_t>(metricScopes.size()));
for (uint32_t i = 0; i < *pMetricScopesCount; i++) {
phMetricScopes[i] = metricScopes[i]->toHandle();
}
return ZE_RESULT_SUCCESS;
}
uint32_t MetricDeviceContext::addMetricScope(std::string_view scopeName, std::string_view scopeDescription, uint32_t scopeSubDeviceIndex) {
// If scope exists, return id
for (const auto &scopePtr : metricScopes) {
if (scopePtr->isName(scopeName)) {
return scopePtr->getId();
}
}
// Create new scope
zet_intel_metric_scope_properties_exp_t properties = {};
snprintf(properties.name, sizeof(properties.name), "%s", scopeName.data());
snprintf(properties.description, sizeof(properties.description), "%s", scopeDescription.data());
properties.iD = static_cast<uint32_t>(metricScopes.size());
bool aggregated = (scopeName == aggregatedScopeName);
auto newScope = MetricScopeImp::create(properties, aggregated, scopeSubDeviceIndex);
metricScopes.push_back(std::move(newScope));
return properties.iD;
}
ze_result_t MetricScopeImp::getProperties(zet_intel_metric_scope_properties_exp_t *pProperties) {
*pProperties = properties;
return ZE_RESULT_SUCCESS;
}
ze_result_t MultiDeviceMetricImp::getProperties(zet_metric_properties_t *pProperties) {
return subDeviceMetrics[0]->getProperties(pProperties);
}
MultiDeviceMetricImp *MultiDeviceMetricImp::create(MetricSource &metricSource, std::vector<MetricImp *> &subDeviceMetrics) {
return new (std::nothrow) MultiDeviceMetricImp(metricSource, subDeviceMetrics);
}
MetricImp *MultiDeviceMetricImp::getMetricAtSubDeviceIndex(uint32_t index) {
if (index < subDeviceMetrics.size()) {
return subDeviceMetrics.at(index);
}
return nullptr;
}
ze_result_t MetricImp::getScopes(uint32_t *pCount, zet_intel_metric_scope_exp_handle_t *phScopes) {
if (*pCount == 0) {
*pCount = static_cast<uint32_t>(scopes.size());
return ZE_RESULT_SUCCESS;
}
*pCount = std::min(*pCount, static_cast<uint32_t>(scopes.size()));
for (uint32_t i = 0; i < *pCount; i++) {
phScopes[i] = scopes[i];
}
return ZE_RESULT_SUCCESS;
}
ze_result_t metricGroupGet(zet_device_handle_t hDevice, uint32_t *pCount, zet_metric_group_handle_t *phMetricGroups) {
auto device = Device::fromHandle(hDevice);
return device->getMetricDeviceContext().metricGroupGet(pCount, phMetricGroups);
}
ze_result_t metricStreamerOpen(zet_context_handle_t hContext, zet_device_handle_t hDevice, zet_metric_group_handle_t hMetricGroup,
zet_metric_streamer_desc_t *pDesc, ze_event_handle_t hNotificationEvent,
zet_metric_streamer_handle_t *phMetricStreamer) {
return MetricGroup::fromHandle(hMetricGroup)->streamerOpen(hContext, hDevice, pDesc, hNotificationEvent, phMetricStreamer);
}
bool MultiDomainDeferredActivationTracker::activateMetricGroupsDeferred(uint32_t count, zet_metric_group_handle_t *phMetricGroups) {
// Activation: postpone until zetMetricStreamerOpen or zeCommandQueueExecuteCommandLists
// Deactivation: execute immediately.
if (phMetricGroups == nullptr) {
deActivateAllDomains();
return true;
}
auto isMetricGroupProvided = [phMetricGroups, count](const zet_metric_group_handle_t hMetricGroup) {
for (auto index = 0u; index < count; index++) {
if (hMetricGroup == phMetricGroups[index]) {
return true;
}
}
return false;
};
// Deactivate existing metric groups which are not provided in phMetricGroups
std::vector<uint32_t> deactivateList = {};
for (const auto &[domainId, metricGroupPair] : domains) {
const auto &hMetricGroup = metricGroupPair.first;
if (isMetricGroupProvided(hMetricGroup) == false) {
deActivateDomain(domainId);
deactivateList.push_back(domainId);
}
}
// Remove deactivated ones from the map
for (const auto &domainId : deactivateList) {
domains.erase(domainId);
}
// Activate-deferred new metric groups if any
for (auto index = 0u; index < count; index++) {
zet_metric_group_handle_t hMetricGroup = MetricGroup::fromHandle(phMetricGroups[index])->getMetricGroupForSubDevice(subDeviceIndex);
zet_metric_group_properties_t properties = {ZET_STRUCTURE_TYPE_METRIC_GROUP_PROPERTIES, nullptr};
auto metricGroup = MetricGroup::fromHandle(hMetricGroup);
metricGroup->getProperties(&properties);
auto domain = properties.domain;
// Domain already associated with the same handle.
if (domains[domain].first == hMetricGroup) {
continue;
}
domains[domain].first = hMetricGroup;
domains[domain].second = false;
}
return true;
}
ze_result_t MultiDomainDeferredActivationTracker::activateMetricGroupsAlreadyDeferred() {
for (auto &entry : domains) {
auto &metricGroupEntry = entry.second;
DEBUG_BREAK_IF(metricGroupEntry.first == nullptr);
auto metricGroup = MetricGroup::fromHandle(metricGroupEntry.first);
metricGroup->activate();
metricGroupEntry.second = true;
}
return ZE_RESULT_SUCCESS;
}
void MultiDomainDeferredActivationTracker::deActivateDomain(uint32_t domain) {
auto &metricGroupPair = domains[domain];
if (metricGroupPair.second == true) {
MetricGroup::fromHandle(metricGroupPair.first)->deactivate();
}
}
void MultiDomainDeferredActivationTracker::deActivateAllDomains() {
for (auto &entry : domains) {
deActivateDomain(entry.first);
}
domains.clear();
}
bool MultiDomainDeferredActivationTracker::isMetricGroupActivated(const zet_metric_group_handle_t hMetricGroup) const {
for (auto const &entry : domains) {
auto const &metricGroup = entry.second;
if (metricGroup.first == hMetricGroup) {
return true;
}
}
return false;
}
bool MultiDomainDeferredActivationTracker::isMetricGroupActivatedInHw() const {
for (auto const &entry : domains) {
auto const &metricGroup = entry.second;
if (metricGroup.second == true) {
return true;
}
}
return false;
}
void MetricCollectorEventNotify::attachEvent(ze_event_handle_t hEvent) {
// Associate L0 notification event with metric notification.
pNotificationEvent = Event::fromHandle(hEvent);
if (pNotificationEvent != nullptr) {
pNotificationEvent->setMetricNotification(this);
}
}
void MetricCollectorEventNotify::detachEvent() {
// Remove association to L0 event.
if (pNotificationEvent != nullptr) {
pNotificationEvent->setMetricNotification(nullptr);
}
}
void MetricGroupUserDefined::updateErrorString(std::string &errorString, size_t *errorStringSize, char *pErrorString) {
if (*errorStringSize == 0) {
*errorStringSize = errorString.length() + 1;
} else {
*errorStringSize = std::min(*errorStringSize, errorString.length() + 1);
strncpy_s(pErrorString, *errorStringSize, errorString.data(), *errorStringSize);
}
}
MetricProgrammable *HomogeneousMultiDeviceMetricProgrammable::create(MetricSource &metricSource,
std::vector<MetricProgrammable *> &subDeviceProgrammables) {
return static_cast<MetricProgrammable *>(new (std::nothrow) HomogeneousMultiDeviceMetricProgrammable(metricSource, subDeviceProgrammables));
}
ze_result_t HomogeneousMultiDeviceMetricProgrammable::getProperties(zet_metric_programmable_exp_properties_t *pProperties) {
return subDeviceProgrammables[0]->getProperties(pProperties);
}
ze_result_t HomogeneousMultiDeviceMetricProgrammable::getParamInfo(uint32_t *pParameterCount, zet_metric_programmable_param_info_exp_t *pParameterInfo) {
return subDeviceProgrammables[0]->getParamInfo(pParameterCount, pParameterInfo);
}
ze_result_t HomogeneousMultiDeviceMetricProgrammable::getParamValueInfo(uint32_t parameterOrdinal, uint32_t *pValueInfoCount,
zet_metric_programmable_param_value_info_exp_t *pValueInfo) {
return subDeviceProgrammables[0]->getParamValueInfo(parameterOrdinal, pValueInfoCount, pValueInfo);
}
ze_result_t HomogeneousMultiDeviceMetricProgrammable::createMetric(zet_metric_programmable_param_value_exp_t *pParameterValues,
uint32_t parameterCount, const char name[ZET_MAX_METRIC_NAME],
const char description[ZET_MAX_METRIC_DESCRIPTION],
uint32_t *pMetricHandleCount, zet_metric_handle_t *phMetricHandles) {
auto isCountEstimationPath = *pMetricHandleCount == 0;
ze_result_t status = ZE_RESULT_SUCCESS;
uint32_t expectedMetricHandleCount = 0;
auto isExpectedHandleCount = [&](const uint32_t actualHandleCount) {
if (expectedMetricHandleCount != 0 && expectedMetricHandleCount != actualHandleCount) {
METRICS_LOG_ERR("Unexpected Metric Handle Count for subdevice expected:%d, actual:%d", expectedMetricHandleCount, actualHandleCount);
return false;
}
expectedMetricHandleCount = actualHandleCount;
return true;
};
if (isCountEstimationPath) {
for (auto &subDeviceProgrammable : subDeviceProgrammables) {
uint32_t metricHandleCount = 0;
status = subDeviceProgrammable->createMetric(pParameterValues, parameterCount, name, description, &metricHandleCount, nullptr);
if (status != ZE_RESULT_SUCCESS || metricHandleCount == 0u) {
*pMetricHandleCount = 0;
return status;
}
if (!isExpectedHandleCount(metricHandleCount)) {
*pMetricHandleCount = 0;
return ZE_RESULT_ERROR_UNKNOWN;
}
}
*pMetricHandleCount = expectedMetricHandleCount;
return status;
}
std::vector<std::vector<zet_metric_handle_t>> metricHandlesPerSubDeviceList{};
auto cleanupApi = [&]() {
for (auto &metricHandlesPerSubDevice : metricHandlesPerSubDeviceList) {
for (auto &metricHandle : metricHandlesPerSubDevice) {
if (metricHandle != nullptr) {
[[maybe_unused]] auto status = static_cast<MetricImp *>(metricHandle)->destroy();
DEBUG_BREAK_IF(status != ZE_RESULT_SUCCESS);
}
}
metricHandlesPerSubDevice.clear();
}
metricHandlesPerSubDeviceList.clear();
};
metricHandlesPerSubDeviceList.resize(subDeviceProgrammables.size());
for (uint32_t index = 0; index < static_cast<uint32_t>(subDeviceProgrammables.size()); index++) {
auto &subDeviceProgrammable = subDeviceProgrammables[index];
uint32_t metricHandleCount = *pMetricHandleCount;
metricHandlesPerSubDeviceList[index].resize(metricHandleCount);
status = subDeviceProgrammable->createMetric(pParameterValues, parameterCount, name, description, &metricHandleCount, metricHandlesPerSubDeviceList[index].data());
if (status != ZE_RESULT_SUCCESS || metricHandleCount == 0u) {
cleanupApi();
*pMetricHandleCount = 0;
return status;
}
if (!isExpectedHandleCount(metricHandleCount)) {
cleanupApi();
*pMetricHandleCount = 0;
return ZE_RESULT_ERROR_UNKNOWN;
}
metricHandlesPerSubDeviceList[index].resize(metricHandleCount);
}
DEBUG_BREAK_IF(metricHandlesPerSubDeviceList[0].size() > *pMetricHandleCount);
// Create Root device Metric handles from sub-device handles
for (uint32_t index = 0; index < static_cast<uint32_t>(metricHandlesPerSubDeviceList[0].size()); index++) {
std::vector<MetricImp *> homogenousMetricList{};
homogenousMetricList.reserve(subDeviceProgrammables.size());
for (auto &metricHandlesPerSubdevice : metricHandlesPerSubDeviceList) {
homogenousMetricList.push_back(static_cast<MetricImp *>(Metric::fromHandle(metricHandlesPerSubdevice[index])));
}
phMetricHandles[index] = HomogeneousMultiDeviceMetricCreated::create(metricSource, homogenousMetricList)->toHandle();
}
*pMetricHandleCount = static_cast<uint32_t>(metricHandlesPerSubDeviceList[0].size());
return ZE_RESULT_SUCCESS;
}
ze_result_t HomogeneousMultiDeviceMetricCreated::destroy() {
auto status = ZE_RESULT_SUCCESS;
for (auto &subDeviceMetric : subDeviceMetrics) {
ze_result_t subDeviceStatus = subDeviceMetric->destroy();
// Hold the first error
if (status == ZE_RESULT_SUCCESS) {
status = subDeviceStatus;
}
}
// release only if object is unused
if (status != ZE_RESULT_ERROR_HANDLE_OBJECT_IN_USE) {
subDeviceMetrics.clear();
delete this;
}
return status;
}
MetricImp *HomogeneousMultiDeviceMetricCreated::create(MetricSource &metricSource, std::vector<MetricImp *> &subDeviceMetrics) {
return new (std::nothrow) HomogeneousMultiDeviceMetricCreated(metricSource, subDeviceMetrics);
}
ze_result_t MetricCalcOpImp::getMetricsFromCalcOp(uint32_t *pCount, zet_metric_handle_t *phMetrics, bool isExcludedMetrics, zet_intel_metric_scope_exp_handle_t *phMetricScopes) {
uint32_t requestedSize = *pCount;
uint32_t metricsInReportCount = getMetricsInReportCount();
*pCount = isExcludedMetrics ? getExcludedMetricsCount() : metricsInReportCount;
if (requestedSize == 0) {
return ZE_RESULT_SUCCESS;
}
if (requestedSize < *pCount) {
METRICS_LOG_ERR("%s", "Metric count can't be smaller than report size");
*pCount = 0;
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
if (isExcludedMetrics) {
for (uint32_t index = 0; index < *pCount; index++) {
phMetrics[index] = excludedMetrics[index]->toHandle();
}
} else {
for (uint32_t metricIndex = 0; metricIndex < metricsInReportCount; metricIndex++) {
phMetrics[metricIndex] = metricsInReport[metricIndex]->toHandle();
phMetricScopes[metricIndex] = metricScopesInReport[metricIndex]->toHandle();
}
}
return ZE_RESULT_SUCCESS;
}
ze_result_t MetricCalcOpImp::getReportFormat(uint32_t *pCount, zet_metric_handle_t *phMetrics, zet_intel_metric_scope_exp_handle_t *phMetricScopes) {
return getMetricsFromCalcOp(pCount, phMetrics, false, phMetricScopes);
}
ze_result_t MetricCalcOpImp::getExcludedMetrics(uint32_t *pCount, zet_metric_handle_t *phMetrics) {
return getMetricsFromCalcOp(pCount, phMetrics, true, nullptr);
}
ze_result_t metricProgrammableGet(zet_device_handle_t hDevice, uint32_t *pCount, zet_metric_programmable_exp_handle_t *phMetricProgrammables) {
auto device = Device::fromHandle(hDevice);
return static_cast<MetricDeviceContext &>(device->getMetricDeviceContext()).metricProgrammableGet(pCount, phMetricProgrammables);
}
ze_result_t metricProgrammableGetProperties(
zet_metric_programmable_exp_handle_t hMetricProgrammable,
zet_metric_programmable_exp_properties_t *pProperties) {
return L0::MetricProgrammable::fromHandle(hMetricProgrammable)->getProperties(pProperties);
}
ze_result_t metricProgrammableGetParamInfo(
zet_metric_programmable_exp_handle_t hMetricProgrammable,
uint32_t *pParameterCount,
zet_metric_programmable_param_info_exp_t *pParameterInfo) {
return L0::MetricProgrammable::fromHandle(hMetricProgrammable)->getParamInfo(pParameterCount, pParameterInfo);
}
ze_result_t metricProgrammableGetParamValueInfo(
zet_metric_programmable_exp_handle_t hMetricProgrammable,
uint32_t parameterOrdinal,
uint32_t *pValueInfoCount,
zet_metric_programmable_param_value_info_exp_t *pValueInfo) {
return L0::MetricProgrammable::fromHandle(hMetricProgrammable)->getParamValueInfo(parameterOrdinal, pValueInfoCount, pValueInfo);
}
ze_result_t metricCreateFromProgrammable(
zet_metric_programmable_exp_handle_t hMetricProgrammable,
zet_metric_programmable_param_value_exp_t *pParameterValues,
uint32_t parameterCount,
const char name[ZET_MAX_METRIC_NAME],
const char description[ZET_MAX_METRIC_DESCRIPTION],
uint32_t *pMetricHandleCount,
zet_metric_handle_t *phMetricHandles) {
return L0::MetricProgrammable::fromHandle(hMetricProgrammable)->createMetric(pParameterValues, parameterCount, name, description, pMetricHandleCount, phMetricHandles);
}
ze_result_t metricCalculationOperationCreate(
zet_context_handle_t hContext,
zet_device_handle_t hDevice,
zet_intel_metric_calculation_exp_desc_t *pCalculationDesc,
zet_intel_metric_calculation_operation_exp_handle_t *phCalculationOperation) {
DeviceImp *deviceImp = static_cast<DeviceImp *>(L0::Device::fromHandle(hDevice));
return deviceImp->getMetricDeviceContext().calcOperationCreate(hContext, pCalculationDesc, phCalculationOperation);
}
ze_result_t metricCalculationOperationDestroy(
zet_intel_metric_calculation_operation_exp_handle_t hCalculationOperation) {
return MetricCalcOp::fromHandle(hCalculationOperation)->destroy();
}
ze_result_t metricCalculationGetReportFormat(
zet_intel_metric_calculation_operation_exp_handle_t hCalculationOperation,
uint32_t *pCount,
zet_metric_handle_t *phMetrics,
zet_intel_metric_scope_exp_handle_t *phMetricScopes) {
return MetricCalcOp::fromHandle(hCalculationOperation)->getReportFormat(pCount, phMetrics, phMetricScopes);
}
ze_result_t metricCalculationGetExcludedMetrics(
zet_intel_metric_calculation_operation_exp_handle_t hCalculationOperation,
uint32_t *pCount,
zet_metric_handle_t *phMetrics) {
return MetricCalcOp::fromHandle(hCalculationOperation)->getExcludedMetrics(pCount, phMetrics);
}
ze_result_t metricCalculateValues(
const size_t rawDataSize,
const uint8_t *pRawData,
zet_intel_metric_calculation_operation_exp_handle_t hCalculationOperation,
bool final,
size_t *usedSize,
uint32_t *pTotalMetricReportsCount,
zet_intel_metric_result_exp_t *pMetricResults) {
return MetricCalcOp::fromHandle(hCalculationOperation)->metricCalculateValues(rawDataSize, pRawData, final, usedSize, pTotalMetricReportsCount, pMetricResults);
}
ze_result_t metricsEnable(zet_device_handle_t hDevice) {
auto isFailed = false;
MetricDeviceContext::enableMetricApiForDevice(hDevice, isFailed);
return isFailed ? ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE : ZE_RESULT_SUCCESS;
}
ze_result_t metricsDisable(zet_device_handle_t hDevice) {
return MetricDeviceContext::disableMetricApiForDevice(hDevice);
}
ze_result_t metricScopesGet(
zet_context_handle_t hContext,
zet_device_handle_t hDevice,
uint32_t *pMetricScopesCount,
zet_intel_metric_scope_exp_handle_t *phMetricScopes) {
DeviceImp *deviceImp = static_cast<DeviceImp *>(L0::Device::fromHandle(hDevice));
return deviceImp->getMetricDeviceContext().metricScopesGet(hContext, pMetricScopesCount, phMetricScopes);
}
ze_result_t metricScopeGetProperties(
zet_intel_metric_scope_exp_handle_t hMetricScope,
zet_intel_metric_scope_properties_exp_t *pMetricScopeProperties) {
return static_cast<MetricScopeImp *>(MetricScopeImp::fromHandle(hMetricScope))->getProperties(pMetricScopeProperties);
}
ze_result_t metricAppendMarker(zet_command_list_handle_t hCommandList, zet_metric_group_handle_t hMetricGroup, uint32_t value) {
auto metricGroupImp = static_cast<MetricGroupImp *>(L0::MetricGroup::fromHandle(hMetricGroup));
return metricGroupImp->getMetricSource().appendMarker(hCommandList, hMetricGroup, value);
}
ze_result_t getMetricSupportedScopes(
zet_metric_handle_t *phMetric,
uint32_t *pScopesCount,
zet_intel_metric_scope_exp_handle_t *phMetricScopes) {
auto metricImp = static_cast<MetricImp *>(Metric::fromHandle(*phMetric));
return metricImp->getScopes(pScopesCount, phMetricScopes);
}
} // namespace L0
|