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