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