diff --git a/BUILD.gn b/BUILD.gn index fc23c698274daa161258abb68748c08216c5a744..a5359e5b58b673c6d4b90a89797a44fe6044bf71 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 b0746e845e1248815e31e4f26ffa9e1b7df34a3f..3d7200faee567d3915cf637caac8933c3c954a08 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 35f06d2edffd651f85d81f42e7726543cae94820..dbd7f53c89617812c6268e2892b5f13d42634cd8 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 6c98ef9ae3dbfc69ca8436df1a31e24723764cd3..eb80d3cf0cebf32a081cfef2efcda432dd12f90d 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 330aa753fd7f8bf7127c62d46690a08dfe95fdfb..4e4b846d577e3feaeaa7b92b6557043a171d869a 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 b513fd3b36854d28021bbf050eeb17fc68c76b50..65d73a64c47f0a84fe73af11357d50d0dbc57e66 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 24f62115112ddbf85129e1af24c99bd0cf6b4cb4..a5fe95ceb412e6c940f024b389cdae23f8ad161b 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