diff --git a/device/ohos_test.xml b/device/ohos_test.xml
index 254d8b20e37fef3b58970151a886dfbddab52578..c8cca99d36ba6feaef5ac0fbc7bcbaa833619b1f 100644
--- a/device/ohos_test.xml
+++ b/device/ohos_test.xml
@@ -117,6 +117,8 @@
+
+
diff --git a/device/plugins/native_daemon/BUILD.gn b/device/plugins/native_daemon/BUILD.gn
index 85fd183685f3cece84078d9f505c21a9f22b7686..772938c62108f8724dbf345ea6577986e8c5a5ba 100644
--- a/device/plugins/native_daemon/BUILD.gn
+++ b/device/plugins/native_daemon/BUILD.gn
@@ -130,6 +130,7 @@ ohos_executable("native_daemon") {
"access_token:libaccesstoken_sdk",
"access_token:libtokensetproc_shared",
"bounds_checking_function:libsec_shared",
+ "cJSON:cjson",
"c_utils:utils",
"hicollie:libhicollie",
"hisysevent:libhisysevent",
diff --git a/device/plugins/native_daemon/include/hook_manager.h b/device/plugins/native_daemon/include/hook_manager.h
index 8245f99bb2aaa2ee0c846926413caf1edb2ea5f8..0662f452585be868c45be18e23bd230462e3810d 100644
--- a/device/plugins/native_daemon/include/hook_manager.h
+++ b/device/plugins/native_daemon/include/hook_manager.h
@@ -172,6 +172,7 @@ private:
uint32_t largestSize_ = 0;
uint32_t secondLargestSize_ = 0;
uint32_t maxGrowthSize_ = 0;
+ int32_t maxProcessCount_ = 4;
std::shared_ptr factory_{nullptr};
};
}
diff --git a/device/plugins/native_daemon/include/utilities.h b/device/plugins/native_daemon/include/utilities.h
index 25d39c3f64d73d7e298039031ce1b237c71463c7..00cbd3083e4062204d9600f4727c1d762ddeb54c 100644
--- a/device/plugins/native_daemon/include/utilities.h
+++ b/device/plugins/native_daemon/include/utilities.h
@@ -21,6 +21,7 @@
#include
#include
#include
+#include "cJSON.h"
#include
#include
#include
@@ -299,6 +300,7 @@ float Percentage(const T &a, const T &b)
}
bool PowerOfTwo(int n);
+int GetValueFromJsonFile(const std::string& filePath, const std::string key);
#define INDENT_ONE_LEVEL (indent + 1)
#define INDENT_TWO_LEVEL (indent + 2)
diff --git a/device/plugins/native_daemon/native_memory_profiler_sa/include/native_memory_profiler_sa_service.h b/device/plugins/native_daemon/native_memory_profiler_sa/include/native_memory_profiler_sa_service.h
index 351e60d88704f4be797c2d5dbe39e9f95885099d..0d9bb99e8d1a448c8cf007acd5c3ecb0d1846cae 100644
--- a/device/plugins/native_daemon/native_memory_profiler_sa/include/native_memory_profiler_sa_service.h
+++ b/device/plugins/native_daemon/native_memory_profiler_sa/include/native_memory_profiler_sa_service.h
@@ -74,6 +74,8 @@ private:
private:
std::mutex nmdMtx_;
+ int32_t taskNum_{0};
+ int32_t taskMaxNum_ = 4;
std::string startupModeProcessName_;
};
} // namespace OHOS::Developtools::NativeDaemon
diff --git a/device/plugins/native_daemon/native_memory_profiler_sa/src/native_memory_profiler_sa_service.cpp b/device/plugins/native_daemon/native_memory_profiler_sa/src/native_memory_profiler_sa_service.cpp
index d79b293346fa8a3ca3c67b77b169a10a93871f01..84ed1e0d780d45b5a29238d748fbcc17eee85aad 100644
--- a/device/plugins/native_daemon/native_memory_profiler_sa/src/native_memory_profiler_sa_service.cpp
+++ b/device/plugins/native_daemon/native_memory_profiler_sa/src/native_memory_profiler_sa_service.cpp
@@ -30,9 +30,9 @@
namespace OHOS::Developtools::NativeDaemon {
namespace {
constexpr int32_t TIME_BASE = 1000;
-constexpr int32_t MAX_TASK_NUM = 4;
const std::string FILE_PATH_HEAD = "/data/local/tmp/native_memory_";
const std::string FILE_PATH_TAIL = ".htrace";
+const std::string PRODUCT_CONFIG_PATH = "/sys_prod/etc/hiview/hiprofiler/hiprofiler_cfg.json";
constexpr int32_t DELAYED_SHUTDOWN_TIME = 20;
constexpr int FILE_MODE = 0644;
constexpr int32_t SIMP_NMD = 3;
@@ -431,7 +431,9 @@ int32_t NativeMemoryProfilerSaService::StartHook(std::shared_ptr& config, uint32_t fd)
{
- if (g_taskNum + 1 > MAX_TASK_NUM) {
+ auto maxTaskCount = GetValueFromJsonFile(PRODUCT_CONFIG_PATH, "hiprofiler_hook_process_count");
+ taskMaxNum_ = maxTaskCount > 0 ? maxTaskCount : taskMaxNum_;
+ if (taskNum_ + 1 > taskMaxNum_) {
PROFILER_LOG_INFO(LOG_CORE, "NativeMemoryProfilerSaService: Support up to 4 tasks at the same time");
return false;
}
diff --git a/device/plugins/native_daemon/native_memory_profiler_sa/test/BUILD.gn b/device/plugins/native_daemon/native_memory_profiler_sa/test/BUILD.gn
index 508dcb537e616521bbeee40f658a97450ec67000..7614387348f25e65385afafdfd9bbe651e6c9f22 100644
--- a/device/plugins/native_daemon/native_memory_profiler_sa/test/BUILD.gn
+++ b/device/plugins/native_daemon/native_memory_profiler_sa/test/BUILD.gn
@@ -129,6 +129,7 @@ ohos_unittest("native_memory_profiler_sa_ut") {
"access_token:libaccesstoken_sdk",
"access_token:libtokensetproc_shared",
"bounds_checking_function:libsec_shared",
+ "cJSON:cjson",
"c_utils:utils",
"faultloggerd:libdfx_dumpcatcher",
"googletest:gtest",
diff --git a/device/plugins/native_daemon/src/hook_manager.cpp b/device/plugins/native_daemon/src/hook_manager.cpp
index 4cb469f5f3f8e83c781e5bd76b59367c364bb481..9f714b091d9faddf93f42dbabf0195c4321a3087 100644
--- a/device/plugins/native_daemon/src/hook_manager.cpp
+++ b/device/plugins/native_daemon/src/hook_manager.cpp
@@ -42,6 +42,7 @@ constexpr uint32_t PAGE_BYTES = 4096;
std::shared_ptr g_buffWriter;
const std::string STARTUP = "startup:";
const std::string PARAM_NAME = "libc.hook_mode";
+const std::string PRODUCT_CONFIG_PATH = "/sys_prod/etc/hiview/hiprofiler/hiprofiler_cfg.json";
constexpr int SIGNAL_START_HOOK = 36;
const std::string VERSION = "1.02";
constexpr int32_t RESPONSE_MAX_PID_COUNT = 8;
@@ -107,8 +108,10 @@ bool HookManager::CheckProcess()
return false;
}
} else {
- if (hookCtx_.size() > static_cast(MAX_PID_COUNT)) {
- PROFILER_LOG_ERROR(LOG_CORE, "%s: The maximum allowed is to set %d PIDs.", __func__, MAX_PID_COUNT);
+ auto maxPidCount = GetValueFromJsonFile(PRODUCT_CONFIG_PATH, "hiprofiler_hook_process_count");
+ maxProcessCount_ = maxPidCount > 0 ? maxPidCount : maxProcessCount_;
+ if (hookCtx_.size() > maxProcessCount_) {
+ PROFILER_LOG_ERROR(LOG_CORE, "%s: The maximum allowed is to set %d PIDs.", __func__, maxProcessCount_);
return false;
}
}
diff --git a/device/plugins/native_daemon/src/utilities.cpp b/device/plugins/native_daemon/src/utilities.cpp
index 8bdff1fc2334169401bb52bfb5660b4b83421831..d45126781041090899a451318e694700de602943 100644
--- a/device/plugins/native_daemon/src/utilities.cpp
+++ b/device/plugins/native_daemon/src/utilities.cpp
@@ -54,6 +54,34 @@ std::string StringReplace(std::string source, const std::string &from, const std
return source;
}
+int GetValueFromJsonFile(const std::string& filePath, const std::string key)
+{
+ std::ifstream inFile(filePath, std::ios::in);
+ if (!inFile.is_open()) {
+ HLOGE("parse json file: %s is not existed.", filePath.c_str());
+ return -1;
+ }
+ std::string fileContent((std::istreambuf_iterator(inFile)), std::istreambuf_iterator());
+ cJSON* jsonNode = cJSON_Parse(fileContent.c_str());
+ inFile.close();
+
+ if (jsonNode == nullptr) {
+ HLOGE("parse json file: %s failed.", filePath.c_str());
+ return -1;
+ }
+
+ cJSON* vaulezNode = cJSON_GetObjectItem(jsonNode, key.c_str());
+ if (vaulezNode == nullptr) {
+ HLOGE("ParseJson hiprofiler_hook_process_count json node not found.");
+ return -1;
+ }
+ if (!cJSON_IsNumber(vaulezNode)) {
+ HLOGE("ParseJson: hiprofiler_hook_process_count item is illegal.");
+ return -1;
+ }
+ return vaulezNode->valueint;
+}
+
size_t SubStringCount(const std::string &source, const std::string &sub)
{
size_t count(0);
diff --git a/device/plugins/native_daemon/test/BUILD.gn b/device/plugins/native_daemon/test/BUILD.gn
index 73e4b78dc1e29f5e6546c765b5a4205a2afeccc5..b2285b4eb905c7a3352cab183dce1197e4bef61d 100644
--- a/device/plugins/native_daemon/test/BUILD.gn
+++ b/device/plugins/native_daemon/test/BUILD.gn
@@ -131,6 +131,7 @@ ohos_unittest("native_daemon_ut") {
"access_token:libaccesstoken_sdk",
"access_token:libtokensetproc_shared",
"bounds_checking_function:libsec_shared",
+ "cJSON:cjson",
"c_utils:utils",
"faultloggerd:libunwinder",
"googletest:gtest",
diff --git a/device/plugins/native_daemon/test/unittest/common/native/utilities_test.cpp b/device/plugins/native_daemon/test/unittest/common/native/utilities_test.cpp
index f48ef32e39cf3517ac92ddc647329deeae2f6ea7..251f1ef00ea19535c41d28392ceb6d979b64e665 100644
--- a/device/plugins/native_daemon/test/unittest/common/native/utilities_test.cpp
+++ b/device/plugins/native_daemon/test/unittest/common/native/utilities_test.cpp
@@ -628,6 +628,54 @@ HWTEST_F(UtilitiesTest, GetSubthreadIDs, TestSize.Level1)
}
ExitThreads();
}
+
+/**
+ * @tc.name: test funtion GetValueFromJsonFile return normal result
+ * @tc.desc:
+ * @tc.type: FUNC
+ */
+HWTEST_F(UtilitiesTest, GetValueFromJsonFile, TestSize.Level1)
+{
+ std::string filePath = "/data/test/resource/testdata/hiprofiler_cfg.json";
+ auto res = GetValueFromJsonFile(filePath, "hiprofiler_hook_process_count");
+ EXPECT_EQ(res, 2);
+}
+
+/**
+ * @tc.name: test funtion GetValueFromJsonFile when path is not exit
+ * @tc.desc:
+ * @tc.type: FUNC
+ */
+HWTEST_F(UtilitiesTest, GetValueFromJsonFile002, TestSize.Level1)
+{
+ std::string filePath = "/data/test/resource/testdata/hiprofiler_cfg22.json";
+ auto res = GetValueFromJsonFile(filePath, "hiprofiler_hook_process_count");
+ EXPECT_EQ(res, -1);
+}
+
+/**
+ * @tc.name: test funtion GetValueFromJsonFile when key is not exit
+ * @tc.desc:
+ * @tc.type: FUNC
+ */
+HWTEST_F(UtilitiesTest, GetValueFromJsonFile003, TestSize.Level1)
+{
+ std::string filePath = "/data/test/resource/testdata/hiprofiler_cfg.json";
+ auto res = GetValueFromJsonFile(filePath, "test");
+ EXPECT_EQ(res, -1);
+}
+
+/**
+ * @tc.name: test funtion GetValueFromJsonFile when value is not int
+ * @tc.desc:
+ * @tc.type: FUNC
+ */
+HWTEST_F(UtilitiesTest, GetValueFromJsonFile004, TestSize.Level1)
+{
+ std::string filePath = "/data/test/resource/testdata/hiprofiler_cfg_2.json";
+ auto res = GetValueFromJsonFile(filePath, "hiprofiler_hook_process_count");
+ EXPECT_EQ(res, -1);
+}
} // namespace NativeDaemon
} // namespace Developtools
} // namespace OHOS
diff --git a/device/plugins/native_daemon/test/unittest/resource/testdata/hiprofiler_cfg.json b/device/plugins/native_daemon/test/unittest/resource/testdata/hiprofiler_cfg.json
new file mode 100644
index 0000000000000000000000000000000000000000..e700aa16d7eb2ca71785a2e87aaacb882d39c8b8
--- /dev/null
+++ b/device/plugins/native_daemon/test/unittest/resource/testdata/hiprofiler_cfg.json
@@ -0,0 +1,3 @@
+{
+ "hiprofiler_hook_process_count": 2
+}
\ No newline at end of file
diff --git a/device/plugins/native_daemon/test/unittest/resource/testdata/hiprofiler_cfg2.json b/device/plugins/native_daemon/test/unittest/resource/testdata/hiprofiler_cfg2.json
new file mode 100644
index 0000000000000000000000000000000000000000..6365e9e309f262023389c9f03568b0b9d4815a2a
--- /dev/null
+++ b/device/plugins/native_daemon/test/unittest/resource/testdata/hiprofiler_cfg2.json
@@ -0,0 +1,4 @@
+{
+ "hiprofiler_hook_process_count": "4",
+ "test": "native_daemon"
+}
\ No newline at end of file
diff --git a/device/plugins/native_hook/BUILD.gn b/device/plugins/native_hook/BUILD.gn
index b9ce8ff29c31b48782ea3d0e2c22b1b00ab0f781..0d64a43768433bf8276caf381ae6948620485abf 100644
--- a/device/plugins/native_hook/BUILD.gn
+++ b/device/plugins/native_hook/BUILD.gn
@@ -66,6 +66,7 @@ ohos_source_set("native_hook_source") {
}
external_deps = [
"bounds_checking_function:libsec_shared",
+ "cJSON:cjson",
"c_utils:utils",
"ffrt:libffrt",
"hitrace:libhitracechain",
diff --git a/device/plugins/native_hook/test/BUILD.gn b/device/plugins/native_hook/test/BUILD.gn
index 6d484bdd00975ff31ac8a13fe8287d397af67dcf..92c62c8d701547fcec0c6add3d9d44b48314b55d 100644
--- a/device/plugins/native_hook/test/BUILD.gn
+++ b/device/plugins/native_hook/test/BUILD.gn
@@ -76,6 +76,7 @@ ohos_unittest("nativehook_ut") {
"access_token:libaccesstoken_sdk",
"access_token:libtokensetproc_shared",
"bounds_checking_function:libsec_shared",
+ "cJSON:cjson",
"c_utils:utils",
"faultloggerd:libunwinder",
"googletest:gtest",