1be168c0dSopenharmony_ciFrom 4c255522b8ad0325e00dcd4d65bb909f6b7e1ab3 Mon Sep 17 00:00:00 2001 2be168c0dSopenharmony_ciFrom: chengfeng27 <chengfeng27@huawei.com> 3be168c0dSopenharmony_ciDate: Fri, 19 Jul 2024 10:43:48 +0800 4be168c0dSopenharmony_ciSubject: [PATCH] hiappevent not report when not hap 5be168c0dSopenharmony_ci 6be168c0dSopenharmony_ci--- 7be168c0dSopenharmony_ci mindspore/lite/BUILD.gn | 2 + 8be168c0dSopenharmony_ci mindspore/lite/src/common/BUILD.gn | 5 + 9be168c0dSopenharmony_ci .../src/common/hi_app_event/handler_thread.cc | 143 ++++++++++++++++++ 10be168c0dSopenharmony_ci .../src/common/hi_app_event/handler_thread.h | 102 +++++++++++++ 11be168c0dSopenharmony_ci .../src/common/hi_app_event/hi_app_event.cc | 80 ++++++++++ 12be168c0dSopenharmony_ci .../src/common/hi_app_event/hi_app_event.h | 31 ++++ 13be168c0dSopenharmony_ci .../hi_app_event/hi_app_event_config.cc | 57 +++++++ 14be168c0dSopenharmony_ci .../common/hi_app_event/hi_app_event_config.h | 45 ++++++ 15be168c0dSopenharmony_ci mindspore/lite/src/litert/c_api/model_c.cc | 6 + 16be168c0dSopenharmony_ci .../lite/src/litert/cxx_api/model/model.cc | 96 +++++++++++- 17be168c0dSopenharmony_ci 10 files changed, 563 insertions(+), 4 deletions(-) 18be168c0dSopenharmony_ci create mode 100644 mindspore/lite/src/common/hi_app_event/handler_thread.cc 19be168c0dSopenharmony_ci create mode 100644 mindspore/lite/src/common/hi_app_event/handler_thread.h 20be168c0dSopenharmony_ci create mode 100644 mindspore/lite/src/common/hi_app_event/hi_app_event.cc 21be168c0dSopenharmony_ci create mode 100644 mindspore/lite/src/common/hi_app_event/hi_app_event.h 22be168c0dSopenharmony_ci create mode 100644 mindspore/lite/src/common/hi_app_event/hi_app_event_config.cc 23be168c0dSopenharmony_ci create mode 100644 mindspore/lite/src/common/hi_app_event/hi_app_event_config.h 24be168c0dSopenharmony_ci 25be168c0dSopenharmony_cidiff --git a/mindspore/lite/BUILD.gn b/mindspore/lite/BUILD.gn 26be168c0dSopenharmony_ciindex 58ee5e51..6f7f85e9 100644 27be168c0dSopenharmony_ci--- a/mindspore/lite/BUILD.gn 28be168c0dSopenharmony_ci+++ b/mindspore/lite/BUILD.gn 29be168c0dSopenharmony_ci@@ -397,6 +397,7 @@ ohos_shared_library("mindspore_lib") { 30be168c0dSopenharmony_ci "RUNTIME_PASS_CLIP", 31be168c0dSopenharmony_ci "ENABLE_MULTI_LAYOUT", 32be168c0dSopenharmony_ci "VERSION_STR=\"2.1.0\"", 33be168c0dSopenharmony_ci+ "ENABLE_HI_APP_EVENT", 34be168c0dSopenharmony_ci ] 35be168c0dSopenharmony_ci 36be168c0dSopenharmony_ci if (target_cpu == "arm") { 37be168c0dSopenharmony_ci@@ -506,6 +507,7 @@ ohos_shared_library("mindspore_ndk") { 38be168c0dSopenharmony_ci "RUNTIME_PASS_CLIP", 39be168c0dSopenharmony_ci "ENABLE_MULTI_LAYOUT", 40be168c0dSopenharmony_ci "VERSION_STR=\"2.1.0\"", 41be168c0dSopenharmony_ci+ "ENABLE_HI_APP_EVENT", 42be168c0dSopenharmony_ci ] 43be168c0dSopenharmony_ci 44be168c0dSopenharmony_ci configs = [ 45be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/common/BUILD.gn b/mindspore/lite/src/common/BUILD.gn 46be168c0dSopenharmony_ciindex 8bb14a30..e08e09ce 100644 47be168c0dSopenharmony_ci--- a/mindspore/lite/src/common/BUILD.gn 48be168c0dSopenharmony_ci+++ b/mindspore/lite/src/common/BUILD.gn 49be168c0dSopenharmony_ci@@ -17,6 +17,9 @@ lite_src_common_mid_sources = [ 50be168c0dSopenharmony_ci "random_data_generator.cc", 51be168c0dSopenharmony_ci "string_util.cc", 52be168c0dSopenharmony_ci "dynamic_library_loader.cc", 53be168c0dSopenharmony_ci+ "hi_app_event/hi_app_event.cc", 54be168c0dSopenharmony_ci+ "hi_app_event/hi_app_event_config.cc", 55be168c0dSopenharmony_ci+ "hi_app_event/handler_thread.cc", 56be168c0dSopenharmony_ci ] 57be168c0dSopenharmony_ci 58be168c0dSopenharmony_ci ohos_source_set("lite_common_mid_obj") { 59be168c0dSopenharmony_ci@@ -36,6 +39,7 @@ ohos_source_set("lite_common_mid_obj") { 60be168c0dSopenharmony_ci 61be168c0dSopenharmony_ci external_deps = [ 62be168c0dSopenharmony_ci "hilog:libhilog", 63be168c0dSopenharmony_ci+ "hiappevent:hiappevent_innerapi", 64be168c0dSopenharmony_ci ] 65be168c0dSopenharmony_ci 66be168c0dSopenharmony_ci configs = [ 67be168c0dSopenharmony_ci@@ -48,6 +52,7 @@ ohos_source_set("lite_common_mid_obj") { 68be168c0dSopenharmony_ci "MS_COMPILE_OHOS", 69be168c0dSopenharmony_ci "PRIMITIVE_WRITEABLE", 70be168c0dSopenharmony_ci "VERSION_STR=\"2.1.0\"", 71be168c0dSopenharmony_ci+ "ENABLE_HI_APP_EVENT", 72be168c0dSopenharmony_ci ] 73be168c0dSopenharmony_ci if (target_cpu == "arm") { 74be168c0dSopenharmony_ci defines += [ 75be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/common/hi_app_event/handler_thread.cc b/mindspore/lite/src/common/hi_app_event/handler_thread.cc 76be168c0dSopenharmony_cinew file mode 100644 77be168c0dSopenharmony_ciindex 00000000..a84504c0 78be168c0dSopenharmony_ci--- /dev/null 79be168c0dSopenharmony_ci+++ b/mindspore/lite/src/common/hi_app_event/handler_thread.cc 80be168c0dSopenharmony_ci@@ -0,0 +1,143 @@ 81be168c0dSopenharmony_ci+/** 82be168c0dSopenharmony_ci+ * Copyright 2024 Huawei Technologies Co., Ltd 83be168c0dSopenharmony_ci+ * 84be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License"); 85be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License. 86be168c0dSopenharmony_ci+ * You may obtain a copy of the License at 87be168c0dSopenharmony_ci+ * 88be168c0dSopenharmony_ci+ * http://www.apache.org/licenses/LICENSE-2.0 89be168c0dSopenharmony_ci+ * 90be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software 91be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS, 92be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 93be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and 94be168c0dSopenharmony_ci+ * limitations under the License. 95be168c0dSopenharmony_ci+ */ 96be168c0dSopenharmony_ci+ 97be168c0dSopenharmony_ci+#include "handler_thread.h" 98be168c0dSopenharmony_ci+ 99be168c0dSopenharmony_ci+namespace mindspore { 100be168c0dSopenharmony_ci+namespace lite { 101be168c0dSopenharmony_ci+HandlerThread::Handler::Handler(HandlerThread& aLooper) 102be168c0dSopenharmony_ci+ : assigned_looper_(aLooper) 103be168c0dSopenharmony_ci+{ } 104be168c0dSopenharmony_ci+ 105be168c0dSopenharmony_ci+bool HandlerThread::Handler::Post(HandlerThread::Runnable && aRunnable) 106be168c0dSopenharmony_ci+{ 107be168c0dSopenharmony_ci+ return assigned_looper_.Post("", std::move(aRunnable)); 108be168c0dSopenharmony_ci+} 109be168c0dSopenharmony_ci+ 110be168c0dSopenharmony_ci+bool HandlerThread::Handler::Post(std::string key, HandlerThread::Runnable && aRunnable) 111be168c0dSopenharmony_ci+{ 112be168c0dSopenharmony_ci+ return assigned_looper_.Post(key, std::move(aRunnable)); 113be168c0dSopenharmony_ci+} 114be168c0dSopenharmony_ci+ 115be168c0dSopenharmony_ci+HandlerThread::HandlerThread() 116be168c0dSopenharmony_ci+ : running_(false), 117be168c0dSopenharmony_ci+ abort_requested_(false), 118be168c0dSopenharmony_ci+ runnables_(), 119be168c0dSopenharmony_ci+ runnables_mutex_(), 120be168c0dSopenharmony_ci+ handler_(std::make_shared< Handler >(*this)) 121be168c0dSopenharmony_ci+{ } 122be168c0dSopenharmony_ci+ 123be168c0dSopenharmony_ci+HandlerThread::~HandlerThread() 124be168c0dSopenharmony_ci+{ 125be168c0dSopenharmony_ci+ AbortAndJoin(); 126be168c0dSopenharmony_ci+} 127be168c0dSopenharmony_ci+ 128be168c0dSopenharmony_ci+bool HandlerThread::Running() const 129be168c0dSopenharmony_ci+{ 130be168c0dSopenharmony_ci+ return running_.load(); 131be168c0dSopenharmony_ci+} 132be168c0dSopenharmony_ci+ 133be168c0dSopenharmony_ci+bool HandlerThread::Run() 134be168c0dSopenharmony_ci+{ 135be168c0dSopenharmony_ci+ thread_ = std::thread(&HandlerThread::RunFunc, this); 136be168c0dSopenharmony_ci+ 137be168c0dSopenharmony_ci+ return true; 138be168c0dSopenharmony_ci+} 139be168c0dSopenharmony_ci+ 140be168c0dSopenharmony_ci+void HandlerThread::Stop() 141be168c0dSopenharmony_ci+{ 142be168c0dSopenharmony_ci+ AbortAndJoin(); 143be168c0dSopenharmony_ci+} 144be168c0dSopenharmony_ci+ 145be168c0dSopenharmony_ci+std::shared_ptr < HandlerThread::Handler > HandlerThread::GetHandler() 146be168c0dSopenharmony_ci+{ 147be168c0dSopenharmony_ci+ return handler_; 148be168c0dSopenharmony_ci+} 149be168c0dSopenharmony_ci+ 150be168c0dSopenharmony_ci+void HandlerThread::RunFunc() 151be168c0dSopenharmony_ci+{ 152be168c0dSopenharmony_ci+ running_.store(true); 153be168c0dSopenharmony_ci+ 154be168c0dSopenharmony_ci+ (void)pthread_setname_np(pthread_self(), "OS_MSEvent"); 155be168c0dSopenharmony_ci+ while (abort_requested_.load() == false) { 156be168c0dSopenharmony_ci+ Runnable r = Next(); 157be168c0dSopenharmony_ci+ if (r != nullptr) { 158be168c0dSopenharmony_ci+ r(); 159be168c0dSopenharmony_ci+ } else { 160be168c0dSopenharmony_ci+ std::unique_lock<std::mutex> lock{mutex_}; 161be168c0dSopenharmony_ci+ condition_.wait(lock); 162be168c0dSopenharmony_ci+ } 163be168c0dSopenharmony_ci+ } 164be168c0dSopenharmony_ci+ 165be168c0dSopenharmony_ci+ running_.store(false); 166be168c0dSopenharmony_ci+} 167be168c0dSopenharmony_ci+ 168be168c0dSopenharmony_ci+void HandlerThread::AbortAndJoin() 169be168c0dSopenharmony_ci+{ 170be168c0dSopenharmony_ci+ abort_requested_.store(true); 171be168c0dSopenharmony_ci+ condition_.notify_one(); 172be168c0dSopenharmony_ci+ if (thread_.joinable()) { 173be168c0dSopenharmony_ci+ thread_.join(); 174be168c0dSopenharmony_ci+ } 175be168c0dSopenharmony_ci+} 176be168c0dSopenharmony_ci+ 177be168c0dSopenharmony_ci+HandlerThread::Runnable HandlerThread::Next() 178be168c0dSopenharmony_ci+{ 179be168c0dSopenharmony_ci+ std::lock_guard guard(runnables_mutex_); 180be168c0dSopenharmony_ci+ 181be168c0dSopenharmony_ci+ if (runnables_.empty()) { 182be168c0dSopenharmony_ci+ return nullptr; 183be168c0dSopenharmony_ci+ } 184be168c0dSopenharmony_ci+ std::shared_ptr<Message> msg = runnables_.front(); 185be168c0dSopenharmony_ci+ Runnable runnable = msg->r; 186be168c0dSopenharmony_ci+ runnables_.pop_front(); 187be168c0dSopenharmony_ci+ return runnable; 188be168c0dSopenharmony_ci+} 189be168c0dSopenharmony_ci+ 190be168c0dSopenharmony_ci+class IsSameKey { 191be168c0dSopenharmony_ci+public: 192be168c0dSopenharmony_ci+ std::string key_; 193be168c0dSopenharmony_ci+ 194be168c0dSopenharmony_ci+ bool operator()(std::shared_ptr < HandlerThread::Message > obj) 195be168c0dSopenharmony_ci+ { 196be168c0dSopenharmony_ci+ return obj->key == key_; 197be168c0dSopenharmony_ci+ } 198be168c0dSopenharmony_ci+ 199be168c0dSopenharmony_ci+ explicit IsSameKey(std::string key) { key_ = key; } 200be168c0dSopenharmony_ci+}; 201be168c0dSopenharmony_ci+ 202be168c0dSopenharmony_ci+bool HandlerThread::Post(std::string key, Runnable && aRunnable) 203be168c0dSopenharmony_ci+{ 204be168c0dSopenharmony_ci+ if (not Running()) { 205be168c0dSopenharmony_ci+ std::cout << "Denying insertion, as the looper is not running.\n"; 206be168c0dSopenharmony_ci+ return false; 207be168c0dSopenharmony_ci+ } 208be168c0dSopenharmony_ci+ 209be168c0dSopenharmony_ci+ std::lock_guard guard(runnables_mutex_); 210be168c0dSopenharmony_ci+ if (!key.empty()) { 211be168c0dSopenharmony_ci+ runnables_.remove_if(IsSameKey(key)); 212be168c0dSopenharmony_ci+ } 213be168c0dSopenharmony_ci+ auto msg = std::make_shared<Message>(); 214be168c0dSopenharmony_ci+ msg->key = key; 215be168c0dSopenharmony_ci+ msg->r = std::move(aRunnable); 216be168c0dSopenharmony_ci+ runnables_.push_back(msg); 217be168c0dSopenharmony_ci+ std::unique_lock<std::mutex> lock{mutex_}; 218be168c0dSopenharmony_ci+ condition_.notify_one(); 219be168c0dSopenharmony_ci+ 220be168c0dSopenharmony_ci+ return true; 221be168c0dSopenharmony_ci+} 222be168c0dSopenharmony_ci+} // mindspore 223be168c0dSopenharmony_ci+} // lite 224be168c0dSopenharmony_ci\ No newline at end of file 225be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/common/hi_app_event/handler_thread.h b/mindspore/lite/src/common/hi_app_event/handler_thread.h 226be168c0dSopenharmony_cinew file mode 100644 227be168c0dSopenharmony_ciindex 00000000..91e5cc19 228be168c0dSopenharmony_ci--- /dev/null 229be168c0dSopenharmony_ci+++ b/mindspore/lite/src/common/hi_app_event/handler_thread.h 230be168c0dSopenharmony_ci@@ -0,0 +1,102 @@ 231be168c0dSopenharmony_ci+/** 232be168c0dSopenharmony_ci+ * Copyright 2024 Huawei Technologies Co., Ltd 233be168c0dSopenharmony_ci+ * 234be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License"); 235be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License. 236be168c0dSopenharmony_ci+ * You may obtain a copy of the License at 237be168c0dSopenharmony_ci+ * 238be168c0dSopenharmony_ci+ * http://www.apache.org/licenses/LICENSE-2.0 239be168c0dSopenharmony_ci+ * 240be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software 241be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS, 242be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 243be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and 244be168c0dSopenharmony_ci+ * limitations under the License. 245be168c0dSopenharmony_ci+ */ 246be168c0dSopenharmony_ci+ 247be168c0dSopenharmony_ci+#ifndef LITE_HANDLER_THREAD_H 248be168c0dSopenharmony_ci+#define LITE_HANDLER_THREAD_H 249be168c0dSopenharmony_ci+ 250be168c0dSopenharmony_ci+#include <list> 251be168c0dSopenharmony_ci+#include <thread> 252be168c0dSopenharmony_ci+#include <atomic> 253be168c0dSopenharmony_ci+#include <memory> 254be168c0dSopenharmony_ci+#include <functional> 255be168c0dSopenharmony_ci+#include <stdexcept> 256be168c0dSopenharmony_ci+#include <mutex> 257be168c0dSopenharmony_ci+#include <queue> 258be168c0dSopenharmony_ci+#include <iostream> 259be168c0dSopenharmony_ci+#include <string> 260be168c0dSopenharmony_ci+#include <condition_variable> 261be168c0dSopenharmony_ci+ 262be168c0dSopenharmony_ci+namespace mindspore { 263be168c0dSopenharmony_ci+namespace lite { 264be168c0dSopenharmony_ci+ 265be168c0dSopenharmony_ci+class HandlerThread { 266be168c0dSopenharmony_ci+public: 267be168c0dSopenharmony_ci+ using Runnable = std::function<void()>; 268be168c0dSopenharmony_ci+ 269be168c0dSopenharmony_ci+ struct Message { 270be168c0dSopenharmony_ci+ std::string key; 271be168c0dSopenharmony_ci+ Runnable r; 272be168c0dSopenharmony_ci+ }; 273be168c0dSopenharmony_ci+ 274be168c0dSopenharmony_ci+ class Handler { 275be168c0dSopenharmony_ci+ friend class HandlerThread; // Allow the looper to access the private constructor. 276be168c0dSopenharmony_ci+ 277be168c0dSopenharmony_ci+ public: 278be168c0dSopenharmony_ci+ bool Post(HandlerThread::Runnable &&aRunnable); 279be168c0dSopenharmony_ci+ 280be168c0dSopenharmony_ci+ /** 281be168c0dSopenharmony_ci+ * 在该线程抛出一个任务,并会移除当前任务等待队列中相同key值的任务 282be168c0dSopenharmony_ci+ * @param key 任务的唯一标志 283be168c0dSopenharmony_ci+ * @param aRunnable 待执行的任务 284be168c0dSopenharmony_ci+ * @return true成功,false失败 285be168c0dSopenharmony_ci+ */ 286be168c0dSopenharmony_ci+ bool Post(std::string key, HandlerThread::Runnable && aRunnable); 287be168c0dSopenharmony_ci+ 288be168c0dSopenharmony_ci+ public: // construction, since we want the looper to expose it's dispatcher exclusively! 289be168c0dSopenharmony_ci+ explicit Handler(HandlerThread &aLooper); 290be168c0dSopenharmony_ci+ 291be168c0dSopenharmony_ci+ private: 292be168c0dSopenharmony_ci+ HandlerThread &assigned_looper_; 293be168c0dSopenharmony_ci+ }; 294be168c0dSopenharmony_ci+ 295be168c0dSopenharmony_ci+public: 296be168c0dSopenharmony_ci+ HandlerThread(); 297be168c0dSopenharmony_ci+ 298be168c0dSopenharmony_ci+ ~HandlerThread(); 299be168c0dSopenharmony_ci+ 300be168c0dSopenharmony_ci+ bool Running() const; 301be168c0dSopenharmony_ci+ 302be168c0dSopenharmony_ci+ bool Run(); 303be168c0dSopenharmony_ci+ 304be168c0dSopenharmony_ci+ void Stop(); 305be168c0dSopenharmony_ci+ 306be168c0dSopenharmony_ci+ std::shared_ptr<Handler> GetHandler(); 307be168c0dSopenharmony_ci+ 308be168c0dSopenharmony_ci+private: 309be168c0dSopenharmony_ci+ void RunFunc(); 310be168c0dSopenharmony_ci+ 311be168c0dSopenharmony_ci+ void AbortAndJoin(); 312be168c0dSopenharmony_ci+ 313be168c0dSopenharmony_ci+ Runnable Next(); 314be168c0dSopenharmony_ci+ 315be168c0dSopenharmony_ci+ bool Post(std::string key, HandlerThread::Runnable && aRunnable); 316be168c0dSopenharmony_ci+ 317be168c0dSopenharmony_ci+private: 318be168c0dSopenharmony_ci+ std::thread thread_; 319be168c0dSopenharmony_ci+ std::atomic_bool running_; 320be168c0dSopenharmony_ci+ std::atomic_bool abort_requested_; 321be168c0dSopenharmony_ci+ 322be168c0dSopenharmony_ci+ std::list<std::shared_ptr<Message>> runnables_; 323be168c0dSopenharmony_ci+ std::recursive_mutex runnables_mutex_; 324be168c0dSopenharmony_ci+ 325be168c0dSopenharmony_ci+ std::shared_ptr<Handler> handler_; 326be168c0dSopenharmony_ci+ 327be168c0dSopenharmony_ci+ std::mutex mutex_; 328be168c0dSopenharmony_ci+ std::condition_variable condition_; 329be168c0dSopenharmony_ci+}; 330be168c0dSopenharmony_ci+} // mindspore 331be168c0dSopenharmony_ci+} // lite 332be168c0dSopenharmony_ci+#endif // LITE_HANDLER_THREAD_H 333be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/common/hi_app_event/hi_app_event.cc b/mindspore/lite/src/common/hi_app_event/hi_app_event.cc 334be168c0dSopenharmony_cinew file mode 100644 335be168c0dSopenharmony_ciindex 00000000..ab46cde3 336be168c0dSopenharmony_ci--- /dev/null 337be168c0dSopenharmony_ci+++ b/mindspore/lite/src/common/hi_app_event/hi_app_event.cc 338be168c0dSopenharmony_ci@@ -0,0 +1,80 @@ 339be168c0dSopenharmony_ci+/** 340be168c0dSopenharmony_ci+ * Copyright 2024 Huawei Technologies Co., Ltd 341be168c0dSopenharmony_ci+ * 342be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License"); 343be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License. 344be168c0dSopenharmony_ci+ * You may obtain a copy of the License at 345be168c0dSopenharmony_ci+ * 346be168c0dSopenharmony_ci+ * http://www.apache.org/licenses/LICENSE-2.0 347be168c0dSopenharmony_ci+ * 348be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software 349be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS, 350be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 351be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and 352be168c0dSopenharmony_ci+ * limitations under the License. 353be168c0dSopenharmony_ci+ */ 354be168c0dSopenharmony_ci+ 355be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 356be168c0dSopenharmony_ci+#include "src/common/hi_app_event/hi_app_event.h" 357be168c0dSopenharmony_ci+#include "src/common/hi_app_event/hi_app_event_config.h" 358be168c0dSopenharmony_ci+#include "app_event.h" 359be168c0dSopenharmony_ci+#include "app_event_processor_mgr.h" 360be168c0dSopenharmony_ci+#include <time.h> 361be168c0dSopenharmony_ci+#include <cstdlib> 362be168c0dSopenharmony_ci+ 363be168c0dSopenharmony_ci+namespace mindspore { 364be168c0dSopenharmony_ci+namespace lite { 365be168c0dSopenharmony_ci+namespace { 366be168c0dSopenharmony_ci+constexpr auto kName = "ha_app_event"; 367be168c0dSopenharmony_ci+constexpr auto kAppId = "com_huawei_hmos_sdk_ocg"; 368be168c0dSopenharmony_ci+constexpr int32_t kTimeOut = 90; 369be168c0dSopenharmony_ci+constexpr int32_t kCondRow = 30; 370be168c0dSopenharmony_ci+constexpr auto kDomain = "api_diagnostic"; 371be168c0dSopenharmony_ci+constexpr auto kEventName = "api_exec_end"; 372be168c0dSopenharmony_ci+constexpr auto kSdkName = "MindSporeLiteKit"; 373be168c0dSopenharmony_ci+constexpr int64_t kAppEventNotHapErrCode = -200; 374be168c0dSopenharmony_ci+static int64_t process_id = -1; 375be168c0dSopenharmony_ci+} 376be168c0dSopenharmony_ci+ 377be168c0dSopenharmony_ci+int64_t HiAppEventAddProcessor() { 378be168c0dSopenharmony_ci+ std::srand(std::time(NULL)); 379be168c0dSopenharmony_ci+ OHOS::HiviewDFX::HiAppEvent::ReportConfig config; 380be168c0dSopenharmony_ci+ config.name = kName; 381be168c0dSopenharmony_ci+ config.appId = kAppId; 382be168c0dSopenharmony_ci+ config.routeInfo = "AUTO"; 383be168c0dSopenharmony_ci+ config.triggerCond.timeout = kTimeOut; 384be168c0dSopenharmony_ci+ config.triggerCond.row = kCondRow; 385be168c0dSopenharmony_ci+ config.eventConfigs.clear(); 386be168c0dSopenharmony_ci+ { 387be168c0dSopenharmony_ci+ OHOS::HiviewDFX::HiAppEvent::EventConfig event; 388be168c0dSopenharmony_ci+ event.domain = kDomain; 389be168c0dSopenharmony_ci+ event.name = kEventName; 390be168c0dSopenharmony_ci+ event.isRealTime = false; 391be168c0dSopenharmony_ci+ config.eventConfigs.push_back(event); 392be168c0dSopenharmony_ci+ } 393be168c0dSopenharmony_ci+ if (process_id == -1) { 394be168c0dSopenharmony_ci+ process_id = OHOS::HiviewDFX::HiAppEvent::AppEventProcessorMgr::AddProcessor(config); 395be168c0dSopenharmony_ci+ } 396be168c0dSopenharmony_ci+ return process_id; 397be168c0dSopenharmony_ci+} 398be168c0dSopenharmony_ci+ 399be168c0dSopenharmony_ci+void HiAppEventWriteEndEvent(const int result, const int err_code, const std::string &api_name, 400be168c0dSopenharmony_ci+ const uint64_t begin_time, const std::string &devices) { 401be168c0dSopenharmony_ci+ if (process_id == kAppEventNotHapErrCode) { 402be168c0dSopenharmony_ci+ return; 403be168c0dSopenharmony_ci+ } 404be168c0dSopenharmony_ci+ OHOS::HiviewDFX::HiAppEvent::Event event(kDomain, kEventName, OHOS::HiviewDFX::HiAppEvent::BEHAVIOR); 405be168c0dSopenharmony_ci+ event.AddParam("trans_id", "transId_" + std::to_string(std::rand() + HiAppEventConfig::GetInstance()->GetTimeMs())); 406be168c0dSopenharmony_ci+ event.AddParam("api_name", api_name); 407be168c0dSopenharmony_ci+ event.AddParam("sdk_name", std::string(kSdkName)); 408be168c0dSopenharmony_ci+ event.AddParam("begin_time", static_cast<int64_t>(begin_time)); 409be168c0dSopenharmony_ci+ event.AddParam("end_time", static_cast<int64_t>(HiAppEventConfig::GetInstance()->GetTimeMs())); 410be168c0dSopenharmony_ci+ event.AddParam("result", result); 411be168c0dSopenharmony_ci+ event.AddParam("error_code", std::to_string(err_code)); 412be168c0dSopenharmony_ci+ event.AddParam("context_devices", devices); 413be168c0dSopenharmony_ci+ event.AddParam("api_language", HiAppEventConfig::GetInstance()->GetApiLanguage()); 414be168c0dSopenharmony_ci+ OHOS::HiviewDFX::HiAppEvent::Write(event); 415be168c0dSopenharmony_ci+} 416be168c0dSopenharmony_ci+} // namespace lite 417be168c0dSopenharmony_ci+} // namespace mindspore 418be168c0dSopenharmony_ci+#endif 419be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/common/hi_app_event/hi_app_event.h b/mindspore/lite/src/common/hi_app_event/hi_app_event.h 420be168c0dSopenharmony_cinew file mode 100644 421be168c0dSopenharmony_ciindex 00000000..a5de1ad3 422be168c0dSopenharmony_ci--- /dev/null 423be168c0dSopenharmony_ci+++ b/mindspore/lite/src/common/hi_app_event/hi_app_event.h 424be168c0dSopenharmony_ci@@ -0,0 +1,31 @@ 425be168c0dSopenharmony_ci+/** 426be168c0dSopenharmony_ci+ * Copyright 2024 Huawei Technologies Co., Ltd 427be168c0dSopenharmony_ci+ * 428be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License"); 429be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License. 430be168c0dSopenharmony_ci+ * You may obtain a copy of the License at 431be168c0dSopenharmony_ci+ * 432be168c0dSopenharmony_ci+ * http://www.apache.org/licenses/LICENSE-2.0 433be168c0dSopenharmony_ci+ * 434be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software 435be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS, 436be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 437be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and 438be168c0dSopenharmony_ci+ * limitations under the License. 439be168c0dSopenharmony_ci+ */ 440be168c0dSopenharmony_ci+ 441be168c0dSopenharmony_ci+#ifndef MINDSPORE_LITE_HI_APP_EVENT_H_ 442be168c0dSopenharmony_ci+#define MINDSPORE_LITE_HI_APP_EVENT_H_ 443be168c0dSopenharmony_ci+ 444be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 445be168c0dSopenharmony_ci+#include <string> 446be168c0dSopenharmony_ci+ 447be168c0dSopenharmony_ci+namespace mindspore { 448be168c0dSopenharmony_ci+namespace lite { 449be168c0dSopenharmony_ci+int64_t HiAppEventAddProcessor(); 450be168c0dSopenharmony_ci+void HiAppEventWriteEndEvent(const int result, const int err_code, const std::string &api_name, 451be168c0dSopenharmony_ci+ const uint64_t begin_time, const std::string &devices = "None"); 452be168c0dSopenharmony_ci+} // namespace lite 453be168c0dSopenharmony_ci+} // namespace mindspore 454be168c0dSopenharmony_ci+#endif 455be168c0dSopenharmony_ci+#endif // MINDSPORE_LITE_HI_APP_EVENT_H_ 456be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/common/hi_app_event/hi_app_event_config.cc b/mindspore/lite/src/common/hi_app_event/hi_app_event_config.cc 457be168c0dSopenharmony_cinew file mode 100644 458be168c0dSopenharmony_ciindex 00000000..c488cd86 459be168c0dSopenharmony_ci--- /dev/null 460be168c0dSopenharmony_ci+++ b/mindspore/lite/src/common/hi_app_event/hi_app_event_config.cc 461be168c0dSopenharmony_ci@@ -0,0 +1,57 @@ 462be168c0dSopenharmony_ci+/** 463be168c0dSopenharmony_ci+ * Copyright 2024 Huawei Technologies Co., Ltd 464be168c0dSopenharmony_ci+ * 465be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License"); 466be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License. 467be168c0dSopenharmony_ci+ * You may obtain a copy of the License at 468be168c0dSopenharmony_ci+ * 469be168c0dSopenharmony_ci+ * http://www.apache.org/licenses/LICENSE-2.0 470be168c0dSopenharmony_ci+ * 471be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software 472be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS, 473be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 474be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and 475be168c0dSopenharmony_ci+ * limitations under the License. 476be168c0dSopenharmony_ci+ */ 477be168c0dSopenharmony_ci+ 478be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 479be168c0dSopenharmony_ci+#include "src/common/hi_app_event/hi_app_event_config.h" 480be168c0dSopenharmony_ci+#include <ctime> 481be168c0dSopenharmony_ci+ 482be168c0dSopenharmony_ci+namespace mindspore { 483be168c0dSopenharmony_ci+namespace lite { 484be168c0dSopenharmony_ci+HiAppEventConfig *HiAppEventConfig::GetInstance() { 485be168c0dSopenharmony_ci+ static HiAppEventConfig instance; 486be168c0dSopenharmony_ci+ return &instance; 487be168c0dSopenharmony_ci+} 488be168c0dSopenharmony_ci+ 489be168c0dSopenharmony_ci+HiAppEventConfig::HiAppEventConfig() { 490be168c0dSopenharmony_ci+ if (handler_thread_ == nullptr) { 491be168c0dSopenharmony_ci+ handler_thread_ = std::make_unique<HandlerThread>(); 492be168c0dSopenharmony_ci+ handler_thread_->Run(); 493be168c0dSopenharmony_ci+ handler_ = handler_thread_->GetHandler(); 494be168c0dSopenharmony_ci+ } 495be168c0dSopenharmony_ci+} 496be168c0dSopenharmony_ci+ 497be168c0dSopenharmony_ci+HiAppEventConfig::~HiAppEventConfig() { 498be168c0dSopenharmony_ci+} 499be168c0dSopenharmony_ci+ 500be168c0dSopenharmony_ci+void HiAppEventConfig::Post(std::function<void()> &&f) { 501be168c0dSopenharmony_ci+ handler_->Post(std::move(f)); 502be168c0dSopenharmony_ci+} 503be168c0dSopenharmony_ci+ 504be168c0dSopenharmony_ci+void HiAppEventConfig::SetApiLanguage(const std::string &api_language) { 505be168c0dSopenharmony_ci+ api_language_ = api_language; 506be168c0dSopenharmony_ci+} 507be168c0dSopenharmony_ci+ 508be168c0dSopenharmony_ci+uint64_t HiAppEventConfig::GetTimeMs() { 509be168c0dSopenharmony_ci+ struct timespec ts = {0, 0}; 510be168c0dSopenharmony_ci+ if (clock_gettime(CLOCK_REALTIME, &ts) != 0) { 511be168c0dSopenharmony_ci+ return 0; 512be168c0dSopenharmony_ci+ } 513be168c0dSopenharmony_ci+ uint64_t ret_val = static_cast<uint64_t>(ts.tv_sec * 1000LL + ts.tv_nsec / 1000000); 514be168c0dSopenharmony_ci+ return ret_val; 515be168c0dSopenharmony_ci+} 516be168c0dSopenharmony_ci+} // namespace lite 517be168c0dSopenharmony_ci+} // namespace mindspore 518be168c0dSopenharmony_ci+#endif 519be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/common/hi_app_event/hi_app_event_config.h b/mindspore/lite/src/common/hi_app_event/hi_app_event_config.h 520be168c0dSopenharmony_cinew file mode 100644 521be168c0dSopenharmony_ciindex 00000000..57bee08d 522be168c0dSopenharmony_ci--- /dev/null 523be168c0dSopenharmony_ci+++ b/mindspore/lite/src/common/hi_app_event/hi_app_event_config.h 524be168c0dSopenharmony_ci@@ -0,0 +1,45 @@ 525be168c0dSopenharmony_ci+/** 526be168c0dSopenharmony_ci+ * Copyright 2024 Huawei Technologies Co., Ltd 527be168c0dSopenharmony_ci+ * 528be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License"); 529be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License. 530be168c0dSopenharmony_ci+ * You may obtain a copy of the License at 531be168c0dSopenharmony_ci+ * 532be168c0dSopenharmony_ci+ * http://www.apache.org/licenses/LICENSE-2.0 533be168c0dSopenharmony_ci+ * 534be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software 535be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS, 536be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 537be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and 538be168c0dSopenharmony_ci+ * limitations under the License. 539be168c0dSopenharmony_ci+ */ 540be168c0dSopenharmony_ci+ 541be168c0dSopenharmony_ci+#ifndef MINDSPORE_LITE_HI_APP_EVENT_CONFIG_H_ 542be168c0dSopenharmony_ci+#define MINDSPORE_LITE_HI_APP_EVENT_CONFIG_H_ 543be168c0dSopenharmony_ci+ 544be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 545be168c0dSopenharmony_ci+#include <string> 546be168c0dSopenharmony_ci+#include "handler_thread.h" 547be168c0dSopenharmony_ci+ 548be168c0dSopenharmony_ci+namespace mindspore { 549be168c0dSopenharmony_ci+namespace lite { 550be168c0dSopenharmony_ci+class HiAppEventConfig { 551be168c0dSopenharmony_ci+public: 552be168c0dSopenharmony_ci+ static HiAppEventConfig *GetInstance(); 553be168c0dSopenharmony_ci+ const std::string &GetApiLanguage() const { return api_language_; } 554be168c0dSopenharmony_ci+ void SetApiLanguage(const std::string &api_language); 555be168c0dSopenharmony_ci+ uint64_t GetTimeMs(); 556be168c0dSopenharmony_ci+ void Post(std::function<void()> &&f); 557be168c0dSopenharmony_ci+ 558be168c0dSopenharmony_ci+private: 559be168c0dSopenharmony_ci+ HiAppEventConfig(); 560be168c0dSopenharmony_ci+ ~HiAppEventConfig(); 561be168c0dSopenharmony_ci+ 562be168c0dSopenharmony_ci+ std::string api_language_ = "ts_api"; 563be168c0dSopenharmony_ci+ std::unique_ptr<HandlerThread> handler_thread_{nullptr}; 564be168c0dSopenharmony_ci+ std::shared_ptr<HandlerThread::Handler> handler_{nullptr}; 565be168c0dSopenharmony_ci+}; 566be168c0dSopenharmony_ci+} // namespace lite 567be168c0dSopenharmony_ci+} // namespace mindspore 568be168c0dSopenharmony_ci+#endif 569be168c0dSopenharmony_ci+#endif // MINDSPORE_LITE_HI_APP_EVENT_CONFIG_H_ 570be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/litert/c_api/model_c.cc b/mindspore/lite/src/litert/c_api/model_c.cc 571be168c0dSopenharmony_ciindex 20e1c227..cbbe2dbb 100644 572be168c0dSopenharmony_ci--- a/mindspore/lite/src/litert/c_api/model_c.cc 573be168c0dSopenharmony_ci+++ b/mindspore/lite/src/litert/c_api/model_c.cc 574be168c0dSopenharmony_ci@@ -23,6 +23,9 @@ 575be168c0dSopenharmony_ci #include "src/litert/cxx_api/tensor/tensor_impl.h" 576be168c0dSopenharmony_ci #include "src/litert/cxx_api/converters.h" 577be168c0dSopenharmony_ci #include "src/litert//cxx_api/model/model_impl.h" 578be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 579be168c0dSopenharmony_ci+#include "src/common/hi_app_event/hi_app_event_config.h" 580be168c0dSopenharmony_ci+#endif 581be168c0dSopenharmony_ci 582be168c0dSopenharmony_ci namespace mindspore { 583be168c0dSopenharmony_ci class ModelC { 584be168c0dSopenharmony_ci@@ -143,6 +146,9 @@ mindspore::MSKernelCallBack ModelC::TransCallBack(const OH_AI_KernelCallBack &oh 585be168c0dSopenharmony_ci } // namespace mindspore 586be168c0dSopenharmony_ci 587be168c0dSopenharmony_ci OH_AI_ModelHandle OH_AI_ModelCreate() { 588be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 589be168c0dSopenharmony_ci+ mindspore::lite::HiAppEventConfig::GetInstance()->SetApiLanguage("c_api"); 590be168c0dSopenharmony_ci+#endif 591be168c0dSopenharmony_ci auto impl = new (std::nothrow) mindspore::ModelC(); 592be168c0dSopenharmony_ci if (impl == nullptr) { 593be168c0dSopenharmony_ci MS_LOG(ERROR) << "Model implement is nullptr."; 594be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/litert/cxx_api/model/model.cc b/mindspore/lite/src/litert/cxx_api/model/model.cc 595be168c0dSopenharmony_ciindex 7bbaca5c..2814da41 100644 596be168c0dSopenharmony_ci--- a/mindspore/lite/src/litert/cxx_api/model/model.cc 597be168c0dSopenharmony_ci+++ b/mindspore/lite/src/litert/cxx_api/model/model.cc 598be168c0dSopenharmony_ci@@ -37,6 +37,10 @@ 599be168c0dSopenharmony_ci #include "src/common/decrypt.h" 600be168c0dSopenharmony_ci #include "src/common/file_utils.h" 601be168c0dSopenharmony_ci #endif 602be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 603be168c0dSopenharmony_ci+#include "src/common/hi_app_event/hi_app_event.h" 604be168c0dSopenharmony_ci+#include "src/common/hi_app_event/hi_app_event_config.h" 605be168c0dSopenharmony_ci+#endif 606be168c0dSopenharmony_ci 607be168c0dSopenharmony_ci namespace mindspore { 608be168c0dSopenharmony_ci #ifdef USE_GLOG 609be168c0dSopenharmony_ci@@ -124,6 +128,13 @@ Status Model::Build(const void *model_data, size_t data_size, ModelType model_ty 610be168c0dSopenharmony_ci 611be168c0dSopenharmony_ci Status Model::Build(const void *model_data, size_t data_size, ModelType model_type, 612be168c0dSopenharmony_ci const std::shared_ptr<Context> &model_context) { 613be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 614be168c0dSopenharmony_ci+ uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); 615be168c0dSopenharmony_ci+ std::string devices; 616be168c0dSopenharmony_ci+ for (auto device : model_context->MutableDeviceInfo()) { 617be168c0dSopenharmony_ci+ devices += std::to_string(device->GetDeviceType()) + " "; 618be168c0dSopenharmony_ci+ } 619be168c0dSopenharmony_ci+#endif 620be168c0dSopenharmony_ci if (impl_ == nullptr) { 621be168c0dSopenharmony_ci MS_LOG(ERROR) << "Model implement is null."; 622be168c0dSopenharmony_ci return kLiteNullptr; 623be168c0dSopenharmony_ci@@ -153,6 +164,12 @@ Status Model::Build(const void *model_data, size_t data_size, ModelType model_ty 624be168c0dSopenharmony_ci } 625be168c0dSopenharmony_ci #endif 626be168c0dSopenharmony_ci ret = impl_->Build(model_data, data_size, model_type, model_context); 627be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 628be168c0dSopenharmony_ci+ mindspore::lite::HiAppEventConfig::GetInstance()->Post([ret, begin_time, devices] { 629be168c0dSopenharmony_ci+ (void)mindspore::lite::HiAppEventWriteEndEvent(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelBuild", begin_time, 630be168c0dSopenharmony_ci+ devices); 631be168c0dSopenharmony_ci+ }); 632be168c0dSopenharmony_ci+#endif 633be168c0dSopenharmony_ci if (ret != kSuccess) { 634be168c0dSopenharmony_ci return ret; 635be168c0dSopenharmony_ci } 636be168c0dSopenharmony_ci@@ -230,6 +247,13 @@ Status Model::Build(const std::vector<char> &model_path, ModelType model_type, 637be168c0dSopenharmony_ci 638be168c0dSopenharmony_ci Status Model::Build(const std::vector<char> &model_path, ModelType model_type, 639be168c0dSopenharmony_ci const std::shared_ptr<Context> &model_context) { 640be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 641be168c0dSopenharmony_ci+ uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); 642be168c0dSopenharmony_ci+ std::string devices; 643be168c0dSopenharmony_ci+ for (auto device : model_context->MutableDeviceInfo()) { 644be168c0dSopenharmony_ci+ devices += std::to_string(device->GetDeviceType()) + " "; 645be168c0dSopenharmony_ci+ } 646be168c0dSopenharmony_ci+#endif 647be168c0dSopenharmony_ci if (impl_ == nullptr) { 648be168c0dSopenharmony_ci MS_LOG(ERROR) << "Model implement is null."; 649be168c0dSopenharmony_ci return kLiteNullptr; 650be168c0dSopenharmony_ci@@ -259,6 +283,12 @@ Status Model::Build(const std::vector<char> &model_path, ModelType model_type, 651be168c0dSopenharmony_ci } 652be168c0dSopenharmony_ci #endif 653be168c0dSopenharmony_ci ret = impl_->Build(CharToString(model_path), model_type, model_context); 654be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 655be168c0dSopenharmony_ci+ mindspore::lite::HiAppEventConfig::GetInstance()->Post([ret, begin_time, devices] { 656be168c0dSopenharmony_ci+ (void)mindspore::lite::HiAppEventWriteEndEvent(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelBuildFromFile", 657be168c0dSopenharmony_ci+ begin_time, devices); 658be168c0dSopenharmony_ci+ }); 659be168c0dSopenharmony_ci+#endif 660be168c0dSopenharmony_ci if (ret != kSuccess) { 661be168c0dSopenharmony_ci return ret; 662be168c0dSopenharmony_ci } 663be168c0dSopenharmony_ci@@ -267,6 +297,13 @@ Status Model::Build(const std::vector<char> &model_path, ModelType model_type, 664be168c0dSopenharmony_ci 665be168c0dSopenharmony_ci Status Model::Build(GraphCell graph, const std::shared_ptr<Context> &model_context, 666be168c0dSopenharmony_ci const std::shared_ptr<TrainCfg> &train_cfg) { 667be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 668be168c0dSopenharmony_ci+ uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); 669be168c0dSopenharmony_ci+ std::string devices; 670be168c0dSopenharmony_ci+ for (auto device : model_context->MutableDeviceInfo()) { 671be168c0dSopenharmony_ci+ devices += std::to_string(device->GetDeviceType()) + " "; 672be168c0dSopenharmony_ci+ } 673be168c0dSopenharmony_ci+#endif 674be168c0dSopenharmony_ci std::stringstream err_msg; 675be168c0dSopenharmony_ci if (impl_ == nullptr) { 676be168c0dSopenharmony_ci MS_LOG(ERROR) << "Model implement is null."; 677be168c0dSopenharmony_ci@@ -312,15 +349,31 @@ Status Model::Build(GraphCell graph, const std::shared_ptr<Context> &model_conte 678be168c0dSopenharmony_ci impl_->SetContext(model_context); 679be168c0dSopenharmony_ci impl_->SetGraph(graph.GetGraph()); 680be168c0dSopenharmony_ci impl_->SetConfig(train_cfg); 681be168c0dSopenharmony_ci- return impl_->Build(); 682be168c0dSopenharmony_ci+ auto ret = impl_->Build(); 683be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 684be168c0dSopenharmony_ci+ mindspore::lite::HiAppEventConfig::GetInstance()->Post([ret, begin_time, devices] { 685be168c0dSopenharmony_ci+ (void)mindspore::lite::HiAppEventWriteEndEvent(ret.StatusCode(), ret.StatusCode(), "OH_AI_TrainModelBuildFromFile", 686be168c0dSopenharmony_ci+ begin_time, devices); 687be168c0dSopenharmony_ci+ }); 688be168c0dSopenharmony_ci+#endif 689be168c0dSopenharmony_ci+ return ret; 690be168c0dSopenharmony_ci } 691be168c0dSopenharmony_ci 692be168c0dSopenharmony_ci Status Model::Resize(const std::vector<MSTensor> &inputs, const std::vector<std::vector<int64_t>> &dims) { 693be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 694be168c0dSopenharmony_ci+ uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); 695be168c0dSopenharmony_ci+#endif 696be168c0dSopenharmony_ci if (impl_ == nullptr) { 697be168c0dSopenharmony_ci MS_LOG(ERROR) << "Model implement is null."; 698be168c0dSopenharmony_ci return kLiteNullptr; 699be168c0dSopenharmony_ci } 700be168c0dSopenharmony_ci- return impl_->Resize(inputs, dims); 701be168c0dSopenharmony_ci+ auto ret = impl_->Resize(inputs, dims); 702be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 703be168c0dSopenharmony_ci+ mindspore::lite::HiAppEventConfig::GetInstance()->Post([ret, begin_time] { 704be168c0dSopenharmony_ci+ (void)mindspore::lite::HiAppEventWriteEndEvent(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelResize", begin_time); 705be168c0dSopenharmony_ci+ }); 706be168c0dSopenharmony_ci+#endif 707be168c0dSopenharmony_ci+ return ret; 708be168c0dSopenharmony_ci } 709be168c0dSopenharmony_ci 710be168c0dSopenharmony_ci Status Model::UpdateWeights(const std::vector<MSTensor> &new_weights) { 711be168c0dSopenharmony_ci@@ -332,22 +385,40 @@ Status Model::UpdateWeights(const std::vector<MSTensor> &new_weights) { 712be168c0dSopenharmony_ci } 713be168c0dSopenharmony_ci 714be168c0dSopenharmony_ci Status Model::RunStep(const MSKernelCallBack &before, const MSKernelCallBack &after) { 715be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 716be168c0dSopenharmony_ci+ uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); 717be168c0dSopenharmony_ci+#endif 718be168c0dSopenharmony_ci if (impl_ == nullptr) { 719be168c0dSopenharmony_ci MS_LOG(ERROR) << "Model implement is null."; 720be168c0dSopenharmony_ci return kLiteNullptr; 721be168c0dSopenharmony_ci } 722be168c0dSopenharmony_ci auto inputs = impl_->GetInputs(); 723be168c0dSopenharmony_ci auto outputs = impl_->GetOutputs(); 724be168c0dSopenharmony_ci- return impl_->Predict(inputs, &outputs, before, after); 725be168c0dSopenharmony_ci+ auto ret = impl_->Predict(inputs, &outputs, before, after); 726be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 727be168c0dSopenharmony_ci+ mindspore::lite::HiAppEventConfig::GetInstance()->Post([ret, begin_time] { 728be168c0dSopenharmony_ci+ (void)mindspore::lite::HiAppEventWriteEndEvent(ret.StatusCode(), ret.StatusCode(), "OH_AI_RunStep", begin_time); 729be168c0dSopenharmony_ci+ }); 730be168c0dSopenharmony_ci+#endif 731be168c0dSopenharmony_ci+ return ret; 732be168c0dSopenharmony_ci } 733be168c0dSopenharmony_ci 734be168c0dSopenharmony_ci Status Model::Predict(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs, 735be168c0dSopenharmony_ci const MSKernelCallBack &before, const MSKernelCallBack &after) { 736be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 737be168c0dSopenharmony_ci+ uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); 738be168c0dSopenharmony_ci+#endif 739be168c0dSopenharmony_ci if (impl_ == nullptr) { 740be168c0dSopenharmony_ci MS_LOG(ERROR) << "Model implement is null."; 741be168c0dSopenharmony_ci return kLiteNullptr; 742be168c0dSopenharmony_ci } 743be168c0dSopenharmony_ci- return impl_->Predict(inputs, outputs, before, after); 744be168c0dSopenharmony_ci+ auto ret = impl_->Predict(inputs, outputs, before, after); 745be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 746be168c0dSopenharmony_ci+ mindspore::lite::HiAppEventConfig::GetInstance()->Post([ret, begin_time] { 747be168c0dSopenharmony_ci+ mindspore::lite::HiAppEventWriteEndEvent(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelPredict", begin_time); 748be168c0dSopenharmony_ci+ }); 749be168c0dSopenharmony_ci+#endif 750be168c0dSopenharmony_ci+ return ret; 751be168c0dSopenharmony_ci } 752be168c0dSopenharmony_ci 753be168c0dSopenharmony_ci Status Model::Predict(const MSKernelCallBack &before, const MSKernelCallBack &after) { 754be168c0dSopenharmony_ci@@ -375,6 +446,10 @@ bool Model::HasPreprocess() { 755be168c0dSopenharmony_ci } 756be168c0dSopenharmony_ci 757be168c0dSopenharmony_ci Model::Model() { 758be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 759be168c0dSopenharmony_ci+ (void)mindspore::lite::HiAppEventAddProcessor(); 760be168c0dSopenharmony_ci+ uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); 761be168c0dSopenharmony_ci+#endif 762be168c0dSopenharmony_ci #ifdef USE_GLOG 763be168c0dSopenharmony_ci mindspore::mindspore_log_init(); 764be168c0dSopenharmony_ci #endif 765be168c0dSopenharmony_ci@@ -382,6 +457,11 @@ Model::Model() { 766be168c0dSopenharmony_ci if (impl_ == nullptr) { 767be168c0dSopenharmony_ci MS_LOG(ERROR) << "Failed to create ModelImpl"; 768be168c0dSopenharmony_ci } 769be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 770be168c0dSopenharmony_ci+ mindspore::lite::HiAppEventConfig::GetInstance()->Post([begin_time] { 771be168c0dSopenharmony_ci+ (void)mindspore::lite::HiAppEventWriteEndEvent(RET_OK, RET_OK, "OH_AI_ModelCreate", begin_time); 772be168c0dSopenharmony_ci+ }); 773be168c0dSopenharmony_ci+#endif 774be168c0dSopenharmony_ci } 775be168c0dSopenharmony_ci 776be168c0dSopenharmony_ci Model::~Model() {} 777be168c0dSopenharmony_ci@@ -489,11 +569,19 @@ Status Model::UpdateConfig(const std::vector<char> §ion, 778be168c0dSopenharmony_ci } 779be168c0dSopenharmony_ci 780be168c0dSopenharmony_ci Status Model::SetTrainMode(bool train) { 781be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 782be168c0dSopenharmony_ci+ uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); 783be168c0dSopenharmony_ci+#endif 784be168c0dSopenharmony_ci if ((impl_ == nullptr) || (impl_->session_ == nullptr)) { 785be168c0dSopenharmony_ci MS_LOG(ERROR) << "Model is null."; 786be168c0dSopenharmony_ci return kLiteUninitializedObj; 787be168c0dSopenharmony_ci } 788be168c0dSopenharmony_ci auto ret = (train) ? impl_->session_->Train() : impl_->session_->Eval(); 789be168c0dSopenharmony_ci+#ifdef ENABLE_HI_APP_EVENT 790be168c0dSopenharmony_ci+ mindspore::lite::HiAppEventConfig::GetInstance()->Post([begin_time] { 791be168c0dSopenharmony_ci+ (void)mindspore::lite::HiAppEventWriteEndEvent(RET_OK, RET_OK, "OH_AI_ModelSetTrainMode", begin_time); 792be168c0dSopenharmony_ci+ }); 793be168c0dSopenharmony_ci+#endif 794be168c0dSopenharmony_ci return (ret == mindspore::lite::RET_OK) ? kSuccess : kLiteError; 795be168c0dSopenharmony_ci } 796be168c0dSopenharmony_ci 797be168c0dSopenharmony_ci-- 798be168c0dSopenharmony_ci2.17.1 799be168c0dSopenharmony_ci 800