1From 3d19d093b845829f4fc8e9149dffe6a3a9d59971 Mon Sep 17 00:00:00 2001 2From: wqg <2593994958@qq.com> 3Date: Mon, 14 Oct 2024 20:40:09 +0800 4Subject: [PATCH] auto-apply 0043-fix-too-many-hi-app-event-reports.patch 5 6--- 7 mindspore/lite/src/common/BUILD.gn | 3 +- 8 .../src/common/hi_app_event/handler_thread.cc | 144 ----------------- 9 .../src/common/hi_app_event/handler_thread.h | 102 ------------ 10 .../src/common/hi_app_event/hi_app_event.cc | 88 +++++----- 11 .../src/common/hi_app_event/hi_app_event.h | 33 +++- 12 .../hi_app_event/hi_app_event_config.cc | 57 ------- 13 .../common/hi_app_event/hi_app_event_config.h | 45 ------ 14 .../hi_app_event/hi_app_event_thread.cc | 153 ++++++++++++++++++ 15 .../common/hi_app_event/hi_app_event_thread.h | 95 +++++++++++ 16 mindspore/lite/src/litert/c_api/model_c.cc | 5 +- 17 .../lite/src/litert/cxx_api/model/model.cc | 53 ++---- 18 11 files changed, 335 insertions(+), 443 deletions(-) 19 delete mode 100644 mindspore/lite/src/common/hi_app_event/handler_thread.cc 20 delete mode 100644 mindspore/lite/src/common/hi_app_event/handler_thread.h 21 delete mode 100644 mindspore/lite/src/common/hi_app_event/hi_app_event_config.cc 22 delete mode 100644 mindspore/lite/src/common/hi_app_event/hi_app_event_config.h 23 create mode 100644 mindspore/lite/src/common/hi_app_event/hi_app_event_thread.cc 24 create mode 100644 mindspore/lite/src/common/hi_app_event/hi_app_event_thread.h 25 26diff --git a/mindspore/lite/src/common/BUILD.gn b/mindspore/lite/src/common/BUILD.gn 27index e08e09ce..40445e38 100644 28--- a/mindspore/lite/src/common/BUILD.gn 29+++ b/mindspore/lite/src/common/BUILD.gn 30@@ -18,8 +18,7 @@ lite_src_common_mid_sources = [ 31 "string_util.cc", 32 "dynamic_library_loader.cc", 33 "hi_app_event/hi_app_event.cc", 34- "hi_app_event/hi_app_event_config.cc", 35- "hi_app_event/handler_thread.cc", 36+ "hi_app_event/hi_app_event_thread.cc", 37 ] 38 39 ohos_source_set("lite_common_mid_obj") { 40diff --git a/mindspore/lite/src/common/hi_app_event/handler_thread.cc b/mindspore/lite/src/common/hi_app_event/handler_thread.cc 41deleted file mode 100644 42index 1cf32bca..00000000 43--- a/mindspore/lite/src/common/hi_app_event/handler_thread.cc 44+++ /dev/null 45@@ -1,144 +0,0 @@ 46-/** 47- * Copyright 2024 Huawei Technologies Co., Ltd 48- * 49- * Licensed under the Apache License, Version 2.0 (the "License"); 50- * you may not use this file except in compliance with the License. 51- * You may obtain a copy of the License at 52- * 53- * http://www.apache.org/licenses/LICENSE-2.0 54- * 55- * Unless required by applicable law or agreed to in writing, software 56- * distributed under the License is distributed on an "AS IS" BASIS, 57- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 58- * See the License for the specific language governing permissions and 59- * limitations under the License. 60- */ 61- 62-#include "handler_thread.h" 63-#include "src/common/log.h" 64- 65-namespace mindspore { 66-namespace lite { 67-HandlerThread::Handler::Handler(HandlerThread& aLooper) 68- : assigned_looper_(aLooper) 69-{ } 70- 71-bool HandlerThread::Handler::Post(HandlerThread::Runnable && aRunnable) 72-{ 73- return assigned_looper_.Post("", std::move(aRunnable)); 74-} 75- 76-bool HandlerThread::Handler::Post(std::string key, HandlerThread::Runnable && aRunnable) 77-{ 78- return assigned_looper_.Post(key, std::move(aRunnable)); 79-} 80- 81-HandlerThread::HandlerThread() 82- : running_(false), 83- abort_requested_(false), 84- runnables_(), 85- runnables_mutex_(), 86- handler_(std::make_shared< Handler >(*this)) 87-{ } 88- 89-HandlerThread::~HandlerThread() 90-{ 91- AbortAndJoin(); 92-} 93- 94-bool HandlerThread::Running() const 95-{ 96- return running_.load(); 97-} 98- 99-bool HandlerThread::Run() 100-{ 101- thread_ = std::thread(&HandlerThread::RunFunc, this); 102- 103- return true; 104-} 105- 106-void HandlerThread::Stop() 107-{ 108- AbortAndJoin(); 109-} 110- 111-std::shared_ptr < HandlerThread::Handler > HandlerThread::GetHandler() 112-{ 113- return handler_; 114-} 115- 116-void HandlerThread::RunFunc() 117-{ 118- running_.store(true); 119- 120- (void)pthread_setname_np(pthread_self(), "OS_MSEvent"); 121- while (abort_requested_.load() == false) { 122- Runnable r = Next(); 123- if (r != nullptr) { 124- r(); 125- } else { 126- std::unique_lock<std::mutex> lock{mutex_}; 127- condition_.wait(lock); 128- } 129- } 130- 131- running_.store(false); 132-} 133- 134-void HandlerThread::AbortAndJoin() 135-{ 136- abort_requested_.store(true); 137- condition_.notify_one(); 138- if (thread_.joinable()) { 139- thread_.join(); 140- } 141-} 142- 143-HandlerThread::Runnable HandlerThread::Next() 144-{ 145- std::lock_guard guard(runnables_mutex_); 146- 147- if (runnables_.empty()) { 148- return nullptr; 149- } 150- std::shared_ptr<Message> msg = runnables_.front(); 151- Runnable runnable = msg->r; 152- runnables_.pop_front(); 153- return runnable; 154-} 155- 156-class IsSameKey { 157-public: 158- std::string key_; 159- 160- bool operator()(std::shared_ptr < HandlerThread::Message > obj) 161- { 162- return obj->key == key_; 163- } 164- 165- explicit IsSameKey(std::string key) { key_ = key; } 166-}; 167- 168-bool HandlerThread::Post(std::string key, Runnable && aRunnable) 169-{ 170- if (!Running()) { 171- MS_LOG(ERROR) << "Denying insertion, as the looper is not running."; 172- return false; 173- } 174- 175- std::lock_guard guard(runnables_mutex_); 176- if (!key.empty()) { 177- runnables_.remove_if(IsSameKey(key)); 178- } 179- auto msg = std::make_shared<Message>(); 180- msg->key = key; 181- msg->r = std::move(aRunnable); 182- runnables_.push_back(msg); 183- std::unique_lock<std::mutex> lock{mutex_}; 184- condition_.notify_one(); 185- 186- return true; 187-} 188-} // mindspore 189-} // lite 190\ No newline at end of file 191diff --git a/mindspore/lite/src/common/hi_app_event/handler_thread.h b/mindspore/lite/src/common/hi_app_event/handler_thread.h 192deleted file mode 100644 193index 91e5cc19..00000000 194--- a/mindspore/lite/src/common/hi_app_event/handler_thread.h 195+++ /dev/null 196@@ -1,102 +0,0 @@ 197-/** 198- * Copyright 2024 Huawei Technologies Co., Ltd 199- * 200- * Licensed under the Apache License, Version 2.0 (the "License"); 201- * you may not use this file except in compliance with the License. 202- * You may obtain a copy of the License at 203- * 204- * http://www.apache.org/licenses/LICENSE-2.0 205- * 206- * Unless required by applicable law or agreed to in writing, software 207- * distributed under the License is distributed on an "AS IS" BASIS, 208- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 209- * See the License for the specific language governing permissions and 210- * limitations under the License. 211- */ 212- 213-#ifndef LITE_HANDLER_THREAD_H 214-#define LITE_HANDLER_THREAD_H 215- 216-#include <list> 217-#include <thread> 218-#include <atomic> 219-#include <memory> 220-#include <functional> 221-#include <stdexcept> 222-#include <mutex> 223-#include <queue> 224-#include <iostream> 225-#include <string> 226-#include <condition_variable> 227- 228-namespace mindspore { 229-namespace lite { 230- 231-class HandlerThread { 232-public: 233- using Runnable = std::function<void()>; 234- 235- struct Message { 236- std::string key; 237- Runnable r; 238- }; 239- 240- class Handler { 241- friend class HandlerThread; // Allow the looper to access the private constructor. 242- 243- public: 244- bool Post(HandlerThread::Runnable &&aRunnable); 245- 246- /** 247- * 在该线程抛出一个任务,并会移除当前任务等待队列中相同key值的任务 248- * @param key 任务的唯一标志 249- * @param aRunnable 待执行的任务 250- * @return true成功,false失败 251- */ 252- bool Post(std::string key, HandlerThread::Runnable && aRunnable); 253- 254- public: // construction, since we want the looper to expose it's dispatcher exclusively! 255- explicit Handler(HandlerThread &aLooper); 256- 257- private: 258- HandlerThread &assigned_looper_; 259- }; 260- 261-public: 262- HandlerThread(); 263- 264- ~HandlerThread(); 265- 266- bool Running() const; 267- 268- bool Run(); 269- 270- void Stop(); 271- 272- std::shared_ptr<Handler> GetHandler(); 273- 274-private: 275- void RunFunc(); 276- 277- void AbortAndJoin(); 278- 279- Runnable Next(); 280- 281- bool Post(std::string key, HandlerThread::Runnable && aRunnable); 282- 283-private: 284- std::thread thread_; 285- std::atomic_bool running_; 286- std::atomic_bool abort_requested_; 287- 288- std::list<std::shared_ptr<Message>> runnables_; 289- std::recursive_mutex runnables_mutex_; 290- 291- std::shared_ptr<Handler> handler_; 292- 293- std::mutex mutex_; 294- std::condition_variable condition_; 295-}; 296-} // mindspore 297-} // lite 298-#endif // LITE_HANDLER_THREAD_H 299diff --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 300index ab46cde3..c2bfb4cd 100644 301--- a/mindspore/lite/src/common/hi_app_event/hi_app_event.cc 302+++ b/mindspore/lite/src/common/hi_app_event/hi_app_event.cc 303@@ -16,64 +16,54 @@ 304 305 #ifdef ENABLE_HI_APP_EVENT 306 #include "src/common/hi_app_event/hi_app_event.h" 307-#include "src/common/hi_app_event/hi_app_event_config.h" 308-#include "app_event.h" 309-#include "app_event_processor_mgr.h" 310 #include <time.h> 311 #include <cstdlib> 312+#include <map> 313+#include "app_event.h" 314+#include "app_event_processor_mgr.h" 315 316 namespace mindspore { 317 namespace lite { 318-namespace { 319-constexpr auto kName = "ha_app_event"; 320-constexpr auto kAppId = "com_huawei_hmos_sdk_ocg"; 321-constexpr int32_t kTimeOut = 90; 322-constexpr int32_t kCondRow = 30; 323-constexpr auto kDomain = "api_diagnostic"; 324-constexpr auto kEventName = "api_exec_end"; 325-constexpr auto kSdkName = "MindSporeLiteKit"; 326-constexpr int64_t kAppEventNotHapErrCode = -200; 327-static int64_t process_id = -1; 328+HiAppEvent& HiAppEvent::GetInstance() { 329+ static HiAppEvent instance; 330+ return instance; 331+} 332+ 333+void HiAppEvent::Init() { 334+ bool ret = event_thread_.Init(); 335+ if (!ret) { 336+ return; 337+ } 338+ is_ready_.store(true); 339+} 340+ 341+void HiAppEvent::Report(const int result, const int err_code, const std::string &api_name, 342+ const uint64_t begin_time, const std::string &devices) { 343+ { 344+ std::lock_guard<std::mutex> init_guard_lock(init_mutex_); 345+ if (!is_ready_.load()) { 346+ Init(); 347+ } 348+ } 349+ uint64_t end_time = GetTimeMs(); 350+ event_thread_.Submit(result, err_code, api_name, begin_time, end_time, devices); 351+} 352+ 353+uint64_t HiAppEvent::GetTimeMs() { 354+ struct timespec ts = {0, 0}; 355+ if (clock_gettime(CLOCK_REALTIME, &ts) != 0) { 356+ return 0; 357+ } 358+ uint64_t ret_val = static_cast<uint64_t>(ts.tv_sec * 1000LL + ts.tv_nsec / 1000000); 359+ return ret_val; 360 } 361 362-int64_t HiAppEventAddProcessor() { 363- std::srand(std::time(NULL)); 364- OHOS::HiviewDFX::HiAppEvent::ReportConfig config; 365- config.name = kName; 366- config.appId = kAppId; 367- config.routeInfo = "AUTO"; 368- config.triggerCond.timeout = kTimeOut; 369- config.triggerCond.row = kCondRow; 370- config.eventConfigs.clear(); 371- { 372- OHOS::HiviewDFX::HiAppEvent::EventConfig event; 373- event.domain = kDomain; 374- event.name = kEventName; 375- event.isRealTime = false; 376- config.eventConfigs.push_back(event); 377- } 378- if (process_id == -1) { 379- process_id = OHOS::HiviewDFX::HiAppEvent::AppEventProcessorMgr::AddProcessor(config); 380- } 381- return process_id; 382+std::string HiAppEvent::GetApiType() const { 383+ return api_type_; 384 } 385 386-void HiAppEventWriteEndEvent(const int result, const int err_code, const std::string &api_name, 387- const uint64_t begin_time, const std::string &devices) { 388- if (process_id == kAppEventNotHapErrCode) { 389- return; 390- } 391- OHOS::HiviewDFX::HiAppEvent::Event event(kDomain, kEventName, OHOS::HiviewDFX::HiAppEvent::BEHAVIOR); 392- event.AddParam("trans_id", "transId_" + std::to_string(std::rand() + HiAppEventConfig::GetInstance()->GetTimeMs())); 393- event.AddParam("api_name", api_name); 394- event.AddParam("sdk_name", std::string(kSdkName)); 395- event.AddParam("begin_time", static_cast<int64_t>(begin_time)); 396- event.AddParam("end_time", static_cast<int64_t>(HiAppEventConfig::GetInstance()->GetTimeMs())); 397- event.AddParam("result", result); 398- event.AddParam("error_code", std::to_string(err_code)); 399- event.AddParam("context_devices", devices); 400- event.AddParam("api_language", HiAppEventConfig::GetInstance()->GetApiLanguage()); 401- OHOS::HiviewDFX::HiAppEvent::Write(event); 402+void HiAppEvent::SetApiType(const std::string &api_type){ 403+ api_type_ = api_type; 404 } 405 } // namespace lite 406 } // namespace mindspore 407diff --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 408index a5de1ad3..017a4912 100644 409--- a/mindspore/lite/src/common/hi_app_event/hi_app_event.h 410+++ b/mindspore/lite/src/common/hi_app_event/hi_app_event.h 411@@ -18,13 +18,40 @@ 412 #define MINDSPORE_LITE_HI_APP_EVENT_H_ 413 414 #ifdef ENABLE_HI_APP_EVENT 415+#include "hi_app_event_thread.h" 416 #include <string> 417+#include <atomic> 418+#include <memory> 419+#include <mutex> 420 421 namespace mindspore { 422 namespace lite { 423-int64_t HiAppEventAddProcessor(); 424-void HiAppEventWriteEndEvent(const int result, const int err_code, const std::string &api_name, 425- const uint64_t begin_time, const std::string &devices = "None"); 426+class HiAppEvent { 427+public: 428+ static HiAppEvent& GetInstance(); 429+ 430+ HiAppEvent(const HiAppEvent &) = delete; 431+ HiAppEvent & operator=(const HiAppEvent &) = delete; 432+ 433+ void Report(const int result, const int err_code, const std::string &api_name, 434+ const uint64_t begin_time, const std::string &devices = "None"); 435+ 436+ static uint64_t GetTimeMs(); 437+ 438+ std::string GetApiType() const; 439+ 440+ void SetApiType(const std::string &api_type); 441+ 442+private: 443+ void Init(); 444+ HiAppEvent() = default; 445+ ~HiAppEvent() = default; 446+ 447+ HiAppEventThread event_thread_; 448+ std::string api_type_ = "ts_api"; 449+ std::atomic_bool is_ready_ = false; 450+ std::mutex init_mutex_; 451+}; 452 } // namespace lite 453 } // namespace mindspore 454 #endif 455diff --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 456deleted file mode 100644 457index c488cd86..00000000 458--- a/mindspore/lite/src/common/hi_app_event/hi_app_event_config.cc 459+++ /dev/null 460@@ -1,57 +0,0 @@ 461-/** 462- * Copyright 2024 Huawei Technologies Co., Ltd 463- * 464- * Licensed under the Apache License, Version 2.0 (the "License"); 465- * you may not use this file except in compliance with the License. 466- * You may obtain a copy of the License at 467- * 468- * http://www.apache.org/licenses/LICENSE-2.0 469- * 470- * Unless required by applicable law or agreed to in writing, software 471- * distributed under the License is distributed on an "AS IS" BASIS, 472- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 473- * See the License for the specific language governing permissions and 474- * limitations under the License. 475- */ 476- 477-#ifdef ENABLE_HI_APP_EVENT 478-#include "src/common/hi_app_event/hi_app_event_config.h" 479-#include <ctime> 480- 481-namespace mindspore { 482-namespace lite { 483-HiAppEventConfig *HiAppEventConfig::GetInstance() { 484- static HiAppEventConfig instance; 485- return &instance; 486-} 487- 488-HiAppEventConfig::HiAppEventConfig() { 489- if (handler_thread_ == nullptr) { 490- handler_thread_ = std::make_unique<HandlerThread>(); 491- handler_thread_->Run(); 492- handler_ = handler_thread_->GetHandler(); 493- } 494-} 495- 496-HiAppEventConfig::~HiAppEventConfig() { 497-} 498- 499-void HiAppEventConfig::Post(std::function<void()> &&f) { 500- handler_->Post(std::move(f)); 501-} 502- 503-void HiAppEventConfig::SetApiLanguage(const std::string &api_language) { 504- api_language_ = api_language; 505-} 506- 507-uint64_t HiAppEventConfig::GetTimeMs() { 508- struct timespec ts = {0, 0}; 509- if (clock_gettime(CLOCK_REALTIME, &ts) != 0) { 510- return 0; 511- } 512- uint64_t ret_val = static_cast<uint64_t>(ts.tv_sec * 1000LL + ts.tv_nsec / 1000000); 513- return ret_val; 514-} 515-} // namespace lite 516-} // namespace mindspore 517-#endif 518diff --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 519deleted file mode 100644 520index 57bee08d..00000000 521--- a/mindspore/lite/src/common/hi_app_event/hi_app_event_config.h 522+++ /dev/null 523@@ -1,45 +0,0 @@ 524-/** 525- * Copyright 2024 Huawei Technologies Co., Ltd 526- * 527- * Licensed under the Apache License, Version 2.0 (the "License"); 528- * you may not use this file except in compliance with the License. 529- * You may obtain a copy of the License at 530- * 531- * http://www.apache.org/licenses/LICENSE-2.0 532- * 533- * Unless required by applicable law or agreed to in writing, software 534- * distributed under the License is distributed on an "AS IS" BASIS, 535- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 536- * See the License for the specific language governing permissions and 537- * limitations under the License. 538- */ 539- 540-#ifndef MINDSPORE_LITE_HI_APP_EVENT_CONFIG_H_ 541-#define MINDSPORE_LITE_HI_APP_EVENT_CONFIG_H_ 542- 543-#ifdef ENABLE_HI_APP_EVENT 544-#include <string> 545-#include "handler_thread.h" 546- 547-namespace mindspore { 548-namespace lite { 549-class HiAppEventConfig { 550-public: 551- static HiAppEventConfig *GetInstance(); 552- const std::string &GetApiLanguage() const { return api_language_; } 553- void SetApiLanguage(const std::string &api_language); 554- uint64_t GetTimeMs(); 555- void Post(std::function<void()> &&f); 556- 557-private: 558- HiAppEventConfig(); 559- ~HiAppEventConfig(); 560- 561- std::string api_language_ = "ts_api"; 562- std::unique_ptr<HandlerThread> handler_thread_{nullptr}; 563- std::shared_ptr<HandlerThread::Handler> handler_{nullptr}; 564-}; 565-} // namespace lite 566-} // namespace mindspore 567-#endif 568-#endif // MINDSPORE_LITE_HI_APP_EVENT_CONFIG_H_ 569diff --git a/mindspore/lite/src/common/hi_app_event/hi_app_event_thread.cc b/mindspore/lite/src/common/hi_app_event/hi_app_event_thread.cc 570new file mode 100644 571index 00000000..8118d36a 572--- /dev/null 573+++ b/mindspore/lite/src/common/hi_app_event/hi_app_event_thread.cc 574@@ -0,0 +1,153 @@ 575+/** 576+ * Copyright 2024 Huawei Technologies Co., Ltd 577+ * 578+ * Licensed under the Apache License, Version 2.0 (the "License"); 579+ * you may not use this file except in compliance with the License. 580+ * You may obtain a copy of the License at 581+ * 582+ * http://www.apache.org/licenses/LICENSE-2.0 583+ * 584+ * Unless required by applicable law or agreed to in writing, software 585+ * distributed under the License is distributed on an "AS IS" BASIS, 586+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 587+ * See the License for the specific language governing permissions and 588+ * limitations under the License. 589+ */ 590+ 591+#include "hi_app_event_thread.h" 592+#include <time.h> 593+#include <cstdlib> 594+#include "src/common/log.h" 595+#include "app_event.h" 596+#include "app_event_processor_mgr.h" 597+ 598+namespace mindspore { 599+namespace lite { 600+HiAppEventThread::HiAppEventThread() 601+: running_(false), event_map_() 602+{ } 603+ 604+HiAppEventThread::~HiAppEventThread() { 605+ AbortAndJoin(); 606+} 607+ 608+bool HiAppEventThread::Running() const { 609+ return running_.load(); 610+} 611+ 612+bool HiAppEventThread::Init() { 613+ int ret = HiAppEventAddProcessor(); 614+ if (ret == -1) { 615+ return false; 616+ } 617+ running_.store(true); 618+ thread_ = std::thread(&HiAppEventThread::RunFunc, this); 619+ return true; 620+} 621+ 622+void HiAppEventThread::WriteHiAppEvent() { 623+ // when there is no sandbox environment for hap, for example, xts, skip writing app events. 624+ if (kProcessID == kAppEventNotHapErrCode) { 625+ return; 626+ } 627+ for (auto &e : event_map_) { 628+ OHOS::HiviewDFX::HiAppEvent::Event event(kDomain, kEventName, OHOS::HiviewDFX::HiAppEvent::BEHAVIOR); 629+ std::string api_name = e.first; 630+ auto e_info = e.second; 631+ event.AddParam("api_name", api_name); 632+ event.AddParam("sdk_name", std::string(kSdkName)); 633+ event.AddParam("begin_time", static_cast<int64_t>(e_info->begin_time)); 634+ event.AddParam("success_times", e_info->success_times); 635+ event.AddParam("call_times", e_info->call_times); 636+ event.AddParam("min_cost_time", e_info->min_cost_time); 637+ event.AddParam("max_cost_time", e_info->max_cost_time); 638+ event.AddParam("total_cost_time", e_info->total_cost_time); 639+ OHOS::HiviewDFX::HiAppEvent::Write(event); 640+ } 641+ event_map_.clear(); 642+} 643+ 644+int64_t HiAppEventThread::HiAppEventAddProcessor() { 645+ std::srand(std::time(NULL)); 646+ OHOS::HiviewDFX::HiAppEvent::ReportConfig config; 647+ config.name = kName; 648+ config.appId = kAppId; 649+ config.routeInfo = "AUTO"; 650+ config.triggerCond.timeout = kTimeOut; 651+ config.triggerCond.row = kCondRow; 652+ config.eventConfigs.clear(); 653+ {// not allow to modify 654+ OHOS::HiviewDFX::HiAppEvent::EventConfig event1; 655+ event1.domain = "api_diagnostic"; 656+ event1.name = "api_exec_end"; 657+ event1.isRealTime = false; 658+ config.eventConfigs.push_back(event1); 659+ } 660+ { // not allow to modify 661+ OHOS::HiviewDFX::HiAppEvent::EventConfig event2; 662+ event2.domain = "api_diagnostic"; 663+ event2.name = "api_called_stat"; 664+ event2.isRealTime = true; 665+ config.eventConfigs.push_back(event2); 666+ } 667+ { // not allow to modify 668+ OHOS::HiviewDFX::HiAppEvent::EventConfig event3; 669+ event3.domain = "api_diagnostic"; 670+ event3.name = "api_called_stat_cnt"; 671+ event3.isRealTime = true; 672+ config.eventConfigs.push_back(event3); 673+ } 674+ if (kProcessID == -1) { 675+ kProcessID = OHOS::HiviewDFX::HiAppEvent::AppEventProcessorMgr::AddProcessor(config); 676+ } 677+ return kProcessID; 678+} 679+ 680+void HiAppEventThread::RunFunc() { 681+ (void) pthread_setname_np(pthread_self(), "OS_MSEvent"); 682+ while (running_.load()) { 683+ std::unique_lock<std::mutex> lock(mutex_); 684+ auto status = condition_.wait_for(lock, std::chrono::seconds(kWaitTime)); 685+ 686+ if (!running_.load() || status == std::cv_status::timeout) { 687+ // write all events and clear event_map 688+ WriteHiAppEvent(); 689+ } 690+ } 691+ running_.store(false); 692+} 693+ 694+void HiAppEventThread::AbortAndJoin() { 695+ running_.store(false); 696+ condition_.notify_one(); 697+ if (thread_.joinable()) { 698+ thread_.join(); 699+ } 700+} 701+ 702+void HiAppEventThread::Submit(const int result, const int err_code, const std::string &api_name, 703+ const uint64_t begin_time, const uint64_t end_time, const std::string &devices) { 704+ // add and merge the data by api_name 705+ std::lock_guard<std::mutex> lock(mutex_); 706+ int64_t cost_time = end_time - begin_time; 707+ auto iter = event_map_.find(api_name); 708+ if (iter != event_map_.end()) { 709+ std::shared_ptr<EventInfo> event_info = iter->second; 710+ event_info->call_times++; 711+ event_info->success_times += (result == RET_OK); 712+ event_info->total_cost_time += cost_time; 713+ event_info->min_cost_time = std::min(event_info->min_cost_time, cost_time); 714+ event_info->max_cost_time = std::max(event_info->max_cost_time, cost_time); 715+ return; 716+ } 717+ std::shared_ptr<EventInfo> event_info = std::make_shared<EventInfo>(); 718+ event_info->begin_time = begin_time; 719+ event_info->call_times = 1; 720+ event_info->success_times = (result == RET_OK); 721+ event_info->total_cost_time = cost_time; 722+ event_info->min_cost_time = cost_time; 723+ event_info->max_cost_time = cost_time; 724+ event_map_.emplace(api_name, event_info); 725+} 726+} // mindspore 727+} // lite 728\ No newline at end of file 729diff --git a/mindspore/lite/src/common/hi_app_event/hi_app_event_thread.h b/mindspore/lite/src/common/hi_app_event/hi_app_event_thread.h 730new file mode 100644 731index 00000000..e48e335b 732--- /dev/null 733+++ b/mindspore/lite/src/common/hi_app_event/hi_app_event_thread.h 734@@ -0,0 +1,95 @@ 735+/** 736+ * Copyright 2024 Huawei Technologies Co., Ltd 737+ * 738+ * Licensed under the Apache License, Version 2.0 (the "License"); 739+ * you may not use this file except in compliance with the License. 740+ * You may obtain a copy of the License at 741+ * 742+ * http://www.apache.org/licenses/LICENSE-2.0 743+ * 744+ * Unless required by applicable law or agreed to in writing, software 745+ * distributed under the License is distributed on an "AS IS" BASIS, 746+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 747+ * See the License for the specific language governing permissions and 748+ * limitations under the License. 749+ */ 750+ 751+#ifndef LITE_HANDLER_THREAD_H 752+#define LITE_HANDLER_THREAD_H 753+ 754+#include <list> 755+#include <thread> 756+#include <atomic> 757+#include <memory> 758+#include <functional> 759+#include <stdexcept> 760+#include <mutex> 761+#include <iostream> 762+#include <string> 763+#include <condition_variable> 764+#include <map> 765+#include <cstdint> 766+ 767+namespace mindspore { 768+namespace lite { 769+namespace { 770+ constexpr auto kName = "ha_app_event"; 771+ constexpr auto kAppId = "com_huawei_hmos_sdk_ocg"; 772+ constexpr int32_t kTimeOut = 90; 773+ constexpr int32_t kCondRow = 30; 774+ constexpr auto kDomain = "api_diagnostic"; 775+ constexpr auto kEventName = "api_called_stat"; 776+ constexpr auto kSdkName = "MindSporeLiteKit"; 777+ constexpr int64_t kAppEventNotHapErrCode = -200; 778+ constexpr int32_t kWaitTime = 60; 779+ static int64_t kProcessID = -1; 780+ constexpr int32_t RET_OK = 0; 781+ 782+ struct EventInfo { 783+ int64_t begin_time; 784+ int64_t call_times; 785+ int64_t success_times; 786+ int64_t min_cost_time; 787+ int64_t max_cost_time; 788+ int64_t total_cost_time; 789+ EventInfo() : begin_time(0), call_times(0), success_times(0), min_cost_time(0), max_cost_time(0), 790+ total_cost_time(0) {} 791+ }; 792+} 793+ 794+class HiAppEventThread { 795+public: 796+ HiAppEventThread(); 797+ ~HiAppEventThread(); 798+ 799+ HiAppEventThread(const HiAppEventThread &) = delete; 800+ HiAppEventThread &operator()(const HiAppEventThread &) = delete; 801+ 802+ bool Init(); 803+ 804+ void Submit(const int result, const int err_code, const std::string &api_name, 805+ const uint64_t begin_time, const uint64_t end_time, const std::string &devices); 806+ 807+ void WriteHiAppEvent(); 808+ 809+ int64_t HiAppEventAddProcessor(); 810+ 811+private: 812+ 813+ bool Running() const; 814+ 815+ void RunFunc(); 816+ 817+ void AbortAndJoin(); 818+ 819+private: 820+ std::thread thread_; 821+ std::mutex mutex_; 822+ std::condition_variable condition_; 823+ 824+ std::atomic_bool running_; 825+ std::map<std::string, std::shared_ptr<EventInfo>> event_map_; 826+}; 827+} // mindspore 828+} // lite 829+#endif // LITE_HANDLER_THREAD_H 830diff --git a/mindspore/lite/src/litert/c_api/model_c.cc b/mindspore/lite/src/litert/c_api/model_c.cc 831index d8632338..8068cd8c 100644 832--- a/mindspore/lite/src/litert/c_api/model_c.cc 833+++ b/mindspore/lite/src/litert/c_api/model_c.cc 834@@ -25,7 +25,7 @@ 835 #include "src/litert/cxx_api/converters.h" 836 #include "src/litert//cxx_api/model/model_impl.h" 837 #ifdef ENABLE_HI_APP_EVENT 838-#include "src/common/hi_app_event/hi_app_event_config.h" 839+#include "src/common/hi_app_event/hi_app_event.h" 840 #endif 841 842 namespace mindspore { 843@@ -147,9 +147,6 @@ mindspore::MSKernelCallBack ModelC::TransCallBack(const OH_AI_KernelCallBack &oh 844 } // namespace mindspore 845 846 OH_AI_ModelHandle OH_AI_ModelCreate() { 847-#ifdef ENABLE_HI_APP_EVENT 848- mindspore::lite::HiAppEventConfig::GetInstance()->SetApiLanguage("c_api"); 849-#endif 850 auto impl = new (std::nothrow) mindspore::ModelC(); 851 if (impl == nullptr) { 852 MS_LOG(ERROR) << "Model implement is nullptr."; 853diff --git a/mindspore/lite/src/litert/cxx_api/model/model.cc b/mindspore/lite/src/litert/cxx_api/model/model.cc 854index 282c752e..46ee88f7 100644 855--- a/mindspore/lite/src/litert/cxx_api/model/model.cc 856+++ b/mindspore/lite/src/litert/cxx_api/model/model.cc 857@@ -39,7 +39,6 @@ 858 #endif 859 #ifdef ENABLE_HI_APP_EVENT 860 #include "src/common/hi_app_event/hi_app_event.h" 861-#include "src/common/hi_app_event/hi_app_event_config.h" 862 #endif 863 864 namespace mindspore { 865@@ -129,7 +128,7 @@ Status Model::Build(const void *model_data, size_t data_size, ModelType model_ty 866 Status Model::Build(const void *model_data, size_t data_size, ModelType model_type, 867 const std::shared_ptr<Context> &model_context) { 868 #ifdef ENABLE_HI_APP_EVENT 869- uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); 870+ uint64_t begin_time = mindspore::lite::HiAppEvent::GetTimeMs(); 871 std::string devices; 872 for (auto device : model_context->MutableDeviceInfo()) { 873 devices += std::to_string(device->GetDeviceType()) + " "; 874@@ -165,10 +164,7 @@ Status Model::Build(const void *model_data, size_t data_size, ModelType model_ty 875 #endif 876 ret = impl_->Build(model_data, data_size, model_type, model_context); 877 #ifdef ENABLE_HI_APP_EVENT 878- mindspore::lite::HiAppEventConfig::GetInstance()->Post([ret, begin_time, devices] { 879- (void)mindspore::lite::HiAppEventWriteEndEvent(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelBuild", begin_time, 880- devices); 881- }); 882+ mindspore::lite::HiAppEvent::GetInstance().Report(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelBuild", begin_time, devices); 883 #endif 884 if (ret != kSuccess) { 885 return ret; 886@@ -248,7 +244,7 @@ Status Model::Build(const std::vector<char> &model_path, ModelType model_type, 887 Status Model::Build(const std::vector<char> &model_path, ModelType model_type, 888 const std::shared_ptr<Context> &model_context) { 889 #ifdef ENABLE_HI_APP_EVENT 890- uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); 891+ uint64_t begin_time = mindspore::lite::HiAppEvent::GetTimeMs(); 892 std::string devices; 893 for (auto device : model_context->MutableDeviceInfo()) { 894 devices += std::to_string(device->GetDeviceType()) + " "; 895@@ -284,10 +280,7 @@ Status Model::Build(const std::vector<char> &model_path, ModelType model_type, 896 #endif 897 ret = impl_->Build(CharToString(model_path), model_type, model_context); 898 #ifdef ENABLE_HI_APP_EVENT 899- mindspore::lite::HiAppEventConfig::GetInstance()->Post([ret, begin_time, devices] { 900- (void)mindspore::lite::HiAppEventWriteEndEvent(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelBuildFromFile", 901- begin_time, devices); 902- }); 903+ mindspore::lite::HiAppEvent::GetInstance().Report(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelBuildFromFile", begin_time, devices); 904 #endif 905 if (ret != kSuccess) { 906 return ret; 907@@ -304,7 +297,7 @@ if (model_context == nullptr) { 908 return Status(kLiteNullptr, err_msg.str()); 909 } 910 #ifdef ENABLE_HI_APP_EVENT 911- uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); 912+ uint64_t begin_time = mindspore::lite::HiAppEvent::GetTimeMs(); 913 std::string devices; 914 for (auto device : model_context->MutableDeviceInfo()) { 915 devices += std::to_string(device->GetDeviceType()) + " "; 916@@ -351,17 +344,14 @@ if (model_context == nullptr) { 917 impl_->SetConfig(train_cfg); 918 auto ret = impl_->Build(); 919 #ifdef ENABLE_HI_APP_EVENT 920- mindspore::lite::HiAppEventConfig::GetInstance()->Post([ret, begin_time, devices] { 921- (void)mindspore::lite::HiAppEventWriteEndEvent(ret.StatusCode(), ret.StatusCode(), "OH_AI_TrainModelBuildFromFile", 922- begin_time, devices); 923- }); 924+ mindspore::lite::HiAppEvent::GetInstance().Report(ret.StatusCode(), ret.StatusCode(), "OH_AI_TrainModelBuildFromFile", begin_time, devices); 925 #endif 926 return ret; 927 } 928 929 Status Model::Resize(const std::vector<MSTensor> &inputs, const std::vector<std::vector<int64_t>> &dims) { 930 #ifdef ENABLE_HI_APP_EVENT 931- uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); 932+ uint64_t begin_time = mindspore::lite::HiAppEvent::GetTimeMs(); 933 #endif 934 if (impl_ == nullptr) { 935 MS_LOG(ERROR) << "Model implement is null."; 936@@ -369,9 +359,7 @@ Status Model::Resize(const std::vector<MSTensor> &inputs, const std::vector<std: 937 } 938 auto ret = impl_->Resize(inputs, dims); 939 #ifdef ENABLE_HI_APP_EVENT 940- mindspore::lite::HiAppEventConfig::GetInstance()->Post([ret, begin_time] { 941- (void)mindspore::lite::HiAppEventWriteEndEvent(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelResize", begin_time); 942- }); 943+ mindspore::lite::HiAppEvent::GetInstance().Report(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelResize", begin_time); 944 #endif 945 return ret; 946 } 947@@ -386,7 +374,7 @@ Status Model::UpdateWeights(const std::vector<MSTensor> &new_weights) { 948 949 Status Model::RunStep(const MSKernelCallBack &before, const MSKernelCallBack &after) { 950 #ifdef ENABLE_HI_APP_EVENT 951- uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); 952+ uint64_t begin_time = mindspore::lite::HiAppEvent::GetTimeMs(); 953 #endif 954 if (impl_ == nullptr) { 955 MS_LOG(ERROR) << "Model implement is null."; 956@@ -396,9 +384,7 @@ Status Model::RunStep(const MSKernelCallBack &before, const MSKernelCallBack &af 957 auto outputs = impl_->GetOutputs(); 958 auto ret = impl_->Predict(inputs, &outputs, before, after); 959 #ifdef ENABLE_HI_APP_EVENT 960- mindspore::lite::HiAppEventConfig::GetInstance()->Post([ret, begin_time] { 961- (void)mindspore::lite::HiAppEventWriteEndEvent(ret.StatusCode(), ret.StatusCode(), "OH_AI_RunStep", begin_time); 962- }); 963+ mindspore::lite::HiAppEvent::GetInstance().Report(ret.StatusCode(), ret.StatusCode(), "OH_AI_RunStep", begin_time); 964 #endif 965 return ret; 966 } 967@@ -406,7 +392,7 @@ Status Model::RunStep(const MSKernelCallBack &before, const MSKernelCallBack &af 968 Status Model::Predict(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs, 969 const MSKernelCallBack &before, const MSKernelCallBack &after) { 970 #ifdef ENABLE_HI_APP_EVENT 971- uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); 972+ uint64_t begin_time = mindspore::lite::HiAppEvent::GetTimeMs(); 973 #endif 974 if (impl_ == nullptr) { 975 MS_LOG(ERROR) << "Model implement is null."; 976@@ -414,9 +400,7 @@ Status Model::Predict(const std::vector<MSTensor> &inputs, std::vector<MSTensor> 977 } 978 auto ret = impl_->Predict(inputs, outputs, before, after); 979 #ifdef ENABLE_HI_APP_EVENT 980- mindspore::lite::HiAppEventConfig::GetInstance()->Post([ret, begin_time] { 981- mindspore::lite::HiAppEventWriteEndEvent(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelPredict", begin_time); 982- }); 983+ mindspore::lite::HiAppEvent::GetInstance().Report(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelPredict", begin_time); 984 #endif 985 return ret; 986 } 987@@ -447,8 +431,7 @@ bool Model::HasPreprocess() { 988 989 Model::Model() { 990 #ifdef ENABLE_HI_APP_EVENT 991- (void)mindspore::lite::HiAppEventAddProcessor(); 992- uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); 993+ uint64_t begin_time = mindspore::lite::HiAppEvent::GetTimeMs(); 994 #endif 995 #ifdef USE_GLOG 996 mindspore::mindspore_log_init(); 997@@ -458,9 +441,7 @@ Model::Model() { 998 MS_LOG(ERROR) << "Failed to create ModelImpl"; 999 } 1000 #ifdef ENABLE_HI_APP_EVENT 1001- mindspore::lite::HiAppEventConfig::GetInstance()->Post([begin_time] { 1002- (void)mindspore::lite::HiAppEventWriteEndEvent(RET_OK, RET_OK, "OH_AI_ModelCreate", begin_time); 1003- }); 1004+ mindspore::lite::HiAppEvent::GetInstance().Report(RET_OK, RET_OK, "OH_AI_ModelCreate", begin_time); 1005 #endif 1006 } 1007 1008@@ -570,7 +551,7 @@ Status Model::UpdateConfig(const std::vector<char> §ion, 1009 1010 Status Model::SetTrainMode(bool train) { 1011 #ifdef ENABLE_HI_APP_EVENT 1012- uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); 1013+ uint64_t begin_time = mindspore::lite::HiAppEvent::GetTimeMs(); 1014 #endif 1015 if ((impl_ == nullptr) || (impl_->session_ == nullptr)) { 1016 MS_LOG(ERROR) << "Model is null."; 1017@@ -578,9 +559,7 @@ Status Model::SetTrainMode(bool train) { 1018 } 1019 auto ret = (train) ? impl_->session_->Train() : impl_->session_->Eval(); 1020 #ifdef ENABLE_HI_APP_EVENT 1021- mindspore::lite::HiAppEventConfig::GetInstance()->Post([begin_time] { 1022- (void)mindspore::lite::HiAppEventWriteEndEvent(RET_OK, RET_OK, "OH_AI_ModelSetTrainMode", begin_time); 1023- }); 1024+ mindspore::lite::HiAppEvent::GetInstance().Report(RET_OK, RET_OK, "OH_AI_ModelSetTrainMode", begin_time); 1025 #endif 1026 return (ret == mindspore::lite::RET_OK) ? kSuccess : kLiteError; 1027 } 1028-- 10292.34.1 1030 1031