From 3d19d093b845829f4fc8e9149dffe6a3a9d59971 Mon Sep 17 00:00:00 2001 From: wqg <2593994958@qq.com> Date: Mon, 14 Oct 2024 20:40:09 +0800 Subject: [PATCH] auto-apply 0043-fix-too-many-hi-app-event-reports.patch --- mindspore/lite/src/common/BUILD.gn | 3 +- .../src/common/hi_app_event/handler_thread.cc | 144 ----------------- .../src/common/hi_app_event/handler_thread.h | 102 ------------ .../src/common/hi_app_event/hi_app_event.cc | 88 +++++----- .../src/common/hi_app_event/hi_app_event.h | 33 +++- .../hi_app_event/hi_app_event_config.cc | 57 ------- .../common/hi_app_event/hi_app_event_config.h | 45 ------ .../hi_app_event/hi_app_event_thread.cc | 153 ++++++++++++++++++ .../common/hi_app_event/hi_app_event_thread.h | 95 +++++++++++ mindspore/lite/src/litert/c_api/model_c.cc | 5 +- .../lite/src/litert/cxx_api/model/model.cc | 53 ++---- 11 files changed, 335 insertions(+), 443 deletions(-) delete mode 100644 mindspore/lite/src/common/hi_app_event/handler_thread.cc delete mode 100644 mindspore/lite/src/common/hi_app_event/handler_thread.h delete mode 100644 mindspore/lite/src/common/hi_app_event/hi_app_event_config.cc delete mode 100644 mindspore/lite/src/common/hi_app_event/hi_app_event_config.h create mode 100644 mindspore/lite/src/common/hi_app_event/hi_app_event_thread.cc create mode 100644 mindspore/lite/src/common/hi_app_event/hi_app_event_thread.h diff --git a/mindspore/lite/src/common/BUILD.gn b/mindspore/lite/src/common/BUILD.gn index e08e09ce..40445e38 100644 --- a/mindspore/lite/src/common/BUILD.gn +++ b/mindspore/lite/src/common/BUILD.gn @@ -18,8 +18,7 @@ lite_src_common_mid_sources = [ "string_util.cc", "dynamic_library_loader.cc", "hi_app_event/hi_app_event.cc", - "hi_app_event/hi_app_event_config.cc", - "hi_app_event/handler_thread.cc", + "hi_app_event/hi_app_event_thread.cc", ] ohos_source_set("lite_common_mid_obj") { diff --git a/mindspore/lite/src/common/hi_app_event/handler_thread.cc b/mindspore/lite/src/common/hi_app_event/handler_thread.cc deleted file mode 100644 index 1cf32bca..00000000 --- a/mindspore/lite/src/common/hi_app_event/handler_thread.cc +++ /dev/null @@ -1,144 +0,0 @@ -/** - * Copyright 2024 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "handler_thread.h" -#include "src/common/log.h" - -namespace mindspore { -namespace lite { -HandlerThread::Handler::Handler(HandlerThread& aLooper) - : assigned_looper_(aLooper) -{ } - -bool HandlerThread::Handler::Post(HandlerThread::Runnable && aRunnable) -{ - return assigned_looper_.Post("", std::move(aRunnable)); -} - -bool HandlerThread::Handler::Post(std::string key, HandlerThread::Runnable && aRunnable) -{ - return assigned_looper_.Post(key, std::move(aRunnable)); -} - -HandlerThread::HandlerThread() - : running_(false), - abort_requested_(false), - runnables_(), - runnables_mutex_(), - handler_(std::make_shared< Handler >(*this)) -{ } - -HandlerThread::~HandlerThread() -{ - AbortAndJoin(); -} - -bool HandlerThread::Running() const -{ - return running_.load(); -} - -bool HandlerThread::Run() -{ - thread_ = std::thread(&HandlerThread::RunFunc, this); - - return true; -} - -void HandlerThread::Stop() -{ - AbortAndJoin(); -} - -std::shared_ptr < HandlerThread::Handler > HandlerThread::GetHandler() -{ - return handler_; -} - -void HandlerThread::RunFunc() -{ - running_.store(true); - - (void)pthread_setname_np(pthread_self(), "OS_MSEvent"); - while (abort_requested_.load() == false) { - Runnable r = Next(); - if (r != nullptr) { - r(); - } else { - std::unique_lock lock{mutex_}; - condition_.wait(lock); - } - } - - running_.store(false); -} - -void HandlerThread::AbortAndJoin() -{ - abort_requested_.store(true); - condition_.notify_one(); - if (thread_.joinable()) { - thread_.join(); - } -} - -HandlerThread::Runnable HandlerThread::Next() -{ - std::lock_guard guard(runnables_mutex_); - - if (runnables_.empty()) { - return nullptr; - } - std::shared_ptr msg = runnables_.front(); - Runnable runnable = msg->r; - runnables_.pop_front(); - return runnable; -} - -class IsSameKey { -public: - std::string key_; - - bool operator()(std::shared_ptr < HandlerThread::Message > obj) - { - return obj->key == key_; - } - - explicit IsSameKey(std::string key) { key_ = key; } -}; - -bool HandlerThread::Post(std::string key, Runnable && aRunnable) -{ - if (!Running()) { - MS_LOG(ERROR) << "Denying insertion, as the looper is not running."; - return false; - } - - std::lock_guard guard(runnables_mutex_); - if (!key.empty()) { - runnables_.remove_if(IsSameKey(key)); - } - auto msg = std::make_shared(); - msg->key = key; - msg->r = std::move(aRunnable); - runnables_.push_back(msg); - std::unique_lock lock{mutex_}; - condition_.notify_one(); - - return true; -} -} // mindspore -} // lite \ No newline at end of file diff --git a/mindspore/lite/src/common/hi_app_event/handler_thread.h b/mindspore/lite/src/common/hi_app_event/handler_thread.h deleted file mode 100644 index 91e5cc19..00000000 --- a/mindspore/lite/src/common/hi_app_event/handler_thread.h +++ /dev/null @@ -1,102 +0,0 @@ -/** - * Copyright 2024 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef LITE_HANDLER_THREAD_H -#define LITE_HANDLER_THREAD_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace mindspore { -namespace lite { - -class HandlerThread { -public: - using Runnable = std::function; - - struct Message { - std::string key; - Runnable r; - }; - - class Handler { - friend class HandlerThread; // Allow the looper to access the private constructor. - - public: - bool Post(HandlerThread::Runnable &&aRunnable); - - /** - * 在该线程抛出一个任务,并会移除当前任务等待队列中相同key值的任务 - * @param key 任务的唯一标志 - * @param aRunnable 待执行的任务 - * @return true成功,false失败 - */ - bool Post(std::string key, HandlerThread::Runnable && aRunnable); - - public: // construction, since we want the looper to expose it's dispatcher exclusively! - explicit Handler(HandlerThread &aLooper); - - private: - HandlerThread &assigned_looper_; - }; - -public: - HandlerThread(); - - ~HandlerThread(); - - bool Running() const; - - bool Run(); - - void Stop(); - - std::shared_ptr GetHandler(); - -private: - void RunFunc(); - - void AbortAndJoin(); - - Runnable Next(); - - bool Post(std::string key, HandlerThread::Runnable && aRunnable); - -private: - std::thread thread_; - std::atomic_bool running_; - std::atomic_bool abort_requested_; - - std::list> runnables_; - std::recursive_mutex runnables_mutex_; - - std::shared_ptr handler_; - - std::mutex mutex_; - std::condition_variable condition_; -}; -} // mindspore -} // lite -#endif // LITE_HANDLER_THREAD_H diff --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 index ab46cde3..c2bfb4cd 100644 --- a/mindspore/lite/src/common/hi_app_event/hi_app_event.cc +++ b/mindspore/lite/src/common/hi_app_event/hi_app_event.cc @@ -16,64 +16,54 @@ #ifdef ENABLE_HI_APP_EVENT #include "src/common/hi_app_event/hi_app_event.h" -#include "src/common/hi_app_event/hi_app_event_config.h" -#include "app_event.h" -#include "app_event_processor_mgr.h" #include #include +#include +#include "app_event.h" +#include "app_event_processor_mgr.h" namespace mindspore { namespace lite { -namespace { -constexpr auto kName = "ha_app_event"; -constexpr auto kAppId = "com_huawei_hmos_sdk_ocg"; -constexpr int32_t kTimeOut = 90; -constexpr int32_t kCondRow = 30; -constexpr auto kDomain = "api_diagnostic"; -constexpr auto kEventName = "api_exec_end"; -constexpr auto kSdkName = "MindSporeLiteKit"; -constexpr int64_t kAppEventNotHapErrCode = -200; -static int64_t process_id = -1; +HiAppEvent& HiAppEvent::GetInstance() { + static HiAppEvent instance; + return instance; +} + +void HiAppEvent::Init() { + bool ret = event_thread_.Init(); + if (!ret) { + return; + } + is_ready_.store(true); +} + +void HiAppEvent::Report(const int result, const int err_code, const std::string &api_name, + const uint64_t begin_time, const std::string &devices) { + { + std::lock_guard init_guard_lock(init_mutex_); + if (!is_ready_.load()) { + Init(); + } + } + uint64_t end_time = GetTimeMs(); + event_thread_.Submit(result, err_code, api_name, begin_time, end_time, devices); +} + +uint64_t HiAppEvent::GetTimeMs() { + struct timespec ts = {0, 0}; + if (clock_gettime(CLOCK_REALTIME, &ts) != 0) { + return 0; + } + uint64_t ret_val = static_cast(ts.tv_sec * 1000LL + ts.tv_nsec / 1000000); + return ret_val; } -int64_t HiAppEventAddProcessor() { - std::srand(std::time(NULL)); - OHOS::HiviewDFX::HiAppEvent::ReportConfig config; - config.name = kName; - config.appId = kAppId; - config.routeInfo = "AUTO"; - config.triggerCond.timeout = kTimeOut; - config.triggerCond.row = kCondRow; - config.eventConfigs.clear(); - { - OHOS::HiviewDFX::HiAppEvent::EventConfig event; - event.domain = kDomain; - event.name = kEventName; - event.isRealTime = false; - config.eventConfigs.push_back(event); - } - if (process_id == -1) { - process_id = OHOS::HiviewDFX::HiAppEvent::AppEventProcessorMgr::AddProcessor(config); - } - return process_id; +std::string HiAppEvent::GetApiType() const { + return api_type_; } -void HiAppEventWriteEndEvent(const int result, const int err_code, const std::string &api_name, - const uint64_t begin_time, const std::string &devices) { - if (process_id == kAppEventNotHapErrCode) { - return; - } - OHOS::HiviewDFX::HiAppEvent::Event event(kDomain, kEventName, OHOS::HiviewDFX::HiAppEvent::BEHAVIOR); - event.AddParam("trans_id", "transId_" + std::to_string(std::rand() + HiAppEventConfig::GetInstance()->GetTimeMs())); - event.AddParam("api_name", api_name); - event.AddParam("sdk_name", std::string(kSdkName)); - event.AddParam("begin_time", static_cast(begin_time)); - event.AddParam("end_time", static_cast(HiAppEventConfig::GetInstance()->GetTimeMs())); - event.AddParam("result", result); - event.AddParam("error_code", std::to_string(err_code)); - event.AddParam("context_devices", devices); - event.AddParam("api_language", HiAppEventConfig::GetInstance()->GetApiLanguage()); - OHOS::HiviewDFX::HiAppEvent::Write(event); +void HiAppEvent::SetApiType(const std::string &api_type){ + api_type_ = api_type; } } // namespace lite } // namespace mindspore diff --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 index a5de1ad3..017a4912 100644 --- a/mindspore/lite/src/common/hi_app_event/hi_app_event.h +++ b/mindspore/lite/src/common/hi_app_event/hi_app_event.h @@ -18,13 +18,40 @@ #define MINDSPORE_LITE_HI_APP_EVENT_H_ #ifdef ENABLE_HI_APP_EVENT +#include "hi_app_event_thread.h" #include +#include +#include +#include namespace mindspore { namespace lite { -int64_t HiAppEventAddProcessor(); -void HiAppEventWriteEndEvent(const int result, const int err_code, const std::string &api_name, - const uint64_t begin_time, const std::string &devices = "None"); +class HiAppEvent { +public: + static HiAppEvent& GetInstance(); + + HiAppEvent(const HiAppEvent &) = delete; + HiAppEvent & operator=(const HiAppEvent &) = delete; + + void Report(const int result, const int err_code, const std::string &api_name, + const uint64_t begin_time, const std::string &devices = "None"); + + static uint64_t GetTimeMs(); + + std::string GetApiType() const; + + void SetApiType(const std::string &api_type); + +private: + void Init(); + HiAppEvent() = default; + ~HiAppEvent() = default; + + HiAppEventThread event_thread_; + std::string api_type_ = "ts_api"; + std::atomic_bool is_ready_ = false; + std::mutex init_mutex_; +}; } // namespace lite } // namespace mindspore #endif diff --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 deleted file mode 100644 index c488cd86..00000000 --- a/mindspore/lite/src/common/hi_app_event/hi_app_event_config.cc +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Copyright 2024 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifdef ENABLE_HI_APP_EVENT -#include "src/common/hi_app_event/hi_app_event_config.h" -#include - -namespace mindspore { -namespace lite { -HiAppEventConfig *HiAppEventConfig::GetInstance() { - static HiAppEventConfig instance; - return &instance; -} - -HiAppEventConfig::HiAppEventConfig() { - if (handler_thread_ == nullptr) { - handler_thread_ = std::make_unique(); - handler_thread_->Run(); - handler_ = handler_thread_->GetHandler(); - } -} - -HiAppEventConfig::~HiAppEventConfig() { -} - -void HiAppEventConfig::Post(std::function &&f) { - handler_->Post(std::move(f)); -} - -void HiAppEventConfig::SetApiLanguage(const std::string &api_language) { - api_language_ = api_language; -} - -uint64_t HiAppEventConfig::GetTimeMs() { - struct timespec ts = {0, 0}; - if (clock_gettime(CLOCK_REALTIME, &ts) != 0) { - return 0; - } - uint64_t ret_val = static_cast(ts.tv_sec * 1000LL + ts.tv_nsec / 1000000); - return ret_val; -} -} // namespace lite -} // namespace mindspore -#endif diff --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 deleted file mode 100644 index 57bee08d..00000000 --- a/mindspore/lite/src/common/hi_app_event/hi_app_event_config.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright 2024 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MINDSPORE_LITE_HI_APP_EVENT_CONFIG_H_ -#define MINDSPORE_LITE_HI_APP_EVENT_CONFIG_H_ - -#ifdef ENABLE_HI_APP_EVENT -#include -#include "handler_thread.h" - -namespace mindspore { -namespace lite { -class HiAppEventConfig { -public: - static HiAppEventConfig *GetInstance(); - const std::string &GetApiLanguage() const { return api_language_; } - void SetApiLanguage(const std::string &api_language); - uint64_t GetTimeMs(); - void Post(std::function &&f); - -private: - HiAppEventConfig(); - ~HiAppEventConfig(); - - std::string api_language_ = "ts_api"; - std::unique_ptr handler_thread_{nullptr}; - std::shared_ptr handler_{nullptr}; -}; -} // namespace lite -} // namespace mindspore -#endif -#endif // MINDSPORE_LITE_HI_APP_EVENT_CONFIG_H_ diff --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 new file mode 100644 index 00000000..8118d36a --- /dev/null +++ b/mindspore/lite/src/common/hi_app_event/hi_app_event_thread.cc @@ -0,0 +1,153 @@ +/** + * Copyright 2024 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "hi_app_event_thread.h" +#include +#include +#include "src/common/log.h" +#include "app_event.h" +#include "app_event_processor_mgr.h" + +namespace mindspore { +namespace lite { +HiAppEventThread::HiAppEventThread() +: running_(false), event_map_() +{ } + +HiAppEventThread::~HiAppEventThread() { + AbortAndJoin(); +} + +bool HiAppEventThread::Running() const { + return running_.load(); +} + +bool HiAppEventThread::Init() { + int ret = HiAppEventAddProcessor(); + if (ret == -1) { + return false; + } + running_.store(true); + thread_ = std::thread(&HiAppEventThread::RunFunc, this); + return true; +} + +void HiAppEventThread::WriteHiAppEvent() { + // when there is no sandbox environment for hap, for example, xts, skip writing app events. + if (kProcessID == kAppEventNotHapErrCode) { + return; + } + for (auto &e : event_map_) { + OHOS::HiviewDFX::HiAppEvent::Event event(kDomain, kEventName, OHOS::HiviewDFX::HiAppEvent::BEHAVIOR); + std::string api_name = e.first; + auto e_info = e.second; + event.AddParam("api_name", api_name); + event.AddParam("sdk_name", std::string(kSdkName)); + event.AddParam("begin_time", static_cast(e_info->begin_time)); + event.AddParam("success_times", e_info->success_times); + event.AddParam("call_times", e_info->call_times); + event.AddParam("min_cost_time", e_info->min_cost_time); + event.AddParam("max_cost_time", e_info->max_cost_time); + event.AddParam("total_cost_time", e_info->total_cost_time); + OHOS::HiviewDFX::HiAppEvent::Write(event); + } + event_map_.clear(); +} + +int64_t HiAppEventThread::HiAppEventAddProcessor() { + std::srand(std::time(NULL)); + OHOS::HiviewDFX::HiAppEvent::ReportConfig config; + config.name = kName; + config.appId = kAppId; + config.routeInfo = "AUTO"; + config.triggerCond.timeout = kTimeOut; + config.triggerCond.row = kCondRow; + config.eventConfigs.clear(); + {// not allow to modify + OHOS::HiviewDFX::HiAppEvent::EventConfig event1; + event1.domain = "api_diagnostic"; + event1.name = "api_exec_end"; + event1.isRealTime = false; + config.eventConfigs.push_back(event1); + } + { // not allow to modify + OHOS::HiviewDFX::HiAppEvent::EventConfig event2; + event2.domain = "api_diagnostic"; + event2.name = "api_called_stat"; + event2.isRealTime = true; + config.eventConfigs.push_back(event2); + } + { // not allow to modify + OHOS::HiviewDFX::HiAppEvent::EventConfig event3; + event3.domain = "api_diagnostic"; + event3.name = "api_called_stat_cnt"; + event3.isRealTime = true; + config.eventConfigs.push_back(event3); + } + if (kProcessID == -1) { + kProcessID = OHOS::HiviewDFX::HiAppEvent::AppEventProcessorMgr::AddProcessor(config); + } + return kProcessID; +} + +void HiAppEventThread::RunFunc() { + (void) pthread_setname_np(pthread_self(), "OS_MSEvent"); + while (running_.load()) { + std::unique_lock lock(mutex_); + auto status = condition_.wait_for(lock, std::chrono::seconds(kWaitTime)); + + if (!running_.load() || status == std::cv_status::timeout) { + // write all events and clear event_map + WriteHiAppEvent(); + } + } + running_.store(false); +} + +void HiAppEventThread::AbortAndJoin() { + running_.store(false); + condition_.notify_one(); + if (thread_.joinable()) { + thread_.join(); + } +} + +void HiAppEventThread::Submit(const int result, const int err_code, const std::string &api_name, + const uint64_t begin_time, const uint64_t end_time, const std::string &devices) { + // add and merge the data by api_name + std::lock_guard lock(mutex_); + int64_t cost_time = end_time - begin_time; + auto iter = event_map_.find(api_name); + if (iter != event_map_.end()) { + std::shared_ptr event_info = iter->second; + event_info->call_times++; + event_info->success_times += (result == RET_OK); + event_info->total_cost_time += cost_time; + event_info->min_cost_time = std::min(event_info->min_cost_time, cost_time); + event_info->max_cost_time = std::max(event_info->max_cost_time, cost_time); + return; + } + std::shared_ptr event_info = std::make_shared(); + event_info->begin_time = begin_time; + event_info->call_times = 1; + event_info->success_times = (result == RET_OK); + event_info->total_cost_time = cost_time; + event_info->min_cost_time = cost_time; + event_info->max_cost_time = cost_time; + event_map_.emplace(api_name, event_info); +} +} // mindspore +} // lite \ No newline at end of file diff --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 new file mode 100644 index 00000000..e48e335b --- /dev/null +++ b/mindspore/lite/src/common/hi_app_event/hi_app_event_thread.h @@ -0,0 +1,95 @@ +/** + * Copyright 2024 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LITE_HANDLER_THREAD_H +#define LITE_HANDLER_THREAD_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace mindspore { +namespace lite { +namespace { + constexpr auto kName = "ha_app_event"; + constexpr auto kAppId = "com_huawei_hmos_sdk_ocg"; + constexpr int32_t kTimeOut = 90; + constexpr int32_t kCondRow = 30; + constexpr auto kDomain = "api_diagnostic"; + constexpr auto kEventName = "api_called_stat"; + constexpr auto kSdkName = "MindSporeLiteKit"; + constexpr int64_t kAppEventNotHapErrCode = -200; + constexpr int32_t kWaitTime = 60; + static int64_t kProcessID = -1; + constexpr int32_t RET_OK = 0; + + struct EventInfo { + int64_t begin_time; + int64_t call_times; + int64_t success_times; + int64_t min_cost_time; + int64_t max_cost_time; + int64_t total_cost_time; + EventInfo() : begin_time(0), call_times(0), success_times(0), min_cost_time(0), max_cost_time(0), + total_cost_time(0) {} + }; +} + +class HiAppEventThread { +public: + HiAppEventThread(); + ~HiAppEventThread(); + + HiAppEventThread(const HiAppEventThread &) = delete; + HiAppEventThread &operator()(const HiAppEventThread &) = delete; + + bool Init(); + + void Submit(const int result, const int err_code, const std::string &api_name, + const uint64_t begin_time, const uint64_t end_time, const std::string &devices); + + void WriteHiAppEvent(); + + int64_t HiAppEventAddProcessor(); + +private: + + bool Running() const; + + void RunFunc(); + + void AbortAndJoin(); + +private: + std::thread thread_; + std::mutex mutex_; + std::condition_variable condition_; + + std::atomic_bool running_; + std::map> event_map_; +}; +} // mindspore +} // lite +#endif // LITE_HANDLER_THREAD_H diff --git a/mindspore/lite/src/litert/c_api/model_c.cc b/mindspore/lite/src/litert/c_api/model_c.cc index d8632338..8068cd8c 100644 --- a/mindspore/lite/src/litert/c_api/model_c.cc +++ b/mindspore/lite/src/litert/c_api/model_c.cc @@ -25,7 +25,7 @@ #include "src/litert/cxx_api/converters.h" #include "src/litert//cxx_api/model/model_impl.h" #ifdef ENABLE_HI_APP_EVENT -#include "src/common/hi_app_event/hi_app_event_config.h" +#include "src/common/hi_app_event/hi_app_event.h" #endif namespace mindspore { @@ -147,9 +147,6 @@ mindspore::MSKernelCallBack ModelC::TransCallBack(const OH_AI_KernelCallBack &oh } // namespace mindspore OH_AI_ModelHandle OH_AI_ModelCreate() { -#ifdef ENABLE_HI_APP_EVENT - mindspore::lite::HiAppEventConfig::GetInstance()->SetApiLanguage("c_api"); -#endif auto impl = new (std::nothrow) mindspore::ModelC(); if (impl == nullptr) { MS_LOG(ERROR) << "Model implement is nullptr."; diff --git a/mindspore/lite/src/litert/cxx_api/model/model.cc b/mindspore/lite/src/litert/cxx_api/model/model.cc index 282c752e..46ee88f7 100644 --- a/mindspore/lite/src/litert/cxx_api/model/model.cc +++ b/mindspore/lite/src/litert/cxx_api/model/model.cc @@ -39,7 +39,6 @@ #endif #ifdef ENABLE_HI_APP_EVENT #include "src/common/hi_app_event/hi_app_event.h" -#include "src/common/hi_app_event/hi_app_event_config.h" #endif namespace mindspore { @@ -129,7 +128,7 @@ Status Model::Build(const void *model_data, size_t data_size, ModelType model_ty Status Model::Build(const void *model_data, size_t data_size, ModelType model_type, const std::shared_ptr &model_context) { #ifdef ENABLE_HI_APP_EVENT - uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); + uint64_t begin_time = mindspore::lite::HiAppEvent::GetTimeMs(); std::string devices; for (auto device : model_context->MutableDeviceInfo()) { devices += std::to_string(device->GetDeviceType()) + " "; @@ -165,10 +164,7 @@ Status Model::Build(const void *model_data, size_t data_size, ModelType model_ty #endif ret = impl_->Build(model_data, data_size, model_type, model_context); #ifdef ENABLE_HI_APP_EVENT - mindspore::lite::HiAppEventConfig::GetInstance()->Post([ret, begin_time, devices] { - (void)mindspore::lite::HiAppEventWriteEndEvent(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelBuild", begin_time, - devices); - }); + mindspore::lite::HiAppEvent::GetInstance().Report(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelBuild", begin_time, devices); #endif if (ret != kSuccess) { return ret; @@ -248,7 +244,7 @@ Status Model::Build(const std::vector &model_path, ModelType model_type, Status Model::Build(const std::vector &model_path, ModelType model_type, const std::shared_ptr &model_context) { #ifdef ENABLE_HI_APP_EVENT - uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); + uint64_t begin_time = mindspore::lite::HiAppEvent::GetTimeMs(); std::string devices; for (auto device : model_context->MutableDeviceInfo()) { devices += std::to_string(device->GetDeviceType()) + " "; @@ -284,10 +280,7 @@ Status Model::Build(const std::vector &model_path, ModelType model_type, #endif ret = impl_->Build(CharToString(model_path), model_type, model_context); #ifdef ENABLE_HI_APP_EVENT - mindspore::lite::HiAppEventConfig::GetInstance()->Post([ret, begin_time, devices] { - (void)mindspore::lite::HiAppEventWriteEndEvent(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelBuildFromFile", - begin_time, devices); - }); + mindspore::lite::HiAppEvent::GetInstance().Report(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelBuildFromFile", begin_time, devices); #endif if (ret != kSuccess) { return ret; @@ -304,7 +297,7 @@ if (model_context == nullptr) { return Status(kLiteNullptr, err_msg.str()); } #ifdef ENABLE_HI_APP_EVENT - uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); + uint64_t begin_time = mindspore::lite::HiAppEvent::GetTimeMs(); std::string devices; for (auto device : model_context->MutableDeviceInfo()) { devices += std::to_string(device->GetDeviceType()) + " "; @@ -351,17 +344,14 @@ if (model_context == nullptr) { impl_->SetConfig(train_cfg); auto ret = impl_->Build(); #ifdef ENABLE_HI_APP_EVENT - mindspore::lite::HiAppEventConfig::GetInstance()->Post([ret, begin_time, devices] { - (void)mindspore::lite::HiAppEventWriteEndEvent(ret.StatusCode(), ret.StatusCode(), "OH_AI_TrainModelBuildFromFile", - begin_time, devices); - }); + mindspore::lite::HiAppEvent::GetInstance().Report(ret.StatusCode(), ret.StatusCode(), "OH_AI_TrainModelBuildFromFile", begin_time, devices); #endif return ret; } Status Model::Resize(const std::vector &inputs, const std::vector> &dims) { #ifdef ENABLE_HI_APP_EVENT - uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); + uint64_t begin_time = mindspore::lite::HiAppEvent::GetTimeMs(); #endif if (impl_ == nullptr) { MS_LOG(ERROR) << "Model implement is null."; @@ -369,9 +359,7 @@ Status Model::Resize(const std::vector &inputs, const std::vectorResize(inputs, dims); #ifdef ENABLE_HI_APP_EVENT - mindspore::lite::HiAppEventConfig::GetInstance()->Post([ret, begin_time] { - (void)mindspore::lite::HiAppEventWriteEndEvent(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelResize", begin_time); - }); + mindspore::lite::HiAppEvent::GetInstance().Report(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelResize", begin_time); #endif return ret; } @@ -386,7 +374,7 @@ Status Model::UpdateWeights(const std::vector &new_weights) { Status Model::RunStep(const MSKernelCallBack &before, const MSKernelCallBack &after) { #ifdef ENABLE_HI_APP_EVENT - uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); + uint64_t begin_time = mindspore::lite::HiAppEvent::GetTimeMs(); #endif if (impl_ == nullptr) { MS_LOG(ERROR) << "Model implement is null."; @@ -396,9 +384,7 @@ Status Model::RunStep(const MSKernelCallBack &before, const MSKernelCallBack &af auto outputs = impl_->GetOutputs(); auto ret = impl_->Predict(inputs, &outputs, before, after); #ifdef ENABLE_HI_APP_EVENT - mindspore::lite::HiAppEventConfig::GetInstance()->Post([ret, begin_time] { - (void)mindspore::lite::HiAppEventWriteEndEvent(ret.StatusCode(), ret.StatusCode(), "OH_AI_RunStep", begin_time); - }); + mindspore::lite::HiAppEvent::GetInstance().Report(ret.StatusCode(), ret.StatusCode(), "OH_AI_RunStep", begin_time); #endif return ret; } @@ -406,7 +392,7 @@ Status Model::RunStep(const MSKernelCallBack &before, const MSKernelCallBack &af Status Model::Predict(const std::vector &inputs, std::vector *outputs, const MSKernelCallBack &before, const MSKernelCallBack &after) { #ifdef ENABLE_HI_APP_EVENT - uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); + uint64_t begin_time = mindspore::lite::HiAppEvent::GetTimeMs(); #endif if (impl_ == nullptr) { MS_LOG(ERROR) << "Model implement is null."; @@ -414,9 +400,7 @@ Status Model::Predict(const std::vector &inputs, std::vector } auto ret = impl_->Predict(inputs, outputs, before, after); #ifdef ENABLE_HI_APP_EVENT - mindspore::lite::HiAppEventConfig::GetInstance()->Post([ret, begin_time] { - mindspore::lite::HiAppEventWriteEndEvent(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelPredict", begin_time); - }); + mindspore::lite::HiAppEvent::GetInstance().Report(ret.StatusCode(), ret.StatusCode(), "OH_AI_ModelPredict", begin_time); #endif return ret; } @@ -447,8 +431,7 @@ bool Model::HasPreprocess() { Model::Model() { #ifdef ENABLE_HI_APP_EVENT - (void)mindspore::lite::HiAppEventAddProcessor(); - uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); + uint64_t begin_time = mindspore::lite::HiAppEvent::GetTimeMs(); #endif #ifdef USE_GLOG mindspore::mindspore_log_init(); @@ -458,9 +441,7 @@ Model::Model() { MS_LOG(ERROR) << "Failed to create ModelImpl"; } #ifdef ENABLE_HI_APP_EVENT - mindspore::lite::HiAppEventConfig::GetInstance()->Post([begin_time] { - (void)mindspore::lite::HiAppEventWriteEndEvent(RET_OK, RET_OK, "OH_AI_ModelCreate", begin_time); - }); + mindspore::lite::HiAppEvent::GetInstance().Report(RET_OK, RET_OK, "OH_AI_ModelCreate", begin_time); #endif } @@ -570,7 +551,7 @@ Status Model::UpdateConfig(const std::vector §ion, Status Model::SetTrainMode(bool train) { #ifdef ENABLE_HI_APP_EVENT - uint64_t begin_time = mindspore::lite::HiAppEventConfig::GetInstance()->GetTimeMs(); + uint64_t begin_time = mindspore::lite::HiAppEvent::GetTimeMs(); #endif if ((impl_ == nullptr) || (impl_->session_ == nullptr)) { MS_LOG(ERROR) << "Model is null."; @@ -578,9 +559,7 @@ Status Model::SetTrainMode(bool train) { } auto ret = (train) ? impl_->session_->Train() : impl_->session_->Eval(); #ifdef ENABLE_HI_APP_EVENT - mindspore::lite::HiAppEventConfig::GetInstance()->Post([begin_time] { - (void)mindspore::lite::HiAppEventWriteEndEvent(RET_OK, RET_OK, "OH_AI_ModelSetTrainMode", begin_time); - }); + mindspore::lite::HiAppEvent::GetInstance().Report(RET_OK, RET_OK, "OH_AI_ModelSetTrainMode", begin_time); #endif return (ret == mindspore::lite::RET_OK) ? kSuccess : kLiteError; } -- 2.34.1