优化采集命令

Signed-off-by: wwx1285007 <wanglu234@huawei.com>
This commit is contained in:
wwx1285007 2023-12-29 09:20:27 +08:00
parent f7297cd09d
commit 6694afc789
12 changed files with 97 additions and 80 deletions

View File

@ -55,7 +55,6 @@ ohos_executable("SP_daemon") {
"parse_start_frame_trace.cpp",
"parse_start_trace_noh.cpp",
"parse_trace.cpp",
"processes_cpu.cpp",
"profiler_fps.cpp",
"smartperf_command.cpp",
"smartperf_main.cpp",
@ -65,7 +64,6 @@ ohos_executable("SP_daemon") {
"sp_task.cpp",
"sp_utils.cpp",
"startup_delay.cpp",
"system_ram.cpp",
]
include_dirs = [ "include" ]
configs = [ ":config" ]

View File

@ -35,39 +35,27 @@ namespace SmartPerf {
std::map<std::string, std::string> CPU::ItemData()
{
std::map<std::string, std::string> result;
std::vector<CpuFreqs> cpuFreqInfo;
cpuFreqInfo = GetCpuFreq();
std::string cpuFreqStr;
std::string cpuId;
std::vector<CpuFreqs> cpuFreqInfo = GetCpuFreq();
for (size_t i = 0; i < cpuFreqInfo.size(); i++) {
cpuFreqStr = std::to_string(cpuFreqInfo[i].curFreq);
cpuId = std::to_string(cpuFreqInfo[i].cpuId);
std::string cpuFreqStr = std::to_string(cpuFreqInfo[i].curFreq);
std::string cpuId = std::to_string(cpuFreqInfo[i].cpuId);
result["cpu" + cpuId + "Frequency"] = cpuFreqStr;
}
std::vector<CpuUsageInfos> workLoads = GetCpuUsage();
std::string cpuIdStr;
std::string userUsageStr;
std::string niceUsageStr;
std::string systemUsageStr;
std::string idleUsageStr;
std::string ioWaitUsageStr;
std::string irqUsageStr;
std::string softIrqUsageStr;
std::string totalUsageStr;
const size_t oneHundred = 100;
if (workLoads.empty()) {
return result;
}
for (size_t i = 1; i < workLoads.size(); i++) {
cpuIdStr = workLoads[i].cpuId;
userUsageStr = std::to_string(workLoads[i].userUsage * oneHundred);
niceUsageStr = std::to_string(workLoads[i].niceUsage * oneHundred);
systemUsageStr = std::to_string(workLoads[i].systemUsage * oneHundred);
idleUsageStr = std::to_string(workLoads[i].idleUsage * oneHundred);
ioWaitUsageStr = std::to_string(workLoads[i].ioWaitUsage * oneHundred);
irqUsageStr = std::to_string(workLoads[i].irqUsage * oneHundred);
softIrqUsageStr = std::to_string(workLoads[i].softIrqUsage * oneHundred);
totalUsageStr = std::to_string((workLoads[i].userUsage + workLoads[i].niceUsage +
std::string cpuIdStr = workLoads[i].cpuId;
std::string userUsageStr = std::to_string(workLoads[i].userUsage * oneHundred);
std::string niceUsageStr = std::to_string(workLoads[i].niceUsage * oneHundred);
std::string systemUsageStr = std::to_string(workLoads[i].systemUsage * oneHundred);
std::string idleUsageStr = std::to_string(workLoads[i].idleUsage * oneHundred);
std::string ioWaitUsageStr = std::to_string(workLoads[i].ioWaitUsage * oneHundred);
std::string irqUsageStr = std::to_string(workLoads[i].irqUsage * oneHundred);
std::string softIrqUsageStr = std::to_string(workLoads[i].softIrqUsage * oneHundred);
std::string totalUsageStr = std::to_string((workLoads[i].userUsage + workLoads[i].niceUsage +
workLoads[i].systemUsage + workLoads[i].ioWaitUsage +
workLoads[i].irqUsage + workLoads[i].softIrqUsage) * oneHundred);
result[cpuIdStr + "userUsage"] = userUsageStr;
@ -79,9 +67,20 @@ std::map<std::string, std::string> CPU::ItemData()
result[cpuIdStr + "softIrqUsage"] = softIrqUsageStr;
result[cpuIdStr + "Usage"] = totalUsageStr;
}
std::map<std::string, std::string> processCpuInfo = CPU::GetSysProcessCpuLoad();
if (!processCpuInfo.empty()) {
for (auto it = processCpuInfo.begin(); it != processCpuInfo.end(); ++it) {
result.insert(*it);
}
}
return result;
}
void RAM::SetPackageName(std::string pName)
{
OHOS::SmartPerf::StartUpDelay sp;
processId = sp.GetPidByPkg(pName);
}
std::vector<CpuFreqs> CPU::GetCpuFreq()
{
OHOS::SmartPerf::CpuFreqs cpuFreqs;
@ -120,5 +119,25 @@ std::vector<CpuUsageInfos> CPU::GetCpuUsage()
}
return workload;
}
std::map<std::string, std::string> CPU::GetSysProcessCpuLoad() const
{
std::map<std::string, std::string> processCpuInfo;
const size_t oneHundred = 100;
if (processId.length() > 0) {
int32_t procId = 0;
procId = std::stoi(processId);
std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
auto collectResult = collector->CollectProcessCpuStatInfos(true);
auto data = collectResult.data;
processCpuInfo["ProcId"] = std::to_string(data.pid);
processCpuInfo["ProcAppName"] = data.procName;
processCpuInfo["ProcCpuLoad"] = std::to_string(data.cpuLoad * oneHundred);
processCpuInfo["ProcCpuUsage"] = std::to_string(data.cpuUsage * oneHundred);
processCpuInfo["ProcUCpuUsage"] = std::to_string(data.uCpuUsage * oneHundred);
processCpuInfo["ProcSCpuUsage"] = std::to_string(data.sCpuUsage * oneHundred);
return processCpuInfo;
}
return processCpuInfo;
}
}
}

View File

@ -23,33 +23,45 @@
#include <string>
#include <regex>
#include "include/sp_utils.h"
#include "memory_collector.h"
#include "collect_result.h"
#include "include/startup_delay.h"
using namespace OHOS::HiviewDFX;
using namespace OHOS::HiviewDFX::UCollectUtil;
using namespace OHOS::HiviewDFX::UCollect;
namespace OHOS {
namespace SmartPerf {
std::map<std::string, std::string> RAM::ItemData()
{
std::map<std::string, std::string> result;
std::map<std::string, std::string> ramInfo = RAM::GetRamInfo();
result = ramInfo;
std::map<std::string, std::string> sysRamInfo = RAM::GetSysRamInfo();
for (auto it = sysRamInfo.begin(); it != sysRamInfo.end(); ++it) {
result.insert(*it);
}
std::map<std::string, std::string> procRamInfo = RAM::GetRamInfo();
for (auto it = procRamInfo.begin(); it != procRamInfo.end(); ++it) {
result.insert(*it);
}
return result;
}
void RAM::SetProcessId(std::string pid)
void RAM::SetPackageName(std::string pName)
{
processId = std::move(pid);
OHOS::SmartPerf::StartUpDelay sp;
processId = sp.GetPidByPkg(pName);
}
std::map<std::string, std::string> RAM::GetRamInfo() const
{
std::map<std::string, std::string> ramInfo;
std::map<std::string, std::string> procRamInfo;
std::string pssValue = "";
ramInfo["pss"] = "-1";
if (processId.size() == 0) {
return ramInfo;
return procRamInfo;
}
std::string cmd = "cat /proc/" + processId + "/smaps_rollup";
FILE *fd = popen(cmd.c_str(), "r");
if (fd == nullptr) {
return ramInfo;
return procRamInfo;
}
char buf[1024] = {'\0'};
while ((fgets(buf, sizeof(buf), fd)) != nullptr) {
@ -70,9 +82,19 @@ std::map<std::string, std::string> RAM::GetRamInfo() const
}
pclose(fd);
if (pssValue.size() > 0) {
ramInfo["pss"] = pssValue;
procRamInfo["pss"] = pssValue;
}
return ramInfo;
return procRamInfo;
}
std::map<std::string, std::string> SystemRam::GetSysRamInfo() const
{
std::map<std::string, std::string> sysRamInfo;
std::shared_ptr<MemoryCollector> collector = MemoryCollector::Create();
CollectResult<SysMemory> result = collector->CollectSysMemory();
sysRamInfo["memTotal"] = std::to_string(result.data.memTotal);
sysRamInfo["memFree"] = std::to_string(result.data.memFree);
sysRamInfo["memAvailable"] = std::to_string(result.data.memAvailable);
return sysRamInfo;
}
}
}

View File

@ -44,11 +44,13 @@ public:
std::map<std::string, std::string> ItemData() override;
std::vector<CpuFreqs> GetCpuFreq();
std::vector<CpuUsageInfos> GetCpuUsage();
std::map<std::string, std::string> GetSysProcessCpuLoad() const;
void SetPackageName(std::string pName);
private:
CPU() {};
CPU(const CPU &);
CPU &operator = (const CPU &);
std::string processId = "";
};
}
}

