From 296912154f21f6d4fdf31c8b744faee38a6fa5ee Mon Sep 17 00:00:00 2001 From: leiguangyu Date: Wed, 23 Apr 2025 10:36:10 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8D=E5=B0=8F=E5=9E=8B=E5=8C=96?= =?UTF-8?q?=E5=88=86=E9=85=8DMmapPages=E5=A4=A7=E5=B0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I7bb0288191507cb78bb3dddbb9680ee18ee23ccc Signed-off-by: leiguangyu --- BUILD.gn | 4 ++ bundle.json | 2 + include/subcommand_record.h | 8 +++ src/perf_events.cpp | 2 +- src/subcommand_record.cpp | 52 +++++++++++++++++++ test/BUILD.gn | 2 + .../common/native/subcommand_record_test.cpp | 11 ++++ 7 files changed, 80 insertions(+), 1 deletion(-) diff --git a/BUILD.gn b/BUILD.gn index fc23c69..a5359e5 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -261,7 +261,9 @@ ohos_source_set("hiperf_platform_linux") { } external_deps += [ "bounds_checking_function:libsec_shared", + "cJSON:cjson", "c_utils:utils", + "config_policy:configpolicy_util", "zlib:libz", ] if (hiperf_independent_compilation) { @@ -428,7 +430,9 @@ ohos_executable("hiperf") { "abseil-cpp:absl_log", "abseil-cpp:absl_strings", "bounds_checking_function:libsec_shared", + "cJSON:cjson", "c_utils:utils", + "config_policy:configpolicy_util", "faultloggerd:libunwinder", "hilog:libhilog", "ipc:ipc_single", diff --git a/bundle.json b/bundle.json index b0746e8..3d7200f 100644 --- a/bundle.json +++ b/bundle.json @@ -23,7 +23,9 @@ "abseil-cpp", "bounds_checking_function", "bundle_framework", + "cJSON", "c_utils", + "config_policy", "faultloggerd", "hilog", "hisysevent", diff --git a/include/subcommand_record.h b/include/subcommand_record.h index 35f06d2..dbd7f53 100644 --- a/include/subcommand_record.h +++ b/include/subcommand_record.h @@ -30,6 +30,7 @@ #include #include #include +#include "cJSON.h" #include "perf_event_record.h" #include "perf_events.h" #include "perf_file_writer.h" @@ -280,6 +281,13 @@ private: void OutputRecordFile(); bool PostOutputRecordFile(bool output); + static constexpr char PRODUCT_CONFIG_PATH[] = "etc/hiperf/hiperf_cfg.json"; + static constexpr char CFG_MAP_PAGES[] = "MmapPages"; + + cJSON* GetProductCfgRoot(const char* cfgPath); + cJSON* ParseJson(const std::string &filePath); + bool GetJsonNum(cJSON* tag, const char* key, int &value); + void GetCfgOptions(); bool GetOptions(std::vector &args); bool CheckArgsRange(); bool CheckExcludeArgs(); diff --git a/src/perf_events.cpp b/src/perf_events.cpp index 6c98ef9..eb80d3c 100644 --- a/src/perf_events.cpp +++ b/src/perf_events.cpp @@ -1333,7 +1333,7 @@ bool PerfEvents::CreateMmap(const FdItem &item, const perf_event_attr &attr) cpuMmap_[item.cpu] = mmapItem; pollFds_.emplace_back(pollfd {mmapItem.fd, POLLIN, 0}); - HLOGD("CreateMmap success cpu %d fd %d", item.cpu, mmapItem.fd); + HLOGD("CreateMmap success cpu %d fd %d mmapPages_ %d", item.cpu, mmapItem.fd, mmapPages_); } else { const MmapFd &mmapItem = it->second; int rc = ioctl(item.fd.Get(), PERF_EVENT_IOC_SET_OUTPUT, mmapItem.fd); diff --git a/src/subcommand_record.cpp b/src/subcommand_record.cpp index 330aa75..4e4b846 100644 --- a/src/subcommand_record.cpp +++ b/src/subcommand_record.cpp @@ -28,6 +28,8 @@ #include #include +#include "config_policy_utils.h" + #include "command.h" #include "debug_logger.h" #include "hiperf_client.h" @@ -550,8 +552,58 @@ bool SubCommandRecord::CheckOptions() return true; } +cJSON* SubCommandRecord::GetProductCfgRoot(const char* cfgPath) +{ + char buf[PATH_MAX] = {0}; + char* configFilePath = GetOneCfgFile(cfgPath, buf, PATH_MAX); + if (configFilePath == nullptr || (configFilePath[0] == '\0') || (strlen(configFilePath) > PATH_MAX)) { + HLOGD("get configFilePath from cfgPath %s failed", cfgPath); + return nullptr; + } + HLOGD("get configFilePath %s from cfgPath %s", configFilePath, cfgPath); + return ParseJson(configFilePath); +} + +cJSON* SubCommandRecord::ParseJson(const std::string &filePath) +{ + std::ifstream inFile(filePath, std::ios::in); + if (!inFile.is_open()) { + HLOGE("open file %s failed", filePath.c_str()); + return nullptr; + } + std::string fileContent((std::istreambuf_iterator(inFile)), std::istreambuf_iterator()); + cJSON* root = cJSON_Parse(fileContent.c_str()); + inFile.close(); + return root; +} + +bool SubCommandRecord::GetJsonNum(cJSON* tag, const char* key, int &value) +{ + cJSON* subNode = cJSON_GetObjectItem(tag, key); + if (subNode != nullptr && cJSON_IsNumber(subNode)) { + value = static_cast(subNode->valueint); + return true; + } + HLOGE("GetJsonNum key %s failed", key); + return false; +} + +void SubCommandRecord::GetCfgOptions() +{ + cJSON* root = GetProductCfgRoot(PRODUCT_CONFIG_PATH); + if (root == nullptr) { + HLOGD("GetProductCfgRoot file %s failed", PRODUCT_CONFIG_PATH); + return; + } + if (GetJsonNum(root, CFG_MAP_PAGES, mmapPages_)) { + HLOGD("GetCfgOptions %s: %d", CFG_MAP_PAGES, mmapPages_); + } + cJSON_Delete(root); +} + bool SubCommandRecord::ParseOption(std::vector &args) { + GetCfgOptions(); if (!GetOptions(args)) { return false; } diff --git a/test/BUILD.gn b/test/BUILD.gn index b513fd3..65d73a6 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -114,7 +114,9 @@ ohos_unittest("hiperf_unittest") { "abseil-cpp:absl_log", "abseil-cpp:absl_strings", "bounds_checking_function:libsec_shared", + "cJSON:cjson", "c_utils:utils", + "config_policy:configpolicy_util", "faultloggerd:libunwinder", "googletest:gmock", "hilog:libhilog", diff --git a/test/unittest/common/native/subcommand_record_test.cpp b/test/unittest/common/native/subcommand_record_test.cpp index 24f6211..a5fe95c 100644 --- a/test/unittest/common/native/subcommand_record_test.cpp +++ b/test/unittest/common/native/subcommand_record_test.cpp @@ -2176,6 +2176,17 @@ HWTEST_F(SubCommandRecordTest, CheckGetCountFromFile, TestSize.Level1) uint32_t cpuOnline = cmd.GetCountFromFile("/sys/devices/system/cpu/online"); ASSERT_GT(cpuOnline, 1); } + +HWTEST_F(SubCommandRecordTest, CheckProductCfg, TestSize.Level1) +{ + SubCommandRecord cmd; + cmd.GetCfgOptions(); + cJSON* root = cmd.GetProductCfgRoot(cmd.PRODUCT_CONFIG_PATH); + if (root) { + EXPECT_EQ(cmd.GetJsonNum(root, cmd.CFG_MAP_PAGES, cmd.mmapPages_), true); + cJSON_Delete(root); + } +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS -- Gitee