• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "server_executor/include/engine.h"
17 
18 #include <cstring>
19 
20 #include "platform/time/include/time.h"
21 #include "plugin_manager/include/aie_plugin_info.h"
22 #include "server_executor/include/async_msg_handler.h"
23 #include "server_executor/include/sync_msg_handler.h"
24 #include "utils/constants/constants.h"
25 #include "utils/log/aie_log.h"
26 
27 namespace OHOS {
28 namespace AI {
Engine(std::shared_ptr<Plugin> & plugin,std::shared_ptr<Thread> & thread,std::shared_ptr<Queue<Task>> & queue)29 Engine::Engine(std::shared_ptr<Plugin> &plugin, std::shared_ptr<Thread> &thread,
30     std::shared_ptr<Queue<Task>> &queue)
31     : refCount_(0),
32       plugin_(plugin),
33       thread_(thread),
34       queue_(queue),
35       msgHandler_(nullptr),
36       worker_(*queue)
37 {
38 }
39 
~Engine()40 Engine::~Engine()
41 {
42     Uninitialize();
43 }
44 
IsSyncMode(const std::shared_ptr<Plugin> & plugin)45 static bool IsSyncMode(const std::shared_ptr<Plugin> &plugin)
46 {
47     const char *inferMode = plugin->GetPluginAlgorithm()->GetInferMode();
48     if (inferMode == nullptr) {
49         return false;
50     }
51     return (strcmp(PLUGIN_SYNC_INFER, inferMode) == 0);
52 }
53 
GetPlugin() const54 std::shared_ptr<Plugin> Engine::GetPlugin() const
55 {
56     return plugin_;
57 }
58 
GetEngineReference() const59 int Engine::GetEngineReference() const
60 {
61     return refCount_;
62 }
63 
AddEngineReference()64 void Engine::AddEngineReference()
65 {
66     ++refCount_;
67 }
68 
DelEngineReference()69 void Engine::DelEngineReference()
70 {
71     refCount_--;
72 }
73 
Initialize()74 int Engine::Initialize()
75 {
76     if (plugin_->GetPluginAlgorithm() == nullptr) {
77         return RETCODE_NULL_PARAM;
78     }
79     if (IsSyncMode(plugin_)) {
80         AIE_NEW(msgHandler_, SyncMsgHandler(*queue_, plugin_->GetPluginAlgorithm()));
81     } else {
82         AIE_NEW(msgHandler_, AsyncMsgHandler(*queue_, plugin_->GetPluginAlgorithm()));
83     }
84     if (msgHandler_ == nullptr) {
85         HILOGE("[Engine]Allocate massage handler failed, algoType is [%lld], algoName is [%s].",
86             plugin_->GetVersion(), plugin_->GetAid().c_str());
87         return RETCODE_OUT_OF_MEMORY;
88     }
89 
90     bool isStarted = thread_->StartThread(&worker_);
91     if (!isStarted) {
92         AIE_DELETE(msgHandler_);
93         HILOGE("[Engine]Engine(aid is [%s], version is [%lld]) start thread failed.",
94             plugin_->GetAid().c_str(), plugin_->GetVersion());
95         return RETCODE_START_THREAD_FAILED;
96     }
97     return RETCODE_SUCCESS;
98 }
99 
Uninitialize()100 void Engine::Uninitialize()
101 {
102     if (thread_) {
103         thread_->StopThread();
104         ThreadPool *threadPool = ThreadPool::GetInstance();
105         CHK_RET_NONE(threadPool == nullptr);
106         threadPool->Push(thread_);
107         thread_ = nullptr;
108     }
109 
110     if (queue_ != nullptr) {
111         QueuePool<Task> *queuePool = QueuePool<Task>::GetInstance();
112         if (queuePool == nullptr) {
113             HILOGE("[Engine]Allocate queue pool failed");
114             return;
115         }
116         queuePool->Push(queue_);
117     }
118 
119     AIE_DELETE(msgHandler_);
120 }
121 
SyncExecute(IRequest * request,IResponse * & response)122 int Engine::SyncExecute(IRequest *request, IResponse *&response)
123 {
124     if (plugin_ == nullptr) {
125         HILOGE("[Engine]The plugin_ is null.");
126         return RETCODE_PLUGIN_LOAD_FAILED;
127     }
128 
129     SyncMsgHandler *handler = reinterpret_cast<SyncMsgHandler *>(msgHandler_);
130     if (handler == nullptr) {
131         HILOGE("[Engine]MsgHandler is null, synchronous execution is not supported.");
132         return RETCODE_NULL_PARAM;
133     }
134 
135     if (request == nullptr) {
136         HILOGE("[Engine]The request is null.");
137         return RETCODE_NULL_PARAM;
138     }
139 
140     SimpleEventNotifier<IResponse> notifier(IResponse::Destroy);
141     int sendRequestRet = handler->SendRequest(request, notifier);
142     if (sendRequestRet != RETCODE_SUCCESS) {
143         HILOGE("[Engine][transactionId:%lld]Send sync request failed, retCode is [%d].",
144             request->GetTransactionId(), sendRequestRet);
145         return sendRequestRet;
146     }
147 
148     int recvResponseRet = handler->ReceiveResponse(SYNC_MSG_TIMEOUT, notifier, response);
149     if (recvResponseRet != RETCODE_SUCCESS) {
150         HILOGE("[Engine]Receive response failed, retCode is [%d].", recvResponseRet);
151         return recvResponseRet;
152     }
153     return response->GetRetCode();
154 }
155 
AsyncExecute(IRequest * request)156 int Engine::AsyncExecute(IRequest *request)
157 {
158     if (plugin_ == nullptr) {
159         HILOGE("[Engine]The plugin_ is null.");
160         return RETCODE_PLUGIN_LOAD_FAILED;
161     }
162 
163     AsyncMsgHandler *handler = reinterpret_cast<AsyncMsgHandler *>(msgHandler_);
164     if (handler == nullptr) {
165         HILOGE("[Engine]MsgHandler is null, asynchronous execution is not supported.");
166         return RETCODE_NULL_PARAM;
167     }
168 
169     return handler->SendRequest(request);
170 }
171 } // namespace AI
172 } // namespace OHOS