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