Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "CounterDirectory.hpp" |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 7 | #include "ProfilingUtils.hpp" |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 8 | |
Cathal Corbett | 6f07372 | 2022-03-04 12:11:09 +0000 | [diff] [blame] | 9 | #include <armnn/BackendId.hpp> |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 10 | |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 11 | #include <common/include/Assert.hpp> |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 12 | #include <common/include/SwTrace.hpp> |
| 13 | |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 14 | #include <fmt/format.h> |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 15 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 16 | namespace arm |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 17 | { |
| 18 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 19 | namespace pipe |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 20 | { |
| 21 | |
Sadik Armagan | 4c99899 | 2020-02-25 12:44:44 +0000 | [diff] [blame] | 22 | const Category* CounterDirectory::RegisterCategory(const std::string& categoryName) |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 23 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 24 | // Check that the given category name is valid |
| 25 | if (categoryName.empty() || |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 26 | !arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>(categoryName)) |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 27 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 28 | throw arm::pipe::InvalidArgumentException("Trying to register a category with an invalid name"); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 29 | } |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 30 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 31 | // Check that the given category is not already registered |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 32 | if (IsCategoryRegistered(categoryName)) |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 33 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 34 | throw arm::pipe::InvalidArgumentException(fmt::format( |
| 35 | "Trying to register a category already registered (\"{}\")", |
| 36 | categoryName)); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 37 | } |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 38 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 39 | // Create the category |
Sadik Armagan | 4c99899 | 2020-02-25 12:44:44 +0000 | [diff] [blame] | 40 | CategoryPtr category = std::make_unique<Category>(categoryName); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 41 | ARM_PIPE_ASSERT(category); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 42 | |
| 43 | // Get the raw category pointer |
| 44 | const Category* categoryPtr = category.get(); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 45 | ARM_PIPE_ASSERT(categoryPtr); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 46 | |
| 47 | // Register the category |
| 48 | m_Categories.insert(std::move(category)); |
| 49 | |
| 50 | return categoryPtr; |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 53 | const Device* CounterDirectory::RegisterDevice(const std::string& deviceName, |
| 54 | uint16_t cores, |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 55 | const armnn::Optional<std::string>& parentCategoryName) |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 56 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 57 | // Check that the given device name is valid |
| 58 | if (deviceName.empty() || |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 59 | !arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>(deviceName)) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 60 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 61 | throw arm::pipe::InvalidArgumentException("Trying to register a device with an invalid name"); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // Check that a device with the given name is not already registered |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 65 | if (IsDeviceRegistered(deviceName)) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 66 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 67 | throw arm::pipe::InvalidArgumentException(fmt::format( |
| 68 | "Trying to register a device already registered (\"{}\")", |
| 69 | deviceName)); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 72 | // Check that a category with the given (optional) parent category name is already registered |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 73 | if (parentCategoryName.has_value()) |
| 74 | { |
| 75 | // Get the (optional) parent category name |
| 76 | const std::string& parentCategoryNameValue = parentCategoryName.value(); |
| 77 | if (parentCategoryNameValue.empty()) |
| 78 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 79 | throw arm::pipe::InvalidArgumentException( |
| 80 | fmt::format("Trying to connect a device (name: \"{}\") to an invalid " |
| 81 | "parent category (name: \"{}\")", |
| 82 | deviceName, |
| 83 | parentCategoryNameValue)); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // Check that the given parent category is already registered |
| 87 | auto categoryIt = FindCategory(parentCategoryNameValue); |
| 88 | if (categoryIt == m_Categories.end()) |
| 89 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 90 | throw arm::pipe::InvalidArgumentException( |
| 91 | fmt::format("Trying to connect a device (name: \"{}\") to a parent category that " |
| 92 | "is not registered (name: \"{}\")", |
| 93 | deviceName, |
| 94 | parentCategoryNameValue)); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 95 | } |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Get the device UID |
| 99 | uint16_t deviceUid = GetNextUid(); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 100 | |
| 101 | // Create the device |
| 102 | DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 103 | ARM_PIPE_ASSERT(device); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 104 | |
| 105 | // Get the raw device pointer |
| 106 | const Device* devicePtr = device.get(); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 107 | ARM_PIPE_ASSERT(devicePtr); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 108 | |
| 109 | // Register the device |
| 110 | m_Devices.insert(std::make_pair(deviceUid, std::move(device))); |
| 111 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 112 | return devicePtr; |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 115 | const CounterSet* CounterDirectory::RegisterCounterSet(const std::string& counterSetName, |
| 116 | uint16_t count, |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 117 | const armnn::Optional<std::string>& parentCategoryName) |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 118 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 119 | // Check that the given counter set name is valid |
| 120 | if (counterSetName.empty() || |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 121 | !arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>(counterSetName)) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 122 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 123 | throw arm::pipe::InvalidArgumentException("Trying to register a counter set with an invalid name"); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // Check that a counter set with the given name is not already registered |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 127 | if (IsCounterSetRegistered(counterSetName)) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 128 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 129 | throw arm::pipe::InvalidArgumentException( |
| 130 | fmt::format("Trying to register a counter set already registered (\"{}\")", counterSetName)); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // Peek the next UID, do not get an actual valid UID just now as we don't want to waste a good UID in case |
| 134 | // the registration fails. We'll get a proper one once we're sure that the counter set can be registered |
| 135 | uint16_t counterSetUidPeek = GetNextUid(true); |
| 136 | |
| 137 | // Check that a category with the given (optional) parent category name is already registered |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 138 | if (parentCategoryName.has_value()) |
| 139 | { |
| 140 | // Get the (optional) parent category name |
| 141 | const std::string& parentCategoryNameValue = parentCategoryName.value(); |
| 142 | if (parentCategoryNameValue.empty()) |
| 143 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 144 | throw arm::pipe::InvalidArgumentException( |
| 145 | fmt::format("Trying to connect a counter set (UID: {}) to an invalid " |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 146 | "parent category (name: \"{}\")", |
| 147 | counterSetUidPeek, |
| 148 | parentCategoryNameValue)); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // Check that the given parent category is already registered |
| 152 | auto it = FindCategory(parentCategoryNameValue); |
| 153 | if (it == m_Categories.end()) |
| 154 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 155 | throw arm::pipe::InvalidArgumentException( |
| 156 | fmt::format("Trying to connect a counter set (UID: {}) to a parent category " |
| 157 | "that is not registered (name: \"{}\")", |
| 158 | counterSetUidPeek, |
| 159 | parentCategoryNameValue)); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 160 | } |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // Get the counter set UID |
| 164 | uint16_t counterSetUid = GetNextUid(); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 165 | ARM_PIPE_ASSERT(counterSetUid == counterSetUidPeek); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 166 | |
| 167 | // Create the counter set |
| 168 | CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 169 | ARM_PIPE_ASSERT(counterSet); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 170 | |
| 171 | // Get the raw counter set pointer |
| 172 | const CounterSet* counterSetPtr = counterSet.get(); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 173 | ARM_PIPE_ASSERT(counterSetPtr); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 174 | |
| 175 | // Register the counter set |
| 176 | m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet))); |
| 177 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 178 | return counterSetPtr; |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Cathal Corbett | 6f07372 | 2022-03-04 12:11:09 +0000 | [diff] [blame] | 181 | const Counter* CounterDirectory::RegisterCounter(const std::string& /*backendId*/, |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 182 | const uint16_t uid, |
| 183 | const std::string& parentCategoryName, |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 184 | uint16_t counterClass, |
| 185 | uint16_t interpolation, |
| 186 | double multiplier, |
| 187 | const std::string& name, |
| 188 | const std::string& description, |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 189 | const armnn::Optional<std::string>& units, |
| 190 | const armnn::Optional<uint16_t>& numberOfCores, |
| 191 | const armnn::Optional<uint16_t>& deviceUid, |
| 192 | const armnn::Optional<uint16_t>& counterSetUid) |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 193 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 194 | // Check that the given parent category name is valid |
| 195 | if (parentCategoryName.empty() || |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 196 | !arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>(parentCategoryName)) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 197 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 198 | throw arm::pipe::InvalidArgumentException("Trying to register a counter with an invalid parent category name"); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // Check that the given class is valid |
| 202 | if (counterClass != 0 && counterClass != 1) |
| 203 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 204 | throw arm::pipe::InvalidArgumentException("Trying to register a counter with an invalid class"); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // Check that the given interpolation is valid |
| 208 | if (interpolation != 0 && interpolation != 1) |
| 209 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 210 | throw arm::pipe::InvalidArgumentException("Trying to register a counter with an invalid interpolation"); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // Check that the given multiplier is valid |
| 214 | if (multiplier == .0f) |
| 215 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 216 | throw arm::pipe::InvalidArgumentException("Trying to register a counter with an invalid multiplier"); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Check that the given name is valid |
| 220 | if (name.empty() || |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 221 | !arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>(name)) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 222 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 223 | throw arm::pipe::InvalidArgumentException("Trying to register a counter with an invalid name"); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // Check that the given description is valid |
| 227 | if (description.empty() || |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 228 | !arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>(description)) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 229 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 230 | throw arm::pipe::InvalidArgumentException("Trying to register a counter with an invalid description"); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // Check that the given units are valid |
| 234 | if (units.has_value() |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 235 | && !arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>(units.value())) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 236 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 237 | throw arm::pipe::InvalidArgumentException("Trying to register a counter with a invalid units"); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | // Check that the given parent category is registered |
| 241 | auto categoryIt = FindCategory(parentCategoryName); |
| 242 | if (categoryIt == m_Categories.end()) |
| 243 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 244 | throw arm::pipe::InvalidArgumentException( |
| 245 | fmt::format("Trying to connect a counter to a category that is not registered (name: \"{}\")", |
| 246 | parentCategoryName)); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | // Get the parent category |
| 250 | const CategoryPtr& parentCategory = *categoryIt; |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 251 | ARM_PIPE_ASSERT(parentCategory); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 252 | |
| 253 | // Check that a counter with the given name is not already registered within the parent category |
| 254 | const std::vector<uint16_t>& parentCategoryCounters = parentCategory->m_Counters; |
| 255 | for (uint16_t parentCategoryCounterUid : parentCategoryCounters) |
| 256 | { |
| 257 | const Counter* parentCategoryCounter = GetCounter(parentCategoryCounterUid); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 258 | ARM_PIPE_ASSERT(parentCategoryCounter); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 259 | |
| 260 | if (parentCategoryCounter->m_Name == name) |
| 261 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 262 | throw arm::pipe::InvalidArgumentException( |
| 263 | fmt::format("Trying to register a counter to category \"{}\" with a name that " |
| 264 | "is already used within that category (name: \"{}\")", |
| 265 | parentCategoryName, |
| 266 | name)); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
| 270 | // Check that a counter set with the given (optional) UID is already registered |
| 271 | uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0; |
| 272 | if (counterSetUidValue > 0) |
| 273 | { |
| 274 | // Check that the (optional) counter set is already registered |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 275 | if (!IsCounterSetRegistered(counterSetUidValue)) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 276 | { |
| 277 | throw InvalidArgumentException( |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 278 | fmt::format("Trying to connect a counter to a counter set that is " |
| 279 | "not registered (counter set UID: {})", |
| 280 | counterSetUidValue)); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
| 284 | // Get the number of cores (this call may throw) |
| 285 | uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0; |
Sadik Armagan | 4c99899 | 2020-02-25 12:44:44 +0000 | [diff] [blame] | 286 | uint16_t deviceCores = GetNumberOfCores(numberOfCores, deviceUidValue); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 287 | |
| 288 | // Get the counter UIDs and calculate the max counter UID |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 289 | std::vector<uint16_t> counterUids = GetNextCounterUids(uid, deviceCores); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 290 | ARM_PIPE_ASSERT(!counterUids.empty()); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 291 | uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back(); |
| 292 | |
| 293 | // Get the counter units |
| 294 | const std::string unitsValue = units.has_value() ? units.value() : ""; |
| 295 | |
| 296 | // Create the counter |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 297 | CounterPtr counter = std::make_shared<Counter>(armnn::profiling::BACKEND_ID, |
| 298 | counterUids.front(), |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 299 | maxCounterUid, |
| 300 | counterClass, |
| 301 | interpolation, |
| 302 | multiplier, |
| 303 | name, |
| 304 | description, |
| 305 | unitsValue, |
| 306 | deviceUidValue, |
| 307 | counterSetUidValue); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 308 | ARM_PIPE_ASSERT(counter); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 309 | |
| 310 | // Get the raw counter pointer |
| 311 | const Counter* counterPtr = counter.get(); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 312 | ARM_PIPE_ASSERT(counterPtr); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 313 | |
| 314 | // Process multiple counters if necessary |
| 315 | for (uint16_t counterUid : counterUids) |
| 316 | { |
| 317 | // Connect the counter to the parent category |
| 318 | parentCategory->m_Counters.push_back(counterUid); |
| 319 | |
| 320 | // Register the counter |
| 321 | m_Counters.insert(std::make_pair(counterUid, counter)); |
| 322 | } |
| 323 | |
| 324 | return counterPtr; |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 325 | } |
| 326 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 327 | const Category* CounterDirectory::GetCategory(const std::string& categoryName) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 328 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 329 | auto it = FindCategory(categoryName); |
| 330 | if (it == m_Categories.end()) |
| 331 | { |
| 332 | return nullptr; |
| 333 | } |
| 334 | |
| 335 | const Category* category = it->get(); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 336 | ARM_PIPE_ASSERT(category); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 337 | |
| 338 | return category; |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 341 | const Device* CounterDirectory::GetDevice(uint16_t deviceUid) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 342 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 343 | auto it = FindDevice(deviceUid); |
| 344 | if (it == m_Devices.end()) |
| 345 | { |
| 346 | return nullptr; |
| 347 | } |
| 348 | |
| 349 | const Device* device = it->second.get(); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 350 | ARM_PIPE_ASSERT(device); |
| 351 | ARM_PIPE_ASSERT(device->m_Uid == deviceUid); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 352 | |
| 353 | return device; |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 354 | } |
| 355 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 356 | const CounterSet* CounterDirectory::GetCounterSet(uint16_t counterSetUid) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 357 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 358 | auto it = FindCounterSet(counterSetUid); |
| 359 | if (it == m_CounterSets.end()) |
| 360 | { |
| 361 | return nullptr; |
| 362 | } |
| 363 | |
| 364 | const CounterSet* counterSet = it->second.get(); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 365 | ARM_PIPE_ASSERT(counterSet); |
| 366 | ARM_PIPE_ASSERT(counterSet->m_Uid == counterSetUid); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 367 | |
| 368 | return counterSet; |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 369 | } |
| 370 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 371 | const Counter* CounterDirectory::GetCounter(uint16_t counterUid) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 372 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 373 | auto it = FindCounter(counterUid); |
| 374 | if (it == m_Counters.end()) |
| 375 | { |
| 376 | return nullptr; |
| 377 | } |
| 378 | |
| 379 | const Counter* counter = it->second.get(); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 380 | ARM_PIPE_ASSERT(counter); |
| 381 | ARM_PIPE_ASSERT(counter->m_Uid <= counterUid); |
| 382 | ARM_PIPE_ASSERT(counter->m_Uid <= counter->m_MaxCounterUid); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 383 | |
| 384 | return counter; |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 385 | } |
| 386 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 387 | bool CounterDirectory::IsCategoryRegistered(const std::string& categoryName) const |
| 388 | { |
| 389 | auto it = FindCategory(categoryName); |
| 390 | |
| 391 | return it != m_Categories.end(); |
| 392 | } |
| 393 | |
| 394 | bool CounterDirectory::IsDeviceRegistered(uint16_t deviceUid) const |
| 395 | { |
| 396 | auto it = FindDevice(deviceUid); |
| 397 | |
| 398 | return it != m_Devices.end(); |
| 399 | } |
| 400 | |
| 401 | bool CounterDirectory::IsDeviceRegistered(const std::string& deviceName) const |
| 402 | { |
| 403 | auto it = FindDevice(deviceName); |
| 404 | |
| 405 | return it != m_Devices.end(); |
| 406 | } |
| 407 | |
| 408 | bool CounterDirectory::IsCounterSetRegistered(uint16_t counterSetUid) const |
| 409 | { |
| 410 | auto it = FindCounterSet(counterSetUid); |
| 411 | |
| 412 | return it != m_CounterSets.end(); |
| 413 | } |
| 414 | |
| 415 | bool CounterDirectory::IsCounterSetRegistered(const std::string& counterSetName) const |
| 416 | { |
| 417 | auto it = FindCounterSet(counterSetName); |
| 418 | |
| 419 | return it != m_CounterSets.end(); |
| 420 | } |
| 421 | |
| 422 | bool CounterDirectory::IsCounterRegistered(uint16_t counterUid) const |
| 423 | { |
| 424 | auto it = FindCounter(counterUid); |
| 425 | |
| 426 | return it != m_Counters.end(); |
| 427 | } |
| 428 | |
| 429 | bool CounterDirectory::IsCounterRegistered(const std::string& counterName) const |
| 430 | { |
| 431 | auto it = FindCounter(counterName); |
| 432 | |
| 433 | return it != m_Counters.end(); |
| 434 | } |
| 435 | |
| 436 | void CounterDirectory::Clear() |
| 437 | { |
| 438 | // Clear all the counter directory contents |
| 439 | m_Categories.clear(); |
| 440 | m_Devices.clear(); |
| 441 | m_CounterSets.clear(); |
| 442 | m_Counters.clear(); |
| 443 | } |
| 444 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 445 | CategoriesIt CounterDirectory::FindCategory(const std::string& categoryName) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 446 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 447 | return std::find_if(m_Categories.begin(), m_Categories.end(), [&categoryName](const CategoryPtr& category) |
| 448 | { |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 449 | ARM_PIPE_ASSERT(category); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 450 | |
| 451 | return category->m_Name == categoryName; |
| 452 | }); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 453 | } |
| 454 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 455 | DevicesIt CounterDirectory::FindDevice(uint16_t deviceUid) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 456 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 457 | return m_Devices.find(deviceUid); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 460 | DevicesIt CounterDirectory::FindDevice(const std::string& deviceName) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 461 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 462 | return std::find_if(m_Devices.begin(), m_Devices.end(), [&deviceName](const auto& pair) |
| 463 | { |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 464 | ARM_PIPE_ASSERT(pair.second); |
| 465 | ARM_PIPE_ASSERT(pair.second->m_Uid == pair.first); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 466 | |
| 467 | return pair.second->m_Name == deviceName; |
| 468 | }); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 469 | } |
| 470 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 471 | CounterSetsIt CounterDirectory::FindCounterSet(uint16_t counterSetUid) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 472 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 473 | return m_CounterSets.find(counterSetUid); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 474 | } |
| 475 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 476 | CounterSetsIt CounterDirectory::FindCounterSet(const std::string& counterSetName) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 477 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 478 | return std::find_if(m_CounterSets.begin(), m_CounterSets.end(), [&counterSetName](const auto& pair) |
| 479 | { |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 480 | ARM_PIPE_ASSERT(pair.second); |
| 481 | ARM_PIPE_ASSERT(pair.second->m_Uid == pair.first); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 482 | |
| 483 | return pair.second->m_Name == counterSetName; |
| 484 | }); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 487 | CountersIt CounterDirectory::FindCounter(uint16_t counterUid) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 488 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 489 | return m_Counters.find(counterUid); |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 490 | } |
| 491 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 492 | CountersIt CounterDirectory::FindCounter(const std::string& counterName) const |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 493 | { |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 494 | return std::find_if(m_Counters.begin(), m_Counters.end(), [&counterName](const auto& pair) |
| 495 | { |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 496 | ARM_PIPE_ASSERT(pair.second); |
| 497 | ARM_PIPE_ASSERT(pair.first >= pair.second->m_Uid && pair.first <= pair.second->m_MaxCounterUid); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 498 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 499 | return pair.second->m_Name == counterName; |
| 500 | }); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 501 | } |
| 502 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 503 | uint16_t CounterDirectory::GetNumberOfCores(const armnn::Optional<uint16_t>& numberOfCores, |
Sadik Armagan | 4c99899 | 2020-02-25 12:44:44 +0000 | [diff] [blame] | 504 | uint16_t deviceUid) |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 505 | { |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 506 | // To get the number of cores, apply the following rules: |
| 507 | // |
| 508 | // 1. If numberOfCores is set then take it as the deviceCores value |
| 509 | // 2. If numberOfCores is not set then check to see if this counter is directly associated with a device, |
| 510 | // if so then that devices number of cores is taken as the deviceCores value |
Sadik Armagan | 4c99899 | 2020-02-25 12:44:44 +0000 | [diff] [blame] | 511 | // 3. If none of the above holds then set deviceCores to zero |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 512 | |
| 513 | // 1. If numberOfCores is set then take it as the deviceCores value |
| 514 | if (numberOfCores.has_value()) |
| 515 | { |
| 516 | // Get the number of cores |
| 517 | return numberOfCores.value(); |
| 518 | } |
| 519 | |
| 520 | // 2. If numberOfCores is not set then check to see if this counter is directly associated with a device, |
| 521 | // if so then that devices number of cores is taken as the deviceCores value |
| 522 | if (deviceUid > 0) |
| 523 | { |
| 524 | // Check that the (optional) device is already registered |
| 525 | auto deviceIt = FindDevice(deviceUid); |
| 526 | if (deviceIt == m_Devices.end()) |
| 527 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 528 | throw arm::pipe::InvalidArgumentException( |
| 529 | fmt::format("Trying to connect a counter to a device that is not registered (device UID {})", |
| 530 | deviceUid)); |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | // Get the associated device |
| 534 | const DevicePtr& device = deviceIt->second; |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame^] | 535 | ARM_PIPE_ASSERT(device); |
| 536 | |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 537 | |
| 538 | // Get the number of cores of the associated device |
| 539 | return device->m_Cores; |
| 540 | } |
| 541 | |
Sadik Armagan | 4c99899 | 2020-02-25 12:44:44 +0000 | [diff] [blame] | 542 | // 3. If none of the above holds then set deviceCores to zero |
Matteo Martincigh | 6db5f20 | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 543 | return 0; |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 544 | } |
| 545 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 546 | } // namespace pipe |
Aron Virginas-Tar | 4e5fc1f | 2019-08-22 18:10:52 +0100 | [diff] [blame] | 547 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 548 | } // namespace arm |