From 54fc5f96ef668f643c3ee147192b0f1c5fa7a8e7 Mon Sep 17 00:00:00 2001 From: leiguangyu Date: Tue, 15 Jul 2025 12:14:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Elperf=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I1c5286c31e355fefc6e3567792a4c1282064565c Signed-off-by: leiguangyu --- test/unittest/process_dump/BUILD.gn | 7 +- .../process_dump/lperf_event_record_test.cpp | 122 +++++++++++++++ .../process_dump/lperf_events_test.cpp | 141 ++++++++++++++++++ tools/process_dump/lperf/lperf_events.cpp | 13 +- 4 files changed, 274 insertions(+), 9 deletions(-) create mode 100644 test/unittest/process_dump/lperf_event_record_test.cpp create mode 100644 test/unittest/process_dump/lperf_events_test.cpp diff --git a/test/unittest/process_dump/BUILD.gn b/test/unittest/process_dump/BUILD.gn index 9e8b450de..e560eb281 100644 --- a/test/unittest/process_dump/BUILD.gn +++ b/test/unittest/process_dump/BUILD.gn @@ -72,6 +72,7 @@ if (defined(ohos_lite)) { "$faultloggerd_path/test/utils", "$faultloggerd_path/tools/process_dump", "$faultloggerd_path/tools/process_dump/coredump", + "$faultloggerd_path/tools/process_dump/lperf", ] } @@ -80,10 +81,14 @@ if (defined(ohos_lite)) { ohos_unittest("test_processdump") { module_out_path = module_output_path sources = [ + "$faultloggerd_path/tools/process_dump/lperf/lperf_event_record.cpp", + "$faultloggerd_path/tools/process_dump/lperf/lperf_events.cpp", "dfx_processdump_test.cpp", "dump_info_header_test.cpp", "dump_utils_test.cpp", "fault_stack_test.cpp", + "lperf_event_record_test.cpp", + "lperf_events_test.cpp", "maps_test.cpp", "memory_near_register_test.cpp", "multithread_constructor.c", @@ -128,8 +133,8 @@ if (defined(ohos_lite)) { "googletest:gtest_main", "hilog:libhilog", "hitrace:libhitracechain", - "jsoncpp:jsoncpp", "ipc:ipc_core", + "jsoncpp:jsoncpp", "samgr:samgr_proxy", ] if (support_jsapi) { diff --git a/test/unittest/process_dump/lperf_event_record_test.cpp b/test/unittest/process_dump/lperf_event_record_test.cpp new file mode 100644 index 000000000..684193538 --- /dev/null +++ b/test/unittest/process_dump/lperf_event_record_test.cpp @@ -0,0 +1,122 @@ +/* + * 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 +#include +#include "lperf_event_record.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace HiviewDFX { +class LperfEventRecordTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void LperfEventRecordTest::SetUpTestCase() +{} + +void LperfEventRecordTest::TearDownTestCase() +{} + +void LperfEventRecordTest::SetUp() +{} + +void LperfEventRecordTest::TearDown() +{} + +struct TestRecordSample { + perf_event_header header_; + LperfRecordSampleData data_; +}; + +/** + * @tc.name: CreateLperfRecordTest001 + * @tc.desc: test LperfEventRecord type PERF_RECORD_SAMPLE + * @tc.type: FUNC + */ +HWTEST_F(LperfEventRecordTest, CreateLperfRecordTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CreateLperfRecordTest001: start."; + TestRecordSample sample = { + {PERF_RECORD_SAMPLE, PERF_RECORD_MISC_KERNEL, sizeof(TestRecordSample)}, + {}}; + uint64_t ips[4] = {0, 1, 2, 3}; + sample.data_.ips = ips; + sample.data_.nr = 4; + sample.data_.pid = 2; + sample.data_.tid = 3; + sample.data_.time = 4; + + LperfRecordSample record = LperfRecordFactory::GetLperfRecord(PERF_RECORD_SAMPLE, (uint8_t *)&sample); + EXPECT_EQ(record.GetType(), PERF_RECORD_SAMPLE); + EXPECT_EQ(record.GetName(), "sample"); + EXPECT_EQ(record.data_.pid, 2); + EXPECT_EQ(record.data_.tid, 3); + EXPECT_EQ(record.data_.time, 4); + EXPECT_EQ(record.data_.nr, 4); + GTEST_LOG_(INFO) << "CreateLperfRecordTest001: end."; +} + +/** + * @tc.name: CreateLperfRecordTest002 + * @tc.desc: test LperfEventRecord invalid type + * @tc.type: FUNC + */ +HWTEST_F(LperfEventRecordTest, CreateLperfRecordTest002, TestSize.Level2) +{ + GTEST_LOG_(INFO) << "CreateLperfRecordTest002: start."; + TestRecordSample sample = { + {PERF_RECORD_MMAP, PERF_RECORD_MISC_KERNEL, sizeof(TestRecordSample)}, + {}}; + uint64_t ips[4] = {0, 1, 2, 3}; + sample.data_.ips = ips; + sample.data_.nr = 4; + sample.data_.pid = 2; + sample.data_.tid = 3; + sample.data_.time = 4; + LperfRecordSample record = LperfRecordFactory::GetLperfRecord(PERF_RECORD_MMAP, (uint8_t *)&sample); + EXPECT_EQ(record.data_.pid, 0); + EXPECT_EQ(record.data_.tid, 0); + EXPECT_EQ(record.data_.time, 0); + EXPECT_EQ(record.data_.nr, 0); + EXPECT_EQ(record.data_.ips, nullptr); + GTEST_LOG_(INFO) << "CreateLperfRecordTest002: end."; +} + +/** + * @tc.name: CreateLperfRecordTest003 + * @tc.desc: test LperfEventRecord invalid data + * @tc.type: FUNC + */ +HWTEST_F(LperfEventRecordTest, CreateLperfRecordTest003, TestSize.Level2) +{ + GTEST_LOG_(INFO) << "CreateLperfRecordTest003: start."; + LperfRecordSample record = LperfRecordFactory::GetLperfRecord(PERF_RECORD_SAMPLE, nullptr); + EXPECT_EQ(record.GetType(), PERF_RECORD_MMAP); + EXPECT_EQ(record.data_.pid, 0); + EXPECT_EQ(record.data_.tid, 0); + EXPECT_EQ(record.data_.time, 0); + EXPECT_EQ(record.data_.nr, 0); + EXPECT_EQ(record.data_.ips, nullptr); + GTEST_LOG_(INFO) << "CreateLperfRecordTest003: end."; +} +} // namespace HiviewDFX +} // namespace OHOS diff --git a/test/unittest/process_dump/lperf_events_test.cpp b/test/unittest/process_dump/lperf_events_test.cpp new file mode 100644 index 000000000..ead95825c --- /dev/null +++ b/test/unittest/process_dump/lperf_events_test.cpp @@ -0,0 +1,141 @@ +/* + * 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 +#include +#include "dfx_test_util.h" +#include "lperf_events.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace HiviewDFX { +class LperfEventsTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void LperfEventsTest::SetUpTestCase() +{} + +void LperfEventsTest::TearDownTestCase() +{} + +void LperfEventsTest::SetUp() +{} + +void LperfEventsTest::TearDown() +{} + +/** + * @tc.name: LperfEventsTestTest002 + * @tc.desc: test LperfEvents invalid tids + * @tc.type: FUNC + */ +HWTEST_F(LperfEventsTest, LperfEventsTestTest002, TestSize.Level2) +{ + GTEST_LOG_(INFO) << "LperfEventsTestTest002: start."; + if (IsLinuxKernel()) { + return; + } + LperfEvents lperfEvents_; + lperfEvents_.SetTid({}); + lperfEvents_.SetTimeOut(100); + lperfEvents_.SetSampleFrequency(5000); + EXPECT_EQ(lperfEvents_.PrepareRecord(), -1); + GTEST_LOG_(INFO) << "LperfEventsTestTest002: end."; +} + +/** + * @tc.name: LperfEventsTestTest003 + * @tc.desc: test LperfEvents invalid freq + * @tc.type: FUNC + */ +HWTEST_F(LperfEventsTest, LperfEventsTestTest003, TestSize.Level2) +{ + GTEST_LOG_(INFO) << "LperfEventsTestTest003: start."; + if (IsLinuxKernel()) { + return; + } + LperfEvents lperfEvents_; + lperfEvents_.SetTid({getpid()}); + lperfEvents_.SetTimeOut(2000); + lperfEvents_.SetSampleFrequency(5000); + EXPECT_EQ(lperfEvents_.PrepareRecord(), -1); + GTEST_LOG_(INFO) << "LperfEventsTestTest003: end."; +} + +/** + * @tc.name: LperfEventsTestTest004 + * @tc.desc: test LperfEvents invalid freq -1 + * @tc.type: FUNC + */ +HWTEST_F(LperfEventsTest, LperfEventsTestTest004, TestSize.Level2) +{ + GTEST_LOG_(INFO) << "LperfEventsTestTest004: start."; + if (IsLinuxKernel()) { + return; + } + LperfEvents lperfEvents_; + lperfEvents_.SetTid({getpid()}); + lperfEvents_.SetTimeOut(5000); + lperfEvents_.SetSampleFrequency(-1); + EXPECT_EQ(lperfEvents_.PrepareRecord(), -1); + GTEST_LOG_(INFO) << "LperfEventsTestTest004: end."; +} + +/** + * @tc.name: LperfEventsTestTest005 + * @tc.desc: test LperfEvents invalid time + * @tc.type: FUNC + */ +HWTEST_F(LperfEventsTest, LperfEventsTestTest005, TestSize.Level2) +{ + GTEST_LOG_(INFO) << "LperfEventsTestTest005: start."; + if (IsLinuxKernel()) { + return; + } + LperfEvents lperfEvents_; + lperfEvents_.SetTid({getpid()}); + lperfEvents_.SetTimeOut(20000); + lperfEvents_.SetSampleFrequency(100); + EXPECT_EQ(lperfEvents_.PrepareRecord(), -1); + GTEST_LOG_(INFO) << "LperfEventsTestTest005: end."; +} + +/** + * @tc.name: LperfEventsTestTest006 + * @tc.desc: test LperfEvents invalid time -1 + * @tc.type: FUNC + */ +HWTEST_F(LperfEventsTest, LperfEventsTestTest006, TestSize.Level2) +{ + GTEST_LOG_(INFO) << "LperfEventsTestTest006: start."; + if (IsLinuxKernel()) { + return; + } + LperfEvents lperfEvents_; + lperfEvents_.SetTid({getpid()}); + lperfEvents_.SetTimeOut(-1); + lperfEvents_.SetSampleFrequency(100); + EXPECT_EQ(lperfEvents_.PrepareRecord(), -1); + GTEST_LOG_(INFO) << "LperfEventsTestTest006: end."; +} +} // namespace HiviewDFX +} // namespace OHOS diff --git a/tools/process_dump/lperf/lperf_events.cpp b/tools/process_dump/lperf/lperf_events.cpp index 3db674247..2c546947f 100644 --- a/tools/process_dump/lperf/lperf_events.cpp +++ b/tools/process_dump/lperf/lperf_events.cpp @@ -119,9 +119,9 @@ void LperfEvents::SetRecordCallBack(ProcessRecordCB recordCallBack) bool LperfEvents::PrepareFdEvents() { - unsigned int times = 4; + constexpr unsigned int sizekiB = 1024; struct LperfInitArg initArg = { - .rbSizeIntKb = (mmapPages_ + 1) * times, + .rbSizeIntKb = (mmapPages_ + 1) * pageSize_ / sizekiB, .samplePeriod = sampleFreq_, .sampleInterval = timeOut_, .watermark = DEFAULT_WATER_MARK, @@ -238,20 +238,17 @@ bool LperfEvents::RecordLoop() void LperfEvents::Clear() { LperfRecordFactory::ClearData(); - const unsigned int size = 4096; if (lperfMmap_.mmapPage != nullptr) { - if (munmap(lperfMmap_.mmapPage, static_cast((mmapPages_ + 1) * size)) < 0) { + if (munmap(lperfMmap_.mmapPage, static_cast((mmapPages_ + 1) * pageSize_)) < 0) { DFXLOGE("munmap lperfMmap failed"); } lperfMmap_.mmapPage = nullptr; } - if (pollFds_.size() > 0) { - pollFds_.clear(); - } + pollFds_.clear(); if (lperfFd_ != -1) { close(lperfFd_); lperfFd_ = -1; } } } // namespace HiviewDFX -} // namespace OHOS \ No newline at end of file +} // namespace OHOS -- Gitee