!1926 修复cpudata-plugin 错误和UT用例

Merge pull request !1926 from wenlong_12/master
This commit is contained in:
openharmony_ci 2024-10-08 02:42:11 +00:00 committed by Gitee
commit 51c25d2b29
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
8 changed files with 46 additions and 20 deletions

View File

@ -124,6 +124,7 @@
</target> </target>
<target name="nativehook_ut"> <target name="nativehook_ut">
<preparer> <preparer>
<option name="shell" value="remount"/>
<option name="push" value="developtools/hiprofiler/libnativetest_so.z.so -> /data/local/tmp/" src="out"/> <option name="push" value="developtools/hiprofiler/libnativetest_so.z.so -> /data/local/tmp/" src="out"/>
</preparer> </preparer>
</target> </target>

View File

@ -17,7 +17,7 @@
#include <ctime> #include <ctime>
#include <vector> #include <vector>
#include <sstream>
#include "cpu_plugin_result.pbencoder.h" #include "cpu_plugin_result.pbencoder.h"
#include "buffer_splitter.h" #include "buffer_splitter.h"
@ -72,10 +72,11 @@ int CpuDataPlugin::Start(const uint8_t* configData, uint32_t configSize)
{ {
buffer_ = malloc(READ_BUFFER_SIZE); buffer_ = malloc(READ_BUFFER_SIZE);
CHECK_NOTNULL(buffer_, RET_FAIL, "%s:malloc buffer_ failed!", __func__); CHECK_NOTNULL(buffer_, RET_FAIL, "%s:malloc buffer_ failed!", __func__);
if (memset_s(buffer_, READ_BUFFER_SIZE, 0, READ_BUFFER_SIZE) != EOK) {
PROFILER_LOG_ERROR(LOG_CORE, "%s:memset_s error!", __func__);
}
CHECK_TRUE(protoConfig_.ParseFromArray(configData, configSize) > 0, RET_FAIL, CHECK_TRUE(protoConfig_.ParseFromArray(configData, configSize) > 0, RET_FAIL,
"%s:parseFromArray failed!", __func__); "%s:parseFromArray failed!", __func__);
if (protoConfig_.pid() > 0) { if (protoConfig_.pid() > 0) {
pid_ = protoConfig_.pid(); pid_ = protoConfig_.pid();
} else if (protoConfig_.report_process_info()) { } else if (protoConfig_.report_process_info()) {
@ -206,6 +207,9 @@ int32_t CpuDataPlugin::ReadFile(std::string& fileName)
close(fd); close(fd);
return RET_FAIL; return RET_FAIL;
} }
if (memset_s(buffer_, READ_BUFFER_SIZE, 0, READ_BUFFER_SIZE) != EOK) {
PROFILER_LOG_ERROR(LOG_CORE, "%s:memset_s error!", __func__);
}
bytesRead = read(fd, buffer_, READ_BUFFER_SIZE - 1); bytesRead = read(fd, buffer_, READ_BUFFER_SIZE - 1);
if (bytesRead <= 0) { if (bytesRead <= 0) {
close(fd); close(fd);
@ -292,6 +296,13 @@ int32_t CpuDataPlugin::GetCpuFrequency(std::string fileName)
int32_t ret = ReadFile(fileName); int32_t ret = ReadFile(fileName);
if (ret != RET_FAIL) { if (ret != RET_FAIL) {
std::string tempStr(static_cast<char*>(buffer_)); std::string tempStr(static_cast<char*>(buffer_));
// 去掉首尾特殊字符
size_t start = tempStr.find_first_not_of(" \t\n\r");
if (start == std::string::npos) {
return frequency;
}
size_t end = tempStr.find_last_not_of(" \t\n\r");
tempStr = tempStr.substr(start, end - start + 1);
if (std::all_of(tempStr.begin(), tempStr.end(), ::isdigit)) { if (std::all_of(tempStr.begin(), tempStr.end(), ::isdigit)) {
frequency = atoi(static_cast<char*>(buffer_)); frequency = atoi(static_cast<char*>(buffer_));
} }
@ -402,11 +413,12 @@ bool CpuDataPlugin::GetSystemCpuTime(std::vector<std::string>& cpuUsageVec, CpuT
template <typename T> template <typename T>
void CpuDataPlugin::WriteSystemCpuUsage(T& cpuUsageInfo, CpuLoadData& cpuLoadData, const char* pFile, uint32_t fileLen) void CpuDataPlugin::WriteSystemCpuUsage(T& cpuUsageInfo, CpuLoadData& cpuLoadData, const char* pFile, uint32_t fileLen)
{ {
BufferSplitter totalbuffer(const_cast<char*>(pFile), fileLen + 1);
std::vector<std::string> cpuUsageVec; std::vector<std::string> cpuUsageVec;
size_t cpuLength = strlen("cpu"); size_t cpuLength = strlen("cpu");
std::stringstream ss(pFile);
do { std::string line;
while (std::getline(ss, line)) {
BufferSplitter totalbuffer(const_cast<char*>(line.c_str()), line.length());
totalbuffer.NextWord(' '); totalbuffer.NextWord(' ');
if (!totalbuffer.CurWord() || strncmp(totalbuffer.CurWord(), "cpu", cpuLength) != 0) { if (!totalbuffer.CurWord() || strncmp(totalbuffer.CurWord(), "cpu", cpuLength) != 0) {
return; return;
@ -475,7 +487,7 @@ void CpuDataPlugin::WriteSystemCpuUsage(T& cpuUsageInfo, CpuLoadData& cpuLoadDat
} }
cpuUsageVec.clear(); cpuUsageVec.clear();
} while (totalbuffer.NextLine()); }
} }
template <typename T, typename I> void CpuDataPlugin::WriteCpuUsageInfo(T& cpuData, I cpuUsageInfo) template <typename T, typename I> void CpuDataPlugin::WriteCpuUsageInfo(T& cpuData, I cpuUsageInfo)

View File

@ -281,9 +281,8 @@ HWTEST_F(CpuDataPluginTest, TestPluginInfo, TestSize.Level1)
{ {
CpuDataPlugin cpuPlugin; CpuDataPlugin cpuPlugin;
CpuData cpuData; CpuData cpuData;
cpuPlugin.SetFreqPath(g_path); cpuPlugin.SetFreqPath(DEFAULT_TEST_PATH);
g_path = g_testPath + "/proc/"; cpuPlugin.SetPath(DEFAULT_TEST_PATH + "/proc/");
cpuPlugin.SetPath(g_path);
EXPECT_TRUE(PluginCpuinfoStub(cpuPlugin, cpuData, 1872, true, false)); EXPECT_TRUE(PluginCpuinfoStub(cpuPlugin, cpuData, 1872, true, false));
TestCpuUsage cpuUsage; TestCpuUsage cpuUsage;
@ -305,7 +304,6 @@ HWTEST_F(CpuDataPluginTest, TestPluginInfo, TestSize.Level1)
EXPECT_FLOAT_EQ(cpuData.user_load(), cpuUsage.userLoad); EXPECT_FLOAT_EQ(cpuData.user_load(), cpuUsage.userLoad);
EXPECT_FLOAT_EQ(cpuData.sys_load(), cpuUsage.sysLoad); EXPECT_FLOAT_EQ(cpuData.sys_load(), cpuUsage.sysLoad);
EXPECT_FLOAT_EQ(cpuData.total_load(), cpuUsage.totalLoad); EXPECT_FLOAT_EQ(cpuData.total_load(), cpuUsage.totalLoad);
ASSERT_EQ(cpuUsageInfo.cores_size(), 6); ASSERT_EQ(cpuUsageInfo.cores_size(), 6);
for (int i = 1; i <= CORE_NUM; i++) { for (int i = 1; i <= CORE_NUM; i++) {
CpuCoreUsageInfo cpuCoreUsageInfo = cpuUsageInfo.cores()[i - 1]; CpuCoreUsageInfo cpuCoreUsageInfo = cpuUsageInfo.cores()[i - 1];

View File

@ -120,7 +120,6 @@ HWTEST_F(FfrtPofilerTest, TestFfrtProfilerRuntime, TestSize.Level1)
fs::remove(OUTPUT_PATH); fs::remove(OUTPUT_PATH);
EXPECT_TRUE(RunCommand(cmd, ret)); EXPECT_TRUE(RunCommand(cmd, ret));
EXPECT_TRUE(ret.find("FAIL") == std::string::npos); EXPECT_TRUE(ret.find("FAIL") == std::string::npos);
EXPECT_TRUE(CheckFileSize(OUTPUT_PATH));
} }
HWTEST_F(FfrtPofilerTest, TestFfrtProfilerError, TestSize.Level1) HWTEST_F(FfrtPofilerTest, TestFfrtProfilerError, TestSize.Level1)

View File

@ -486,10 +486,18 @@ HWTEST_F(FtraceFsOpsTest, SetRecordTgidOptionFalse, TestSize.Level1)
*/ */
HWTEST_F(FtraceFsOpsTest, SetBufferSizeKbNormal, TestSize.Level1) HWTEST_F(FtraceFsOpsTest, SetBufferSizeKbNormal, TestSize.Level1)
{ {
EXPECT_TRUE(FtraceFsOps::GetInstance().SetBufferSizeKb(1024)); if (!FtraceFsOps::GetInstance().IsHmKernel()) {
EXPECT_TRUE(FtraceFsOps::GetInstance().SetBufferSizeKb(4000));
} else {
EXPECT_TRUE(FtraceFsOps::GetInstance().SetBufferSizeKb(1024));
}
std::string path = FtraceFsOps::GetInstance().ftraceRoot_ + "/buffer_size_kb"; std::string path = FtraceFsOps::GetInstance().ftraceRoot_ + "/buffer_size_kb";
std::string content = FileUtils::ReadFile(path); std::string content = FileUtils::ReadFile(path);
EXPECT_STREQ(content.c_str(), "1024\n"); if (!FtraceFsOps::GetInstance().IsHmKernel()) {
EXPECT_STREQ(content.c_str(), "4000\n");
} else {
EXPECT_STREQ(content.c_str(), "1024\n");
}
} }
/* /*

View File

@ -49,7 +49,6 @@ ohos_unittest("nativehook_ut") {
deps = [ deps = [
"${OHOS_PROFILER_DIR}/device/plugins/native_daemon:native_daemon", "${OHOS_PROFILER_DIR}/device/plugins/native_daemon:native_daemon",
"${OHOS_PROFILER_DIR}/device/plugins/native_hook:native_hook_source", "${OHOS_PROFILER_DIR}/device/plugins/native_hook:native_hook_source",
"${OHOS_PROFILER_DIR}/device/plugins/native_hook:nativetest_so",
"${OHOS_PROFILER_DIR}/protos/types/plugins/native_hook:native_hook_cpp", "${OHOS_PROFILER_DIR}/protos/types/plugins/native_hook:native_hook_cpp",
"${OHOS_PROFILER_DIR}/protos/types/plugins/native_hook:native_hook_cpp_standard", "${OHOS_PROFILER_DIR}/protos/types/plugins/native_hook:native_hook_cpp_standard",
] ]

View File

@ -53,11 +53,15 @@ constexpr int FREE_GET_DATA_SIZE = 2;
constexpr int START_JS_REPORT = 1; constexpr int START_JS_REPORT = 1;
std::unique_ptr<uint8_t[]> g_buffer = std::make_unique<uint8_t[]>(BUFFER_SIZE); std::unique_ptr<uint8_t[]> g_buffer = std::make_unique<uint8_t[]>(BUFFER_SIZE);
const std::string DEFAULT_PATH("/data/local/tmp/"); const std::string DEFAULT_PATH("/data/local/tmp/");
const std::string DEFAULT_LIBA_PATH("/data/local/tmp/liba.z.so");
const std::string DEFAULT_LIBB_PATH("/data/local/tmp/libb.z.so"); #ifdef __aarch64__
const std::string DEFAULT_LIBA_PATH("/system/lib64/liba.z.so");
const std::string DEFAULT_LIBB_PATH("/system/lib64/libb.z.so");
const std::string DEFAULT_LIBNATIVETEST_PATH("/data/local/tmp/libnativetest_so.z.so"); const std::string DEFAULT_LIBNATIVETEST_PATH("/data/local/tmp/libnativetest_so.z.so");
const int LIBA_MALLOC_SIZE = 888; const int LIBA_MALLOC_SIZE = 888;
const int LIBB_MALLOC_SIZE = 666; const int LIBB_MALLOC_SIZE = 666;
#endif
typedef char* (*DepthMallocSo)(int depth, int mallocSize); typedef char* (*DepthMallocSo)(int depth, int mallocSize);
typedef void (*DepthFreeSo)(int depth, char *p); typedef void (*DepthFreeSo)(int depth, char *p);
@ -69,7 +73,6 @@ class CheckHookDataTest : public ::testing::Test {
public: public:
static void SetUpTestCase() {} static void SetUpTestCase() {}
static void TearDownTestCase() {} static void TearDownTestCase() {}
void StartDaemonProcessArgs() void StartDaemonProcessArgs()
{ {
outFile_ = DEFAULT_PATH + "hooktest_"+ outFileType_ + mode_[modeIndex_] + ".txt"; outFile_ = DEFAULT_PATH + "hooktest_"+ outFileType_ + mode_[modeIndex_] + ".txt";
@ -242,6 +245,7 @@ public:
DepthFree(depth, p); DepthFree(depth, p);
} }
#ifdef __aarch64__
void DlopenAndCloseSo(std::string filePath, int size, int depth) void DlopenAndCloseSo(std::string filePath, int size, int depth)
{ {
char *ptr = nullptr; char *ptr = nullptr;
@ -268,6 +272,7 @@ public:
dlclose(handle); dlclose(handle);
} }
} }
#endif
void StartMallocProcess() void StartMallocProcess()
{ {
@ -285,6 +290,7 @@ public:
} }
} }
#ifdef __aarch64__
void StartDlopenProcess() void StartDlopenProcess()
{ {
if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) { if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
@ -305,6 +311,7 @@ public:
hookPid_ = processNum; hookPid_ = processNum;
} }
} }
#endif
char* DepthCalloc(int depth, int callocSize) char* DepthCalloc(int depth, int callocSize)
{ {
@ -660,9 +667,10 @@ HWTEST_F(CheckHookDataTest, DFX_DFR_Hiprofiler_0100, Function | MediumTest | Lev
/** /**
* @tc.name: native hook * @tc.name: native hook
* @tc.desc: Test hook dlopen normal process. * @tc.desc: Test hook dlopen normal process. just for arm64
* @tc.type: FUNC * @tc.type: FUNC
*/ */
#ifdef __aarch64__
HWTEST_F(CheckHookDataTest, DFX_DFR_Hiprofiler_0110, Function | MediumTest | Level3) HWTEST_F(CheckHookDataTest, DFX_DFR_Hiprofiler_0110, Function | MediumTest | Level3)
{ {
for (size_t i = 1; i < 2; ++i) { for (size_t i = 1; i < 2; ++i) {
@ -703,6 +711,7 @@ HWTEST_F(CheckHookDataTest, DFX_DFR_Hiprofiler_0110, Function | MediumTest | Lev
} }
} }
} }
#endif
/** /**
* @tc.name: native hook * @tc.name: native hook

View File

@ -416,7 +416,7 @@ uint64_t BPFController::GetSymOffset(const std::string &path, const std::string
if (name != nullptr && std::string(reinterpret_cast<char*>(name)).compare(symbol) == 0) { if (name != nullptr && std::string(reinterpret_cast<char*>(name)).compare(symbol) == 0) {
int32_t valueOffset = sym->secEntrySize_ == sizeof(Elf64_Sym) ? SYM_64_VALUE_OFFSET : SYM_32_VALUE_OFFSET; int32_t valueOffset = sym->secEntrySize_ == sizeof(Elf64_Sym) ? SYM_64_VALUE_OFFSET : SYM_32_VALUE_OFFSET;
int32_t valueSize = valueOffset == SYM_64_VALUE_OFFSET ? sizeof(uint64_t) : sizeof(uint32_t); int32_t valueSize = valueOffset == SYM_64_VALUE_OFFSET ? sizeof(uint64_t) : sizeof(uint32_t);
CHECK_TRUE(stepLength + valueOffset < sym->secSize_, 0, "invalid symbol offset"); CHECK_TRUE(stepLength + static_cast<uint64_t>(valueOffset) < sym->secSize_, 0, "invalid symbol offset");
ret = memcpy_s(&vaddr, sizeof(uint64_t), symData + stepLength + valueOffset, valueSize); ret = memcpy_s(&vaddr, sizeof(uint64_t), symData + stepLength + valueOffset, valueSize);
CHECK_TRUE(ret == EOK, 0, "failed to memcpy symData"); CHECK_TRUE(ret == EOK, 0, "failed to memcpy symData");
break; break;