!423 优化comm事件写入

Merge pull request !423 from yuyanqing/master
This commit is contained in:
openharmony_ci 2024-01-15 09:16:53 +00:00 committed by Gitee
commit cde45cb03e
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 30 additions and 11 deletions

View File

@ -263,6 +263,7 @@ private:
bool isFifoServer_ = false;
bool isFifoClient_ = false;
bool dedupStack_ = false;
std::map<pid_t, std::vector<pid_t>> mapPids_;
bool ProcessControl();
bool CreateFifoServer();
bool SendFifoAndWaitReply(const std::string &cmd, const std::chrono::milliseconds &timeOut);
@ -319,6 +320,7 @@ private:
bool ParseControlCmd(const std::string cmd);
bool CheckTargetProcessOptions();
bool CheckTargetPids();
void WriteCommEventBeforeSampling();
VirtualRuntime virtualRuntime_;
#if USE_COLLECT_SYMBOLIC

View File

@ -132,7 +132,7 @@ public:
DfxSymbol GetSymbol(uint64_t ip, pid_t pid, pid_t tid,
const perf_callchain_context &context = PERF_CONTEXT_MAX);
VirtualThread &GetThread(pid_t pid, pid_t tid);
VirtualThread &GetThread(pid_t pid, pid_t tid, const std::string name = "");
const std::map<pid_t, VirtualThread> &GetThreads() const
{
return userSpaceThreadMap_;
@ -204,7 +204,7 @@ private:
void DedupFromRecord(PerfRecordSample *recordSample);
// threads
VirtualThread &UpdateThread(pid_t pid, pid_t tid, const std::string name = "");
VirtualThread &CreateThread(pid_t pid, pid_t tid);
VirtualThread &CreateThread(pid_t pid, pid_t tid, const std::string name = "");
// maps
void UpdateThreadMaps(pid_t pid, pid_t tid, const std::string filename, uint64_t begin,

View File

@ -513,6 +513,7 @@ bool SubCommandRecord::CheckTargetPids()
auto tids = GetSubthreadIDs(pid);
if (!tids.empty()) {
selectTids_.insert(selectTids_.end(), tids.begin(), tids.end());
mapPids_[pid] = tids;
}
}
}
@ -884,6 +885,16 @@ bool SubCommandRecord::PrepareVirtualRuntime()
return true;
}
void SubCommandRecord::WriteCommEventBeforeSampling()
{
for (auto it = mapPids_.begin(); it != mapPids_.end(); ++it) {
virtualRuntime_.GetThread(it->first, it->first);
for (auto tid : it->second) {
virtualRuntime_.GetThread(it->first, tid);
}
}
}
bool SubCommandRecord::ClientCommandResponse(bool OK)
{
using namespace HiperfClient;
@ -1165,6 +1176,9 @@ bool SubCommandRecord::OnSubCommand(std::vector<std::string> &args)
return false;
}
//write comm event
WriteCommEventBeforeSampling();
// make a thread wait the other command
if (clientPipeOutput_ != -1) {
clientCommandHanle_ = std::thread(&SubCommandRecord::ClientCommandHandle, this);

View File

@ -77,12 +77,12 @@ std::string VirtualRuntime::ReadThreadName(pid_t tid, bool isThread)
comm = DEVHOST_FILE_NAME;
} else if (isThread) {
comm = ReadFileToString(StringPrintf("/proc/%d/comm", tid)).c_str();
if (comm == EMPTY_STRING) {
comm = ReadFromSavedCmdLines(tid);
}
} else {
comm = ReadFileToString(StringPrintf("/proc/%d/cmdline", tid)).c_str();
}
if (comm == EMPTY_STRING) {
comm = ReadFromSavedCmdLines(tid);
}
comm.erase(std::remove(comm.begin(), comm.end(), '\r'), comm.end());
comm.erase(std::remove(comm.begin(), comm.end(), '\n'), comm.end());
return comm;
@ -93,7 +93,7 @@ VirtualThread &VirtualRuntime::UpdateThread(pid_t pid, pid_t tid, const std::str
#ifdef HIPERF_DEBUG_TIME
const auto startTime = steady_clock::now();
#endif
VirtualThread &thread = GetThread(pid, tid);
VirtualThread &thread = GetThread(pid, tid, name);
if (!name.empty()) {
thread.name_ = name;
}
@ -103,7 +103,7 @@ VirtualThread &VirtualRuntime::UpdateThread(pid_t pid, pid_t tid, const std::str
return thread;
}
VirtualThread &VirtualRuntime::CreateThread(pid_t pid, pid_t tid)
VirtualThread &VirtualRuntime::CreateThread(pid_t pid, pid_t tid, const std::string name)
{
// make a new one
if (pid == tid) {
@ -129,7 +129,10 @@ VirtualThread &VirtualRuntime::CreateThread(pid_t pid, pid_t tid)
#ifdef HIPERF_DEBUG_TIME
const auto startCreateMmapTime = steady_clock::now();
#endif
thread.name_ = ReadThreadName(tid, pid != tid);
thread.name_ = name;
if (thread.name_.empty()) {
thread.name_ = ReadThreadName(tid, pid != tid);
}
HLOGD("create a new thread record for %u:%u:%s with %zu dso", pid, tid,
thread.name_.c_str(), thread.GetMaps().size());
// we need make a PerfRecordComm
@ -192,18 +195,18 @@ bool VirtualRuntime::UpdateHapSymbols(std::shared_ptr<DfxMap> map)
return true;
}
VirtualThread &VirtualRuntime::GetThread(pid_t pid, pid_t tid)
VirtualThread &VirtualRuntime::GetThread(pid_t pid, pid_t tid, const std::string name)
{
if (userSpaceThreadMap_.find(pid) == userSpaceThreadMap_.end()) {
// no pid found
// create process first
CreateThread(pid, pid);
CreateThread(pid, pid, name);
}
auto it = userSpaceThreadMap_.find(tid);
if (it == userSpaceThreadMap_.end()) {
// we also need thread
return CreateThread(pid, tid);
return CreateThread(pid, tid, name);
} else {
return it->second;
}