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 
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 
35 namespace android::nn {
36 namespace {
37 
38 using namespace hal;
39 
40 using hardware::MQDescriptorSync;
41 using V1_2::FmqRequestDatum;
42 using V1_2::FmqResultDatum;
43 using V1_2::IBurstCallback;
44 using V1_2::IBurstContext;
45 
46 constexpr Timing kNoTiming = {std::numeric_limits<uint64_t>::max(),
47                               std::numeric_limits<uint64_t>::max()};
48 
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:
DefaultBurstExecutorWithCache(V1_2::IPreparedModel * preparedModel)56     DefaultBurstExecutorWithCache(V1_2::IPreparedModel* preparedModel)
57         : mpPreparedModel(preparedModel) {}
58 
isCacheEntryPresent(int32_t slot) const59     bool isCacheEntryPresent(int32_t slot) const override {
60         const auto it = mMemoryCache.find(slot);
61         return (it != mMemoryCache.end()) && it->second.valid();
62     }
63 
addCacheEntry(const hidl_memory & memory,int32_t slot)64     void addCacheEntry(const hidl_memory& memory, int32_t slot) override {
65         mMemoryCache[slot] = memory;
66     }
67 
removeCacheEntry(int32_t slot)68     void removeCacheEntry(int32_t slot) override { mMemoryCache.erase(slot); }
69 
execute(const V1_0::Request & request,const std::vector<int32_t> & slots,MeasureTiming measure)70     std::tuple<V1_0::ErrorStatus, hidl_vec<OutputShape>, Timing> execute(
71             const V1_0::Request& request, const std::vector<int32_t>& slots,
72             MeasureTiming measure) override {
73         // convert slots to pools
74         hidl_vec<hidl_memory> pools(slots.size());
75         std::transform(slots.begin(), slots.end(), pools.begin(),
76                        [this](int32_t slot) { return mMemoryCache[slot]; });
77 
78         // create full request
79         V1_0::Request fullRequest = request;
80         fullRequest.pools = std::move(pools);
81 
82         // setup execution
83         V1_0::ErrorStatus returnedStatus = V1_0::ErrorStatus::GENERAL_FAILURE;
84         hidl_vec<OutputShape> returnedOutputShapes;
85         Timing returnedTiming;
86         auto cb = [&returnedStatus, &returnedOutputShapes, &returnedTiming](
87                           V1_0::ErrorStatus status, const hidl_vec<OutputShape>& outputShapes,
88                           const Timing& timing) {
89             returnedStatus = status;
90             returnedOutputShapes = outputShapes;
91             returnedTiming = timing;
92         };
93 
94         // execute
95         const Return<void> ret = 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, hidl_memory> mMemoryCache;
107 };
108 
109 }  // anonymous namespace
110 
111 // serialize result
serialize(V1_0::ErrorStatus errorStatus,const std::vector<OutputShape> & outputShapes,Timing timing)112 std::vector<FmqResultDatum> serialize(V1_0::ErrorStatus errorStatus,
113                                       const std::vector<OutputShape>& outputShapes, Timing timing) {
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
136         FmqResultDatum::OperandInformation info{};
137         info.isSufficient = operand.isSufficient;
138         info.numberOfDimensions = static_cast<uint32_t>(operand.dimensions.size());
139 
140         FmqResultDatum datum;
141         datum.operandInformation(info);
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 
163 // deserialize request
deserialize(const std::vector<FmqRequestDatum> & data)164 std::optional<std::tuple<V1_0::Request, std::vector<int32_t>, MeasureTiming>> deserialize(
165         const std::vector<FmqRequestDatum>& data) {
166     using discriminator = FmqRequestDatum::hidl_discriminator;
167 
168     size_t index = 0;
169 
170     // validate packet information
171     if (data.size() == 0 || data[index].getDiscriminator() != discriminator::packetInformation) {
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 
184     // verify packet size
185     if (data.size() != packetSize) {
186         LOG(ERROR) << "FMQ Request packet ill-formed";
187         return std::nullopt;
188     }
189 
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;
198         }
199 
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;
207 
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
307     V1_0::Request request = {/*.inputs=*/inputs, /*.outputs=*/outputs, /*.pools=*/{}};
308     return std::make_tuple(std::move(request), std::move(slots), measure);
309 }
310 
311 // RequestChannelReceiver methods
312 
create(const FmqRequestDescriptor & requestChannel,std::chrono::microseconds pollingTimeWindow)313 std::unique_ptr<RequestChannelReceiver> RequestChannelReceiver::create(
314         const FmqRequestDescriptor& requestChannel, std::chrono::microseconds pollingTimeWindow) {
315     std::unique_ptr<FmqRequestChannel> fmqRequestChannel =
316             std::make_unique<FmqRequestChannel>(requestChannel);
317 
318     if (!fmqRequestChannel->isValid()) {
319         LOG(ERROR) << "Unable to create RequestChannelReceiver";
320         return nullptr;
321     }
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);
330 }
331 
RequestChannelReceiver(std::unique_ptr<FmqRequestChannel> fmqRequestChannel,std::chrono::microseconds pollingTimeWindow)332 RequestChannelReceiver::RequestChannelReceiver(std::unique_ptr<FmqRequestChannel> fmqRequestChannel,
333                                                std::chrono::microseconds pollingTimeWindow)
334     : mFmqRequestChannel(std::move(fmqRequestChannel)), kPollingTimeWindow(pollingTimeWindow) {}
335 
336 std::optional<std::tuple<V1_0::Request, std::vector<int32_t>, MeasureTiming>>
getBlocking()337 RequestChannelReceiver::getBlocking() {
338     const auto packet = getPacketBlocking();
339     if (!packet) {
340         return std::nullopt;
341     }
342 
343     return deserialize(*packet);
344 }
345 
invalidate()346 void RequestChannelReceiver::invalidate() {
347     mTeardown = true;
348 
349     // force unblock
350     // ExecutionBurstServer is by default waiting on a request packet. If the
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);
360 }
361 
getPacketBlocking()362 std::optional<std::vector<FmqRequestDatum>> RequestChannelReceiver::getPacketBlocking() {
363 
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 
create(const FmqResultDescriptor & resultChannel)445 std::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 
ResultChannelSender(std::unique_ptr<FmqResultChannel> fmqResultChannel)462 ResultChannelSender::ResultChannelSender(std::unique_ptr<FmqResultChannel> fmqResultChannel)
463     : mFmqResultChannel(std::move(fmqResultChannel)) {}
464 
send(V1_0::ErrorStatus errorStatus,const std::vector<OutputShape> & outputShapes,Timing timing)465 bool ResultChannelSender::send(V1_0::ErrorStatus errorStatus,
466                                const std::vector<OutputShape>& outputShapes, Timing timing) {
467     const std::vector<FmqResultDatum> serialized = serialize(errorStatus, outputShapes, timing);
468     return sendPacket(serialized);
469 }
470 
sendPacket(const std::vector<FmqResultDatum> & packet)471 bool ResultChannelSender::sendPacket(const std::vector<FmqResultDatum>& packet) {
472     if (packet.size() > mFmqResultChannel->availableToWrite()) {
473         LOG(ERROR)
474                 << "ResultChannelSender::sendPacket -- packet size exceeds size available in FMQ";
475         const std::vector<FmqResultDatum> errorPacket =
476                 serialize(V1_0::ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
477 
478         // Always send the packet with "blocking" because this signals the futex
479         // and unblocks the consumer if it is waiting on the futex.
480         return mFmqResultChannel->writeBlocking(errorPacket.data(), errorPacket.size());
481     }
482 
483     // Always send the packet with "blocking" because this signals the futex and
484     // unblocks the consumer if it is waiting on the futex.
485     return mFmqResultChannel->writeBlocking(packet.data(), packet.size());
486 }
487 
488 // ExecutionBurstServer methods
489 
create(const sp<IBurstCallback> & callback,const MQDescriptorSync<FmqRequestDatum> & requestChannel,const MQDescriptorSync<FmqResultDatum> & resultChannel,std::shared_ptr<IBurstExecutorWithCache> executorWithCache,std::chrono::microseconds pollingTimeWindow)490 sp<ExecutionBurstServer> ExecutionBurstServer::create(
491         const sp<IBurstCallback>& callback, const MQDescriptorSync<FmqRequestDatum>& requestChannel,
492         const MQDescriptorSync<FmqResultDatum>& resultChannel,
493         std::shared_ptr<IBurstExecutorWithCache> executorWithCache,
494         std::chrono::microseconds pollingTimeWindow) {
495     // check inputs
496     if (callback == nullptr || executorWithCache == nullptr) {
497         LOG(ERROR) << "ExecutionBurstServer::create passed a nullptr";
498         return nullptr;
499     }
500 
501     // create FMQ objects
502     std::unique_ptr<RequestChannelReceiver> requestChannelReceiver =
503             RequestChannelReceiver::create(requestChannel, pollingTimeWindow);
504     std::unique_ptr<ResultChannelSender> resultChannelSender =
505             ResultChannelSender::create(resultChannel);
506 
507     // check FMQ objects
508     if (!requestChannelReceiver || !resultChannelSender) {
509         LOG(ERROR) << "ExecutionBurstServer::create failed to create FastMessageQueue";
510         return nullptr;
511     }
512 
513     // make and return context
514     return new ExecutionBurstServer(callback, std::move(requestChannelReceiver),
515                                     std::move(resultChannelSender), std::move(executorWithCache));
516 }
517 
create(const sp<IBurstCallback> & callback,const MQDescriptorSync<FmqRequestDatum> & requestChannel,const MQDescriptorSync<FmqResultDatum> & resultChannel,V1_2::IPreparedModel * preparedModel,std::chrono::microseconds pollingTimeWindow)518 sp<ExecutionBurstServer> ExecutionBurstServer::create(
519         const sp<IBurstCallback>& callback, const MQDescriptorSync<FmqRequestDatum>& requestChannel,
520         const MQDescriptorSync<FmqResultDatum>& resultChannel, V1_2::IPreparedModel* preparedModel,
521         std::chrono::microseconds pollingTimeWindow) {
522     // check relevant input
523     if (preparedModel == nullptr) {
524         LOG(ERROR) << "ExecutionBurstServer::create passed a nullptr";
525         return nullptr;
526     }
527 
528     // adapt IPreparedModel to have caching
529     const std::shared_ptr<DefaultBurstExecutorWithCache> preparedModelAdapter =
530             std::make_shared<DefaultBurstExecutorWithCache>(preparedModel);
531 
532     // make and return context
533     return ExecutionBurstServer::create(callback, requestChannel, resultChannel,
534                                         preparedModelAdapter, pollingTimeWindow);
535 }
536 
ExecutionBurstServer(const sp<IBurstCallback> & callback,std::unique_ptr<RequestChannelReceiver> requestChannel,std::unique_ptr<ResultChannelSender> resultChannel,std::shared_ptr<IBurstExecutorWithCache> executorWithCache)537 ExecutionBurstServer::ExecutionBurstServer(
538         const sp<IBurstCallback>& callback, std::unique_ptr<RequestChannelReceiver> requestChannel,
539         std::unique_ptr<ResultChannelSender> resultChannel,
540         std::shared_ptr<IBurstExecutorWithCache> executorWithCache)
541     : mCallback(callback),
542       mRequestChannelReceiver(std::move(requestChannel)),
543       mResultChannelSender(std::move(resultChannel)),
544       mExecutorWithCache(std::move(executorWithCache)) {
545     // TODO: highly document the threading behavior of this class
546     mWorker = std::thread([this] { task(); });
547 }
548 
~ExecutionBurstServer()549 ExecutionBurstServer::~ExecutionBurstServer() {
550     // set teardown flag
551     mTeardown = true;
552     mRequestChannelReceiver->invalidate();
553 
554     // wait for task thread to end
555     mWorker.join();
556 }
557 
freeMemory(int32_t slot)558 Return<void> ExecutionBurstServer::freeMemory(int32_t slot) {
559     std::lock_guard<std::mutex> hold(mMutex);
560     mExecutorWithCache->removeCacheEntry(slot);
561     return Void();
562 }
563 
ensureCacheEntriesArePresentLocked(const std::vector<int32_t> & slots)564 void ExecutionBurstServer::ensureCacheEntriesArePresentLocked(const std::vector<int32_t>& slots) {
565     const auto slotIsKnown = [this](int32_t slot) {
566         return mExecutorWithCache->isCacheEntryPresent(slot);
567     };
568 
569     // find unique unknown slots
570     std::vector<int32_t> unknownSlots = slots;
571     auto unknownSlotsEnd = unknownSlots.end();
572     std::sort(unknownSlots.begin(), unknownSlotsEnd);
573     unknownSlotsEnd = std::unique(unknownSlots.begin(), unknownSlotsEnd);
574     unknownSlotsEnd = std::remove_if(unknownSlots.begin(), unknownSlotsEnd, slotIsKnown);
575     unknownSlots.erase(unknownSlotsEnd, unknownSlots.end());
576 
577     // quick-exit if all slots are known
578     if (unknownSlots.empty()) {
579         return;
580     }
581 
582     V1_0::ErrorStatus errorStatus = V1_0::ErrorStatus::GENERAL_FAILURE;
583     std::vector<hidl_memory> returnedMemories;
584     auto cb = [&errorStatus, &returnedMemories](V1_0::ErrorStatus status,
585                                                 const hidl_vec<hidl_memory>& memories) {
586         errorStatus = status;
587         returnedMemories = memories;
588     };
589 
590     const Return<void> ret = mCallback->getMemories(unknownSlots, cb);
591 
592     if (!ret.isOk() || errorStatus != V1_0::ErrorStatus::NONE ||
593         returnedMemories.size() != unknownSlots.size()) {
594         LOG(ERROR) << "Error retrieving memories";
595         return;
596     }
597 
598     // add memories to unknown slots
599     for (size_t i = 0; i < unknownSlots.size(); ++i) {
600         mExecutorWithCache->addCacheEntry(returnedMemories[i], unknownSlots[i]);
601     }
602 }
603 
task()604 void ExecutionBurstServer::task() {
605     // loop until the burst object is being destroyed
606     while (!mTeardown) {
607         // receive request
608         auto arguments = mRequestChannelReceiver->getBlocking();
609 
610         // if the request packet was not properly received, return a generic
611         // error and skip the execution
612         //
613         // if the  burst is being torn down, skip the execution exection so the
614         // "task" function can end
615         if (!arguments) {
616             if (!mTeardown) {
617                 mResultChannelSender->send(V1_0::ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
618             }
619             continue;
620         }
621 
622         // otherwise begin tracing execution
623         NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION,
624                      "ExecutionBurstServer getting memory, executing, and returning results");
625 
626         // unpack the arguments; types are Request, std::vector<int32_t>, and
627         // MeasureTiming, respectively
628         const auto [requestWithoutPools, slotsOfPools, measure] = std::move(*arguments);
629 
630         // ensure executor with cache has required memory
631         std::lock_guard<std::mutex> hold(mMutex);
632         ensureCacheEntriesArePresentLocked(slotsOfPools);
633 
634         // perform computation; types are ErrorStatus, hidl_vec<OutputShape>,
635         // and Timing, respectively
636         const auto [errorStatus, outputShapes, returnedTiming] =
637                 mExecutorWithCache->execute(requestWithoutPools, slotsOfPools, measure);
638 
639         // return result
640         mResultChannelSender->send(errorStatus, outputShapes, returnedTiming);
641     }
642 }
643 
644 }  // namespace android::nn
645