From 04a8fc94a8bbd6312d3bfc127ce3d3b7eca88940 Mon Sep 17 00:00:00 2001 From: Dai Li Date: Fri, 10 Jun 2022 18:32:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=A4=84=E7=90=86=E6=B5=81?= =?UTF-8?q?=E7=A8=8B=E6=9E=B6=E6=9E=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1、优化上层消息来源,规范使用应用切前后台、创建/死亡消息、焦点消息; 2、去除冗余的rtg_msg_mgr、app_info_mgr模块,由intellisense_server统一管理服务侧消息入口,简化代码; 3、规范化前台应用管理,使用队列保存存活的历史应用,并记录其前后台信息; 4、为系统桌面、系统ui、锁屏配置黑名单,不进行RTG分组增加; Signed-off-by: Dai Li --- common/include/rme_constants.h | 11 - .../include/rme_core_sched.h | 2 - .../src/rme_core_sched.cpp | 8 - .../frame_aware_policy/include/app_info.h | 16 ++ .../frame_aware_policy/include/app_info_mgr.h | 49 ---- .../include/intellisense_server.h | 21 +- .../frame_aware_policy/include/rtg_msg_mgr.h | 42 ---- .../core/frame_aware_policy/src/app_info.cpp | 10 + .../frame_aware_policy/src/app_info_mgr.cpp | 149 ------------ .../src/intellisense_server.cpp | 229 +++++++++++++----- .../frame_aware_policy/src/rtg_msg_mgr.cpp | 104 -------- interfaces/innerkits/frameintf/BUILD.gn | 2 - .../innerkits/frameintf/frame_msg_intf.cpp | 44 +++- .../innerkits/frameintf/frame_msg_intf.h | 8 +- test/unittest/phone/frame_msg_intf_test.cpp | 141 ----------- 15 files changed, 251 insertions(+), 585 deletions(-) delete mode 100644 frameworks/core/frame_aware_policy/include/app_info_mgr.h delete mode 100644 frameworks/core/frame_aware_policy/include/rtg_msg_mgr.h delete mode 100644 frameworks/core/frame_aware_policy/src/app_info_mgr.cpp delete mode 100644 frameworks/core/frame_aware_policy/src/rtg_msg_mgr.cpp diff --git a/common/include/rme_constants.h b/common/include/rme_constants.h index f11c8ce..119d4bc 100644 --- a/common/include/rme_constants.h +++ b/common/include/rme_constants.h @@ -18,17 +18,6 @@ namespace OHOS { namespace RME { -enum class AppStateUpdateReason { - APP_FOREGROUND, - APP_BACKGROUND, - APP_TERMINATED, - PROCESS_CREATE, - PROCESS_READY, - PROCESS_SUSPEND, - PROCESS_DIED, - REASON_UNKNOWN, -}; - enum class ThreadType { UI, RENDER, diff --git a/frameworks/core/frame_aware_collector/include/rme_core_sched.h b/frameworks/core/frame_aware_collector/include/rme_core_sched.h index f5ef86a..f8e52c1 100644 --- a/frameworks/core/frame_aware_collector/include/rme_core_sched.h +++ b/frameworks/core/frame_aware_collector/include/rme_core_sched.h @@ -52,9 +52,7 @@ public: private: int m_pid = -1; int m_rtg = -1; - int m_renderTid = -1; int m_uiTid = -1; - bool m_renderHasSend = false; bool m_uiHasSend = false; }; } // namespace RME diff --git a/frameworks/core/frame_aware_collector/src/rme_core_sched.cpp b/frameworks/core/frame_aware_collector/src/rme_core_sched.cpp index 21f98a0..a1d7af1 100644 --- a/frameworks/core/frame_aware_collector/src/rme_core_sched.cpp +++ b/frameworks/core/frame_aware_collector/src/rme_core_sched.cpp @@ -110,13 +110,6 @@ void RmeCoreSched::AnimateStart() RME_LOGW("[AnimateStart]: search rtg empty! Rtg:%{public}d, Pid:%{public}d", m_rtg, m_pid); return; } - if (!m_renderHasSend) { - m_renderTid = gettid(); - int ret = AddThreadToRtg(m_renderTid, m_rtg); - RmeTraceBegin(("FrameS-Add rtg:" + to_string(m_rtg) + " ret:" + to_string(ret)).c_str()); - m_renderHasSend = true; - RmeTraceEnd(); - } } void RmeCoreSched::RenderStart() @@ -143,7 +136,6 @@ void RmeCoreSched::HandleEndScene() return; } int ret = EndScene(m_rtg); - m_renderHasSend = false; m_uiHasSend = false; RmeTraceBegin(("FrameS-EndFrameFreq-rtg:" + to_string(m_rtg) + " ret:" + to_string(ret)).c_str()); RmeTraceEnd(); diff --git a/frameworks/core/frame_aware_policy/include/app_info.h b/frameworks/core/frame_aware_policy/include/app_info.h index 86cb10f..ea5b620 100644 --- a/frameworks/core/frame_aware_policy/include/app_info.h +++ b/frameworks/core/frame_aware_policy/include/app_info.h @@ -20,16 +20,32 @@ namespace OHOS { namespace RME { +namespace { + constexpr int RT_PRIO = 0; + constexpr int RT_NUM = 4; +} + enum class AppState { APP_FOREGROUND, + APP_FOREGROUND_WITHOUT_RTG, APP_BACKGROUND, APP_TERMINATED, APP_UNKNOWN, }; +enum class CgroupPolicy { + SP_DEFAULT = 0, + SP_BACKGROUND = 1, + SP_FOREGROUND = 2, + SP_SYSTEM_BACKGROUND = 3, + SP_TOP_APP = 4, + SP_SYSTEM_DEFAULT = SP_DEFAULT +}; + class AppInfo { public: AppInfo(std::string appName, int pid, int uiTid, int renderTid, int isFocus, AppState state); + AppInfo(int pid); ~AppInfo() = default; void SetRenderTid(const int tid); diff --git a/frameworks/core/frame_aware_policy/include/app_info_mgr.h b/frameworks/core/frame_aware_policy/include/app_info_mgr.h deleted file mode 100644 index 1da4985..0000000 --- a/frameworks/core/frame_aware_policy/include/app_info_mgr.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device 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 APP_INFO_MGR_H -#define APP_INFO_MGR_H - -#include -#include -#include - -#include "app_info.h" -#include "rme_log_domain.h" -#include "single_instance.h" - -namespace OHOS { -namespace RME { -class AppInfoMgr { - DECLARE_SINGLE_INSTANCE(AppInfoMgr); - -public: - std::map> GetForegroundApp() const; - void OnForegroundChanged(const int pid, const std::string appName, const int rtGrp); - void OnBackgroundChanged(const int pid, const std::string appName); - void OnAppTerminateChanged(const int pid, const std::string appName); - void OnWindowFocus(const int pid, bool isFocus); - bool OnProcessDied(const int pid, const int tid); - std::shared_ptr GetFocusApp() const; - void SetFocusApp(const int pid, bool isFocus); - int GetAppRtgrp(const int pid); -private: - std::map> mForegroundAppList; - std::map> mBackgroundAppList; - std::shared_ptr mFocusApp; -}; -} // namespace RME -} // namespace OHOS -#endif diff --git a/frameworks/core/frame_aware_policy/include/intellisense_server.h b/frameworks/core/frame_aware_policy/include/intellisense_server.h index 79e1bbe..37b1403 100644 --- a/frameworks/core/frame_aware_policy/include/intellisense_server.h +++ b/frameworks/core/frame_aware_policy/include/intellisense_server.h @@ -17,14 +17,17 @@ #define INTELLI_SENSE_SERVER_H #include +#include #include #include +#include #include #include #include "rme_constants.h" #include "single_instance.h" #include "event_handler.h" +#include "app_info.h" namespace OHOS { namespace RME { @@ -34,14 +37,24 @@ class IntelliSenseServer { DECLARE_SINGLE_INSTANCE(IntelliSenseServer); public: void Init(); - void ReportMessage(std::string appName, std::string processName, int pid, - AppStateUpdateReason reason); - void ReportWindowFocus(const int pid, int isFocus); - void ReportProcessInfo(const int pid, const int tid, ThreadState state); + void ReportAppInfo(const int pid, const int uid, const std::string bundleName, ThreadState state); + void ReportProcessInfo(const int pid, const int uid, const std::string bundleName, ThreadState state); + void ReportCgroupChange(const int pid, const int uid, const int oldGroup, const int newGroup); + void ReportWindowFocus(const int pid, const int uid, int isFocus); + void ReportRenderThread(const int pid, const int uid, int renderTid); bool ReadXml(); void SetPara(const int32_t currentFps, const int32_t currentRenderType); private: + void NewForeground(int pid); + void NewBackground(int pid); + void NewDiedProcess(int pid); + void NewAppRecord(int pid); + int TryCreateRtgForApp(AppInfo *app); + inline CgroupPolicy CheckCgroupState(const CgroupPolicy cgroup); + std::list::iterator GetRecordOfPid(int pid); + std::list m_historyApp = {}; + std::set m_unsupportApp = {}; std::map m_generalPara {}; std::map> m_subEventPara {}; std::vector m_fpsList {}; diff --git a/frameworks/core/frame_aware_policy/include/rtg_msg_mgr.h b/frameworks/core/frame_aware_policy/include/rtg_msg_mgr.h deleted file mode 100644 index 3914f87..0000000 --- a/frameworks/core/frame_aware_policy/include/rtg_msg_mgr.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device 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 RTG_MSG_MGR_H -#define RTG_MSG_MGR_H - -#include -#include - -#include "rme_log_domain.h" -#include "rtg_interface.h" -#include "single_instance.h" - - -namespace OHOS { -namespace RME { -class RtgMsgMgr { - DECLARE_SINGLE_INSTANCE(RtgMsgMgr); -public: - void Init(); - int OnForeground(const std::string appName, const int pid); - void OnBackground(const std::string appName, const int pid, const int grpId); - void ProcessStart(const int tid, const int grpId); - void ProcessDied(const int pid, const int tid); - void FpsChanged(); - void FocusChanged(const int pid, bool isFocus); // for multiwindow -}; -} // namespace RME -} // namespace OHOS -#endif diff --git a/frameworks/core/frame_aware_policy/src/app_info.cpp b/frameworks/core/frame_aware_policy/src/app_info.cpp index 38efcce..de29658 100644 --- a/frameworks/core/frame_aware_policy/src/app_info.cpp +++ b/frameworks/core/frame_aware_policy/src/app_info.cpp @@ -31,6 +31,16 @@ AppInfo::AppInfo(std::string appName, int pid, int uiTid, int renderTid, int isF m_appState(state) {} +AppInfo::AppInfo(int pid) + : m_appName(""), + m_pid(pid), + m_uiTid(0), + m_renderTid(0), + m_isFocus(0), + m_rtGrp(0), + m_appState(AppState::APP_TERMINATED) +{} + void AppInfo::SetRenderTid(const int tid) { this->m_renderTid = tid; diff --git a/frameworks/core/frame_aware_policy/src/app_info_mgr.cpp b/frameworks/core/frame_aware_policy/src/app_info_mgr.cpp deleted file mode 100644 index 3fc6dc4..0000000 --- a/frameworks/core/frame_aware_policy/src/app_info_mgr.cpp +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device 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 "app_info_mgr.h" - -namespace OHOS { -namespace RME { -namespace { - constexpr int INIT_VAL = -1; -} -DEFINE_RMELOG_SERVICE("ueaServer-AppInfoMgr"); -IMPLEMENT_SINGLE_INSTANCE(AppInfoMgr); - -void AppInfoMgr::OnForegroundChanged(const int pid, const std::string appName, const int rtGrp) -{ - if (mBackgroundAppList.count(pid)) { - RME_LOGI("[OnForegroundChanged]: back yes!pid:%{public}d", pid); - mBackgroundAppList[pid]->SetAppName(appName); - mBackgroundAppList[pid]->SetAppState(AppState::APP_FOREGROUND); - mForegroundAppList[pid] = mBackgroundAppList[pid]; - mBackgroundAppList.erase(pid); - } else { - RME_LOGI("[OnForegroundChanged]: back No!pid:%{public}d", pid); - auto appInfo = std::make_shared(appName, pid, INIT_VAL, INIT_VAL, INIT_VAL, AppState::APP_FOREGROUND); - mForegroundAppList[pid] = appInfo; - } - mForegroundAppList[pid]->SetRtgrp(rtGrp); - RME_LOGI("[OnForegroundChanged]:pid:%{public}d, rtgrp:%{public}d", pid, rtGrp); -} - -void AppInfoMgr::OnBackgroundChanged(const int pid, const std::string appName) -{ - std::map>::iterator it = mForegroundAppList.find(pid); - if (it != mForegroundAppList.end()) { - mForegroundAppList[pid]->SetAppState(AppState::APP_BACKGROUND); - mForegroundAppList[pid]->SetRtgrp(INIT_VAL); - mBackgroundAppList[pid] = mForegroundAppList[pid]; - mForegroundAppList.erase(pid); - } else { - RME_LOGI("[OnBackgroundChanged]: fore No! pid:%{public}d", pid); - auto appInfo = std::make_shared(appName, pid, INIT_VAL, INIT_VAL, INIT_VAL, AppState::APP_BACKGROUND); - mBackgroundAppList[pid] = appInfo; - } - mBackgroundAppList[pid]->SetRtgrp(INIT_VAL); - RME_LOGI("[OnBackgroundChanged]:pid:%{public}d, appName:%{public}s", pid, appName.c_str()); -} - -void AppInfoMgr::OnAppTerminateChanged(const int pid, const std::string appName) -{ - mForegroundAppList.erase(pid); - mBackgroundAppList.erase(pid); - RME_LOGI("[OnAppTerminatedChanged]: pid: %{public}d, appName: %{public}s, \ - ", pid, appName.c_str()); -} - -void AppInfoMgr::OnWindowFocus(const int pid, bool isFocus) -{ - // isFocus: 0:focused, 1:unfocused. - std::shared_ptr appInfo = nullptr; - if (!isFocus) { - appInfo = mForegroundAppList[pid]; - if (appInfo == nullptr) { - RME_LOGE("[OnAppFocus]:not found pid %{public}d in fore map", pid); - return; - } - } else { - appInfo = mBackgroundAppList[pid]; - if (appInfo == nullptr) { - RME_LOGE("[OnAppFocus]:not found pid %{public}d in back map", pid); - return; - } - } - SetFocusApp(pid, isFocus); - appInfo->SetFocusState(isFocus); - RME_LOGI("[OnAppFocus]: pid:%{public}d, isFocus:%{public}d, \ - ", pid, isFocus); -} - -bool AppInfoMgr::OnProcessDied(const int pid, const int tid) -{ - // TO DO: need to add tid set mgr. now has process then erase. - bool deleted = false; - bool isUi = pid == tid ? true : false; - if (mForegroundAppList.count(pid)) { - if (isUi) { - mForegroundAppList.erase(pid); - } else { - // TO DO: if render died, do not need process. - mForegroundAppList[pid]->SetRenderTid(INIT_VAL); - } - deleted = true; - } else if (mBackgroundAppList.count(pid)) { - if (isUi) { - mBackgroundAppList.erase(pid); - } else { - // TO DO: if render died, do not need process. - mBackgroundAppList[pid]->SetRenderTid(INIT_VAL); - } - } - RME_LOGI("[OnProcessDied]: pid: %{public}d, tid:%{public}d, deleted:%{public}d.", pid, tid, deleted); - return deleted; -} - -void AppInfoMgr::SetFocusApp(const int pid, bool isFocus) -{ - if (!isFocus) { - mFocusApp = mForegroundAppList[pid]; - } else { - mFocusApp = nullptr; - } - RME_LOGI("[SetFocusApp]:pid: %{public}d, isFocus%{public}d", pid, isFocus); -} - -std::shared_ptr AppInfoMgr::GetFocusApp() const -{ - return mFocusApp; -} - -int AppInfoMgr::GetAppRtgrp(const int pid) -{ - int rtGrp = -1; - if (mForegroundAppList.count(pid) != 0) { - rtGrp = mForegroundAppList[pid]->GetRtgrp(); - } else if (mBackgroundAppList.count(pid) != 0) { - rtGrp = mBackgroundAppList[pid]->GetRtgrp(); - } else { - RME_LOGE("[GetAppRtgrp]: do not have this pid, please add!"); - } - return rtGrp; -} - -std::map> AppInfoMgr::GetForegroundApp() const -{ - return mForegroundAppList; -} -} // namespace RME -} // namespace OHOS diff --git a/frameworks/core/frame_aware_policy/src/intellisense_server.cpp b/frameworks/core/frame_aware_policy/src/intellisense_server.cpp index a2be5a3..23b329d 100644 --- a/frameworks/core/frame_aware_policy/src/intellisense_server.cpp +++ b/frameworks/core/frame_aware_policy/src/intellisense_server.cpp @@ -18,9 +18,8 @@ #include #include "para_config.h" -#include "rtg_msg_mgr.h" +#include "rtg_interface.h" #include "rme_log_domain.h" -#include "app_info_mgr.h" #include "rme_scoped_trace.h" namespace OHOS { @@ -44,13 +43,22 @@ void IntelliSenseServer::Init() RME_LOGI("[Init]:xml switch close!"); return; } - RtgMsgMgr::GetInstance().Init(); + int ret = EnableRtg(true); + if (ret < 0) { + RME_LOGE("[Init]: enable rtg failed!"); + return; + } + m_unsupportApp = { + "com.ohos.launcher", + "com.ohos.systemui", + "com.ohos.screenlock" + }; RME_LOGI("[Init]:Init rtg and readXml finish!"); } bool IntelliSenseServer::ReadXml() { - if (!m_needReadXml) { // do this value really need? + if (!m_needReadXml) { return false; } m_needReadXml = false; @@ -69,96 +77,197 @@ bool IntelliSenseServer::ReadXml() return false; } -void IntelliSenseServer::ReportMessage(std::string appName, std::string processName, - int pid, AppStateUpdateReason reason) +void IntelliSenseServer::NewForeground(int pid) { - if (!m_switch) { - return; - } RME_FUNCTION_TRACE(); - int rtGrp = AppInfoMgr::GetInstance().GetAppRtgrp(pid); - switch (reason) { - case AppStateUpdateReason::APP_FOREGROUND: - { - rtGrp = RtgMsgMgr::GetInstance().OnForeground(appName, pid); - AppInfoMgr::GetInstance().OnForegroundChanged(pid, appName, rtGrp); - RME_LOGI("[ReportMessage]: App_foreground! rtGrp: %{public}d", rtGrp); + bool found = false; + int newCreatedRtg = 0; + for (auto iter = m_historyApp.begin(); iter != m_historyApp.end(); iter++) { + if (iter->GetAppPid() == pid) { + found = true; + RME_LOGI("[ReportMessage]pid %{public}d change to foreground.", pid); + if (iter->GetAppState() != AppState::APP_FOREGROUND) { + iter->SetUiTid(pid); + newCreatedRtg = TryCreateRtgForApp(&*iter); } - break; - case AppStateUpdateReason::APP_BACKGROUND: - { - RtgMsgMgr::GetInstance().OnBackground(appName, pid, rtGrp); - AppInfoMgr::GetInstance().OnBackgroundChanged(pid, appName); - RME_LOGI("[ReportMessage]: App_background! rtGrp: %{public}d", rtGrp); + if (newCreatedRtg) { + iter->SetAppState(AppState::APP_FOREGROUND); + } else { + iter->SetAppState(AppState::APP_FOREGROUND_WITHOUT_RTG); } break; - case AppStateUpdateReason::APP_TERMINATED: - { - RtgMsgMgr::GetInstance().ProcessDied(pid, -1); - AppInfoMgr::GetInstance().OnAppTerminateChanged(pid, appName); - RME_LOGI("[ReportMessage]: App terminated! rtGrp: %{public}d", rtGrp); + } + } +} + +int IntelliSenseServer::TryCreateRtgForApp(AppInfo *app) +{ + if (!app) { + RME_LOGE("[TryCreateRtg]: null app!"); + return 0; + } + int grpId = CreateNewRtgGrp(RT_PRIO, RT_NUM); + if (grpId <= 0) { + RME_LOGE("[TryCreateRtg]: createNewRtgGroup failed! grpId:%{public}d", grpId); + app->SetRtgrp(0); + return grpId; + } + app->SetRtgrp(grpId); + int uiTid = app->GetUiTid(); + int renderTid = app->GetRenderTid(); + if (uiTid > 0) { + AddThreadToRtg(uiTid, grpId, 0); // add ui thread + } + if (renderTid > 0) { + AddThreadToRtg(renderTid, grpId, 0); // add render thread + } + return grpId; +} + +void IntelliSenseServer::NewBackground(int pid) +{ + RME_FUNCTION_TRACE(); + RME_LOGI("[ReportMessage]pid %{public}d change to background.", pid); + for (auto iter = m_historyApp.begin(); iter != m_historyApp.end(); iter++) { + if (iter->GetAppPid() != pid) { + continue; + } + iter->SetAppState(AppState::APP_BACKGROUND); + int grpId = iter->GetRtgrp(); + if (grpId > 0) { + DestroyRtgGrp(grpId); + } + } +} + +void IntelliSenseServer::NewAppRecord(int pid) +{ + for (auto iter = m_historyApp.begin(); iter != m_historyApp.end(); iter++) { + if (iter->GetAppPid() == pid) { + RME_LOGI("[NewAppRecord]pid %{public}d already exist.", pid); + return; + } + } + AppInfo *tempRecord = new AppInfo(pid); + m_historyApp.push_back(*tempRecord); + tempRecord->SetAppState(AppState::APP_FOREGROUND); +} + +void IntelliSenseServer::NewDiedProcess(int pid) +{ + RME_FUNCTION_TRACE(); + RME_LOGI("[ReportMessage]pid %{public}d died.", pid); + for (auto iter = m_historyApp.begin(); iter != m_historyApp.end(); iter++) { + if (iter->GetAppPid() == pid) { + int grpId = iter->GetRtgrp(); + if (grpId > 0) { + DestroyRtgGrp(grpId); } - break; - default: - RME_LOGI("[ReportMessage]: get unuse app state msg!"); - break; + m_historyApp.erase(iter++); + } } - return; } -void IntelliSenseServer::ReportWindowFocus(const int pid, int isFocus) +std::list::iterator IntelliSenseServer::GetRecordOfPid(int pid) +{ + for (auto iter = m_historyApp.begin(); iter != m_historyApp.end(); iter++) { + if (iter->GetAppPid() == pid) { + return iter; + } + } + return m_historyApp.end(); +} + +void IntelliSenseServer::ReportRenderThread(const int pid, const int uid, int renderTid) +{ + if (!m_switch) { + return; + } + RME_FUNCTION_TRACE(); + auto record = GetRecordOfPid(pid); + if (record == m_historyApp.end()) { + RME_LOGE("Didn't find render in history app %{public}d with render %{public}d", pid, renderTid); + return; + } + record->SetRenderTid(renderTid); + int grpId = record->GetRtgrp(); + if (grpId >= 0 && record->GetAppState() == AppState::APP_FOREGROUND) { + int ret = AddThreadToRtg(renderTid, grpId, 0); // add render thread + if (ret != 0) { + RME_LOGE("[OnFore]:add render thread fail! pid:%{public}d,rtg:%{public}d!ret:%{publid}d", + renderTid, grpId, ret); + } + } +} + +void IntelliSenseServer::ReportWindowFocus(const int pid, const int uid, int isFocus) { if (!m_switch) { return; } RME_FUNCTION_TRACE(); - int rtGrp = AppInfoMgr::GetInstance().GetAppRtgrp(pid); switch (isFocus) { case static_cast(WindowState::FOCUS_YES): // isFocus: 0 - { - rtGrp = RtgMsgMgr::GetInstance().OnForeground("", pid); - AppInfoMgr::GetInstance().OnForegroundChanged(pid, "", rtGrp); - RME_LOGI("[ReportWindowFocus]: Focus yes!rtGrp: %{public}d", rtGrp); - } + RME_LOGI("[ReportWindowFocus]:%{public}d get focus", pid); break; case static_cast(WindowState::FOCUS_NO): // isFocus: 1 - { - RtgMsgMgr::GetInstance().OnBackground("", pid, rtGrp); - AppInfoMgr::GetInstance().OnBackgroundChanged(pid, ""); - RME_LOGI("[ReportWindowFocus]: Focus No!rtGrp: %{public}d", rtGrp); - } + RME_LOGI("[ReportWindowFocus]:%{public}d lost focus", pid); break; default: RME_LOGI("[ReportWindowFocus]:unknown msg!"); break; } - AppInfoMgr::GetInstance().OnWindowFocus(pid, isFocus); - RtgMsgMgr::GetInstance().FocusChanged(pid, isFocus); } -void IntelliSenseServer::ReportProcessInfo(const int pid, const int tid, ThreadState state) +inline CgroupPolicy IntelliSenseServer::CheckCgroupState(CgroupPolicy cgroup) +{ + return ((cgroup == CgroupPolicy::SP_FOREGROUND) || (cgroup == CgroupPolicy::SP_TOP_APP)) ? + CgroupPolicy::SP_FOREGROUND : CgroupPolicy::SP_BACKGROUND; +} + +void IntelliSenseServer::ReportCgroupChange(const int pid, const int uid, const int oldGroup, const int newGroup) +{ + if (!m_switch) { + return; + } + RME_FUNCTION_TRACE(); + CgroupPolicy oldState = CheckCgroupState(static_cast(oldGroup)); + CgroupPolicy newState = CheckCgroupState(static_cast(newGroup)); + if (oldState == newState) { + return; + } + if (newState == CgroupPolicy::SP_BACKGROUND) { + NewBackground(pid); + } else if (newState == CgroupPolicy::SP_FOREGROUND) { + NewForeground(pid); + } +} + +void IntelliSenseServer::ReportAppInfo(const int pid, const int uid, const std::string bundleName, ThreadState state) +{ + if (!m_switch) { + return; + } + RME_LOGI("Get app info:%{public}d %{public}d %{public}s %{public}d", + pid, uid, bundleName.c_str(), static_cast(state)); +} + +void IntelliSenseServer::ReportProcessInfo(const int pid, + const int uid, const std::string bundleName, ThreadState state) { if (!m_switch) { return; } RME_FUNCTION_TRACE(); + if (m_unsupportApp.find(bundleName) != m_unsupportApp.end()) { + return; + } switch (state) { case ThreadState::DIED: - { - int ret = AppInfoMgr::GetInstance().OnProcessDied(pid, tid); - if (ret) { - RtgMsgMgr::GetInstance().ProcessDied(pid, tid); - RME_LOGI("process died, pid:%{public}d, tid: %{public}d, threadstate: %{public}d", - pid, tid, static_cast(state)); - } else { - RME_LOGI("process died, do not need to delete the rtgrp:pid:%{public}d, tid: %{public}d", - pid, tid); - } - } + NewDiedProcess(pid); break; case ThreadState::CREATE: - RME_LOGI("process create, pid: %{public}d, tid: %{public}d, threadstate: %{public}d", - pid, tid, static_cast(state)); + NewAppRecord(pid); break; default: RME_LOGI("unknown state : %{public}d", static_cast(state)); diff --git a/frameworks/core/frame_aware_policy/src/rtg_msg_mgr.cpp b/frameworks/core/frame_aware_policy/src/rtg_msg_mgr.cpp deleted file mode 100644 index 9294efb..0000000 --- a/frameworks/core/frame_aware_policy/src/rtg_msg_mgr.cpp +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device 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 "rtg_msg_mgr.h" -#include "rme_scoped_trace.h" - -namespace OHOS { -namespace RME { -namespace { - constexpr int PRIO_TYPE = 0; - constexpr int RT_NUM = 4; - constexpr int PID_PRIO_TYPE = 2; -} -DEFINE_RMELOG_INTELLISENSE("ueaServer-RtgMsgMgr"); -IMPLEMENT_SINGLE_INSTANCE(RtgMsgMgr); - -void RtgMsgMgr::Init() -{ - int ret = EnableRtg(true); - if (ret < 0) { - RME_LOGE("[Init]: enable rtg failed!"); - } -} - -int RtgMsgMgr::OnForeground(const std::string appName, const int pid) -{ - // for multiwindow - RmeTraceBegin(("FrameC-createRtgGrp-pid"+ to_string(pid)).c_str()); - int rtGrp = CreateNewRtgGrp(PRIO_TYPE, RT_NUM); - if (rtGrp <= 0) { - RME_LOGE("[OnForeground]: createNewRtgGroup failed! rtGrp:%{public}d, pid: %{public}d", rtGrp, pid); - return rtGrp; - } - RmeTraceEnd(); - RmeTraceBegin(("FrameC-addThread-pid:" + to_string(pid), +" rtgrp:" + to_string(rtGrp)).c_str()); - int ret = AddThreadToRtg(pid, rtGrp, PID_PRIO_TYPE); // add ui thread - if (ret != 0) { - RME_LOGE("[OnFore]:add thread fail! pid:%{public}d,rtg:%{public}d!ret:%{publid}d", pid, rtGrp, ret); - } - RmeTraceEnd(); - return rtGrp; -} - -void RtgMsgMgr::OnBackground(const std::string appName, const int pid, const int grpId) -{ - RmeTraceBegin(("FrameC-destroyRtgGrp-grpId" + to_string(grpId)).c_str()); - if (grpId <= 0) { - RME_LOGE("[OnBackground]:do not find grpid, pid:%{public}d, grpId:%{public}d", pid, grpId); - return; - } - DestroyRtgGrp(grpId); - RmeTraceEnd(); -} - -void RtgMsgMgr::ProcessStart(const int tid, const int grpId) -{ - if (grpId <= 0) { - RME_LOGE("[ProcessStart]:do not find grpid, tid:%{public}d, grpId:%{public}d", tid, grpId); - return; - } - AddThreadToRtg(tid, grpId); -} - -void RtgMsgMgr::ProcessDied(const int pid, const int tid) -{ - int removeTid = -1; - if (tid == -1) { - removeTid = pid; - } else { - removeTid = tid; - } - int ret = RemoveRtgThread(removeTid); - if (ret < 0) { - RME_LOGE("[ProcessDied]: removeRtgGroup failed!pid:%{public}d, tid:%{public}d", pid, tid); - } else { - RME_LOGI("[ProcessDied]: removeRtgGroup success! pid:%{public}d, tid:%{public}d:tid", pid, tid); - } -} - -void RtgMsgMgr::FpsChanged() -{ - RME_LOGI("[FpsChanged]: need to change fps for rtggrp!"); -} - -void RtgMsgMgr::FocusChanged(const int pid, bool isFocus) -{ - (void)pid; - (void)isFocus; - RME_LOGI("[FocusChanged]: need to change prio for rtggrp"); -} -} // namespace RME -} // namespace OHOS diff --git a/interfaces/innerkits/frameintf/BUILD.gn b/interfaces/innerkits/frameintf/BUILD.gn index 6899f3d..b65ef91 100644 --- a/interfaces/innerkits/frameintf/BUILD.gn +++ b/interfaces/innerkits/frameintf/BUILD.gn @@ -77,10 +77,8 @@ ohos_shared_library("frame_msg_intf") { "${frame_aware_path}/common/src/rme_scoped_trace.cpp", "${frame_aware_path}/interfaces/innerkits/frameintf/rtg_interface.cpp", "${framework_path}/frame_aware_policy/src/app_info.cpp", - "${framework_path}/frame_aware_policy/src/app_info_mgr.cpp", "${framework_path}/frame_aware_policy/src/intellisense_server.cpp", "${framework_path}/frame_aware_policy/src/para_config.cpp", - "${framework_path}/frame_aware_policy/src/rtg_msg_mgr.cpp", "frame_msg_intf.cpp", ] diff --git a/interfaces/innerkits/frameintf/frame_msg_intf.cpp b/interfaces/innerkits/frameintf/frame_msg_intf.cpp index 70e2fee..c2f09bc 100644 --- a/interfaces/innerkits/frameintf/frame_msg_intf.cpp +++ b/interfaces/innerkits/frameintf/frame_msg_intf.cpp @@ -59,40 +59,64 @@ bool FrameMsgIntf::GetThreadHandler() return true; } +void FrameMsgIntf::ReportWindowFocus(const int pid, const int uid, const int isFocus) +{ + std::lock_guard autoLock(frameMsgIntfMutex_); + if (threadHandler_ == nullptr) { + RME_LOGE("[ReportWindowFocus]:threandHandler none!"); + return; + } + threadHandler_->PostTask([pid, uid, isFocus] { + IntelliSenseServer::GetInstance().ReportWindowFocus(pid, uid, isFocus); + }); +} + +void FrameMsgIntf::ReportRenderThread(const int pid, const int uid, const int renderTid) +{ + std::lock_guard autoLock(frameMsgIntfMutex_); + RME_LOGI("[ReportRenderThread]:render get %{public}d with render %{pubilc}d", pid, renderTid); + if (threadHandler_ == nullptr) { + RME_LOGE("[ReportRenderThread]:threandHandler none!"); + return; + } + threadHandler_->PostTask([pid, uid, renderTid] { + IntelliSenseServer::GetInstance().ReportRenderThread(pid, uid, renderTid); + }); +} -void FrameMsgIntf::ReportAppInfo(std::string appName, std::string processName, int pid, AppStateUpdateReason reason) +void FrameMsgIntf::ReportAppInfo(const int pid, const int uid, const std::string bundleName, ThreadState state) { std::lock_guard autoLock(frameMsgIntfMutex_); if (threadHandler_ == nullptr) { RME_LOGI("[ReportAppInfo]:threandHandler none!"); return; } - threadHandler_->PostTask([appName, processName, pid, reason] { - IntelliSenseServer::GetInstance().ReportMessage(appName, processName, pid, reason); + threadHandler_->PostTask([pid, uid, bundleName, state] { + IntelliSenseServer::GetInstance().ReportAppInfo(pid, uid, bundleName, state); }); } -void FrameMsgIntf::ReportWindowFocus(const int pid, const int isFocus) +void FrameMsgIntf::ReportProcessInfo(const int pid, const int uid, const std::string bundleName, ThreadState state) { std::lock_guard autoLock(frameMsgIntfMutex_); if (threadHandler_ == nullptr) { - RME_LOGE("[ReportWindowFocus]:threandHandler none!"); + RME_LOGI("[ReportProcessInfo]:threandHandler none!"); return; } - threadHandler_->PostTask([pid, isFocus] { - IntelliSenseServer::GetInstance().ReportWindowFocus(pid, isFocus); + threadHandler_->PostTask([pid, uid, bundleName, state] { + IntelliSenseServer::GetInstance().ReportProcessInfo(pid, uid, bundleName, state); }); } -void FrameMsgIntf::ReportProcessInfo(const int pid, const int tid, ThreadState state) +void FrameMsgIntf::ReportCgroupChange(const int pid, const int uid, const int oldGroup, const int newGroup) { std::lock_guard autoLock(frameMsgIntfMutex_); if (threadHandler_ == nullptr) { RME_LOGI("[ReportProcessInfo]:threandHandler none!"); return; } - threadHandler_->PostTask([pid, tid, state] { - IntelliSenseServer::GetInstance().ReportProcessInfo(pid, tid, state); + threadHandler_->PostTask([pid, uid, oldGroup, newGroup] { + IntelliSenseServer::GetInstance().ReportCgroupChange(pid, uid, oldGroup, newGroup); }); } diff --git a/interfaces/innerkits/frameintf/frame_msg_intf.h b/interfaces/innerkits/frameintf/frame_msg_intf.h index 3f60508..199f250 100644 --- a/interfaces/innerkits/frameintf/frame_msg_intf.h +++ b/interfaces/innerkits/frameintf/frame_msg_intf.h @@ -27,9 +27,11 @@ public: static FrameMsgIntf& GetInstance(); bool Init(); bool GetThreadHandler(); - void ReportAppInfo(std::string appName, std::string processName, int pid, AppStateUpdateReason reason); - void ReportWindowFocus(const int pid, const int isFocus); - void ReportProcessInfo(const int pid, const int tid, ThreadState state); + void ReportAppInfo(const int pid, const int uid, const std::string bundleName, ThreadState state); + void ReportProcessInfo(const int pid, const int uid, const std::string bundleName, ThreadState state); + void ReportCgroupChange(const int pid, const int uid, const int oldGroup, const int newGroup); + void ReportWindowFocus(const int pid, const int uid, const int isFocus); + void ReportRenderThread(const int pid, const int uid, const int renderTid); void Stop(); protected: FrameMsgIntf() = default; diff --git a/test/unittest/phone/frame_msg_intf_test.cpp b/test/unittest/phone/frame_msg_intf_test.cpp index 4904104..c643f8c 100644 --- a/test/unittest/phone/frame_msg_intf_test.cpp +++ b/test/unittest/phone/frame_msg_intf_test.cpp @@ -20,7 +20,6 @@ #include #include #include "frame_msg_intf.h" -#include "app_info_mgr.h" #include "app_info.h" #undef private #undef protected @@ -58,145 +57,5 @@ void FrameMsgIntfTest::TearDown() std::shared_ptr thread = FrameMsgIntf::GetInstance().threadHandler_; EXPECT_TRUE(thread == nullptr); } - -HWTEST_F(FrameMsgIntfTest, FrameMsgIntfReportAppInfoColdStart, TestSize.Level1) -{ - int pid_1 = 1001; - std::string appName_1 = "com.ohos.frameaware.testColdStart"; - - // app foreground - FrameMsgIntf::GetInstance().ReportAppInfo(appName_1, appName_1, pid_1, AppStateUpdateReason::APP_FOREGROUND); - sleep(1); - std::map> mapList = AppInfoMgr::GetInstance().GetForegroundApp(); - bool isAppPidExist = AppInfoMgr::GetInstance().GetForegroundApp().count(pid_1); - EXPECT_TRUE(isAppPidExist); // should be true -} - -HWTEST_F(FrameMsgIntfTest, FrameMsgIntfReportAppInfoWarmStart, TestSize.Level1) -{ - int pid_1 = 1002; - std::string appName_1 = "com.ohos.frameaware.testWarmStart"; - FrameMsgIntf::GetInstance().ReportAppInfo(appName_1, appName_1, pid_1, AppStateUpdateReason::APP_FOREGROUND); - - FrameMsgIntf::GetInstance().ReportAppInfo(appName_1, appName_1, pid_1, AppStateUpdateReason::APP_BACKGROUND); - - FrameMsgIntf::GetInstance().ReportAppInfo(appName_1, appName_1, pid_1, AppStateUpdateReason::APP_FOREGROUND); - sleep(1); - - std::map> mapList = AppInfoMgr::GetInstance().GetForegroundApp(); - bool isAppPidExist = AppInfoMgr::GetInstance().GetForegroundApp().count(pid_1); - EXPECT_TRUE(isAppPidExist); // should be true -} - -HWTEST_F(FrameMsgIntfTest, FrameMsgIntfReportAppInfoTerminated, TestSize.Level1) -{ - int pid_1 = 1003; - std::string appName_1 = "com.ohos.frameaware.testAppDied"; - // start - FrameMsgIntf::GetInstance().ReportAppInfo(appName_1, appName_1, pid_1, AppStateUpdateReason::APP_FOREGROUND); - - // terminated - FrameMsgIntf::GetInstance().ReportAppInfo(appName_1, appName_1, pid_1, AppStateUpdateReason::APP_TERMINATED); - sleep(1); - - std::map> mapList = AppInfoMgr::GetInstance().GetForegroundApp(); - bool isAppPidExist = AppInfoMgr::GetInstance().GetForegroundApp().count(pid_1); - EXPECT_FALSE(isAppPidExist); -} - -HWTEST_F(FrameMsgIntfTest, FrameMsgIntfReportAppInfoSwitch, TestSize.Level1) -{ - int pid_1 = 1004; - std::string appName_1 = "com.ohos.frameaware.testAppSwitch1"; - int pid_2 = 10041; - std::string appName_2 = "com.ohos.frameaware.testAppSwitch2"; - - FrameMsgIntf::GetInstance().ReportAppInfo(appName_1, appName_1, pid_1, AppStateUpdateReason::APP_FOREGROUND); - - FrameMsgIntf::GetInstance().ReportAppInfo(appName_2, appName_2, pid_2, AppStateUpdateReason::APP_FOREGROUND); - FrameMsgIntf::GetInstance().ReportAppInfo(appName_1, appName_1, pid_1, AppStateUpdateReason::APP_BACKGROUND); - - FrameMsgIntf::GetInstance().ReportAppInfo(appName_2, appName_2, pid_2, AppStateUpdateReason::APP_BACKGROUND); - FrameMsgIntf::GetInstance().ReportAppInfo(appName_1, appName_1, pid_1, AppStateUpdateReason::APP_FOREGROUND); - sleep(1); - - std::map> mapList = AppInfoMgr::GetInstance().GetForegroundApp(); - bool isAppPidExist = AppInfoMgr::GetInstance().GetForegroundApp().count(pid_1); - bool isAppPidExist2 = AppInfoMgr::GetInstance().GetForegroundApp().count(pid_2); - EXPECT_TRUE(isAppPidExist); // should be true - EXPECT_FALSE(isAppPidExist2); // should be false -} - -HWTEST_F(FrameMsgIntfTest, FrameMsgIntfReportWindowFocus, TestSize.Level1) -{ - int pid_1 = 1005; - std::string appName_1 = "com.ohos.frameaware.testWindowFocus"; - - FrameMsgIntf::GetInstance().ReportAppInfo(appName_1, appName_1, pid_1, AppStateUpdateReason::APP_FOREGROUND); - FrameMsgIntf::GetInstance().ReportWindowFocus(pid_1, 0); - sleep(1); - std::shared_ptr app_1 = AppInfoMgr::GetInstance().GetFocusApp(); - bool focusSta_1 = app_1->GetFocusState(); - EXPECT_EQ(focusSta_1, false); // 0 means focus -} - -HWTEST_F(FrameMsgIntfTest, FrameMsgIntfReportWindowUnFocus, TestSize.Level1) -{ - int pid_1 = 1006; - std::string appName_1 = "com.ohos.frameaware.testWindowUnfocus"; - - FrameMsgIntf::GetInstance().ReportAppInfo(appName_1, appName_1, pid_1, AppStateUpdateReason::APP_FOREGROUND); - FrameMsgIntf::GetInstance().ReportAppInfo(appName_1, appName_1, pid_1, AppStateUpdateReason::APP_BACKGROUND); - FrameMsgIntf::GetInstance().ReportWindowFocus(pid_1, 0); - FrameMsgIntf::GetInstance().ReportWindowFocus(pid_1, 1); - sleep(1); - std::shared_ptr app_1 = AppInfoMgr::GetInstance().GetFocusApp(); - EXPECT_EQ(app_1 == nullptr, true); // 1 means unfocus -} - -HWTEST_F(FrameMsgIntfTest, FrameMsgIntfReportProcessInfoUiDiedFore, TestSize.Level1) -{ - int pid_1 = 1007; - int ui_tid = pid_1; - - FrameMsgIntf::GetInstance().ReportWindowFocus(pid_1, 0); - sleep(1); - std::map> mapList = AppInfoMgr::GetInstance().GetForegroundApp(); - EXPECT_EQ(mapList.count(pid_1), 1); - - FrameMsgIntf::GetInstance().ReportProcessInfo(pid_1, ui_tid, ThreadState::DIED); - sleep(1); - std::map> mapList1 = AppInfoMgr::GetInstance().GetForegroundApp(); - EXPECT_EQ(mapList1.count(pid_1), 0); -} - -HWTEST_F(FrameMsgIntfTest, FrameMsgIntfReportProcessInfoUiDiedBack, TestSize.Level1) -{ - int pid_1 = 1008; - int ui_tid = pid_1; - - FrameMsgIntf::GetInstance().ReportWindowFocus(pid_1, 0); - FrameMsgIntf::GetInstance().ReportWindowFocus(pid_1, 1); - FrameMsgIntf::GetInstance().ReportProcessInfo(pid_1, ui_tid, ThreadState::DIED); - sleep(1); - std::map> mapList = AppInfoMgr::GetInstance().GetForegroundApp(); - EXPECT_EQ(mapList.count(pid_1), 0); -} - -HWTEST_F(FrameMsgIntfTest, FrameMsgIntfReportProcessInfoRenderDiedFore, TestSize.Level1) -{ - int pid_2 = 1009; - int render_tid = 10091; - - FrameMsgIntf::GetInstance().ReportWindowFocus(pid_2, 0); - sleep(1); - std::map> mapList = AppInfoMgr::GetInstance().GetForegroundApp(); - EXPECT_EQ(mapList.count(pid_2), 1); - - FrameMsgIntf::GetInstance().ReportProcessInfo(pid_2, render_tid, ThreadState::DIED); - sleep(1); - std::map> mapList2 = AppInfoMgr::GetInstance().GetForegroundApp(); - EXPECT_EQ(mapList2.count(pid_2), 1); -} } // namespace RME } // namespace OHOS -- Gitee