From 112277cc4a994e941492edf301c712c0ffd99ff4 Mon Sep 17 00:00:00 2001 From: edwardcaoyue Date: Mon, 19 Aug 2024 17:24:59 +0800 Subject: [PATCH] add process info Signed-off-by: edwardcaoyue --- .../include/concurrent_task_type.h | 2 + services/BUILD.gn | 1 + services/include/process_manager.h | 83 +++++ services/src/concurrent_task_controller.cpp | 96 +++--- services/src/process_manager.cpp | 291 ++++++++++++++++++ test/BUILD.gn | 19 ++ test/unittest/phone/process_manager_test.cpp | 104 +++++++ 7 files changed, 537 insertions(+), 59 deletions(-) create mode 100644 services/include/process_manager.h create mode 100644 services/src/process_manager.cpp create mode 100644 test/unittest/phone/process_manager_test.cpp diff --git a/frameworks/concurrent_task_client/include/concurrent_task_type.h b/frameworks/concurrent_task_client/include/concurrent_task_type.h index a83277f..ecbbee3 100644 --- a/frameworks/concurrent_task_client/include/concurrent_task_type.h +++ b/frameworks/concurrent_task_client/include/concurrent_task_type.h @@ -24,6 +24,8 @@ enum MsgType { MSG_BACKGROUND, MSG_APP_START, MSG_APP_KILLED, + MSG_PROCESS_CREATE, + MSG_PROCESS_DIED, MSG_CONTINUOUS_TASK_START, MSG_CONTINUOUS_TASK_END, MSG_GET_FOCUS, diff --git a/services/BUILD.gn b/services/BUILD.gn index 21036d9..daa0f5e 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -59,6 +59,7 @@ ohos_shared_library("concurrentsvc") { "src/concurrent_task_service.cpp", "src/concurrent_task_service_ability.cpp", "src/concurrent_task_service_stub.cpp", + "src/process_manager.cpp", "src/qos_interface.cpp", "src/qos_policy.cpp", ] diff --git a/services/include/process_manager.h b/services/include/process_manager.h new file mode 100644 index 0000000..8a86c71 --- /dev/null +++ b/services/include/process_manager.h @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2024 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 CONCURRENT_TASK_SERVICES_CONCURRENTSEVICE_INCLUDE_PROCESS_MANAGER_H +#define CONCURRENT_TASK_SERVICES_CONCURRENTSEVICE_INCLUDE_PROCESS_MANAGER_H + +#include +#include +#include +#include +#include +#include "json/json.h" +#include "concurrent_task_log.h" +#include "concurrent_task_type.h" + +namespace OHOS { +namespace ConcurrentTask { + +class AppInfo { +public: + AppInfo(int32_t uid, int32_t pid, const std::string& bundleName); + void RemoveChild(int32_t childPid); + + int32_t mainPid_; // main thread process id + std::string appName_; // bundleName + unsigned int status_; + std::vector childPids_; + std::mutex appInfoMutex_; + +private: + int32_t uid_; +}; + +class ProcInfo { +public: + ProcInfo(int32_t pid, int32_t hostPid); + int32_t GetHostPid() const; +private: + int32_t pid_; + bool isHost_; + int32_t hostPid_; +}; + +class ProcessManager { +public: + static ProcessManager& GetInstance(); + void Init(); + void Exit(); + void ReportMsg(int32_t msg, int32_t pid, const Json::Value& payload); + void AppCreate(int32_t uid, int32_t mainPid, const std::string& bundleName); + void AppDied(int32_t mainPid); + void AppForeground(std::shared_ptr app); + void AppBackground(std::shared_ptr app); + void AppFocus(std::shared_ptr app); + + std::shared_ptr GetAppInfo(int32_t pid); + +private: + std::mutex procMapMutex_; + std::mutex appMapMutex_; + std::map> procInfoMap_; + std::map> appInfoMap_; + + void ProcessCreate(int32_t pid, int32_t hostPid); + void ProcessDied(int32_t pid); + int32_t ParseHostPid(const Json::Value& payload); + std::shared_ptr GetProcInfo(int32_t pid); +}; // class ProcessManager +} // namespace ConcurrentTask +} // namespace OHOS + +#endif // CONCURRENT_TASK_SERVICES_CONCURRENTSEVICE_INCLUDE_PROCESS_MANAGER_H diff --git a/services/src/concurrent_task_controller.cpp b/services/src/concurrent_task_controller.cpp index dc4ad7a..d76353e 100644 --- a/services/src/concurrent_task_controller.cpp +++ b/services/src/concurrent_task_controller.cpp @@ -25,6 +25,7 @@ #include "rtg_interface.h" #include "ipc_skeleton.h" #include "parameters.h" +#include "process_manager.h" #include "concurrent_task_controller.h" using namespace OHOS::RME; @@ -331,6 +332,7 @@ void TaskController::Init() TypeMapInit(); qosPolicy_.Init(); TryCreateRsGroup(); + ProcessManager::GetInstance().Init(); std::lock_guard autolock(configReaderMutex_); if (!configEnable_) { @@ -371,6 +373,7 @@ void TaskController::Release() renderServiceRenderGrpId_ = -1; } ddlSceneSchedSwitch_ = false; + ProcessManager::GetInstance().Exit(); std::lock_guard autolock(configReaderMutex_); configReader_ = nullptr; @@ -383,6 +386,8 @@ void TaskController::TypeMapInit() msgType_.insert(pair("background", MSG_BACKGROUND)); msgType_.insert(pair("appStart", MSG_APP_START)); msgType_.insert(pair("appKilled", MSG_APP_KILLED)); + msgType_.insert(pair("procCreate", MSG_PROCESS_CREATE)); + msgType_.insert(pair("procDied", MSG_PROCESS_DIED)); msgType_.insert(pair("continuousStart", MSG_CONTINUOUS_TASK_START)); msgType_.insert(pair("continuousEnd", MSG_CONTINUOUS_TASK_END)); msgType_.insert(pair("getFocus", MSG_GET_FOCUS)); @@ -468,6 +473,10 @@ void TaskController::DealSystemRequest(int requestType, const Json::Value& paylo return; } switch (requestType) { + case MSG_PROCESS_CREATE: + case MSG_PROCESS_DIED: + ProcessManager::GetInstance().ReportMsg(requestType, pid, payload); + break; case MSG_FOREGROUND: NewForeground(uid, pid); break; @@ -512,25 +521,17 @@ ForegroundAppRecord* TaskController::GetRecordOfPid(int pid) void TaskController::NewForeground(int uid, int pid) { int uiTid = pid; - auto it = find(authApps_.begin(), authApps_.end(), pid); - if (it == authApps_.end()) { - CONCUR_LOGI("un-authed pid %{public}d", pid); + shared_ptr app = ProcessManager::GetInstance().GetAppInfo(pid); + if (app == nullptr) { + CONCUR_LOGD("[ProcMgr] %{public}s pid %{public}d is not an authed app", __func__, pid); return; } int ret = AuthGet(pid); if (ret != static_cast(AuthStatus::AUTH_STATUS_FOCUS)) { - CONCUR_LOGI("pid %{public}d change to foreground.", pid); - unsigned int pidParam = static_cast(pid); - unsigned int uaFlag = AF_RTG_ALL; - unsigned int status = static_cast(AuthStatus::AUTH_STATUS_FOREGROUND); - int ret = AuthEnable(pidParam, uaFlag, status); - if (ret == 0) { - CONCUR_LOGI("auth_enable %{public}d success", pid); - } else { - CONCUR_LOGE("auth_enable %{public}d fail with ret %{public}d", pid, ret); - } + CONCUR_LOGI("[ProcMgr] app %{public}d foreground", pid); + ProcessManager::GetInstance().AppForeground(app); } else { - CONCUR_LOGI("pid %{public}d is already focus", pid); + CONCUR_LOGI("[ProcMgr] app %{public}d is already focus", pid); } bool found = false; bool ddlEnabled = OHOS::system::GetBoolParameter(INTERVAL_DDL, false); @@ -567,20 +568,13 @@ void TaskController::NewForegroundAppRecord(int pid, int uiTid, bool ddlEnabled) void TaskController::NewBackground(int uid, int pid) { - auto it = find(authApps_.begin(), authApps_.end(), pid); - if (it == authApps_.end()) { - CONCUR_LOGI("un-authed pid %{public}d", pid); + shared_ptr app = ProcessManager::GetInstance().GetAppInfo(pid); + if (app == nullptr) { + CONCUR_LOGD("[ProcMgr] %{public}s pid %{public}d is not an authed app", __func__, pid); return; } - CONCUR_LOGI("pid %{public}d change to background.", pid); - unsigned int pidParam = static_cast(pid); - - int ret = AuthPause(pidParam); - if (ret == 0) { - CONCUR_LOGI("auth_pause %{public}d success", pid); - } else { - CONCUR_LOGI("auth_pause %{public}d fail with %{public}d", pid, ret); - } + CONCUR_LOGI("[ProcMgr] app %{public}d background", pid); + ProcessManager::GetInstance().AppBackground(app); std::lock_guard lock(appInfoLock_); for (auto iter = foregroundApp_.begin(); iter != foregroundApp_.end(); iter++) { if (iter->GetPid() == pid) { @@ -592,20 +586,9 @@ void TaskController::NewBackground(int uid, int pid) void TaskController::NewAppStart(int uid, int pid, const std::string& bundleName, int appType) { - CONCUR_LOGI("pid %{public}d start.", pid); - unsigned int pidParam = static_cast(pid); - unsigned int uaFlag = AF_RTG_ALL; - unsigned int status = static_cast(AuthStatus::AUTH_STATUS_DEFAULT); - - int ret = AuthEnable(pidParam, uaFlag, status); - if (ret == 0) { - CONCUR_LOGI("auth_enable %{public}d success", pid); - } else { - CONCUR_LOGE("auth_enable %{public}d fail with ret %{public}d", pid, ret); - return; - } + CONCUR_LOGI("[ProcMgr] app %{public}d start", pid); + ProcessManager::GetInstance().AppCreate(uid, pid, bundleName); std::lock_guard lock(appInfoLock_); - authApps_.push_back(pid); appBundleName[pid] = bundleName; if (ddlSceneSchedSwitch_ && appType != APP_TYPE_INVALID) { appTypeCache_[pid] = appType; @@ -614,14 +597,13 @@ void TaskController::NewAppStart(int uid, int pid, const std::string& bundleName void TaskController::AppKilled(int uid, int pid) { - CONCUR_LOGI("pid %{public}d killed.", pid); - unsigned int pidParam = static_cast(pid); - int ret = AuthDelete(pidParam); - if (ret == 0) { - CONCUR_LOGI("auth_delete %{public}d success", pid); - } else { - CONCUR_LOGE("auth_delete %{public}d fail with %{public}d", pid, ret); + shared_ptr app = ProcessManager::GetInstance().GetAppInfo(pid); + if (app == nullptr) { + CONCUR_LOGD("[ProcMgr] %{public}s pid %{public}d is not an authed app", __func__, pid); + return; } + CONCUR_LOGI("[ProcMgr] app %{public}d died", pid); + ProcessManager::GetInstance().AppDied(pid); std::lock_guard lock(appInfoLock_); for (auto iter = foregroundApp_.begin(); iter != foregroundApp_.end(); iter++) { if (iter->GetPid() == pid) { @@ -629,12 +611,6 @@ void TaskController::AppKilled(int uid, int pid) break; } } - for (auto iter = authApps_.begin(); iter != authApps_.end(); iter++) { - if (*iter == pid) { - authApps_.erase(iter); - break; - } - } appBundleName.erase(pid); if (ddlSceneSchedSwitch_) { appTypeCache_.erase(pid); @@ -670,12 +646,14 @@ void TaskController::ContinuousTaskProcess(int uid, int pid, int status) void TaskController::FocusStatusProcess(int uid, int pid, int status) { - int ret = -1; - unsigned int rtgFlag = AF_RTG_ALL; - unsigned int qosFlag = AF_QOS_DELEGATED; + shared_ptr app = ProcessManager::GetInstance().GetAppInfo(pid); + if (app == nullptr) { + CONCUR_LOGD("[ProcMgr] %{public}s pid %{public}d is not an authed app", __func__, pid); + return; + } if (status == static_cast(MSG_GET_FOCUS)) { - ret = AuthSwitch(pid, rtgFlag, qosFlag, static_cast(AuthStatus::AUTH_STATUS_FOCUS)); - CONCUR_LOGI("pid %{public}d get focus. ret %{public}d", pid, ret); + CONCUR_LOGI("[ProcMgr] app %{public}d get focus", app->mainPid_); + ProcessManager::GetInstance().AppFocus(app); if (ddlSceneSchedSwitch_) { if (IsVideoApp(pid)) { isVideoApp_ = true; @@ -685,8 +663,8 @@ void TaskController::FocusStatusProcess(int uid, int pid, int status) } } } else if (status == static_cast(MSG_LOSE_FOCUS)) { - ret = AuthSwitch(pid, rtgFlag, qosFlag, static_cast(AuthStatus::AUTH_STATUS_FOREGROUND)); - CONCUR_LOGI("pid %{public}d lose focus. ret %{public}d", pid, ret); + CONCUR_LOGI("[ProcMgr] app %{public}d lose focus", app->mainPid_); + ProcessManager::GetInstance().AppForeground(app); isVideoApp_ = false; } else { CONCUR_LOGE("Invalid focus status %{public}d", status); diff --git a/services/src/process_manager.cpp b/services/src/process_manager.cpp new file mode 100644 index 0000000..b999070 --- /dev/null +++ b/services/src/process_manager.cpp @@ -0,0 +1,291 @@ +/* + * Copyright (c) 2024 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 "process_manager.h" +#include "qos_interface.h" + +namespace OHOS { +namespace ConcurrentTask { +using namespace std; +void ProcessManager::Init() +{ + CONCUR_LOGI("[ProcMgr] Init success"); +} + +void ProcessManager::Exit() +{ + procInfoMap_.clear(); + appInfoMap_.clear(); + CONCUR_LOGI("[ProcMgr] Exit success"); +} + +ProcessManager& ProcessManager::GetInstance() +{ + static ProcessManager instance; + return instance; +} + +ProcInfo::ProcInfo(int32_t pid, int32_t hostPid) +{ + this->pid_ = pid; + if (hostPid == -1) { + this->isHost_ = true; + } else { + this->isHost_ = false; + } + this->hostPid_ = hostPid; + CONCUR_LOGD("[ProcMgr] ProcInfo Init, pid = %{public}d, hostPid = %{public}d", + this->pid_, this->hostPid_); +} + +void ProcessManager::AppCreate(int32_t uid, int32_t mainPid, const string& bundleName) +{ + shared_ptr appInfo = GetAppInfo(mainPid); + if (appInfo != nullptr) { + CONCUR_LOGI("[ProcMgr] app %{public}d is already created", mainPid); + return; + } + appInfo = make_shared(uid, mainPid, bundleName); + if (appInfo == nullptr) { + CONCUR_LOGE("[ProcMgr] create appInfo %{public}d failed", mainPid); + return; + } + { + lock_guard autoLock(appMapMutex_); + appInfoMap_.insert(pair>(mainPid, appInfo)); + } + + unsigned int status = static_cast(AuthStatus::AUTH_STATUS_DEFAULT); + int ret = AuthEnable(static_cast(mainPid), AF_RTG_ALL, status); + if (ret == 0) { + { + lock_guard autoLock(appInfo->appInfoMutex_); + appInfo->status_ = status; + } + CONCUR_LOGI("[ProcMgr] auth status 1 for %{public}d success", mainPid); + } else { + CONCUR_LOGE("[ProcMgr] auth status 1 for %{public}d fail with ret %{public}d", mainPid, ret); + } +} + +void ProcessManager::AppDied(int32_t mainPid) +{ + lock_guard autoLock(appMapMutex_); + auto iter = appInfoMap_.find(mainPid); + if (iter == appInfoMap_.end()) { + CONCUR_LOGI("[ProcMgr] app %{public}d is already died", mainPid); + return; + } + + int ret = AuthDelete(static_cast(mainPid)); + if (ret == 0) { + CONCUR_LOGI("[ProcMgr] auth delete for %{public}d success", mainPid); + } else { + CONCUR_LOGI("[ProcMgr] auth delete for %{public}d fail with %{public}d", mainPid, ret); + } + appInfoMap_.erase(iter); +} + +void ProcessManager::AppForeground(std::shared_ptr app) +{ + if (app == nullptr) { + return; + } + + unsigned int status = static_cast(AuthStatus::AUTH_STATUS_FOREGROUND); + lock_guard autoLock(app->appInfoMutex_); + int32_t ret = AuthEnable(static_cast(app->mainPid_), AF_RTG_ALL, status); + CONCUR_LOGI("[ProcMgr] auth status 3 for app %{public}d, ret = %{public}d", app->mainPid_, ret); + if (ret != 0) { + return; + } + app->status_ = status; + for (auto iter = app->childPids_.begin(); iter != app->childPids_.end(); iter++) { + ret = AuthEnable(static_cast(*iter), AF_RTG_ALL, status); + CONCUR_LOGI("[ProcMgr] auth status 3 for pid %{public}d, ret = %{public}d", *iter, ret); + } +} + +void ProcessManager::AppBackground(std::shared_ptr app) +{ + if (app == nullptr) { + return; + } + + lock_guard autoLock(app->appInfoMutex_); + int32_t ret = AuthPause(static_cast(app->mainPid_)); + CONCUR_LOGI("[ProcMgr] auth status 4 for app %{public}d, ret = %{public}d", app->mainPid_, ret); + if (ret != 0) { + return; + } + app->status_ = static_cast(AuthStatus::AUTH_STATUS_BACKGROUND); + for (auto iter = app->childPids_.begin(); iter != app->childPids_.end(); iter++) { + ret = AuthPause(static_cast(*iter)); + CONCUR_LOGI("[ProcMgr] auth status 4 for pid %{public}d, ret = %{public}d", *iter, ret); + } +} + +void ProcessManager::AppFocus(std::shared_ptr app) +{ + if (app == nullptr) { + return; + } + + unsigned int status = static_cast(AuthStatus::AUTH_STATUS_FOCUS); + lock_guard autoLock(app->appInfoMutex_); + int32_t ret = AuthSwitch(static_cast(app->mainPid_), AF_RTG_ALL, AF_QOS_DELEGATED, status); + CONCUR_LOGI("[ProcMgr] auth status 5 for app %{public}d, ret = %{public}d", app->mainPid_, ret); + if (ret != 0) { + return; + } + app->status_ = status; + for (auto iter = app->childPids_.begin(); iter != app->childPids_.end(); iter++) { + ret = AuthSwitch(static_cast(*iter), AF_RTG_ALL, AF_QOS_DELEGATED, status); + CONCUR_LOGI("[ProcMgr] auth status 5 for pid %{public}d, ret = %{public}d", *iter, ret); + } +} + +int32_t ProcInfo::GetHostPid() const +{ + return this->hostPid_; +} + +void ProcessManager::ReportMsg(int32_t msg, int32_t pid, const Json::Value& payload) +{ + CONCUR_LOGD("[ProcMgr] msg = %{public}d, pid = %{public}d", msg, pid); + switch (msg) { + case MSG_PROCESS_CREATE: { + int32_t hostPid = ParseHostPid(payload); + ProcessCreate(pid, hostPid); + break; + } + case MSG_PROCESS_DIED: { + ProcessDied(pid); + break; + } + default: + break; + } +} + +int32_t ProcessManager::ParseHostPid(const Json::Value& payload) +{ + int32_t host = -1; + if (payload.isMember("hostPid") && payload["hostPid"].isString()) { + try { + host = stoi(payload["hostPid"].asString()); + } catch (...) { + CONCUR_LOGE("[ProcMgr] Unexpected hostPid format"); + return host; + } + } + return host; +} + +void ProcessManager::ProcessCreate(int32_t pid, int32_t hostPid) +{ + if (hostPid <= 0) { + CONCUR_LOGI("[ProcMgr] invalid hostPid %{public}d", hostPid); + return; + } + shared_ptr procInfo = GetProcInfo(pid); + if (procInfo != nullptr) { + CONCUR_LOGI("[ProcMgr] process %{public}d is already created", pid); + return; + } + CONCUR_LOGI("[ProcMgr] process %{public}d create", pid); + procInfo = make_shared(pid, hostPid); + if (procInfo == nullptr) { + CONCUR_LOGE("[ProcMgr] create procInfo %{public}d failed", pid); + return; + } + { + lock_guard autoLock(procMapMutex_); + procInfoMap_.insert(pair>(pid, procInfo)); + } + + shared_ptr hostInfo = GetAppInfo(hostPid); + if (hostInfo == nullptr) { + CONCUR_LOGE("[ProcMgr] pid %{public}d's host %{public}d is null", pid, hostPid); + return; + } + lock_guard autoLock(hostInfo->appInfoMutex_); + hostInfo->childPids_.push_back(pid); + CONCUR_LOGD("[ProcMgr] app %{public}d add child %{public}d", hostPid, pid); + unsigned int status = hostInfo->status_; + CONCUR_LOGI("[ProcMgr] auth status %{public}u for pid %{public}d, ret = %{public}d", + status, pid, AuthEnable(static_cast(pid), AF_RTG_ALL, status)); +} + +void ProcessManager::ProcessDied(int32_t pid) +{ + lock_guard autoLock(procMapMutex_); + auto iter = procInfoMap_.find(pid); + if (iter == procInfoMap_.end()) { + CONCUR_LOGI("[ProcMgr] pid %{public}d is already died", pid); + return; + } + int32_t hostPid = iter->second->GetHostPid(); + procInfoMap_.erase(iter); + CONCUR_LOGI("[ProcMgr] process %{public}d died", pid); + if (hostPid != -1) { + shared_ptr hostInfo = GetAppInfo(hostPid); + if (!hostInfo) { + CONCUR_LOGD("[ProcMgr] host app %{public}d not exist", hostPid); + return; + } + hostInfo->RemoveChild(pid); + } +} + +shared_ptr ProcessManager::GetProcInfo(int32_t pid) +{ + lock_guard autoLock(procMapMutex_); + if (procInfoMap_.find(pid) != procInfoMap_.end()) { + return procInfoMap_[pid]; + } + return nullptr; +} + +shared_ptr ProcessManager::GetAppInfo(int32_t pid) +{ + lock_guard autoLock(appMapMutex_); + if (appInfoMap_.find(pid) != appInfoMap_.end()) { + return appInfoMap_[pid]; + } + return nullptr; +} + +AppInfo::AppInfo(int32_t uid, int32_t pid, const string& bundleName) +{ + this->uid_ = uid; + this->mainPid_ = pid; + this->status_ = 0; + this->appName_ = bundleName; + CONCUR_LOGD("[ProcMgr] init app pid = %{public}d, bundleName = %{public}s", pid, bundleName.c_str()); +} + +void AppInfo::RemoveChild(int32_t childPid) +{ + lock_guard autoLock(appInfoMutex_); + auto iter = find(childPids_.begin(), childPids_.end(), childPid); + if (iter != childPids_.end()) { + childPids_.erase(iter); + return; + } + CONCUR_LOGD("[ProcMgr] child pid %{public}d not in app %{public}d", childPid, this->mainPid_); +} +} // namespace ConcurrentTask +} // namespace OHOS \ No newline at end of file diff --git a/test/BUILD.gn b/test/BUILD.gn index f65a1cd..2cde28e 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -285,6 +285,24 @@ ohos_unittest("config_reader_test") { part_name = "qos_manager" } +ohos_unittest("process_manager_test") { + module_out_path = module_output_path + configs = [ ":test_config" ] + + sources = [ "unittest/phone/process_manager_test.cpp" ] + deps = [ "../services:concurrentsvc" ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "init:libbegetutil", + "jsoncpp:jsoncpp", + ] + + subsystem_name = "resourceschedule" + part_name = "qos_manager" +} + group("concurrent_unittest") { testonly = true deps = [] @@ -296,6 +314,7 @@ group("concurrent_unittest") { ":concurrent_task_service_ability_test", ":concurrent_task_service_test", ":config_reader_test", + ":process_manager_test", ":qos_interface_test", ":qos_ndk_test", ":qos_policy_test", diff --git a/test/unittest/phone/process_manager_test.cpp b/test/unittest/phone/process_manager_test.cpp new file mode 100644 index 0000000..20b212e --- /dev/null +++ b/test/unittest/phone/process_manager_test.cpp @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2024 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 +#include "process_manager.h" +#include "gtest/gtest.h" + +namespace OHOS { +namespace ConcurrentTask { +using namespace testing; +using namespace testing::ext; +using namespace OHOS::ConcurrentTask; +using namespace std; + +class ProcessManagerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void ProcessManagerTest::SetUpTestCase() +{ +} + +void ProcessManagerTest::TearDownTestCase() +{ +} + +void ProcessManagerTest::SetUp() +{ +} + +void ProcessManagerTest::TearDown() +{ +} + +/** + * @tc.name: AppInfoBasicFunc + * @tc.desc: class AppInfo basic func + * @tc.type: FUNC + */ +HWTEST_F(ProcessManagerTest, AppInfoBasicFunc, TestSize.Level1) +{ + int32_t uid = 123456; + int32_t mainPid = 6666; + string bundleName = "test"; + shared_ptr appInfo = make_shared(uid, mainPid, bundleName); + EXPECT_EQ(appInfo->mainPid_, mainPid); + EXPECT_EQ(appInfo->childPids_.size(), 0); + appInfo->childPids_.push_back(7777); + appInfo->childPids_.push_back(8888); + EXPECT_EQ(appInfo->childPids_.size(), 2); + appInfo->RemoveChild(7777); + EXPECT_EQ(appInfo->childPids_.size(), 1); +} + +/** + * @tc.name: ProcInfoBasicFunc + * @tc.desc: class ProcInfo basic func + * @tc.type: FUNC + */ +HWTEST_F(ProcessManagerTest, ProcInfoBasicFunc, TestSize.Level1) +{ + int32_t pid = 11111; + int32_t hostPid = 1111; + shared_ptr procInfo = make_shared(pid, hostPid); + EXPECT_EQ(procInfo->GetHostPid(), hostPid); +} + +/** + * @tc.name: ProcessManagerBasicFunc + * @tc.desc: class ProcessManager basic func + * @tc.type: FUNC + */ +HWTEST_F(ProcessManagerTest, ProcessManagerBasicFunc, TestSize.Level1) +{ + int32_t uid = 123456; + int32_t mainPid = 6666; + string bundleName = "test"; + ProcessManager::GetInstance().AppCreate(uid, mainPid, bundleName); + + shared_ptr appInfo = ProcessManager::GetInstance().GetAppInfo(mainPid); + EXPECT_NE(appInfo, nullptr); + ProcessManager::GetInstance().AppForeground(appInfo); + ProcessManager::GetInstance().AppBackground(appInfo); + ProcessManager::GetInstance().AppDied(mainPid); + appInfo = ProcessManager::GetInstance().GetAppInfo(mainPid); + EXPECT_EQ(appInfo, nullptr); +} +} +} \ No newline at end of file -- Gitee