!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 name="nativehook_ut">
<preparer>
<option name="shell" value="remount"/>
<option name="push" value="developtools/hiprofiler/libnativetest_so.z.so -> /data/local/tmp/" src="out"/>
</preparer>
</target>

View File

@ -17,7 +17,7 @@
#include <ctime>
#include <vector>
#include <sstream>
#include "cpu_plugin_result.pbencoder.h"
#include "buffer_splitter.h"
@ -72,10 +72,11 @@ int CpuDataPlugin::Start(const uint8_t* configData, uint32_t configSize)
{
buffer_ = malloc(READ_BUFFER_SIZE);
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,
"%s:parseFromArray failed!", __func__);
if (protoConfig_.pid() > 0) {
pid_ = protoConfig_.pid();
} else if (protoConfig_.report_process_info()) {
@ -206,6 +207,9 @@ int32_t CpuDataPlugin::ReadFile(std::string& fileName)
close(fd);
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);
if (bytesRead <= 0) {
close(fd);
@ -292,6 +296,13 @@ int32_t CpuDataPlugin::GetCpuFrequency(std::string fileName)
int32_t ret = ReadFile(fileName);
if (ret != RET_FAIL) {
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)) {
frequency = atoi(static_cast<char*>(buffer_));
}
@ -402,11 +413,12 @@ bool CpuDataPlugin::GetSystemCpuTime(std::vector<std::string>& cpuUsageVec, CpuT
template <typename T>
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;
size_t cpuLength = strlen("cpu");
do {
std::stringstream ss(pFile);
std::string line;
while (std::getline(ss, line)) {
BufferSplitter totalbuffer(const_cast<char*>(line.c_str()), line.length());
totalbuffer.NextWord(' ');
if (!totalbuffer.CurWord() || strncmp(totalbuffer.CurWord(), "cpu", cpuLength) != 0) {
return;
@ -475,7 +487,7 @@ void CpuDataPlugin::WriteSystemCpuUsage(T& cpuUsageInfo, CpuLoadData& cpuLoadDat
}
cpuUsageVec.clear();
} while (totalbuffer.NextLine());
}
}
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;
CpuData cpuData;
cpuPlugin.SetFreqPath(g_path);
g_path = g_testPath + "/proc/";
cpuPlugin.SetPath(g_path);
cpuPlugin.SetFreqPath(DEFAULT_TEST_PATH);
cpuPlugin.SetPath(DEFAULT_TEST_PATH + "/proc/");
EXPECT_TRUE(PluginCpuinfoStub(cpuPlugin, cpuData, 1872, true, false));
TestCpuUsage cpuUsage;
@ -305,7 +304,6 @@ HWTEST_F(CpuDataPluginTest, TestPluginInfo, TestSize.Level1)
EXPECT_FLOAT_EQ(cpuData.user_load(), cpuUsage.userLoad);
EXPECT_FLOAT_EQ(cpuData.sys_load(), cpuUsage.sysLoad);
EXPECT_FLOAT_EQ(cpuData.total_load(), cpuUsage.totalLoad);
ASSERT_EQ(cpuUsageInfo.cores_size(), 6);
for (int i = 1; i <= CORE_NUM; i++) {
CpuCoreUsageInfo cpuCoreUsageInfo = cpuUsageInfo.cores()[i - 1];

View File

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

View File

@ -486,10 +486,18 @@ HWTEST_F(FtraceFsOpsTest, SetRecordTgidOptionFalse, TestSize.Level1)
*/
HWTEST_F(FtraceFsOpsTest, SetBufferSizeKbNormal, TestSize.Level1)
{
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 content = FileUtils::ReadFile(path);
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 = [
"${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:nativetest_so",
"${OHOS_PROFILER_DIR}/protos/types/plugins/native_hook:native_hook_cpp",
"${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;
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_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 int LIBA_MALLOC_SIZE = 888;
const int LIBB_MALLOC_SIZE = 666;
#endif
typedef char* (*DepthMallocSo)(int depth, int mallocSize);
typedef void (*DepthFreeSo)(int depth, char *p);
@ -69,7 +73,6 @@ class CheckHookDataTest : public ::testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
void StartDaemonProcessArgs()
{
outFile_ = DEFAULT_PATH + "hooktest_"+ outFileType_ + mode_[modeIndex_] + ".txt";
@ -242,6 +245,7 @@ public:
DepthFree(depth, p);
}
#ifdef __aarch64__
void DlopenAndCloseSo(std::string filePath, int size, int depth)
{
char *ptr = nullptr;
@ -268,6 +272,7 @@ public:
dlclose(handle);
}
}
#endif
void StartMallocProcess()
{
@ -285,6 +290,7 @@ public:
}
}
#ifdef __aarch64__
void StartDlopenProcess()
{
if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
@ -305,6 +311,7 @@ public:
hookPid_ = processNum;
}
}
#endif
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.desc: Test hook dlopen normal process.
* @tc.desc: Test hook dlopen normal process. just for arm64
* @tc.type: FUNC
*/
#ifdef __aarch64__
HWTEST_F(CheckHookDataTest, DFX_DFR_Hiprofiler_0110, Function | MediumTest | Level3)
{
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

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) {
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);
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);
CHECK_TRUE(ret == EOK, 0, "failed to memcpy symData");
break;