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