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 using discriminator = FmqRequestDatum::hidl_discriminator;
364
365 if (mTeardown) {
366 return std::nullopt;
367 }
368
369 // First spend time polling if results are available in FMQ instead of
370 // waiting on the futex. Polling is more responsive (yielding lower
371 // latencies), but can take up more power, so only poll for a limited period
372 // of time.
373
374 auto& getCurrentTime = std::chrono::high_resolution_clock::now;
375 const auto timeToStopPolling = getCurrentTime() + kPollingTimeWindow;
376
377 while (getCurrentTime() < timeToStopPolling) {
378 // if class is being torn down, immediately return
379 if (mTeardown.load(std::memory_order_relaxed)) {
380 return std::nullopt;
381 }
382
383 // Check if data is available. If it is, immediately retrieve it and
384 // return.
385 const size_t available = mFmqRequestChannel->availableToRead();
386 if (available > 0) {
387 // This is the first point when we know an execution is occurring,
388 // so begin to collect systraces. Note that a similar systrace does
389 // not exist at the corresponding point in
390 // ResultChannelReceiver::getPacketBlocking because the execution is
391 // already in flight.
392 NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION,
393 "ExecutionBurstServer getting packet");
394 std::vector<FmqRequestDatum> packet(available);
395 const bool success = mFmqRequestChannel->read(packet.data(), available);
396 if (!success) {
397 LOG(ERROR) << "Error receiving packet";
398 return std::nullopt;
399 }
400 return std::make_optional(std::move(packet));
401 }
402 }
403
404 // If we get to this point, we either stopped polling because it was taking
405 // too long or polling was not allowed. Instead, perform a blocking call
406 // which uses a futex to save power.
407
408 // wait for request packet and read first element of request packet
409 FmqRequestDatum datum;
410 bool success = mFmqRequestChannel->readBlocking(&datum, 1);
411
412 // This is the first point when we know an execution is occurring, so begin
413 // to collect systraces. Note that a similar systrace does not exist at the
414 // corresponding point in ResultChannelReceiver::getPacketBlocking because
415 // the execution is already in flight.
416 NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION, "ExecutionBurstServer getting packet");
417
418 // retrieve remaining elements
419 // NOTE: all of the data is already available at this point, so there's no
420 // need to do a blocking wait to wait for more data. This is known because
421 // in FMQ, all writes are published (made available) atomically. Currently,
422 // the producer always publishes the entire packet in one function call, so
423 // if the first element of the packet is available, the remaining elements
424 // are also available.
425 const size_t count = mFmqRequestChannel->availableToRead();
426 std::vector<FmqRequestDatum> packet(count + 1);
427 std::memcpy(&packet.front(), &datum, sizeof(datum));
428 success &= mFmqRequestChannel->read(packet.data() + 1, count);
429
430 // terminate loop
431 if (mTeardown) {
432 return std::nullopt;
433 }
434
435 // ensure packet was successfully received
436 if (!success) {
437 LOG(ERROR) << "Error receiving packet";
438 return std::nullopt;
439 }
440
441 return std::make_optional(std::move(packet));
442 }
443
444 // ResultChannelSender methods
445
create(const FmqResultDescriptor & resultChannel)446 std::unique_ptr<ResultChannelSender> ResultChannelSender::create(
447 const FmqResultDescriptor& resultChannel) {
448 std::unique_ptr<FmqResultChannel> fmqResultChannel =
449 std::make_unique<FmqResultChannel>(resultChannel);
450
451 if (!fmqResultChannel->isValid()) {
452 LOG(ERROR) << "Unable to create RequestChannelSender";
453 return nullptr;
454 }
455 if (fmqResultChannel->getEventFlagWord() == nullptr) {
456 LOG(ERROR) << "ResultChannelSender::create was passed an MQDescriptor without an EventFlag";
457 return nullptr;
458 }
459
460 return std::make_unique<ResultChannelSender>(std::move(fmqResultChannel));
461 }
462
ResultChannelSender(std::unique_ptr<FmqResultChannel> fmqResultChannel)463 ResultChannelSender::ResultChannelSender(std::unique_ptr<FmqResultChannel> fmqResultChannel)
464 : mFmqResultChannel(std::move(fmqResultChannel)) {}
465
send(V1_0::ErrorStatus errorStatus,const std::vector<OutputShape> & outputShapes,Timing timing)466 bool ResultChannelSender::send(V1_0::ErrorStatus errorStatus,
467 const std::vector<OutputShape>& outputShapes, Timing timing) {
468 const std::vector<FmqResultDatum> serialized = serialize(errorStatus, outputShapes, timing);
469 return sendPacket(serialized);
470 }
471
sendPacket(const std::vector<FmqResultDatum> & packet)472 bool 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
create(const sp<IBurstCallback> & callback,const MQDescriptorSync<FmqRequestDatum> & requestChannel,const MQDescriptorSync<FmqResultDatum> & resultChannel,std::shared_ptr<IBurstExecutorWithCache> executorWithCache,std::chrono::microseconds pollingTimeWindow)491 sp<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
create(const sp<IBurstCallback> & callback,const MQDescriptorSync<FmqRequestDatum> & requestChannel,const MQDescriptorSync<FmqResultDatum> & resultChannel,V1_2::IPreparedModel * preparedModel,std::chrono::microseconds pollingTimeWindow)519 sp<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
ExecutionBurstServer(const sp<IBurstCallback> & callback,std::unique_ptr<RequestChannelReceiver> requestChannel,std::unique_ptr<ResultChannelSender> resultChannel,std::shared_ptr<IBurstExecutorWithCache> executorWithCache)538 ExecutionBurstServer::ExecutionBurstServer(
539 const sp<IBurstCallback>& callback, std::unique_ptr<RequestChannelReceiver> requestChannel,
540 std::unique_ptr<ResultChannelSender> resultChannel,
541 std::shared_ptr<IBurstExecutorWithCache> executorWithCache)
542 : mCallback(callback),
543 mRequestChannelReceiver(std::move(requestChannel)),
544 mResultChannelSender(std::move(resultChannel)),
545 mExecutorWithCache(std::move(executorWithCache)) {
546 // TODO: highly document the threading behavior of this class
547 mWorker = std::thread([this] { task(); });
548 }
549
~ExecutionBurstServer()550 ExecutionBurstServer::~ExecutionBurstServer() {
551 // set teardown flag
552 mTeardown = true;
553 mRequestChannelReceiver->invalidate();
554
555 // wait for task thread to end
556 mWorker.join();
557 }
558
freeMemory(int32_t slot)559 Return<void> ExecutionBurstServer::freeMemory(int32_t slot) {
560 std::lock_guard<std::mutex> hold(mMutex);
561 mExecutorWithCache->removeCacheEntry(slot);
562 return Void();
563 }
564
ensureCacheEntriesArePresentLocked(const std::vector<int32_t> & slots)565 void ExecutionBurstServer::ensureCacheEntriesArePresentLocked(const std::vector<int32_t>& slots) {
566 const auto slotIsKnown = [this](int32_t slot) {
567 return mExecutorWithCache->isCacheEntryPresent(slot);
568 };
569
570 // find unique unknown slots
571 std::vector<int32_t> unknownSlots = slots;
572 auto unknownSlotsEnd = unknownSlots.end();
573 std::sort(unknownSlots.begin(), unknownSlotsEnd);
574 unknownSlotsEnd = std::unique(unknownSlots.begin(), unknownSlotsEnd);
575 unknownSlotsEnd = std::remove_if(unknownSlots.begin(), unknownSlotsEnd, slotIsKnown);
576 unknownSlots.erase(unknownSlotsEnd, unknownSlots.end());
577
578 // quick-exit if all slots are known
579 if (unknownSlots.empty()) {
580 return;
581 }
582
583 V1_0::ErrorStatus errorStatus = V1_0::ErrorStatus::GENERAL_FAILURE;
584 std::vector<hidl_memory> returnedMemories;
585 auto cb = [&errorStatus, &returnedMemories](V1_0::ErrorStatus status,
586 const hidl_vec<hidl_memory>& memories) {
587 errorStatus = status;
588 returnedMemories = memories;
589 };
590
591 const Return<void> ret = mCallback->getMemories(unknownSlots, cb);
592
593 if (!ret.isOk() || errorStatus != V1_0::ErrorStatus::NONE ||
594 returnedMemories.size() != unknownSlots.size()) {
595 LOG(ERROR) << "Error retrieving memories";
596 return;
597 }
598
599 // add memories to unknown slots
600 for (size_t i = 0; i < unknownSlots.size(); ++i) {
601 mExecutorWithCache->addCacheEntry(returnedMemories[i], unknownSlots[i]);
602 }
603 }
604
task()605 void ExecutionBurstServer::task() {
606 // loop until the burst object is being destroyed
607 while (!mTeardown) {
608 // receive request
609 auto arguments = mRequestChannelReceiver->getBlocking();
610
611 // if the request packet was not properly received, return a generic
612 // error and skip the execution
613 //
614 // if the burst is being torn down, skip the execution exection so the
615 // "task" function can end
616 if (!arguments) {
617 if (!mTeardown) {
618 mResultChannelSender->send(V1_0::ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
619 }
620 continue;
621 }
622
623 // otherwise begin tracing execution
624 NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION,
625 "ExecutionBurstServer getting memory, executing, and returning results");
626
627 // unpack the arguments; types are Request, std::vector<int32_t>, and
628 // MeasureTiming, respectively
629 const auto [requestWithoutPools, slotsOfPools, measure] = std::move(*arguments);
630
631 // ensure executor with cache has required memory
632 std::lock_guard<std::mutex> hold(mMutex);
633 ensureCacheEntriesArePresentLocked(slotsOfPools);
634
635 // perform computation; types are ErrorStatus, hidl_vec<OutputShape>,
636 // and Timing, respectively
637 const auto [errorStatus, outputShapes, returnedTiming] =
638 mExecutorWithCache->execute(requestWithoutPools, slotsOfPools, measure);
639
640 // return result
641 mResultChannelSender->send(errorStatus, outputShapes, returnedTiming);
642 }
643 }
644
645 } // namespace android::nn
646