View File

@ -23,13 +23,14 @@ public:
void SetProcessId(std::string pid);
std::map<std::string, std::string> GetRamInfo() const
;
std::map<std::string, std::string> GetSysRamInfo() const;
static RAM &GetInstance()
{
static RAM instance;
return instance;
}
std::map<std::string, std::string> ItemData() override;
void SetPackageName(std::string pName);
private:
RAM() {};
RAM(const RAM &);

View File

@ -76,8 +76,6 @@ enum class CommandType {
CT_SESSIONID,
CT_INTERVAL,
CT_NET,
CT_PL,
CT_PR
};
enum class CommandHelp {
HELP,
@ -94,8 +92,7 @@ const std::unordered_map<std::string, CommandType> commandMap = {
{ std::string("-trace"), CommandType::CT_TTRACE }, { std::string("-snapshot"), CommandType::CT_SNAPSHOT },
{ std::string("-hw"), CommandType::CT_HW }, { std::string("-d"), CommandType::CT_D },
{ std::string("-INTERVAL"), CommandType::CT_INTERVAL }, { std::string("-SESSIONID"), CommandType::CT_SESSIONID },
{ std::string("-net"), CommandType::CT_NET }, { std::string("-pl"), CommandType::CT_PL },
{ std::string("-pr"), CommandType::CT_PR },
{ std::string("-net"), CommandType::CT_NET },
};
const std::unordered_map<CommandType, std::string> COMMAND_MAP_REVERSE = {
@ -108,8 +105,7 @@ const std::unordered_map<CommandType, std::string> COMMAND_MAP_REVERSE = {
{ CommandType::CT_TTRACE, std::string("-trace") }, { CommandType::CT_SNAPSHOT, std::string("-snapshot") },
{ CommandType::CT_HW, std::string("-hw") }, { CommandType::CT_D, std::string("-d") },
{ CommandType::CT_INTERVAL, std::string("-INTERVAL") }, { CommandType::CT_SESSIONID, std::string("-SESSIONID") },
{ CommandType::CT_NET, std::string("-net") }, { CommandType::CT_PL, std::string("-pl") },
{ CommandType::CT_PR, std::string("-pr") },
{ CommandType::CT_NET, std::string("-net") },
};

View File

@ -32,18 +32,20 @@ public:
"These are common commands list:\n"
" -N set num of profiler <must be non-null>\n"
" -PKG set pkgname of profiler \n"
" -PID set process id of profiler \n"
" -OUT set output path of CSV\n"
" -c get cpuFreq and cpuLoad \n"
" -c get device cpuFreq、cpuUsage and process cpuUsage、cpuLoad .. \n"
" -g get gpuFreq and gpuLoad \n"
" -f get fps and fps jitters \n"
" -t get soc-temp gpu-temp .. \n"
" -p get current_now and voltage_now \n"
" -r get ram(pss) \n"
" -r get process memory and total memory .. \n"
" -snapshot get screen capture\n"
" -net get networkUp and networkDown\n"
"--------------------------------------------------------------------\n"
"Example: SP_daemon -N 20 -PKG ohos.samples.ecg -c -g -t -p -f \n"
"--------------------------------------------------------------------\n";
"Example 1: SP_daemon -N 20 -c -g -t -p -r -net -snapshot \n"
"--------------------------------------------------------------------\n"
"---------------------------------------------------------------------------------------\n"
"Example 2: SP_daemon -N 20 -PKG ohos.samples.ecg -c -g -t -p -f -r -net -snapshot\n"
"---------------------------------------------------------------------------------------\n";
const int oneParam = 1;
const int twoParam = 2;
const int threeParamMore = 3;

View File

@ -22,7 +22,6 @@ class SpProfilerFactory {
public:
static SpProfiler *GetProfilerItem(MessageType messageType);
static void SetProfilerPkg(std::string pkg);
static void SetProfilerPid(std::string pid);
static void SetByTrace(std::string message);
static void SetByTraceCmd(std::string cmd);
static SpProfiler *GetCmdProfilerItem(CommandType commandType);

View File

@ -152,12 +152,10 @@ public:
if (profiler == nullptr && (iterator->first == MessageType::SET_PKG_NAME)) {
std::string curPkgName = SplitMsg(recvBuf);
SpProfilerFactory::SetProfilerPkg(curPkgName);
OHOS::SmartPerf::StartUpDelay sd;
std::string pidNum = sd.GetPidByPkg(curPkgName);
SpProfilerFactory::SetProfilerPid(pidNum);
spSocket.Sendto(curPkgName);
} else if (profiler == nullptr && (iterator->first == MessageType::SET_PROCESS_ID)) {
SpProfilerFactory::SetProfilerPid(SplitMsg(recvBuf));
std::string curPkgName1 = SplitMsg(recvBuf);
SpProfilerFactory::SetProfilerPkg(curPkgName1);
} else if (profiler == nullptr && (iterator->first == MessageType::CATCH_TRACE_CONFIG)) {
SpProfilerFactory::SetByTrace(SplitMsg(recvBuf));
} else if (profiler == nullptr && (iterator->first == MessageType::CATCH_TRACE_CMD)) {

View File

@ -88,9 +88,6 @@ void SmartPerfCommand::HandleCommand(std::string argStr, std::string argStr1)
case CommandType::CT_PKG:
pkgName = argStr1;
break;
case CommandType::CT_PID:
pid = argStr1;
break;
case CommandType::CT_OUT:
outPathParam = argStr1;
if (strcmp(outPathParam.c_str(), "") != 0) {
@ -98,7 +95,6 @@ void SmartPerfCommand::HandleCommand(std::string argStr, std::string argStr1)
}
break;
case CommandType::CT_C:
case CommandType::CT_PL:
case CommandType::CT_G:
case CommandType::CT_D:
case CommandType::CT_F:
@ -106,7 +102,6 @@ void SmartPerfCommand::HandleCommand(std::string argStr, std::string argStr1)
case CommandType::CT_P:
case CommandType::CT_R:
case CommandType::CT_NET:
case CommandType::CT_PR:
case CommandType::CT_TTRACE:
case CommandType::CT_SNAPSHOT:
case CommandType::CT_HW:
@ -116,7 +111,6 @@ void SmartPerfCommand::HandleCommand(std::string argStr, std::string argStr1)
std::cout << "other unknown args:" << argStr << std::endl;
break;
}
SpProfilerFactory::SetProfilerPid(pid);
SpProfilerFactory::SetProfilerPkg(pkgName);
}

View File

@ -79,6 +79,10 @@ void SpProfilerFactory::SetProfilerPkg(std::string pkg)
{
FPS &fps = FPS::GetInstance();
fps.SetPackageName(pkg);
RAM &ram = RAM::GetInstance();
ram.SetPackageName(pkg);
CPU &cpu = CPU::GetInstance();
cpu.SetPackageName(pkg);
}
void SpProfilerFactory::SetByTraceCmd(std::string cmd)
{
@ -115,13 +119,6 @@ void SpProfilerFactory::SetByTrace(std::string message)
ByTrace &bTrace = ByTrace::GetInstance();
bTrace.SetTraceConfig(mSum, mInterval, mThreshold, lowFps, mCurNum);
}
void SpProfilerFactory::SetProfilerPid(std::string pid)
{
RAM &ram = RAM::GetInstance();
ram.SetProcessId(pid);
ProcessesCpu &processesCpu = ProcessesCpu::GetInstance();
processesCpu.SetProcessId(pid);
}
SpProfiler *SpProfilerFactory::GetCmdProfilerItem(CommandType commandType)
{
SpProfiler *profiler = nullptr;
@ -129,9 +126,6 @@ SpProfiler *SpProfilerFactory::GetCmdProfilerItem(CommandType commandType)
case CommandType::CT_C:
profiler = &CPU::GetInstance();
break;
case CommandType::CT_PL:
profiler = &ProcessesCpu::GetInstance();
break;
case CommandType::CT_G:
profiler = &GPU::GetInstance();
break;
@ -150,9 +144,6 @@ SpProfiler *SpProfilerFactory::GetCmdProfilerItem(CommandType commandType)
profiler = &Temperature::GetInstance();
break;
case CommandType::CT_R:
profiler = &SystemRam::GetInstance();
break;
case CommandType::CT_PR:
profiler = &RAM::GetInstance();
break;
case CommandType::CT_NET:

View File

@ -89,11 +89,6 @@ ErrCode SPTask::StartTask(std::function<void(std::string data)> msgTask)
startTime = SPUtils::GetCurTime();
if (!curTaskInfo.packageName.empty()) {
SpProfilerFactory::SetProfilerPkg(curTaskInfo.packageName);
std::string pidCmd = "pidof " + curTaskInfo.packageName;
std::string pidResult;
if (SPUtils::LoadCmd(pidCmd, pidResult)) {
SpProfilerFactory::SetProfilerPid(pidResult);
}
}
thread = std::thread([this, msgTask]() {
std::cout << "Task " << curTaskInfo.sessionId << ": collecting data loop..." << std::endl;