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