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