!643 修复stat命令采集不存在的pid

Merge pull request !643 from leiguangyu/master
This commit is contained in:
openharmony_ci 2024-11-14 02:19:52 +00:00 committed by Gitee
commit 5da5a213eb
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 30 additions and 1 deletions

View File

@ -65,6 +65,7 @@ public:
bool ReadDataSection(ProcessRecordCB &callback);
bool ReadRecords(ProcessRecordCB &callback);
bool Read(void *buf, size_t len);
void SetWriteRecordStat(bool isWrite);
private:
std::string fileBuffer_;

View File

@ -214,6 +214,11 @@ bool PerfFileWriter::Read(void *buf, size_t len)
return true;
}
void PerfFileWriter::SetWriteRecordStat(bool isWrite)
{
isWritingRecord = isWrite;
}
uint64_t PerfFileWriter::GetDataSize() const
{
return dataSection_.size;

View File

@ -1260,6 +1260,10 @@ bool SubCommandRecord::OnSubCommand(std::vector<std::string> &args)
}
HIPERF_HILOGI(MODULE_DEFAULT, "SubCommandRecord perfEvents tracking finish");
if (isSpe_) {
HLOGD("stop write spe record");
fileWriter_->SetWriteRecordStat(false);
}
startSaveFileTimes_ = steady_clock::now();
if (!FinishWriteRecordFile()) {
HLOGE("Fail to finish record file %s", outputFilename_.c_str());

View File

@ -651,6 +651,10 @@ bool SubCommandStat::OnSubCommand(std::vector<std::string> &args)
HLOGV("CheckAppIsRunning() failed");
return false;
}
if (!CheckOptionPid(selectPids_)) {
HLOGV("CheckOptionPid() failed");
return false;
}
perfEvents_.SetCpu(selectCpus_);
std::vector<pid_t> pids;

View File

@ -17,6 +17,7 @@
#include <algorithm>
#include <cinttypes>
#include <condition_variable>
#include <cstdlib>
#include <mutex>
#include <regex>
#include <sstream>
@ -2245,10 +2246,24 @@ HWTEST_F(SubCommandStatTest, TestOnSubCommand_app_running, TestSize.Level1)
*/
HWTEST_F(SubCommandStatTest, CheckPidAndApp, TestSize.Level1)
{
pid_t existPid = -1;
const std::string basePath {"/proc/"};
std::vector<std::string> subDirs = GetSubDirs(basePath);
for (const auto &subDir : subDirs) {
if (!IsDigits(subDir)) {
continue;
}
pid_t pid = std::stoll(subDir);
existPid = max(existPid, pid);
}
std::string cmd = "stat -p " + std::to_string(existPid + rand() % 100 + 1) + " -d 2";
EXPECT_EQ(Command::DispatchCommand(cmd), false);
StdoutRecord stdoutRecord;
stdoutRecord.Start();
const auto startTime = chrono::steady_clock::now();
EXPECT_EQ(Command::DispatchCommand("stat -p 700001 -d 2"), true);
std::string existCmd = "stat -p " + std::to_string(existPid) + " -d 2";
EXPECT_EQ(Command::DispatchCommand(existCmd), true);
const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
chrono::steady_clock::now() - startTime);
EXPECT_LE(costMs.count(), defaultRunTimeoutMs);