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