diff --git a/device/plugins/cpu_plugin/include/cpu_data_plugin.h b/device/plugins/cpu_plugin/include/cpu_data_plugin.h index fad86ad2e3f3fb2934acedf88e0ecda71187eaee..961e026e0691b7662cb544e9bf776bd0730d46d9 100644 --- a/device/plugins/cpu_plugin/include/cpu_data_plugin.h +++ b/device/plugins/cpu_plugin/include/cpu_data_plugin.h @@ -121,6 +121,10 @@ private: path_ = path; } void SetFreqPath(std::string path); + template + bool SetSystemCpuUsage(T& cpuUsageInfo, CpuLoadData& cpuLoadData, CpuTimeData& cpuTimeData); + template + bool SetCpuCoreInfo(const std::string& cpuUsageStr, T& cpuUsageInfo, CpuTimeData& cpuTimeData); private: /* data */ diff --git a/device/plugins/cpu_plugin/src/cpu_data_plugin.cpp b/device/plugins/cpu_plugin/src/cpu_data_plugin.cpp index 22bc40a464ee6f5abca910527aa0f8296094803a..48968c991822b5c91c9fc2b96238915322902e0e 100644 --- a/device/plugins/cpu_plugin/src/cpu_data_plugin.cpp +++ b/device/plugins/cpu_plugin/src/cpu_data_plugin.cpp @@ -432,6 +432,63 @@ bool CpuDataPlugin::GetSystemCpuTime(std::vector& cpuUsageVec, CpuT return true; } +template +bool CpuDataPlugin::SetSystemCpuUsage(T& cpuUsageInfo, CpuLoadData& cpuLoadData, CpuTimeData& cpuTimeData) +{ + cpuUsageInfo.set_prev_system_cpu_time_ms(prevCpuTimeData_.systemUsageTime); + cpuUsageInfo.set_prev_system_boot_time_ms(prevCpuTimeData_.systemBootTime); + cpuUsageInfo.set_system_cpu_time_ms(cpuTimeData.systemUsageTime); + cpuUsageInfo.set_system_boot_time_ms(cpuTimeData.systemBootTime); + bool isTest = false; + if (strncmp(path_.c_str(), "/proc/", strlen("/proc/")) != 0) { + isTest = true; // UT needs report load data for the first time + } + if ((protoConfig_.report_process_info() && prevCpuTimeData_.systemBootTime != 0) || isTest) { + cpuLoadData.userLoad = static_cast(cpuTimeData.userModeUsageTime - prevCpuTimeData_.userModeUsageTime) / + static_cast(cpuTimeData.systemBootTime - prevCpuTimeData_.systemBootTime) * + PERCENT; + cpuLoadData.sysLoad = + static_cast(cpuTimeData.systemModeUsageTime - prevCpuTimeData_.systemModeUsageTime) / + static_cast(cpuTimeData.systemBootTime - prevCpuTimeData_.systemBootTime) * PERCENT; + cpuLoadData.totalLoad = static_cast(cpuTimeData.systemUsageTime - prevCpuTimeData_.systemUsageTime) / + static_cast(cpuTimeData.systemBootTime - prevCpuTimeData_.systemBootTime) * + PERCENT; + } + prevCpuTimeData_ = cpuTimeData; + if (pid_ < 0 && protoConfig_.report_process_info()) { + return false; + } + return true; +} + +template +bool CpuDataPlugin::SetCpuCoreInfo(const std::string& cpuUsageStr, T& cpuUsageInfo, CpuTimeData& cpuTimeData) +{ + size_t cpuLength = strlen("cpu"); + std::string core = std::string(cpuUsageStr.c_str() + cpuLength, cpuUsageStr.size() - cpuLength); + if (!COMMON::IsNumeric(core)) { + PROFILER_LOG_ERROR(LOG_CORE, "WriteSystemCpuUsage core is not numeric"); + return false; + } + int32_t coreNum = atoi(core.c_str()); + // 第一次获取数据时需要将前一个数据置为0 + if (prevCoreSystemCpuTimeMap_.size() == static_cast(coreNum)) { + prevCoreSystemCpuTimeMap_[coreNum] = 0; + prevCoreSystemBootTimeMap_[coreNum] = 0; + } + auto* cpuCore = cpuUsageInfo.add_cores(); + cpuCore->set_cpu_core(coreNum); + cpuCore->set_prev_system_cpu_time_ms(prevCoreSystemCpuTimeMap_[coreNum]); + cpuCore->set_prev_system_boot_time_ms(prevCoreSystemBootTimeMap_[coreNum]); + cpuCore->set_system_cpu_time_ms(cpuTimeData.systemUsageTime); + cpuCore->set_system_boot_time_ms(cpuTimeData.systemBootTime); + + SetCpuFrequency(*cpuCore, coreNum); + prevCoreSystemCpuTimeMap_[coreNum] = cpuTimeData.systemUsageTime; + prevCoreSystemBootTimeMap_[coreNum] = cpuTimeData.systemBootTime; + return true; +} + template void CpuDataPlugin::WriteSystemCpuUsage(T& cpuUsageInfo, CpuLoadData& cpuLoadData, const char* pFile, uint32_t fileLen) { @@ -445,7 +502,6 @@ void CpuDataPlugin::WriteSystemCpuUsage(T& cpuUsageInfo, CpuLoadData& cpuLoadDat if (!totalbuffer.CurWord() || strncmp(totalbuffer.CurWord(), "cpu", cpuLength) != 0) { return; } - for (int i = 0; i < SYSTEM_STAT_COUNT; i++) { if (!totalbuffer.CurWord()) { return; @@ -454,64 +510,20 @@ void CpuDataPlugin::WriteSystemCpuUsage(T& cpuUsageInfo, CpuLoadData& cpuLoadDat cpuUsageVec.push_back(curWord); totalbuffer.NextWord(' '); } - // 获取数据失败返回 CpuTimeData cpuTimeData; if (!GetSystemCpuTime(cpuUsageVec, cpuTimeData)) { return; } - if (strcmp(cpuUsageVec[0].c_str(), "cpu") == 0) { - cpuUsageInfo.set_prev_system_cpu_time_ms(prevCpuTimeData_.systemUsageTime); - cpuUsageInfo.set_prev_system_boot_time_ms(prevCpuTimeData_.systemBootTime); - cpuUsageInfo.set_system_cpu_time_ms(cpuTimeData.systemUsageTime); - cpuUsageInfo.set_system_boot_time_ms(cpuTimeData.systemBootTime); - bool isTest = false; - if (strncmp(path_.c_str(), "/proc/", strlen("/proc/")) != 0) { - isTest = true; // UT needs report load data for the first time - } - if ((protoConfig_.report_process_info() && prevCpuTimeData_.systemBootTime != 0) || isTest) { - cpuLoadData.userLoad = static_cast(cpuTimeData.userModeUsageTime - - prevCpuTimeData_.userModeUsageTime) / - static_cast(cpuTimeData.systemBootTime - - prevCpuTimeData_.systemBootTime) * PERCENT; - cpuLoadData.sysLoad = static_cast(cpuTimeData.systemModeUsageTime - - prevCpuTimeData_.systemModeUsageTime) / - static_cast(cpuTimeData.systemBootTime - - prevCpuTimeData_.systemBootTime) * PERCENT; - cpuLoadData.totalLoad = static_cast(cpuTimeData.systemUsageTime - - prevCpuTimeData_.systemUsageTime) / - static_cast(cpuTimeData.systemBootTime - - prevCpuTimeData_.systemBootTime) * PERCENT; - } - prevCpuTimeData_ = cpuTimeData; - if (pid_ < 0 && protoConfig_.report_process_info()) { + if (!SetSystemCpuUsage(cpuUsageInfo, cpuLoadData, cpuTimeData)) { return; } } else { - std::string core = std::string(cpuUsageVec[0].c_str() + cpuLength, cpuUsageVec[0].size() - cpuLength); - if (!COMMON::IsNumeric(core)) { - PROFILER_LOG_ERROR(LOG_CORE, "WriteSystemCpuUsage core is not numeric"); + if (!SetCpuCoreInfo(cpuUsageVec[0], cpuUsageInfo, cpuTimeData)) { return; } - int32_t coreNum = atoi(core.c_str()); - // 第一次获取数据时需要将前一个数据置为0 - if (prevCoreSystemCpuTimeMap_.size() == static_cast(coreNum)) { - prevCoreSystemCpuTimeMap_[coreNum] = 0; - prevCoreSystemBootTimeMap_[coreNum] = 0; - } - auto* cpuCore = cpuUsageInfo.add_cores(); - cpuCore->set_cpu_core(coreNum); - cpuCore->set_prev_system_cpu_time_ms(prevCoreSystemCpuTimeMap_[coreNum]); - cpuCore->set_prev_system_boot_time_ms(prevCoreSystemBootTimeMap_[coreNum]); - cpuCore->set_system_cpu_time_ms(cpuTimeData.systemUsageTime); - cpuCore->set_system_boot_time_ms(cpuTimeData.systemBootTime); - - SetCpuFrequency(*cpuCore, coreNum); - prevCoreSystemCpuTimeMap_[coreNum] = cpuTimeData.systemUsageTime; - prevCoreSystemBootTimeMap_[coreNum] = cpuTimeData.systemBootTime; } - cpuUsageVec.clear(); } } diff --git a/device/plugins/cpu_plugin/test/unittest/cpu_data_plugin_unittest.cpp b/device/plugins/cpu_plugin/test/unittest/cpu_data_plugin_unittest.cpp index e2ae0d1386bb235c858643b59120ed2e63844b47..e9dcb81df4f7d5254954c3cf091401d1dcc621a0 100644 --- a/device/plugins/cpu_plugin/test/unittest/cpu_data_plugin_unittest.cpp +++ b/device/plugins/cpu_plugin/test/unittest/cpu_data_plugin_unittest.cpp @@ -976,4 +976,30 @@ HWTEST_F(CpuDataPluginTest, TestPid, TestSize.Level1) plugin1.Stop(); plugin2.Stop(); } + +/** + * @tc.name: cpu plugin + * @tc.desc: cpu system information test for SetSystemCpuUsage. + * @tc.type: FUNC + */ +HWTEST_F(CpuDataPluginTest, TestSetSystemCpuUsage, TestSize.Level1) +{ + CpuData cpuData; + CpuDataPlugin cpuPlugin; + CpuTimeData cpuTimeData = {1966195, 1702753, 2345678, 1458898}; + CpuLoadData cpuLoadData; + cpuPlugin.SetSystemCpuUsage(*cpuData.mutable_cpu_usage_info(), cpuLoadData, cpuTimeData); + CpuUsageInfo cpuUsageInfo1 = cpuData.cpu_usage_info(); + EXPECT_EQ(cpuUsageInfo1.prev_system_cpu_time_ms(), 0); + EXPECT_EQ(cpuUsageInfo1.prev_system_boot_time_ms(), 0); + EXPECT_EQ(cpuUsageInfo1.system_cpu_time_ms(), 2345678); + EXPECT_EQ(cpuUsageInfo1.system_boot_time_ms(), 1458898); + EXPECT_EQ(cpuUsageInfo1.cores_size(), 0); + cpuPlugin.SetCpuCoreInfo("cpu 5", *cpuData.mutable_cpu_usage_info(), cpuTimeData); + cpuUsageInfo1 = cpuData.cpu_usage_info(); + ASSERT_EQ(cpuUsageInfo1.cores_size(), 1); + CpuCoreUsageInfo cpuCoreUsageInfo = cpuUsageInfo1.cores()[0]; + EXPECT_EQ(cpuCoreUsageInfo.cpu_core(), 5); // 5: cpu core +} + } // namespace diff --git a/device/plugins/diskio_plugin/include/io_stats.h b/device/plugins/diskio_plugin/include/io_stats.h index 913b349b69de903cb55218def2c3988def5d8851..a27f1fae75776b80753eac341fe67eee998c4b2e 100644 --- a/device/plugins/diskio_plugin/include/io_stats.h +++ b/device/plugins/diskio_plugin/include/io_stats.h @@ -227,6 +227,7 @@ private: bool RemoveSpaces(char** p); uint32_t ParseLineFields(const std::string& line); uint32_t ParseLineFields(const std::string& line, std::string& name); + void SetIoInfo(std::shared_ptr& ioInfo, const std::string& name); private: std::mutex mutex_; diff --git a/device/plugins/diskio_plugin/src/io_stats.cpp b/device/plugins/diskio_plugin/src/io_stats.cpp index abc6b7ee371db2779d14a0db041ebb2969a90fdf..3d11ec159534033880c26433f30abcf3253c7895 100644 --- a/device/plugins/diskio_plugin/src/io_stats.cpp +++ b/device/plugins/diskio_plugin/src/io_stats.cpp @@ -155,6 +155,54 @@ bool IoStats::ParseIoStats() return true; } +void IoStats::SetIoInfo(std::shared_ptr& ioInfo, const std::string& name) +{ + int index = 0; + ioInfo->major_ = fields_[index]; + index++; + ioInfo->minor_ = fields_[index]; + index++; + ioInfo->deviceName_ = name; + + ioInfo->rSucc_ = fields_[index]; + index++; + ioInfo->rMerged_ = fields_[index]; + index++; + ioInfo->rSectors_ = fields_[index]; + index++; + ioInfo->timeOfRead_ = fields_[index]; + index++; + + ioInfo->wSucc_ = fields_[index]; + index++; + ioInfo->wMerged_ = fields_[index]; + index++; + ioInfo->wSectors_ = fields_[index]; + index++; + ioInfo->timeOfWrite_ = fields_[index]; + index++; + + ioInfo->ios_ = fields_[index]; + index++; + ioInfo->timeOfIo_ = fields_[index]; + index++; + ioInfo->weighted_ = fields_[index]; + index++; + + ioInfo->dSucc_ = fields_[index]; + index++; + ioInfo->dMerged_ = fields_[index]; + index++; + ioInfo->dSectors_ = fields_[index]; + index++; + ioInfo->timeOfd_ = fields_[index]; + index++; + + ioInfo->flushSucc_ = fields_[index]; + index++; + ioInfo->timeOfFlush_ = fields_[index]; +} + bool IoStats::GetIoStats(std::string& line) { std::string name; @@ -162,51 +210,7 @@ bool IoStats::GetIoStats(std::string& line) CHECK_NOTNULL(ioInfo, false, "create DiskStats FAILED!"); if (ParseLineFields(line, name) > 0) { - int index = 0; - ioInfo->major_ = fields_[index]; - index++; - ioInfo->minor_ = fields_[index]; - index++; - ioInfo->deviceName_ = name; - - ioInfo->rSucc_ = fields_[index]; - index++; - ioInfo->rMerged_ = fields_[index]; - index++; - ioInfo->rSectors_ = fields_[index]; - index++; - ioInfo->timeOfRead_ = fields_[index]; - index++; - - ioInfo->wSucc_ = fields_[index]; - index++; - ioInfo->wMerged_ = fields_[index]; - index++; - ioInfo->wSectors_ = fields_[index]; - index++; - ioInfo->timeOfWrite_ = fields_[index]; - index++; - - ioInfo->ios_ = fields_[index]; - index++; - ioInfo->timeOfIo_ = fields_[index]; - index++; - ioInfo->weighted_ = fields_[index]; - index++; - - ioInfo->dSucc_ = fields_[index]; - index++; - ioInfo->dMerged_ = fields_[index]; - index++; - ioInfo->dSectors_ = fields_[index]; - index++; - ioInfo->timeOfd_ = fields_[index]; - index++; - - ioInfo->flushSucc_ = fields_[index]; - index++; - ioInfo->timeOfFlush_ = fields_[index]; - + SetIoInfo(ioInfo, name); ioDatas_.push_back(ioInfo); fields_.clear(); return true; @@ -238,7 +242,6 @@ bool IoStats::GetIoStats(std::string& line) debugIoInfo->deviceName_ = std::string(name); ioDatas_.push_back(debugIoInfo); #endif - return false; } diff --git a/device/plugins/diskio_plugin/src/test_main.cpp b/device/plugins/diskio_plugin/src/test_main.cpp index 1f845f607f1e85f67618a2185c4a1bfd73e5bc1f..8f10bd848eab898035870643b9367d37ef9adabb 100644 --- a/device/plugins/diskio_plugin/src/test_main.cpp +++ b/device/plugins/diskio_plugin/src/test_main.cpp @@ -53,6 +53,51 @@ void IoTest() std::string command = "rm " + writeFile; system(command.c_str()); } + +static void PrintDiskStats() +{ + DiskioConfig protoConfig; + PluginModuleStruct* diskioPlugin; + void* handle = dlopen("libdiskiodataplugin.z.so", RTLD_LAZY); + if (handle == nullptr) { + std::cout << "test:dlopen err: " << dlerror() << std::endl; + return; + } + std::cout << "test:handle = " << handle << std::endl; + diskioPlugin = (PluginModuleStruct*)dlsym(handle, "g_pluginModule"); + std::cout << "test:name = " << diskioPlugin->name << std::endl; + std::cout << "test:buffer size = " << diskioPlugin->resultBufferSizeHint << std::endl; + // Serialize config + int configLength = protoConfig.ByteSizeLong(); + std::vector configBuffer(configLength); + int ret = protoConfig.SerializeToArray(configBuffer.data(), configBuffer.size()); + std::cout << "test:configLength = " << configLength << std::endl; + std::cout << "test:serialize success start plugin ret = " << ret << std::endl; + // Start + std::vector dataBuffer(diskioPlugin->resultBufferSizeHint); + diskioPlugin->callbacks->onPluginSessionStart(configBuffer.data(), configLength); + while (g_testCount--) { + int len = diskioPlugin->callbacks->onPluginReportResult(dataBuffer.data(), diskioPlugin->resultBufferSizeHint); + std::cout << "test:filler buffer length = " << len << std::endl; + if (len > 0) { + DiskioData diskioData; + diskioData.ParseFromArray(dataBuffer.data(), len); + std::cout << "test:ParseFromArray length = " << len << std::endl; + std::cout << "prev_rd_sectors_kb:" << diskioData.prev_rd_sectors_kb() << std::endl; + std::cout << "prev_wr_sectors_kb:" << diskioData.prev_wr_sectors_kb() << std::endl; + std::cout << "prev_timestamp.tv_sec:" << diskioData.prev_timestamp().tv_sec() << std::endl; + std::cout << "prev_timestamp.tv_nsec:" << diskioData.prev_timestamp().tv_nsec() << std::endl; + std::cout << "rd_sectors_kb:" << diskioData.rd_sectors_kb() << std::endl; + std::cout << "wr_sectors_kb:" << diskioData.wr_sectors_kb() << std::endl; + std::cout << "timestamp.tv_sec:" << diskioData.timestamp().tv_sec() << std::endl; + std::cout << "timestamp.tv_nsec:" << diskioData.timestamp().tv_nsec() << std::endl; + } + std::cout << "test:sleep...................." << std::endl; + sleep(1); + } + diskioPlugin->callbacks->onPluginSessionStop(); + dlclose(handle); +} } // namespace int main(int agrc, char* agrv[]) @@ -61,59 +106,11 @@ int main(int agrc, char* agrv[]) for (int i = 1; i < agrc; i++) { isTestDiskIO = atoi(agrv[i]); } - if (isTestDiskIO) { IoTest(); sleep(SLEEP_TIME); } else { - DiskioConfig protoConfig; - PluginModuleStruct* diskioPlugin; - void* handle = dlopen("libdiskiodataplugin.z.so", RTLD_LAZY); - if (handle == nullptr) { - std::cout << "test:dlopen err: " << dlerror() << std::endl; - return 0; - } - std::cout << "test:handle = " << handle << std::endl; - diskioPlugin = (PluginModuleStruct*)dlsym(handle, "g_pluginModule"); - std::cout << "test:name = " << diskioPlugin->name << std::endl; - std::cout << "test:buffer size = " << diskioPlugin->resultBufferSizeHint << std::endl; - - // Serialize config - int configLength = protoConfig.ByteSizeLong(); - std::vector configBuffer(configLength); - int ret = protoConfig.SerializeToArray(configBuffer.data(), configBuffer.size()); - std::cout << "test:configLength = " << configLength << std::endl; - std::cout << "test:serialize success start plugin ret = " << ret << std::endl; - - // Start - std::vector dataBuffer(diskioPlugin->resultBufferSizeHint); - diskioPlugin->callbacks->onPluginSessionStart(configBuffer.data(), configLength); - while (g_testCount--) { - int len = diskioPlugin->callbacks->onPluginReportResult(dataBuffer.data(), - diskioPlugin->resultBufferSizeHint); - std::cout << "test:filler buffer length = " << len << std::endl; - - if (len > 0) { - DiskioData diskioData; - diskioData.ParseFromArray(dataBuffer.data(), len); - std::cout << "test:ParseFromArray length = " << len << std::endl; - - std::cout << "prev_rd_sectors_kb:" << diskioData.prev_rd_sectors_kb() << std::endl; - std::cout << "prev_wr_sectors_kb:" << diskioData.prev_wr_sectors_kb() << std::endl; - std::cout << "prev_timestamp.tv_sec:" << diskioData.prev_timestamp().tv_sec() << std::endl; - std::cout << "prev_timestamp.tv_nsec:" << diskioData.prev_timestamp().tv_nsec() << std::endl; - std::cout << "rd_sectors_kb:" << diskioData.rd_sectors_kb() << std::endl; - std::cout << "wr_sectors_kb:" << diskioData.wr_sectors_kb() << std::endl; - std::cout << "timestamp.tv_sec:" << diskioData.timestamp().tv_sec() << std::endl; - std::cout << "timestamp.tv_nsec:" << diskioData.timestamp().tv_nsec() << std::endl; - } - - std::cout << "test:sleep...................." << std::endl; - sleep(1); - } - diskioPlugin->callbacks->onPluginSessionStop(); - dlclose(handle); + PrintDiskStats(); } - return 0; } diff --git a/device/plugins/diskio_plugin/test/unittest/diskio_data_plugin_unittest.cpp b/device/plugins/diskio_plugin/test/unittest/diskio_data_plugin_unittest.cpp index e9f3c3ce5dd1e638cf81ff26d7434fda66460eb8..2562260f7fd489dd3603234fec2e51d304a33cdc 100644 --- a/device/plugins/diskio_plugin/test/unittest/diskio_data_plugin_unittest.cpp +++ b/device/plugins/diskio_plugin/test/unittest/diskio_data_plugin_unittest.cpp @@ -256,5 +256,10 @@ HWTEST_F(DiskioDataPluginTest, TestIOStats, TestSize.Level1) IoStats ioStatsIoReportEx(DiskioConfig::IO_REPORT_EX); EXPECT_TRUE(ioStatsIoReportEx.GetIoData()); EXPECT_TRUE(ioStatsIoReportEx.PutPluginStatsData(statsData)); + + auto ioInfo = std::make_shared(); + ioStats.fields_.assign(19, 100); + ioStats.SetIoInfo(ioInfo, "nr_free_pages"); + EXPECT_EQ(ioInfo->timeOfFlush_, 100); } } // namespace diff --git a/device/plugins/ffrt_profiler/service/include/ffrt_profiler_manager.h b/device/plugins/ffrt_profiler/service/include/ffrt_profiler_manager.h index da7222ae69638db842fd873dd27d6d177b6fd624..59299a4fa58658ac464a5c718c889b96c1f68e24 100644 --- a/device/plugins/ffrt_profiler/service/include/ffrt_profiler_manager.h +++ b/device/plugins/ffrt_profiler/service/include/ffrt_profiler_manager.h @@ -83,6 +83,9 @@ private: bool CheckConfig(); bool HandleFfrtProfilerContext(const std::shared_ptr& ctx); clockid_t GetClockId(FfrtProfilerConfig::ClockId clockType); + bool CheckPid(std::set& pidCache); + bool CheckStartupProcessName(); + bool CheckRestartProcessName(std::set& pidCache); private: std::shared_ptr socketService_{nullptr}; diff --git a/device/plugins/ffrt_profiler/service/src/ffrt_profiler_manager.cpp b/device/plugins/ffrt_profiler/service/src/ffrt_profiler_manager.cpp index 20fb477c399aae9950d9491fd434f5f5c3f4daff..f08a2bb438e1ecd03538c87c6d028cac6d1016c9 100644 --- a/device/plugins/ffrt_profiler/service/src/ffrt_profiler_manager.cpp +++ b/device/plugins/ffrt_profiler/service/src/ffrt_profiler_manager.cpp @@ -56,27 +56,31 @@ void FfrtProfilerManager::Init() RegisterAgentPlugin("ffrt-profiler"); } -bool FfrtProfilerManager::CheckConfig() +bool FfrtProfilerManager::CheckPid(std::set& pidCache) { - std::set pidCache; for (const auto& pid : config_.pid()) { - if (pid > 0) { - struct stat statBuf; - std::string pidPath = "/proc/" + std::to_string(pid) + "/status"; - if (stat(pidPath.c_str(), &statBuf) != 0) { - PROFILER_LOG_ERROR(LOG_CORE, "%s: hook process does not exist", __func__); - return false; - } else { - auto [iter, ret] = pidCache.emplace(pid); - if (ret) { - ffrtCtx_.emplace_back(std::make_shared(pid)); - paramValue_ += std::to_string(pid) + ","; - } - continue; + if (pid <= 0) { + continue; + } + struct stat statBuf; + std::string pidPath = "/proc/" + std::to_string(pid) + "/status"; + if (stat(pidPath.c_str(), &statBuf) != 0) { + PROFILER_LOG_ERROR(LOG_CORE, "%s: hook process does not exist", __func__); + return false; + } else { + auto [iter, ret] = pidCache.emplace(pid); + if (ret) { + ffrtCtx_.emplace_back(std::make_shared(pid)); + paramValue_ += std::to_string(pid) + ","; } + continue; } } + return true; +} +bool FfrtProfilerManager::CheckStartupProcessName() +{ for (const auto& name : config_.startup_process_name()) { if (name.empty()) { continue; @@ -91,20 +95,23 @@ bool FfrtProfilerManager::CheckConfig() ffrtCtx_.emplace_back(std::make_shared(name)); } } + return true; +} +bool FfrtProfilerManager::CheckRestartProcessName(std::set& pidCache) +{ for (const auto& name : config_.restart_process_name()) { if (name.empty()) { continue; } - int pidValue = -1; bool isExist = COMMON::IsProcessExist(name, pidValue); if (isExist) { auto [iter, ret] = pidCache.emplace(pidValue); if (!ret) { PROFILER_LOG_ERROR(LOG_CORE, - "FfrtProfilerManager process %s pid is %d, duplicate of pid list in config", - name.c_str(), pidValue); + "FfrtProfilerManager process %s pid is %d, duplicate of pid list in config", + name.c_str(), pidValue); return false; } paramValue_ += name + ","; @@ -114,11 +121,27 @@ bool FfrtProfilerManager::CheckConfig() return false; } } + return true; +} +bool FfrtProfilerManager::CheckConfig() +{ + std::set pidCache; + if (!CheckPid(pidCache)) { + PROFILER_LOG_ERROR(LOG_CORE, "CheckPid failed"); + return false; + } + if (!CheckStartupProcessName()) { + PROFILER_LOG_ERROR(LOG_CORE, "CheckStartupProcessName failed"); + return false; + } + if (!CheckRestartProcessName(pidCache)) { + PROFILER_LOG_ERROR(LOG_CORE, "CheckRestartProcessName failed"); + return false; + } if (config_.flush_interval() == 0) { config_.set_flush_interval(1); } - if (config_.clock_id() == FfrtProfilerConfig::UNKNOW) { PROFILER_LOG_ERROR(LOG_CORE, "FfrtProfilerManager clock_id is unknow"); return false; diff --git a/device/plugins/ffrt_profiler/test/unittest/ffrt_profiler_test.cpp b/device/plugins/ffrt_profiler/test/unittest/ffrt_profiler_test.cpp index ccb5e5d4b9bd3def56c1077200ea7a4659d9e94e..61f830892ab593e48443294912b3d2bc2fbcd2be 100644 --- a/device/plugins/ffrt_profiler/test/unittest/ffrt_profiler_test.cpp +++ b/device/plugins/ffrt_profiler/test/unittest/ffrt_profiler_test.cpp @@ -667,4 +667,27 @@ HWTEST_F(FfrtPofilerTest, TestFunction030, TestSize.Level1) ffrtProfilerMgr->SetConfig(config); EXPECT_TRUE(ffrtProfilerMgr->CheckConfig()); } + +/** + * @tc.name: ffrt plugin + * @tc.desc: FfrtPofiler FfrtProfilerManager check config + * @tc.type: FUNC + */ +HWTEST_F(FfrtPofilerTest, TestFunction040, TestSize.Level1) +{ + std::shared_ptr ffrtProfilerMgr = std::make_shared(); + FfrtProfilerConfig config; + std::set pidCache; + int32_t pid = -1; + config.add_pid(pid); + config.add_pid(ffrtPrfolerExePid_); + config.add_startup_process_name(""); + config.add_startup_process_name("test_name002"); + config.add_restart_process_name(""); + config.set_clock_id(FfrtProfilerConfig::MONOTONIC); + ffrtProfilerMgr->SetConfig(config); + EXPECT_TRUE(ffrtProfilerMgr->CheckPid(pidCache)); + EXPECT_TRUE(ffrtProfilerMgr->CheckStartupProcessName()); + EXPECT_TRUE(ffrtProfilerMgr->CheckRestartProcessName(pidCache)); +} } \ No newline at end of file diff --git a/device/plugins/hiebpf_plugin/tools/include/ebpf_converter.h b/device/plugins/hiebpf_plugin/tools/include/ebpf_converter.h index 2535ba6719d2e6facd4795b8268ac8d02498ab26..8507876beefb2505dd99f3f5a9bacabd396f40a8 100644 --- a/device/plugins/hiebpf_plugin/tools/include/ebpf_converter.h +++ b/device/plugins/hiebpf_plugin/tools/include/ebpf_converter.h @@ -37,6 +37,8 @@ private: void EventMemParsing(); void EventStrParsing(); void EventBIOParsing(); + void WriteEventParsingToFile(FILE *file); + bool ReadMagicHeader(); std::pair> GetSymbolInfo(uint64_t pid, uint64_t ip); public: static std::pair CheckNotExistsFilePath(const std::string& filePath); diff --git a/device/plugins/hiebpf_plugin/tools/src/ebpf_converter.cpp b/device/plugins/hiebpf_plugin/tools/src/ebpf_converter.cpp index 6ad1f455a619462c2b64b8052e1b9382f988f9c9..ef0b93fe534ec7f24187020b756f84163026a489 100644 --- a/device/plugins/hiebpf_plugin/tools/src/ebpf_converter.cpp +++ b/device/plugins/hiebpf_plugin/tools/src/ebpf_converter.cpp @@ -91,49 +91,10 @@ EbpfConverter::~EbpfConverter() } } -void EbpfConverter::StartParsing() +void EbpfConverter::WriteEventParsingToFile(FILE *file) { - if (access(inputPath_.c_str(), R_OK) != 0) { - std::cout << "the input file path is invalid" << std::endl; - return; - } - fd_ = open(inputPath_.c_str(), O_RDONLY); - if (fd_ < 0) { - std::cout << "open " << inputPath_ << " failed" << std::endl; - return; - } - char magic[HEADER_MAGIC + 1]; - if (memset_s(magic, sizeof(magic), 0, sizeof(magic)) != EOK) { - close(fd_); - std::cout << "memset_s error at StartParsing" << std::endl; - return; - } - CHK(Read(reinterpret_cast(&magic), sizeof(magic) - 1)); - outData_ << "magic: " << magic << '\n'; - HeaderDataItem header = {}; - CHK(Read(reinterpret_cast(&header), sizeof(header))); - outData_ << "headSize: " << header.headSize << '\n' << "version: " << header.version << '\n' - << "clock: " << header.clock << '\n' << "cmdLineLen: " << header.cmdLineLen; - - char cmdline[header.cmdLineLen + 1]; - if (header.cmdLineLen > 0) { - size_t dataLen = sizeof(cmdline) > 1 ? static_cast(sizeof(cmdline) - 1) : 0; - CHK(Read(reinterpret_cast(cmdline), dataLen)); - outData_ << "\ncmdline: " << cmdline << '\n'; - } - fileSize_ = lseek(fd_, header.headSize, SEEK_SET); - - FILE *file = fopen(outputPath_.c_str(), "w"); - if (file == nullptr) { - std::cout << "create " << outputPath_ << " failed" << std::endl; - return; - } - - while (true) { - uint32_t type = 0; - if (read(fd_, reinterpret_cast(&type), sizeof(type)) <= 0) { - break; - } + uint32_t type = 0; + while (read(fd_, reinterpret_cast(&type), sizeof(type)) > 0) { switch (type) { case MAPSTRACE: { EventMapsParsing(); @@ -176,6 +137,61 @@ void EbpfConverter::StartParsing() } fd_ = -1; fileSize_ = 0; +} + +bool EbpfConverter::ReadMagicHeader() +{ + if (access(inputPath_.c_str(), R_OK) != 0) { + std::cout << "the input file path is invalid" << std::endl; + return false; + } + fd_ = open(inputPath_.c_str(), O_RDONLY); + if (fd_ < 0) { + std::cout << "open " << inputPath_ << " failed" << std::endl; + return false; + } + char magic[HEADER_MAGIC + 1]; + if (memset_s(magic, sizeof(magic), 0, sizeof(magic)) != EOK) { + close(fd_); + std::cout << "memset_s error at StartParsing" << std::endl; + return false; + } + if (!Read(reinterpret_cast(&magic), sizeof(magic) - 1)) { + close(fd_); + return false; + } + outData_ << "magic: " << magic << '\n'; + HeaderDataItem header = {}; + if (!Read(reinterpret_cast(&header), sizeof(header))) { + close(fd_); + return false; + } + outData_ << "headSize: " << header.headSize << '\n' << "version: " << header.version << '\n' + << "clock: " << header.clock << '\n' << "cmdLineLen: " << header.cmdLineLen; + char cmdline[header.cmdLineLen + 1]; + if (header.cmdLineLen > 0) { + size_t dataLen = sizeof(cmdline) > 1 ? static_cast(sizeof(cmdline) - 1) : 0; + if (!Read(reinterpret_cast(cmdline), dataLen)) { + close(fd_); + return false; + } + outData_ << "\ncmdline: " << cmdline << '\n'; + } + fileSize_ = lseek(fd_, header.headSize, SEEK_SET); + return true; +} + +void EbpfConverter::StartParsing() +{ + if (!ReadMagicHeader()) { + return; + } + FILE *file = fopen(outputPath_.c_str(), "w"); + if (file == nullptr) { + std::cout << "create " << outputPath_ << " failed" << std::endl; + return; + } + WriteEventParsingToFile(file); std::cout << "Data read successfully, output..." << std::endl; size_t len = outData_.str().size(); if (fwrite(outData_.str().c_str(), 1, len, file) != len) { diff --git a/device/plugins/native_hook/test/malloc_test.cpp b/device/plugins/native_hook/test/malloc_test.cpp index 41a563c41ec885fd9350a7347227d78a2da6d4dc..0f71ddf9264af80f96e05036465ce2b8c7161a85 100644 --- a/device/plugins/native_hook/test/malloc_test.cpp +++ b/device/plugins/native_hook/test/malloc_test.cpp @@ -491,27 +491,18 @@ static int CommandParse(int argc, char** argv) } return opterr; } -} // namespace -int main(int argc, char* argv[]) + +static pthread_t** CreateThreadArray(int typeNum) { - // 参数解析 - int ret = CommandParse(argc, argv); - if (ret == -1) { - return 0; - } - int typeNum = BitMapNum(g_hookFlag); - printf(" g_hookFlag = [%u] \n", g_hookFlag); - if (typeNum == 0) { - // 未设置type时默认启动alloc - g_hookFlag |= ALLOC_FLAG; - typeNum++; + if (typeNum <= 0) { + printf("fail:malloc %d memory", typeNum); + return nullptr; } - pthread_t** thrArrayList = static_cast(malloc(sizeof(pthread_t*) * typeNum)); if (thrArrayList == nullptr) { printf("malloc thrArrayList fail\n"); - return 0; + return nullptr; } int type = 0; if ((g_hookFlag & ALLOC_FLAG) != 0) { @@ -528,8 +519,8 @@ int main(int argc, char* argv[]) } int idx; for (idx = 0; idx < threadNum; ++idx) { - int result = pthread_create((thrArrayList[type]) + idx, nullptr, - ThreadFuncC, static_cast(&mallocSize)); + int result = + pthread_create((thrArrayList[type]) + idx, nullptr, ThreadFuncC, static_cast(&mallocSize)); if (result != 0) { printf("Creating thread failed.\n"); } @@ -541,7 +532,6 @@ int main(int argc, char* argv[]) int threadNum = g_threadNum; // 初始化 MmapInit(); - thrArrayList[type] = static_cast(malloc(sizeof(pthread_t) * threadNum)); if (thrArrayList[type] == nullptr) { printf("new thread failed.\n"); @@ -549,20 +539,37 @@ int main(int argc, char* argv[]) int idx; for (idx = 0; idx < threadNum; ++idx) { - int result = pthread_create((thrArrayList[type]) + idx, - nullptr, ThreadMmap, nullptr); + int result = pthread_create((thrArrayList[type]) + idx, nullptr, ThreadMmap, nullptr); if (result != 0) { printf("Creating thread failed.\n"); } } } + return thrArrayList; +} +} // namespace +int main(int argc, char* argv[]) +{ + // 参数解析 + int ret = CommandParse(argc, argv); + if (ret == -1) { + return 0; + } + int typeNum = BitMapNum(g_hookFlag); + printf(" g_hookFlag = [%u] \n", g_hookFlag); + if (typeNum == 0) { + // 未设置type时默认启动alloc + g_hookFlag |= ALLOC_FLAG; + typeNum++; + } + pthread_t** thrArrayList = CreateThreadArray(typeNum); while (getchar() != '\n') { usleep(RESPONSE_SPEED); }; g_runing = 0; int idx; - for (type = 0; type < typeNum; type++) { + for (int type = 0; type < typeNum; type++) { for (idx = 0; idx < g_threadNum; ++idx) { pthread_join((thrArrayList[type])[idx], nullptr); }