|
| 1 | +// MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2024 Advanced Micro Devices, Inc. All rights reserved. |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +#include "lib/rocprofiler-sdk/counters/ioctl.hpp" |
| 24 | +#include "lib/rocprofiler-sdk/details/kfd_ioctl.h" |
| 25 | +#include "lib/rocprofiler-sdk/pc_sampling/ioctl/ioctl_adapter.hpp" |
| 26 | + |
| 27 | +#include <sys/ioctl.h> |
| 28 | +#include <cerrno> |
| 29 | + |
| 30 | +namespace rocprofiler |
| 31 | +{ |
| 32 | +namespace counters |
| 33 | +{ |
| 34 | +bool |
| 35 | +counter_collection_has_device_lock() |
| 36 | +{ |
| 37 | + kfd_ioctl_profiler_args args = {}; |
| 38 | + args.op = KFD_IOC_PROFILER_VERSION; |
| 39 | + int ret = ioctl(pc_sampling::ioctl::get_kfd_fd(), AMDKFD_IOC_PROFILER, &args); |
| 40 | + if(ret == 0) |
| 41 | + { |
| 42 | + return true; |
| 43 | + } |
| 44 | + return false; |
| 45 | +} |
| 46 | + |
| 47 | +rocprofiler_status_t |
| 48 | +counter_collection_device_lock(const rocprofiler_agent_t* agent, bool all_queues) |
| 49 | +{ |
| 50 | + CHECK(agent); |
| 51 | + kfd_ioctl_profiler_args args = {}; |
| 52 | + args.op = KFD_IOC_PROFILER_PMC; |
| 53 | + args.pmc.gpu_id = agent->gpu_id; |
| 54 | + args.pmc.lock = 1; |
| 55 | + args.pmc.perfcount_enable = all_queues ? 1 : 0; |
| 56 | + |
| 57 | + int ret = ioctl(pc_sampling::ioctl::get_kfd_fd(), AMDKFD_IOC_PROFILER, &args); |
| 58 | + if(ret != 0) |
| 59 | + { |
| 60 | + switch(ret) |
| 61 | + { |
| 62 | + case -EBUSY: |
| 63 | + ROCP_WARNING << fmt::format( |
| 64 | + "Device {} has a profiler attached to it. PMC Counters may be inaccurate.", |
| 65 | + agent->id.handle); |
| 66 | + return ROCPROFILER_STATUS_ERROR_OUT_OF_RESOURCES; |
| 67 | + case -EPERM: |
| 68 | + ROCP_WARNING << fmt::format( |
| 69 | + "Device {} could not be locked for profiling due to lack of permissions " |
| 70 | + "(capability SYS_PERFMON). PMC Counters may be inaccurate and System Counter " |
| 71 | + "Collection will be degraded.", |
| 72 | + agent->id.handle); |
| 73 | + return ROCPROFILER_STATUS_ERROR_PERMISSION_DENIED; |
| 74 | + case -EINVAL: |
| 75 | + ROCP_WARNING << fmt::format( |
| 76 | + "Driver/Kernel version does not support locking device {}. PMC Counters may be " |
| 77 | + "inaccurate and System Counter Collection will be degraded.", |
| 78 | + agent->id.handle); |
| 79 | + return ROCPROFILER_STATUS_ERROR_INCOMPATIBLE_ABI; |
| 80 | + default: |
| 81 | + ROCP_WARNING << fmt::format( |
| 82 | + "Failed to lock device {}. PMC Counters may be inaccurate and System Counter " |
| 83 | + "Collection will be degraded.", |
| 84 | + agent->id.handle); |
| 85 | + return ROCPROFILER_STATUS_ERROR; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return ROCPROFILER_STATUS_SUCCESS; |
| 90 | +} |
| 91 | + |
| 92 | +// Not required now but may be useful in the future. |
| 93 | +// rocprofiler_status_t |
| 94 | +// counter_collection_device_unlock(const rocprofiler_agent_t* agent) { |
| 95 | +// CHECK(agent); |
| 96 | +// kfd_ioctl_profiler_args args = {}; |
| 97 | +// args.op = KFD_IOC_PROFILER_PMC; |
| 98 | +// args.pmc.gpu_id = agent->gpu_id; |
| 99 | +// args.pmc.lock = 0; |
| 100 | +// args.pmc.perfcount_enable = 0; |
| 101 | + |
| 102 | +// int ret = ioctl(pc_sampling::ioctl::get_kfd_fd(), AMDKFD_IOC_PROFILER, &args); |
| 103 | +// if (ret != 0) { |
| 104 | +// switch (ret) { |
| 105 | +// case -EBUSY: |
| 106 | +// case -EPERM: |
| 107 | +// ROCP_WARNING << fmt::format("Could not unlock the device {}", agent->id.handle); |
| 108 | +// return ROCPROFILER_STATUS_ERROR; |
| 109 | +// case -EINVAL: |
| 110 | +// return ROCPROFILER_STATUS_ERROR_INCOMPATIBLE_ABI; |
| 111 | +// default: |
| 112 | +// ROCP_WARNING << fmt::format("Could not unlock the device {}", agent->id.handle); |
| 113 | +// return ROCPROFILER_STATUS_ERROR; |
| 114 | +// } |
| 115 | +// } |
| 116 | + |
| 117 | +// return ROCPROFILER_STATUS_SUCCESS; |
| 118 | +// } |
| 119 | +} // namespace counters |
| 120 | +} // namespace rocprofiler |
0 commit comments