diff --git a/services/samgr/native/include/system_ability_manager_dumper.h b/services/samgr/native/include/system_ability_manager_dumper.h index 333d8f9432977f19ca8940db674a884d008598a8..f0091025e20ed5eab23d005d5eaeb050a132c345 100644 --- a/services/samgr/native/include/system_ability_manager_dumper.h +++ b/services/samgr/native/include/system_ability_manager_dumper.h @@ -98,6 +98,9 @@ private: int32_t pid, std::string& result); static void ShowListenerHelp(std::string& result); static std::shared_ptr handler_; + static char* ffrtMetricBuffer; + static bool collectEnable; + static std::mutex ffrtMetricLock; }; } // namespace OHOS #endif // SERVICES_SAMGR_NATIVE_INCLUDE_SYSTEM_ABILITY_MANAGER_DUMPER_H \ No newline at end of file diff --git a/services/samgr/native/source/system_ability_manager_dumper.cpp b/services/samgr/native/source/system_ability_manager_dumper.cpp index 679eba6a12158fe61d2178585845e3536e411fba..7ee5db868effd170428153e5770ea6c6e67d2e37 100644 --- a/services/samgr/native/source/system_ability_manager_dumper.cpp +++ b/services/samgr/native/source/system_ability_manager_dumper.cpp @@ -45,9 +45,6 @@ constexpr int32_t FFRT_DUMP_METRIC_LEN = 3; constexpr int32_t COLLECT_FFRT_METRIC_MAX_SIZE = 5000; constexpr int32_t FFRT_STAT_SIZE = sizeof(ffrt_stat); constexpr int32_t BUFFER_SIZE = FFRT_STAT_SIZE * COLLECT_FFRT_METRIC_MAX_SIZE; -char* g_ffrtMetricBuffer = nullptr; -bool g_collectEnable = false; -std::mutex ffrtMetricLock_; constexpr int32_t DELAY_TIME = 60 * 1000; constexpr const char* FFRT_STAT_STR_START = "--start-stat"; constexpr const char* FFRT_STAT_STR_STOP = "--stop-stat"; @@ -63,6 +60,9 @@ constexpr const char* IPC_DUMP_FAIL = " fail\n"; } std::shared_ptr SystemAbilityManagerDumper::handler_ = nullptr; +char* SystemAbilityManagerDumper::ffrtMetricBuffer = nullptr; +bool SystemAbilityManagerDumper::collectEnable = false; +std::mutex SystemAbilityManagerDumper::ffrtMetricLock; void SystemAbilityManagerDumper::ShowListenerHelp(string& result) { @@ -339,19 +339,19 @@ void SystemAbilityManagerDumper::CollectFfrtMetricInfoInProcs(int32_t fd, const bool SystemAbilityManagerDumper::StartFfrtStatistics(std::string& result) { - if (g_collectEnable) { + if (collectEnable) { result.append("collect has been started\n"); return false; } ClearFfrtStatisticsBufferLocked(); - g_ffrtMetricBuffer = new char[BUFFER_SIZE](); - auto ret = ffrt_dump(ffrt_dump_cmd_t::DUMP_START_STAT, g_ffrtMetricBuffer, BUFFER_SIZE); + ffrtMetricBuffer = new char[BUFFER_SIZE](); + auto ret = ffrt_dump(ffrt_dump_cmd_t::DUMP_START_STAT, ffrtMetricBuffer, BUFFER_SIZE); if (ret != ERR_OK) { ClearFfrtStatisticsBufferLocked(); result.append("collect start failed\n"); return false; } - g_collectEnable = true; + collectEnable = true; result.append("collect start success\n"); if (handler_ == nullptr) { handler_ = std::make_shared("ffrtDumpHandler"); @@ -363,12 +363,12 @@ bool SystemAbilityManagerDumper::StartFfrtStatistics(std::string& result) bool SystemAbilityManagerDumper::StopFfrtStatistics(std::string& result) { - if (!g_collectEnable) { + if (!collectEnable) { result.append("collect has not been started\n"); return false; } - g_collectEnable = false; - auto ret = ffrt_dump(ffrt_dump_cmd_t::DUMP_STOP_STAT, g_ffrtMetricBuffer, BUFFER_SIZE); + collectEnable = false; + auto ret = ffrt_dump(ffrt_dump_cmd_t::DUMP_STOP_STAT, ffrtMetricBuffer, BUFFER_SIZE); if (ret != ERR_OK) { ClearFfrtStatisticsBufferLocked(); result.append("collect stop failed\n"); @@ -380,11 +380,11 @@ bool SystemAbilityManagerDumper::StopFfrtStatistics(std::string& result) bool SystemAbilityManagerDumper::GetFfrtStatistics(std::string& result) { - if (g_collectEnable) { + if (collectEnable) { result.append("collect has not been stopped\n"); return false; } - if (g_ffrtMetricBuffer == nullptr) { + if (ffrtMetricBuffer == nullptr) { result.append("info not collected\n"); return false; } @@ -396,8 +396,8 @@ bool SystemAbilityManagerDumper::GetFfrtStatistics(std::string& result) void SystemAbilityManagerDumper::FfrtStatisticsParser(std::string& result) { - ffrt_stat* currentStat = (ffrt_stat*)g_ffrtMetricBuffer; - char* lastStat = g_ffrtMetricBuffer + BUFFER_SIZE; + ffrt_stat* currentStat = (ffrt_stat*)ffrtMetricBuffer; + char* lastStat = ffrtMetricBuffer + BUFFER_SIZE; std::string taskInfo; uint64_t maxTime = 0; uint64_t minTime = std::numeric_limits::max(); @@ -436,9 +436,9 @@ void SystemAbilityManagerDumper::FfrtStatisticsParser(std::string& result) void SystemAbilityManagerDumper::ClearFfrtStatisticsBufferLocked() { - if (g_ffrtMetricBuffer != nullptr) { - delete[] g_ffrtMetricBuffer; - g_ffrtMetricBuffer = nullptr; + if (ffrtMetricBuffer != nullptr) { + delete[] ffrtMetricBuffer; + ffrtMetricBuffer = nullptr; HILOGI("ClearFfrtStatisticsBuffer success"); } if (handler_ != nullptr) { @@ -449,20 +449,20 @@ void SystemAbilityManagerDumper::ClearFfrtStatisticsBufferLocked() void SystemAbilityManagerDumper::ClearFfrtStatistics() { HILOGW("ClearFfrtStatistics start"); - std::lock_guard autoLock(ffrtMetricLock_); - if (g_collectEnable) { - auto ret = ffrt_dump(ffrt_dump_cmd_t::DUMP_STOP_STAT, g_ffrtMetricBuffer, BUFFER_SIZE); + std::lock_guard autoLock(ffrtMetricLock); + if (collectEnable) { + auto ret = ffrt_dump(ffrt_dump_cmd_t::DUMP_STOP_STAT, ffrtMetricBuffer, BUFFER_SIZE); if (ret != ERR_OK) { HILOGE("ClearFfrtStatistics stop ffrt_dump err:%{public}d", ret); } - g_collectEnable = false; + collectEnable = false; } ClearFfrtStatisticsBufferLocked(); } bool SystemAbilityManagerDumper::CollectFfrtStatistics(int32_t cmd, std::string& result) { - std::lock_guard autoLock(ffrtMetricLock_); + std::lock_guard autoLock(ffrtMetricLock); result.append("pid:" + ToString(getpid()) + " "); auto ret = false; switch (cmd) { diff --git a/services/samgr/native/test/unittest/BUILD.gn b/services/samgr/native/test/unittest/BUILD.gn index 71bb6cef255e212d493506d4fe016f6d2db87a38..1093230ac49c32258816667ce4e2c884f006f2ed 100644 --- a/services/samgr/native/test/unittest/BUILD.gn +++ b/services/samgr/native/test/unittest/BUILD.gn @@ -698,6 +698,52 @@ ohos_unittest("SystemAbilityMgrDumperTest") { } } +ohos_unittest("MockSystemAbilityManagerTest") { + module_out_path = module_output_path + + sources = [ + "${samgr_services_dir}/source/ffrt_handler.cpp", + "${samgr_services_dir}/source/system_ability_manager_dumper.cpp", + "${samgr_services_dir}/test/unittest/src/mock_system_ability_manager_test.cpp", + ] + + configs = [ + ":sam_test_config", + "${samgr_dir}/services/samgr/native:sam_config", + "${samgr_dir}/test/resource:coverage_flags", + ] + + if (target_cpu == "arm") { + cflags = [ "-DBINDER_IPC_32BIT" ] + } + + deps = [ ":samgr_proxy_tdd" ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "c_utils:utils", + "ffrt:libffrt", + "googletest:gtest_main", + "hilog:libhilog", + "hisysevent:libhisysevent", + "hitrace:hitrace_meter", + "init:libbeget_proxy", + "init:libbegetutil", + "ipc:ipc_single", + "ipc:libdbinder", + "json:nlohmann_json_static", + "safwk:system_ability_fwk", + ] + defines = [] + if (samgr_support_access_token) { + external_deps += [ + "access_token:libnativetoken_shared", + "access_token:libtokensetproc_shared", + ] + defines += [ "SUPPORT_ACCESS_TOKEN" ] + } +} + ohos_executable("manual_ondemand") { testonly = true sources = [ @@ -933,6 +979,7 @@ group("unittest") { testonly = true deps = [ ":LocalAbilityManagerProxyTest", + ":MockSystemAbilityManagerTest", ":SystemAbilityMgrCollectTest", ":SystemAbilityMgrDeviceNetworkingTest", ":SystemAbilityMgrDumperTest", diff --git a/services/samgr/native/test/unittest/include/mock_system_ability_manager_test.h b/services/samgr/native/test/unittest/include/mock_system_ability_manager_test.h new file mode 100644 index 0000000000000000000000000000000000000000..cf048648baa0c43773048abf5ab0220fb01a82d3 --- /dev/null +++ b/services/samgr/native/test/unittest/include/mock_system_ability_manager_test.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2025 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 MOCK_SYSTEM_ABILITY_MAGAGER_TEST_H +#define MOCK_SYSTEM_ABILITY_MAGAGER_TEST_H + +#include "gtest/gtest.h" + +namespace OHOS { +class MockSystemAbilityManagerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; +} +#endif /* MOCK_SYSTEM_ABILITY_MAGAGER_TEST_H */ \ No newline at end of file diff --git a/services/samgr/native/test/unittest/src/mock_system_ability_manager_test.cpp b/services/samgr/native/test/unittest/src/mock_system_ability_manager_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc7eb388ea55ba1e8c91de109b4f03a6b6683ece --- /dev/null +++ b/services/samgr/native/test/unittest/src/mock_system_ability_manager_test.cpp @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2025 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 "test_log.h" +#include "mock_system_ability_manager_test.h" +#include "ffrt_inner.h" + +#define private public +#include "system_ability_manager_dumper.h" + +using namespace std; +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace ffrt { +#ifdef __cplusplus +extern "C" { +#endif //__cplusplus +int ffrt_dump(ffrt_dump_cmd_t cmd, char *buf, uint32_t len) +{ + return -1; +} +#ifdef __cplusplus +} +#endif +} +void MockSystemAbilityManagerTest::SetUpTestCase() +{ + DTEST_LOG << "SetUpTestCase" << std::endl; +} + +void MockSystemAbilityManagerTest::TearDownTestCase() +{ + DTEST_LOG << "TearDownTestCase" << std::endl; +} + +void MockSystemAbilityManagerTest::SetUp() +{ + DTEST_LOG << "SetUp" << std::endl; +} + +void MockSystemAbilityManagerTest::TearDown() +{ + DTEST_LOG << "TearDown" << std::endl; +} + +/** + * @tc.name: StartFfrtStatistics001 + * @tc.desc: StartFfrtStatistics + * @tc.type: FUNC + * @tc.require: IBSPUT + */ +HWTEST_F(MockSystemAbilityManagerTest, StartFfrtStatistics001, TestSize.Level3) +{ + DTEST_LOG << "StartFfrtStatistics001 begin" << std::endl; + std::string result; + EXPECT_FALSE(SystemAbilityManagerDumper::collectEnable); + auto ret = SystemAbilityManagerDumper::StartFfrtStatistics(result); + EXPECT_FALSE(ret); + DTEST_LOG << "StartFfrtStatistics001 end" << std::endl; +} + +/** + * @tc.name: StopFfrtStatistics001 + * @tc.desc: StopFfrtStatistics + * @tc.type: FUNC + * @tc.require: IBSPUT + */ +HWTEST_F(MockSystemAbilityManagerTest, StopFfrtStatistics001, TestSize.Level3) +{ + DTEST_LOG << "StopFfrtStatistics001 begin" << std::endl; + std::string result; + SystemAbilityManagerDumper::collectEnable = true; + auto ret = SystemAbilityManagerDumper::StopFfrtStatistics(result); + EXPECT_FALSE(ret); + DTEST_LOG << "StopFfrtStatistics001 end" << std::endl; +} + +/** + * @tc.name: ClearFfrtStatistics001 + * @tc.desc: ClearFfrtStatistics + * @tc.type: FUNC + * @tc.require: IBSPUT + */ +HWTEST_F(MockSystemAbilityManagerTest, ClearFfrtStatistics001, TestSize.Level3) +{ + DTEST_LOG << "ClearFfrtStatistics001 begin" << std::endl; + SystemAbilityManagerDumper::collectEnable = true; + SystemAbilityManagerDumper::ClearFfrtStatistics(); + EXPECT_FALSE(SystemAbilityManagerDumper::collectEnable); + DTEST_LOG << "ClearFfrtStatistics001 end" << std::endl; +} +} \ No newline at end of file diff --git a/services/samgr/native/test/unittest/src/system_ability_manager_dumper_test.cpp b/services/samgr/native/test/unittest/src/system_ability_manager_dumper_test.cpp index 42cb034b31ba84676d62ca629636e62f7118ef14..856df813899bf2109a60c0590ceb7d85255e83ab 100644 --- a/services/samgr/native/test/unittest/src/system_ability_manager_dumper_test.cpp +++ b/services/samgr/native/test/unittest/src/system_ability_manager_dumper_test.cpp @@ -17,6 +17,7 @@ #include "ability_death_recipient.h" #include "system_ability_status_change_proxy.h" #include "ipc_skeleton.h" +#include "ffrt_inner.h" #include "test_log.h" #include @@ -365,8 +366,20 @@ HWTEST_F(SystemAbilityManagerDumperTest, CollectFfrtStatistics001, TestSize.Leve EXPECT_FALSE(ret); ret = SystemAbilityManagerDumper::CollectFfrtStatistics(FFRT_STAT_CMD_GET, result); EXPECT_FALSE(ret); + auto testTask1 = [] () { + DTEST_LOG << "testTask1 end" << std::endl; + }; + auto testTask2 = [] () { + DTEST_LOG << "testTask2 end" << std::endl; + }; + SystemAbilityManagerDumper::handler_->PostTask(testTask1, "testTask1", 0); + SystemAbilityManagerDumper::handler_->PostTask(testTask2, "testTask2", 0); + usleep(10 * 1000); ret = SystemAbilityManagerDumper::CollectFfrtStatistics(FFRT_STAT_CMD_STOP, result); EXPECT_TRUE(ret); + ffrt_stat* currentStat = (ffrt_stat*)SystemAbilityManagerDumper::ffrtMetricBuffer; + ASSERT_FALSE(currentStat == nullptr); + currentStat->endTime = 0; ret = SystemAbilityManagerDumper::CollectFfrtStatistics(FFRT_STAT_CMD_GET, result); EXPECT_TRUE(ret); DTEST_LOG << "CollectFfrtStatistics001 end" << std::endl; @@ -1301,6 +1314,7 @@ HWTEST_F(SystemAbilityManagerDumperTest, GetFfrtDumpInfoProc001, TestSize.Level2 std::make_shared(); std::vector args; args.emplace_back("--ffrt"); + args.emplace_back(""); std::string result; bool ret = SystemAbilityManagerDumper::GetFfrtDumpInfoProc(systemAbilityStateScheduler, args, result); EXPECT_EQ(ret, false); @@ -1320,7 +1334,7 @@ HWTEST_F(SystemAbilityManagerDumperTest, GetFfrtDumpInfoProc002, TestSize.Level2 std::make_shared(); std::vector args; args.emplace_back("--ffrt"); - args.emplace_back(""); + args.emplace_back("12k"); std::string result; bool ret = SystemAbilityManagerDumper::GetFfrtDumpInfoProc(systemAbilityStateScheduler, args, result); EXPECT_EQ(ret, false); @@ -1336,26 +1350,6 @@ HWTEST_F(SystemAbilityManagerDumperTest, GetFfrtDumpInfoProc002, TestSize.Level2 HWTEST_F(SystemAbilityManagerDumperTest, GetFfrtDumpInfoProc003, TestSize.Level2) { DTEST_LOG << "GetFfrtDumpInfoProc003 begin" << std::endl; - std::shared_ptr systemAbilityStateScheduler = - std::make_shared(); - std::vector args; - args.emplace_back("--ffrt"); - args.emplace_back("12k"); - std::string result; - bool ret = SystemAbilityManagerDumper::GetFfrtDumpInfoProc(systemAbilityStateScheduler, args, result); - EXPECT_EQ(ret, false); - DTEST_LOG << "GetFfrtDumpInfoProc003 end" << std::endl; -} - -/** - * @tc.name: GetFfrtDumpInfoProc004 - * @tc.desc: test GetFfrtDumpInfoProc - * @tc.type: FUNC - * @tc.require: I6W28 - */ -HWTEST_F(SystemAbilityManagerDumperTest, GetFfrtDumpInfoProc004, TestSize.Level2) -{ - DTEST_LOG << "GetFfrtDumpInfoProc004 begin" << std::endl; std::shared_ptr systemAbilityStateScheduler = std::make_shared(); sptr saMgr = new SystemAbilityManager; @@ -1368,17 +1362,17 @@ HWTEST_F(SystemAbilityManagerDumperTest, GetFfrtDumpInfoProc004, TestSize.Level2 std::string result; bool ret = SystemAbilityManagerDumper::GetFfrtDumpInfoProc(systemAbilityStateScheduler, args, result); EXPECT_EQ(ret, true); - DTEST_LOG << "GetFfrtDumpInfoProc004 end" << std::endl; + DTEST_LOG << "GetFfrtDumpInfoProc003 end" << std::endl; } /** - * @tc.name: GetFfrtDumpInfoProc005 + * @tc.name: GetFfrtDumpInfoProc004 * @tc.desc: test GetFfrtDumpInfoProc * @tc.type: FUNC */ -HWTEST_F(SystemAbilityManagerDumperTest, GetFfrtDumpInfoProc005, TestSize.Level1) +HWTEST_F(SystemAbilityManagerDumperTest, GetFfrtDumpInfoProc004, TestSize.Level1) { - DTEST_LOG << "GetFfrtDumpInfoProc005 begin" << std::endl; + DTEST_LOG << "GetFfrtDumpInfoProc004 begin" << std::endl; std::shared_ptr systemAbilityStateScheduler = std::make_shared(); sptr saMgr = new SystemAbilityManager; @@ -1397,7 +1391,7 @@ HWTEST_F(SystemAbilityManagerDumperTest, GetFfrtDumpInfoProc005, TestSize.Level1 std::string result; bool ret = SystemAbilityManagerDumper::GetFfrtDumpInfoProc(systemAbilityStateScheduler, args, result); EXPECT_EQ(ret, true); - DTEST_LOG << "GetFfrtDumpInfoProc005 end" << std::endl; + DTEST_LOG << "GetFfrtDumpInfoProc004 end" << std::endl; } /** diff --git a/test/fuzztest/samgrdumper_fuzzer/samgrdumper_fuzzer.cpp b/test/fuzztest/samgrdumper_fuzzer/samgrdumper_fuzzer.cpp index cf97ef68a9a727c4f8e8ef403c2e070b3390bb93..c81daef52609c6e69597f3d0b1cf68467b6400c8 100644 --- a/test/fuzztest/samgrdumper_fuzzer/samgrdumper_fuzzer.cpp +++ b/test/fuzztest/samgrdumper_fuzzer/samgrdumper_fuzzer.cpp @@ -90,7 +90,6 @@ void SamgrDumperFuzzTest(const uint8_t* data, size_t size) SystemAbilityManagerDumper::GetSamgrIpcStatistics(result); SystemAbilityManagerDumper::StopSamgrIpcStatistics(result); SystemAbilityManagerDumper::StartSamgrIpcStatistics(result); - SystemAbilityManagerDumper::GetFfrtDumpInfoProc(scheduler, args, result); SystemAbilityManagerDumper::GetSAMgrFfrtInfo(result); int32_t pid = BuildInt32FromData(data, size); SystemAbilityManagerDumper::DumpFfrtInfoInProc(scheduler, pid, result);