Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Michael Butler | 89e99ba | 2019-01-24 02:36:37 -0800 | [diff] [blame] | 17 | #define LOG_TAG "ExecutionBurstServer" |
| 18 | |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 19 | #include "ExecutionBurstServer.h" |
| 20 | |
| 21 | #include <android-base/logging.h> |
Michael Butler | 3260db9 | 2019-04-26 17:51:23 -0700 | [diff] [blame] | 22 | |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 23 | #include <algorithm> |
Michael Butler | 4ef48f1 | 2019-05-02 14:09:17 -0700 | [diff] [blame] | 24 | #include <cstring> |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 25 | #include <limits> |
Michael Butler | 3260db9 | 2019-04-26 17:51:23 -0700 | [diff] [blame] | 26 | #include <map> |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 27 | #include <memory> |
| 28 | #include <tuple> |
| 29 | #include <utility> |
| 30 | #include <vector> |
Michael Butler | 3260db9 | 2019-04-26 17:51:23 -0700 | [diff] [blame] | 31 | |
Michael Butler | a8f883c | 2020-03-12 15:16:38 -0700 | [diff] [blame] | 32 | #include "HalInterfaces.h" |
Michael Butler | 3db6fe5 | 2019-01-29 11:20:30 -0800 | [diff] [blame] | 33 | #include "Tracing.h" |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 34 | |
Michael Butler | 3db6fe5 | 2019-01-29 11:20:30 -0800 | [diff] [blame] | 35 | namespace android::nn { |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 36 | namespace { |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 37 | |
Michael Butler | 19af9d2 | 2019-07-11 11:45:01 -0700 | [diff] [blame] | 38 | using namespace hal; |
| 39 | |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 40 | using hardware::MQDescriptorSync; |
Michael Butler | a8f883c | 2020-03-12 15:16:38 -0700 | [diff] [blame] | 41 | using V1_2::FmqRequestDatum; |
| 42 | using V1_2::FmqResultDatum; |
| 43 | using V1_2::IBurstCallback; |
| 44 | using V1_2::IBurstContext; |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 45 | |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 46 | constexpr Timing kNoTiming = {std::numeric_limits<uint64_t>::max(), |
| 47 | std::numeric_limits<uint64_t>::max()}; |
| 48 | |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 49 | // DefaultBurstExecutorWithCache adapts an IPreparedModel so that it can be |
| 50 | // used as an IBurstExecutorWithCache. Specifically, the cache simply stores the |
| 51 | // hidl_memory object, and the execution forwards calls to the provided |
| 52 | // IPreparedModel's "executeSynchronously" method. With this class, hidl_memory |
| 53 | // must be mapped and unmapped for each execution. |
| 54 | class DefaultBurstExecutorWithCache : public ExecutionBurstServer::IBurstExecutorWithCache { |
| 55 | public: |
Xusong Wang | 196a187 | 2019-10-25 12:06:20 -0700 | [diff] [blame] | 56 | DefaultBurstExecutorWithCache(V1_2::IPreparedModel* preparedModel) |
| 57 | : mpPreparedModel(preparedModel) {} |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 58 | |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 59 | bool isCacheEntryPresent(int32_t slot) const override { |
Michael Butler | 3260db9 | 2019-04-26 17:51:23 -0700 | [diff] [blame] | 60 | const auto it = mMemoryCache.find(slot); |
Michael Butler | 1ee58a5 | 2019-04-30 13:49:32 -0700 | [diff] [blame] | 61 | return (it != mMemoryCache.end()) && it->second.valid(); |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 62 | } |
Michael Butler | 47c988f6 | 2019-03-14 17:34:48 -0700 | [diff] [blame] | 63 | |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 64 | void addCacheEntry(const hidl_memory& memory, int32_t slot) override { |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 65 | mMemoryCache[slot] = memory; |
| 66 | } |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 67 | |
Michael Butler | 3260db9 | 2019-04-26 17:51:23 -0700 | [diff] [blame] | 68 | void removeCacheEntry(int32_t slot) override { mMemoryCache.erase(slot); } |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 69 | |
Michael Butler | f690d31 | 2019-12-12 16:25:03 -0800 | [diff] [blame] | 70 | std::tuple<V1_0::ErrorStatus, hidl_vec<OutputShape>, Timing> execute( |
Xusong Wang | 7ac6c9d | 2020-01-08 16:52:37 -0800 | [diff] [blame] | 71 | const V1_0::Request& request, const std::vector<int32_t>& slots, |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 72 | MeasureTiming measure) override { |
| 73 | // convert slots to pools |
| 74 | hidl_vec<hidl_memory> pools(slots.size()); |
Michael Butler | 3260db9 | 2019-04-26 17:51:23 -0700 | [diff] [blame] | 75 | std::transform(slots.begin(), slots.end(), pools.begin(), |
| 76 | [this](int32_t slot) { return mMemoryCache[slot]; }); |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 77 | |
| 78 | // create full request |
Xusong Wang | 7ac6c9d | 2020-01-08 16:52:37 -0800 | [diff] [blame] | 79 | V1_0::Request fullRequest = request; |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 80 | fullRequest.pools = std::move(pools); |
| 81 | |
| 82 | // setup execution |
Michael Butler | f690d31 | 2019-12-12 16:25:03 -0800 | [diff] [blame] | 83 | V1_0::ErrorStatus returnedStatus = V1_0::ErrorStatus::GENERAL_FAILURE; |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 84 | hidl_vec<OutputShape> returnedOutputShapes; |
| 85 | Timing returnedTiming; |
| 86 | auto cb = [&returnedStatus, &returnedOutputShapes, &returnedTiming]( |
Michael Butler | f690d31 | 2019-12-12 16:25:03 -0800 | [diff] [blame] | 87 | V1_0::ErrorStatus status, const hidl_vec<OutputShape>& outputShapes, |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 88 | const Timing& timing) { |
| 89 | returnedStatus = status; |
| 90 | returnedOutputShapes = outputShapes; |
| 91 | returnedTiming = timing; |
Michael Butler | 47c988f6 | 2019-03-14 17:34:48 -0700 | [diff] [blame] | 92 | }; |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 93 | |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 94 | // execute |
| 95 | const Return<void> ret = mpPreparedModel->executeSynchronously(fullRequest, measure, cb); |
Michael Butler | f690d31 | 2019-12-12 16:25:03 -0800 | [diff] [blame] | 96 | if (!ret.isOk() || returnedStatus != V1_0::ErrorStatus::NONE) { |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 97 | LOG(ERROR) << "IPreparedModelAdapter::execute -- Error executing"; |
Xusong Wang | a66fee1 | 2020-01-17 10:36:52 -0800 | [diff] [blame] | 98 | return {returnedStatus, std::move(returnedOutputShapes), kNoTiming}; |
Michael Butler | 89e99ba | 2019-01-24 02:36:37 -0800 | [diff] [blame] | 99 | } |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 100 | |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 101 | return std::make_tuple(returnedStatus, std::move(returnedOutputShapes), returnedTiming); |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 104 | private: |
Xusong Wang | 196a187 | 2019-10-25 12:06:20 -0700 | [diff] [blame] | 105 | V1_2::IPreparedModel* const mpPreparedModel; |
Michael Butler | 3260db9 | 2019-04-26 17:51:23 -0700 | [diff] [blame] | 106 | std::map<int32_t, hidl_memory> mMemoryCache; |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 107 | }; |
Michael Butler | 47c988f6 | 2019-03-14 17:34:48 -0700 | [diff] [blame] | 108 | |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 109 | } // anonymous namespace |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 110 | |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 111 | // serialize result |
Michael Butler | f690d31 | 2019-12-12 16:25:03 -0800 | [diff] [blame] | 112 | std::vector<FmqResultDatum> serialize(V1_0::ErrorStatus errorStatus, |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 113 | const std::vector<OutputShape>& outputShapes, Timing timing) { |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 114 | // count how many elements need to be sent for a request |
| 115 | size_t count = 2 + outputShapes.size(); |
| 116 | for (const auto& outputShape : outputShapes) { |
| 117 | count += outputShape.dimensions.size(); |
| 118 | } |
| 119 | |
| 120 | // create buffer to temporarily store elements |
| 121 | std::vector<FmqResultDatum> data; |
| 122 | data.reserve(count); |
| 123 | |
| 124 | // package packetInfo |
| 125 | { |
| 126 | FmqResultDatum datum; |
| 127 | datum.packetInformation({/*.packetSize=*/static_cast<uint32_t>(count), |
| 128 | /*.errorStatus=*/errorStatus, |
| 129 | /*.numberOfOperands=*/static_cast<uint32_t>(outputShapes.size())}); |
| 130 | data.push_back(datum); |
| 131 | } |
| 132 | |
| 133 | // package output shape data |
| 134 | for (const auto& operand : outputShapes) { |
| 135 | // package operand information |
Steven Moreland | 393ac6d | 2019-04-25 15:33:25 -0700 | [diff] [blame] | 136 | FmqResultDatum::OperandInformation info{}; |
| 137 | info.isSufficient = operand.isSufficient; |
| 138 | info.numberOfDimensions = static_cast<uint32_t>(operand.dimensions.size()); |
| 139 | |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 140 | FmqResultDatum datum; |
Steven Moreland | 393ac6d | 2019-04-25 15:33:25 -0700 | [diff] [blame] | 141 | datum.operandInformation(info); |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 142 | data.push_back(datum); |
| 143 | |
| 144 | // package operand dimensions |
| 145 | for (uint32_t dimension : operand.dimensions) { |
| 146 | FmqResultDatum datum; |
| 147 | datum.operandDimensionValue(dimension); |
| 148 | data.push_back(datum); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // package executionTiming |
| 153 | { |
| 154 | FmqResultDatum datum; |
| 155 | datum.executionTiming(timing); |
| 156 | data.push_back(datum); |
| 157 | } |
| 158 | |
| 159 | // return result |
| 160 | return data; |
| 161 | } |
| 162 | |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 163 | // deserialize request |
Xusong Wang | 7ac6c9d | 2020-01-08 16:52:37 -0800 | [diff] [blame] | 164 | std::optional<std::tuple<V1_0::Request, std::vector<int32_t>, MeasureTiming>> deserialize( |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 165 | const std::vector<FmqRequestDatum>& data) { |
| 166 | using discriminator = FmqRequestDatum::hidl_discriminator; |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 167 | |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 168 | size_t index = 0; |
| 169 | |
| 170 | // validate packet information |
Michael Butler | 3260db9 | 2019-04-26 17:51:23 -0700 | [diff] [blame] | 171 | if (data.size() == 0 || data[index].getDiscriminator() != discriminator::packetInformation) { |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 172 | LOG(ERROR) << "FMQ Request packet ill-formed"; |
| 173 | return std::nullopt; |
| 174 | } |
| 175 | |
| 176 | // unpackage packet information |
| 177 | const FmqRequestDatum::PacketInformation& packetInfo = data[index].packetInformation(); |
| 178 | index++; |
| 179 | const uint32_t packetSize = packetInfo.packetSize; |
| 180 | const uint32_t numberOfInputOperands = packetInfo.numberOfInputOperands; |
| 181 | const uint32_t numberOfOutputOperands = packetInfo.numberOfOutputOperands; |
| 182 | const uint32_t numberOfPools = packetInfo.numberOfPools; |
| 183 | |
Michael Butler | 3260db9 | 2019-04-26 17:51:23 -0700 | [diff] [blame] | 184 | // verify packet size |
| 185 | if (data.size() != packetSize) { |
| 186 | LOG(ERROR) << "FMQ Request packet ill-formed"; |
| 187 | return std::nullopt; |
| 188 | } |
| 189 | |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 190 | // unpackage input operands |
| 191 | std::vector<RequestArgument> inputs; |
| 192 | inputs.reserve(numberOfInputOperands); |
| 193 | for (size_t operand = 0; operand < numberOfInputOperands; ++operand) { |
| 194 | // validate input operand information |
| 195 | if (data[index].getDiscriminator() != discriminator::inputOperandInformation) { |
| 196 | LOG(ERROR) << "FMQ Request packet ill-formed"; |
| 197 | return std::nullopt; |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 200 | // unpackage operand information |
| 201 | const FmqRequestDatum::OperandInformation& operandInfo = |
| 202 | data[index].inputOperandInformation(); |
| 203 | index++; |
| 204 | const bool hasNoValue = operandInfo.hasNoValue; |
| 205 | const DataLocation location = operandInfo.location; |
| 206 | const uint32_t numberOfDimensions = operandInfo.numberOfDimensions; |
Michael Butler | 3db6fe5 | 2019-01-29 11:20:30 -0800 | [diff] [blame] | 207 | |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 208 | // unpackage operand dimensions |
| 209 | std::vector<uint32_t> dimensions; |
| 210 | dimensions.reserve(numberOfDimensions); |
| 211 | for (size_t i = 0; i < numberOfDimensions; ++i) { |
| 212 | // validate dimension |
| 213 | if (data[index].getDiscriminator() != discriminator::inputOperandDimensionValue) { |
| 214 | LOG(ERROR) << "FMQ Request packet ill-formed"; |
| 215 | return std::nullopt; |
| 216 | } |
| 217 | |
| 218 | // unpackage dimension |
| 219 | const uint32_t dimension = data[index].inputOperandDimensionValue(); |
| 220 | index++; |
| 221 | |
| 222 | // store result |
| 223 | dimensions.push_back(dimension); |
| 224 | } |
| 225 | |
| 226 | // store result |
| 227 | inputs.push_back( |
| 228 | {/*.hasNoValue=*/hasNoValue, /*.location=*/location, /*.dimensions=*/dimensions}); |
| 229 | } |
| 230 | |
| 231 | // unpackage output operands |
| 232 | std::vector<RequestArgument> outputs; |
| 233 | outputs.reserve(numberOfOutputOperands); |
| 234 | for (size_t operand = 0; operand < numberOfOutputOperands; ++operand) { |
| 235 | // validate output operand information |
| 236 | if (data[index].getDiscriminator() != discriminator::outputOperandInformation) { |
| 237 | LOG(ERROR) << "FMQ Request packet ill-formed"; |
| 238 | return std::nullopt; |
| 239 | } |
| 240 | |
| 241 | // unpackage operand information |
| 242 | const FmqRequestDatum::OperandInformation& operandInfo = |
| 243 | data[index].outputOperandInformation(); |
| 244 | index++; |
| 245 | const bool hasNoValue = operandInfo.hasNoValue; |
| 246 | const DataLocation location = operandInfo.location; |
| 247 | const uint32_t numberOfDimensions = operandInfo.numberOfDimensions; |
| 248 | |
| 249 | // unpackage operand dimensions |
| 250 | std::vector<uint32_t> dimensions; |
| 251 | dimensions.reserve(numberOfDimensions); |
| 252 | for (size_t i = 0; i < numberOfDimensions; ++i) { |
| 253 | // validate dimension |
| 254 | if (data[index].getDiscriminator() != discriminator::outputOperandDimensionValue) { |
| 255 | LOG(ERROR) << "FMQ Request packet ill-formed"; |
| 256 | return std::nullopt; |
| 257 | } |
| 258 | |
| 259 | // unpackage dimension |
| 260 | const uint32_t dimension = data[index].outputOperandDimensionValue(); |
| 261 | index++; |
| 262 | |
| 263 | // store result |
| 264 | dimensions.push_back(dimension); |
| 265 | } |
| 266 | |
| 267 | // store result |
| 268 | outputs.push_back( |
| 269 | {/*.hasNoValue=*/hasNoValue, /*.location=*/location, /*.dimensions=*/dimensions}); |
| 270 | } |
| 271 | |
| 272 | // unpackage pools |
| 273 | std::vector<int32_t> slots; |
| 274 | slots.reserve(numberOfPools); |
| 275 | for (size_t pool = 0; pool < numberOfPools; ++pool) { |
| 276 | // validate input operand information |
| 277 | if (data[index].getDiscriminator() != discriminator::poolIdentifier) { |
| 278 | LOG(ERROR) << "FMQ Request packet ill-formed"; |
| 279 | return std::nullopt; |
| 280 | } |
| 281 | |
| 282 | // unpackage operand information |
| 283 | const int32_t poolId = data[index].poolIdentifier(); |
| 284 | index++; |
| 285 | |
| 286 | // store result |
| 287 | slots.push_back(poolId); |
| 288 | } |
| 289 | |
| 290 | // validate measureTiming |
| 291 | if (data[index].getDiscriminator() != discriminator::measureTiming) { |
| 292 | LOG(ERROR) << "FMQ Request packet ill-formed"; |
| 293 | return std::nullopt; |
| 294 | } |
| 295 | |
| 296 | // unpackage measureTiming |
| 297 | const MeasureTiming measure = data[index].measureTiming(); |
| 298 | index++; |
| 299 | |
| 300 | // validate packet information |
| 301 | if (index != packetSize) { |
| 302 | LOG(ERROR) << "FMQ Result packet ill-formed"; |
| 303 | return std::nullopt; |
| 304 | } |
| 305 | |
| 306 | // return request |
Xusong Wang | 7ac6c9d | 2020-01-08 16:52:37 -0800 | [diff] [blame] | 307 | V1_0::Request request = {/*.inputs=*/inputs, /*.outputs=*/outputs, /*.pools=*/{}}; |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 308 | return std::make_tuple(std::move(request), std::move(slots), measure); |
| 309 | } |
| 310 | |
| 311 | // RequestChannelReceiver methods |
| 312 | |
| 313 | std::unique_ptr<RequestChannelReceiver> RequestChannelReceiver::create( |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 314 | const FmqRequestDescriptor& requestChannel, std::chrono::microseconds pollingTimeWindow) { |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 315 | std::unique_ptr<FmqRequestChannel> fmqRequestChannel = |
| 316 | std::make_unique<FmqRequestChannel>(requestChannel); |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 317 | |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 318 | if (!fmqRequestChannel->isValid()) { |
| 319 | LOG(ERROR) << "Unable to create RequestChannelReceiver"; |
| 320 | return nullptr; |
| 321 | } |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 322 | if (fmqRequestChannel->getEventFlagWord() == nullptr) { |
| 323 | LOG(ERROR) |
| 324 | << "RequestChannelReceiver::create was passed an MQDescriptor without an EventFlag"; |
| 325 | return nullptr; |
| 326 | } |
| 327 | |
| 328 | return std::make_unique<RequestChannelReceiver>(std::move(fmqRequestChannel), |
| 329 | pollingTimeWindow); |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | RequestChannelReceiver::RequestChannelReceiver(std::unique_ptr<FmqRequestChannel> fmqRequestChannel, |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 333 | std::chrono::microseconds pollingTimeWindow) |
| 334 | : mFmqRequestChannel(std::move(fmqRequestChannel)), kPollingTimeWindow(pollingTimeWindow) {} |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 335 | |
Xusong Wang | 7ac6c9d | 2020-01-08 16:52:37 -0800 | [diff] [blame] | 336 | std::optional<std::tuple<V1_0::Request, std::vector<int32_t>, MeasureTiming>> |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 337 | RequestChannelReceiver::getBlocking() { |
| 338 | const auto packet = getPacketBlocking(); |
| 339 | if (!packet) { |
| 340 | return std::nullopt; |
| 341 | } |
| 342 | |
| 343 | return deserialize(*packet); |
| 344 | } |
| 345 | |
| 346 | void RequestChannelReceiver::invalidate() { |
| 347 | mTeardown = true; |
| 348 | |
| 349 | // force unblock |
| 350 | // ExecutionBurstServer is by default waiting on a request packet. If the |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 351 | // client process destroys its burst object, the server may still be waiting |
| 352 | // on the futex. This force unblock wakes up any thread waiting on the |
| 353 | // futex. |
| 354 | // TODO: look for a different/better way to signal/notify the futex to wake |
| 355 | // up any thread waiting on it |
| 356 | FmqRequestDatum datum; |
| 357 | datum.packetInformation({/*.packetSize=*/0, /*.numberOfInputOperands=*/0, |
| 358 | /*.numberOfOutputOperands=*/0, /*.numberOfPools=*/0}); |
| 359 | mFmqRequestChannel->writeBlocking(&datum, 1); |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | std::optional<std::vector<FmqRequestDatum>> RequestChannelReceiver::getPacketBlocking() { |
| 363 | using discriminator = FmqRequestDatum::hidl_discriminator; |
| 364 | |
| 365 | if (mTeardown) { |
| 366 | return std::nullopt; |
| 367 | } |
| 368 | |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 369 | // First spend time polling if results are available in FMQ instead of |
| 370 | // waiting on the futex. Polling is more responsive (yielding lower |
| 371 | // latencies), but can take up more power, so only poll for a limited period |
| 372 | // of time. |
| 373 | |
| 374 | auto& getCurrentTime = std::chrono::high_resolution_clock::now; |
| 375 | const auto timeToStopPolling = getCurrentTime() + kPollingTimeWindow; |
| 376 | |
| 377 | while (getCurrentTime() < timeToStopPolling) { |
| 378 | // if class is being torn down, immediately return |
| 379 | if (mTeardown.load(std::memory_order_relaxed)) { |
| 380 | return std::nullopt; |
| 381 | } |
| 382 | |
| 383 | // Check if data is available. If it is, immediately retrieve it and |
| 384 | // return. |
| 385 | const size_t available = mFmqRequestChannel->availableToRead(); |
| 386 | if (available > 0) { |
| 387 | // This is the first point when we know an execution is occurring, |
| 388 | // so begin to collect systraces. Note that a similar systrace does |
| 389 | // not exist at the corresponding point in |
| 390 | // ResultChannelReceiver::getPacketBlocking because the execution is |
| 391 | // already in flight. |
| 392 | NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION, |
| 393 | "ExecutionBurstServer getting packet"); |
| 394 | std::vector<FmqRequestDatum> packet(available); |
| 395 | const bool success = mFmqRequestChannel->read(packet.data(), available); |
| 396 | if (!success) { |
| 397 | LOG(ERROR) << "Error receiving packet"; |
| 398 | return std::nullopt; |
| 399 | } |
| 400 | return std::make_optional(std::move(packet)); |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 404 | // If we get to this point, we either stopped polling because it was taking |
| 405 | // too long or polling was not allowed. Instead, perform a blocking call |
| 406 | // which uses a futex to save power. |
| 407 | |
| 408 | // wait for request packet and read first element of request packet |
| 409 | FmqRequestDatum datum; |
| 410 | bool success = mFmqRequestChannel->readBlocking(&datum, 1); |
| 411 | |
| 412 | // This is the first point when we know an execution is occurring, so begin |
| 413 | // to collect systraces. Note that a similar systrace does not exist at the |
| 414 | // corresponding point in ResultChannelReceiver::getPacketBlocking because |
| 415 | // the execution is already in flight. |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 416 | NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION, "ExecutionBurstServer getting packet"); |
| 417 | |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 418 | // retrieve remaining elements |
| 419 | // NOTE: all of the data is already available at this point, so there's no |
| 420 | // need to do a blocking wait to wait for more data. This is known because |
| 421 | // in FMQ, all writes are published (made available) atomically. Currently, |
| 422 | // the producer always publishes the entire packet in one function call, so |
| 423 | // if the first element of the packet is available, the remaining elements |
| 424 | // are also available. |
Michael Butler | 3260db9 | 2019-04-26 17:51:23 -0700 | [diff] [blame] | 425 | const size_t count = mFmqRequestChannel->availableToRead(); |
| 426 | std::vector<FmqRequestDatum> packet(count + 1); |
Michael Butler | 4ef48f1 | 2019-05-02 14:09:17 -0700 | [diff] [blame] | 427 | std::memcpy(&packet.front(), &datum, sizeof(datum)); |
Michael Butler | 3260db9 | 2019-04-26 17:51:23 -0700 | [diff] [blame] | 428 | success &= mFmqRequestChannel->read(packet.data() + 1, count); |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 429 | |
Michael Butler | 3260db9 | 2019-04-26 17:51:23 -0700 | [diff] [blame] | 430 | // terminate loop |
| 431 | if (mTeardown) { |
| 432 | return std::nullopt; |
| 433 | } |
| 434 | |
| 435 | // ensure packet was successfully received |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 436 | if (!success) { |
Michael Butler | 3260db9 | 2019-04-26 17:51:23 -0700 | [diff] [blame] | 437 | LOG(ERROR) << "Error receiving packet"; |
| 438 | return std::nullopt; |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Michael Butler | 4ef48f1 | 2019-05-02 14:09:17 -0700 | [diff] [blame] | 441 | return std::make_optional(std::move(packet)); |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // ResultChannelSender methods |
| 445 | |
| 446 | std::unique_ptr<ResultChannelSender> ResultChannelSender::create( |
| 447 | const FmqResultDescriptor& resultChannel) { |
| 448 | std::unique_ptr<FmqResultChannel> fmqResultChannel = |
| 449 | std::make_unique<FmqResultChannel>(resultChannel); |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 450 | |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 451 | if (!fmqResultChannel->isValid()) { |
| 452 | LOG(ERROR) << "Unable to create RequestChannelSender"; |
| 453 | return nullptr; |
| 454 | } |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 455 | if (fmqResultChannel->getEventFlagWord() == nullptr) { |
| 456 | LOG(ERROR) << "ResultChannelSender::create was passed an MQDescriptor without an EventFlag"; |
| 457 | return nullptr; |
| 458 | } |
| 459 | |
| 460 | return std::make_unique<ResultChannelSender>(std::move(fmqResultChannel)); |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 463 | ResultChannelSender::ResultChannelSender(std::unique_ptr<FmqResultChannel> fmqResultChannel) |
| 464 | : mFmqResultChannel(std::move(fmqResultChannel)) {} |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 465 | |
Michael Butler | f690d31 | 2019-12-12 16:25:03 -0800 | [diff] [blame] | 466 | bool ResultChannelSender::send(V1_0::ErrorStatus errorStatus, |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 467 | const std::vector<OutputShape>& outputShapes, Timing timing) { |
| 468 | const std::vector<FmqResultDatum> serialized = serialize(errorStatus, outputShapes, timing); |
| 469 | return sendPacket(serialized); |
| 470 | } |
| 471 | |
| 472 | bool ResultChannelSender::sendPacket(const std::vector<FmqResultDatum>& packet) { |
Michael Butler | 3260db9 | 2019-04-26 17:51:23 -0700 | [diff] [blame] | 473 | if (packet.size() > mFmqResultChannel->availableToWrite()) { |
| 474 | LOG(ERROR) |
| 475 | << "ResultChannelSender::sendPacket -- packet size exceeds size available in FMQ"; |
| 476 | const std::vector<FmqResultDatum> errorPacket = |
Michael Butler | f690d31 | 2019-12-12 16:25:03 -0800 | [diff] [blame] | 477 | serialize(V1_0::ErrorStatus::GENERAL_FAILURE, {}, kNoTiming); |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 478 | |
| 479 | // Always send the packet with "blocking" because this signals the futex |
| 480 | // and unblocks the consumer if it is waiting on the futex. |
| 481 | return mFmqResultChannel->writeBlocking(errorPacket.data(), errorPacket.size()); |
Michael Butler | 3260db9 | 2019-04-26 17:51:23 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 484 | // Always send the packet with "blocking" because this signals the futex and |
| 485 | // unblocks the consumer if it is waiting on the futex. |
| 486 | return mFmqResultChannel->writeBlocking(packet.data(), packet.size()); |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // ExecutionBurstServer methods |
| 490 | |
| 491 | sp<ExecutionBurstServer> ExecutionBurstServer::create( |
| 492 | const sp<IBurstCallback>& callback, const MQDescriptorSync<FmqRequestDatum>& requestChannel, |
| 493 | const MQDescriptorSync<FmqResultDatum>& resultChannel, |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 494 | std::shared_ptr<IBurstExecutorWithCache> executorWithCache, |
| 495 | std::chrono::microseconds pollingTimeWindow) { |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 496 | // check inputs |
| 497 | if (callback == nullptr || executorWithCache == nullptr) { |
| 498 | LOG(ERROR) << "ExecutionBurstServer::create passed a nullptr"; |
| 499 | return nullptr; |
| 500 | } |
| 501 | |
| 502 | // create FMQ objects |
| 503 | std::unique_ptr<RequestChannelReceiver> requestChannelReceiver = |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 504 | RequestChannelReceiver::create(requestChannel, pollingTimeWindow); |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 505 | std::unique_ptr<ResultChannelSender> resultChannelSender = |
| 506 | ResultChannelSender::create(resultChannel); |
| 507 | |
| 508 | // check FMQ objects |
| 509 | if (!requestChannelReceiver || !resultChannelSender) { |
| 510 | LOG(ERROR) << "ExecutionBurstServer::create failed to create FastMessageQueue"; |
| 511 | return nullptr; |
| 512 | } |
| 513 | |
| 514 | // make and return context |
| 515 | return new ExecutionBurstServer(callback, std::move(requestChannelReceiver), |
| 516 | std::move(resultChannelSender), std::move(executorWithCache)); |
| 517 | } |
| 518 | |
| 519 | sp<ExecutionBurstServer> ExecutionBurstServer::create( |
| 520 | const sp<IBurstCallback>& callback, const MQDescriptorSync<FmqRequestDatum>& requestChannel, |
Xusong Wang | 196a187 | 2019-10-25 12:06:20 -0700 | [diff] [blame] | 521 | const MQDescriptorSync<FmqResultDatum>& resultChannel, V1_2::IPreparedModel* preparedModel, |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 522 | std::chrono::microseconds pollingTimeWindow) { |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 523 | // check relevant input |
| 524 | if (preparedModel == nullptr) { |
| 525 | LOG(ERROR) << "ExecutionBurstServer::create passed a nullptr"; |
| 526 | return nullptr; |
| 527 | } |
| 528 | |
| 529 | // adapt IPreparedModel to have caching |
| 530 | const std::shared_ptr<DefaultBurstExecutorWithCache> preparedModelAdapter = |
| 531 | std::make_shared<DefaultBurstExecutorWithCache>(preparedModel); |
| 532 | |
| 533 | // make and return context |
| 534 | return ExecutionBurstServer::create(callback, requestChannel, resultChannel, |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 535 | preparedModelAdapter, pollingTimeWindow); |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | ExecutionBurstServer::ExecutionBurstServer( |
| 539 | const sp<IBurstCallback>& callback, std::unique_ptr<RequestChannelReceiver> requestChannel, |
| 540 | std::unique_ptr<ResultChannelSender> resultChannel, |
| 541 | std::shared_ptr<IBurstExecutorWithCache> executorWithCache) |
| 542 | : mCallback(callback), |
| 543 | mRequestChannelReceiver(std::move(requestChannel)), |
| 544 | mResultChannelSender(std::move(resultChannel)), |
| 545 | mExecutorWithCache(std::move(executorWithCache)) { |
| 546 | // TODO: highly document the threading behavior of this class |
| 547 | mWorker = std::thread([this] { task(); }); |
| 548 | } |
| 549 | |
| 550 | ExecutionBurstServer::~ExecutionBurstServer() { |
| 551 | // set teardown flag |
| 552 | mTeardown = true; |
| 553 | mRequestChannelReceiver->invalidate(); |
| 554 | |
| 555 | // wait for task thread to end |
| 556 | mWorker.join(); |
| 557 | } |
| 558 | |
| 559 | Return<void> ExecutionBurstServer::freeMemory(int32_t slot) { |
Michael Butler | ba59a54 | 2019-06-28 17:06:27 -0700 | [diff] [blame] | 560 | std::lock_guard<std::mutex> hold(mMutex); |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 561 | mExecutorWithCache->removeCacheEntry(slot); |
| 562 | return Void(); |
| 563 | } |
| 564 | |
| 565 | void ExecutionBurstServer::ensureCacheEntriesArePresentLocked(const std::vector<int32_t>& slots) { |
| 566 | const auto slotIsKnown = [this](int32_t slot) { |
| 567 | return mExecutorWithCache->isCacheEntryPresent(slot); |
| 568 | }; |
| 569 | |
| 570 | // find unique unknown slots |
| 571 | std::vector<int32_t> unknownSlots = slots; |
| 572 | auto unknownSlotsEnd = unknownSlots.end(); |
| 573 | std::sort(unknownSlots.begin(), unknownSlotsEnd); |
| 574 | unknownSlotsEnd = std::unique(unknownSlots.begin(), unknownSlotsEnd); |
| 575 | unknownSlotsEnd = std::remove_if(unknownSlots.begin(), unknownSlotsEnd, slotIsKnown); |
| 576 | unknownSlots.erase(unknownSlotsEnd, unknownSlots.end()); |
| 577 | |
| 578 | // quick-exit if all slots are known |
| 579 | if (unknownSlots.empty()) { |
| 580 | return; |
| 581 | } |
| 582 | |
Michael Butler | f690d31 | 2019-12-12 16:25:03 -0800 | [diff] [blame] | 583 | V1_0::ErrorStatus errorStatus = V1_0::ErrorStatus::GENERAL_FAILURE; |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 584 | std::vector<hidl_memory> returnedMemories; |
Michael Butler | f690d31 | 2019-12-12 16:25:03 -0800 | [diff] [blame] | 585 | auto cb = [&errorStatus, &returnedMemories](V1_0::ErrorStatus status, |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 586 | const hidl_vec<hidl_memory>& memories) { |
| 587 | errorStatus = status; |
| 588 | returnedMemories = memories; |
| 589 | }; |
| 590 | |
| 591 | const Return<void> ret = mCallback->getMemories(unknownSlots, cb); |
| 592 | |
Michael Butler | f690d31 | 2019-12-12 16:25:03 -0800 | [diff] [blame] | 593 | if (!ret.isOk() || errorStatus != V1_0::ErrorStatus::NONE || |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 594 | returnedMemories.size() != unknownSlots.size()) { |
| 595 | LOG(ERROR) << "Error retrieving memories"; |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | // add memories to unknown slots |
| 600 | for (size_t i = 0; i < unknownSlots.size(); ++i) { |
| 601 | mExecutorWithCache->addCacheEntry(returnedMemories[i], unknownSlots[i]); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | void ExecutionBurstServer::task() { |
| 606 | // loop until the burst object is being destroyed |
| 607 | while (!mTeardown) { |
| 608 | // receive request |
| 609 | auto arguments = mRequestChannelReceiver->getBlocking(); |
| 610 | |
| 611 | // if the request packet was not properly received, return a generic |
| 612 | // error and skip the execution |
| 613 | // |
| 614 | // if the burst is being torn down, skip the execution exection so the |
| 615 | // "task" function can end |
| 616 | if (!arguments) { |
| 617 | if (!mTeardown) { |
Michael Butler | f690d31 | 2019-12-12 16:25:03 -0800 | [diff] [blame] | 618 | mResultChannelSender->send(V1_0::ErrorStatus::GENERAL_FAILURE, {}, kNoTiming); |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 619 | } |
| 620 | continue; |
| 621 | } |
| 622 | |
| 623 | // otherwise begin tracing execution |
| 624 | NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION, |
| 625 | "ExecutionBurstServer getting memory, executing, and returning results"); |
| 626 | |
| 627 | // unpack the arguments; types are Request, std::vector<int32_t>, and |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 628 | // MeasureTiming, respectively |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 629 | const auto [requestWithoutPools, slotsOfPools, measure] = std::move(*arguments); |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 630 | |
Michael Butler | 238fe72 | 2019-03-21 12:17:27 -0700 | [diff] [blame] | 631 | // ensure executor with cache has required memory |
| 632 | std::lock_guard<std::mutex> hold(mMutex); |
| 633 | ensureCacheEntriesArePresentLocked(slotsOfPools); |
| 634 | |
| 635 | // perform computation; types are ErrorStatus, hidl_vec<OutputShape>, |
| 636 | // and Timing, respectively |
| 637 | const auto [errorStatus, outputShapes, returnedTiming] = |
| 638 | mExecutorWithCache->execute(requestWithoutPools, slotsOfPools, measure); |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 639 | |
| 640 | // return result |
Michael Butler | c932ebb | 2019-04-11 14:24:06 -0700 | [diff] [blame] | 641 | mResultChannelSender->send(errorStatus, outputShapes, returnedTiming); |
Michael Butler | 6029632 | 2019-01-17 17:54:51 -0800 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
Michael Butler | 3db6fe5 | 2019-01-29 11:20:30 -0800 | [diff] [blame] | 645 | } // namespace android::nn |