From 15a0018f783327a3b97b3f21e487312024a51a85 Mon Sep 17 00:00:00 2001 From: yuanye Date: Fri, 22 Nov 2024 22:47:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=89=93=E7=82=B9?= =?UTF-8?q?=E4=B8=8A=E6=8A=A5pid=E4=B8=BA=E8=BF=9B=E7=A8=8B=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yuanye --- include/utilities.h | 22 +++++++++++++++++++ src/subcommand_record.cpp | 6 ++++- src/subcommand_stat.cpp | 6 ++++- .../common/native/subcommand_record_test.cpp | 5 +++-- .../common/native/subcommand_stat_test.cpp | 5 +++-- .../unittest/common/native/utilities_test.cpp | 18 +++++++++++++++ 6 files changed, 56 insertions(+), 6 deletions(-) diff --git a/include/utilities.h b/include/utilities.h index bad5069..6ea8dad 100644 --- a/include/utilities.h +++ b/include/utilities.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -160,6 +161,27 @@ std::string VectorToString(const std::vector &items) } } +template +std::string SetToString(const std::unordered_set &items) +{ + std::string itemsString = ""; + const std::string split = ","; + for (auto item : items) { + if (!itemsString.empty()) { + itemsString.append(split); + } + if constexpr (std::is_same::value) { + itemsString.append(item); + } else { + itemsString.append(std::to_string(item)); + } + } + if (itemsString.empty()) { + itemsString.append(""); + } + return itemsString; +} + std::string BufferToHexString(const std::vector &vec); std::string BufferToHexString(const unsigned char buf[], size_t size); void HexDump(const void *buf, size_t size, size_t max_size = 0); diff --git a/src/subcommand_record.cpp b/src/subcommand_record.cpp index 1492ebd..773d2b6 100644 --- a/src/subcommand_record.cpp +++ b/src/subcommand_record.cpp @@ -1931,7 +1931,11 @@ void SubCommandRecord::AddReportArgs(CommandReporter& reporter) } else if (!appPackage_.empty()) { reporter.targetProcess_ = appPackage_; } else { - reporter.targetProcess_ = VectorToString(selectPids_); + std::unordered_set processNames = {}; + for_each(selectPids_.begin(), selectPids_.end(), [&processNames] (const pid_t& pid) { + processNames.insert(GetProcessName(pid)); + }); + reporter.targetProcess_ = SetToString(processNames); } } diff --git a/src/subcommand_stat.cpp b/src/subcommand_stat.cpp index ee50cb2..a476b10 100644 --- a/src/subcommand_stat.cpp +++ b/src/subcommand_stat.cpp @@ -819,7 +819,11 @@ void SubCommandStat::AddReportArgs(CommandReporter& reporter) } else if (!appPackage_.empty()) { reporter.targetProcess_ = appPackage_; } else { - reporter.targetProcess_ = VectorToString(selectPids_); + std::unordered_set processNames = {}; + for_each(selectPids_.begin(), selectPids_.end(), [&processNames] (const pid_t& pid) { + processNames.insert(GetProcessName(pid)); + }); + reporter.targetProcess_ = SetToString(processNames); } } diff --git a/test/unittest/common/native/subcommand_record_test.cpp b/test/unittest/common/native/subcommand_record_test.cpp index 19e7ba0..221b161 100644 --- a/test/unittest/common/native/subcommand_record_test.cpp +++ b/test/unittest/common/native/subcommand_record_test.cpp @@ -1605,12 +1605,13 @@ HWTEST_F(SubCommandRecordTest, ReportSampleAll, TestSize.Level1) HWTEST_F(SubCommandRecordTest, ReportSamplePid, TestSize.Level1) { SubCommandRecord command; - command.selectPids_ = {1, 2, 3}; + command.selectPids_ = { getpid() }; + std::string name = GetProcessName(getpid()); CommandReporter reporter("record"); reporter.isReported_ = true; command.AddReportArgs(reporter); - EXPECT_EQ(reporter.targetProcess_, "1,2,3"); + EXPECT_EQ(reporter.targetProcess_, name); } /** diff --git a/test/unittest/common/native/subcommand_stat_test.cpp b/test/unittest/common/native/subcommand_stat_test.cpp index fda3ce0..99d8045 100644 --- a/test/unittest/common/native/subcommand_stat_test.cpp +++ b/test/unittest/common/native/subcommand_stat_test.cpp @@ -2298,12 +2298,13 @@ HWTEST_F(SubCommandStatTest, ReportSampleAll, TestSize.Level1) HWTEST_F(SubCommandStatTest, ReportSamplePid, TestSize.Level1) { SubCommandStat command; - command.selectPids_ = {1, 2, 3}; + command.selectPids_ = { getpid() }; + std::string name = GetProcessName(getpid()); CommandReporter reporter("stat"); reporter.isReported_ = true; command.AddReportArgs(reporter); - EXPECT_EQ(reporter.targetProcess_, "1,2,3"); + EXPECT_EQ(reporter.targetProcess_, name); } /** diff --git a/test/unittest/common/native/utilities_test.cpp b/test/unittest/common/native/utilities_test.cpp index 0344f7b..c60e521 100644 --- a/test/unittest/common/native/utilities_test.cpp +++ b/test/unittest/common/native/utilities_test.cpp @@ -233,6 +233,24 @@ HWTEST_F(UtilitiesTest, VectorToString, TestSize.Level1) EXPECT_EQ(VectorToString({1.0, 2.0, 3.0}), "1.000000,2.000000,3.000000"); } +/** + * @tc.name: SetToString + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(UtilitiesTest, SetToString, TestSize.Level1) +{ + EXPECT_EQ(SetToString({}), ""); + EXPECT_EQ(SetToString({"a"}), "a"); + + EXPECT_EQ(SetToString({}), ""); + EXPECT_EQ(SetToString({1}), "1"); + EXPECT_EQ(SetToString({1, 2, 3}).size(), 5); + EXPECT_EQ(SetToString({"a", "b", "c"}).size(), 5); + + EXPECT_EQ(SetToString({}), ""); +} + /** * @tc.name: BufferToHexString * @tc.desc: -- Gitee From 769491a51c33b220cac5106440dc3d7a698b54da Mon Sep 17 00:00:00 2001 From: yuanye Date: Sat, 23 Nov 2024 16:45:39 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yuanye --- include/utilities.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/include/utilities.h b/include/utilities.h index 6ea8dad..a65a65a 100644 --- a/include/utilities.h +++ b/include/utilities.h @@ -164,22 +164,22 @@ std::string VectorToString(const std::vector &items) template std::string SetToString(const std::unordered_set &items) { - std::string itemsString = ""; + std::string result = ""; const std::string split = ","; for (auto item : items) { - if (!itemsString.empty()) { - itemsString.append(split); + if (!result.empty()) { + result.append(split); } if constexpr (std::is_same::value) { - itemsString.append(item); + result.append(item); } else { - itemsString.append(std::to_string(item)); + result.append(std::to_string(item)); } } - if (itemsString.empty()) { - itemsString.append(""); + if (result.empty()) { + result.append(""); } - return itemsString; + return result; } std::string BufferToHexString(const std::vector &vec); -- Gitee