1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "pbreader_process_parser.h"
16 #include "clock_filter_ex.h"
17 #include "process_filter.h"
18 #include "process_plugin_config.pbreader.h"
19 #include "process_plugin_result.pbreader.h"
20 #include "stat_filter.h"
21 namespace SysTuning {
22 namespace TraceStreamer {
PbreaderProcessParser(TraceDataCache * dataCache,const TraceStreamerFilters * ctx)23 PbreaderProcessParser::PbreaderProcessParser(TraceDataCache *dataCache, const TraceStreamerFilters *ctx)
24 : EventParserBase(dataCache, ctx)
25 {
26 }
27
~PbreaderProcessParser()28 PbreaderProcessParser::~PbreaderProcessParser()
29 {
30 TS_LOGI("process ts MIN:%llu, MAX:%llu", static_cast<unsigned long long>(GetPluginStartTime()),
31 static_cast<unsigned long long>(GetPluginEndTime()));
32 TS_LOGI("process real ts MIN:%llu, MAX:%llu", static_cast<unsigned long long>(MinTs()),
33 static_cast<unsigned long long>(MaxTs()));
34 }
Parse(ProtoReader::BytesView tracePacket,uint64_t ts)35 void PbreaderProcessParser::Parse(ProtoReader::BytesView tracePacket, uint64_t ts)
36 {
37 ProtoReader::ProcessData_Reader processData(tracePacket.data_, tracePacket.size_);
38 for (auto i = processData.processesinfo(); i; ++i) {
39 streamFilters_->statFilter_->IncreaseStat(TRACE_PROCESS, STAT_EVENT_START);
40 ProtoReader::ProcessInfo_Reader processInfoParser(i->ToBytes());
41 ProtoReader::CpuInfo_Reader cpuInfoParser(processInfoParser.cpuinfo());
42 ProtoReader::PssInfo_Reader pssInfoParser(processInfoParser.pssinfo());
43 ProtoReader::DiskioInfo_Reader diskioInfoParser(processInfoParser.diskinfo());
44 auto liveProcess = std::make_unique<TsLiveProcessData>();
45 auto processInfo =
46 std::make_unique<ProcessInfo>(processInfoParser.pid(), processInfoParser.name().ToStdString(),
47 processInfoParser.ppid(), processInfoParser.uid());
48 auto cpuInfo = std::make_unique<CpuInfo>(cpuInfoParser.cpu_usage(), cpuInfoParser.thread_sum(),
49 cpuInfoParser.cpu_time_ms());
50 auto pssInfo = std::make_unique<PssInfo>(pssInfoParser.pss_info());
51 auto diskioInfo = std::make_unique<DiskioInfo>(
52 diskioInfoParser.rchar(), diskioInfoParser.wchar(), diskioInfoParser.syscr(), diskioInfoParser.syscw(),
53 diskioInfoParser.rbytes(), diskioInfoParser.wbytes(), diskioInfoParser.cancelled_wbytes());
54 liveProcess->SetLiveProcess(ts, std::move(processInfo), std::move(cpuInfo), std::move(pssInfo),
55 std::move(diskioInfo));
56 liveProcessData_.push_back(std::move(liveProcess));
57 }
58 }
Finish()59 void PbreaderProcessParser::Finish()
60 {
61 if (!liveProcessData_.size()) {
62 TS_LOGW("process no data");
63 return;
64 }
65 auto cmp = [](const std::unique_ptr<TsLiveProcessData> &a, const std::unique_ptr<TsLiveProcessData> &b) {
66 return a->ts_ < b->ts_;
67 };
68 std::stable_sort(liveProcessData_.begin(), liveProcessData_.end(), cmp);
69 bool first = true;
70 uint64_t lastTs = 0;
71 for (auto itor = liveProcessData_.begin(); itor != liveProcessData_.end(); itor++) {
72 auto tsOld = (*itor)->ts_;
73 (*itor)->ts_ = streamFilters_->clockFilter_->ToPrimaryTraceTime(TS_CLOCK_REALTIME, (*itor)->ts_);
74 UpdatePluginTimeRange(TS_CLOCK_REALTIME, tsOld, (*itor)->ts_);
75 if (first) {
76 lastTs = (*itor)->ts_;
77 first = false;
78 continue;
79 }
80 auto dur = (*itor)->ts_ - lastTs;
81 lastTs = (*itor)->ts_;
82 if (!(*itor)->processInfo_->pid) {
83 continue;
84 }
85 LiveProcessDetailRow row;
86 row.newTimeStamp = (*itor)->ts_;
87 row.dur = dur;
88 row.processID = (*itor)->processInfo_->pid;
89 row.processName = (*itor)->processInfo_->name;
90 row.parentProcessID = (*itor)->processInfo_->ppid;
91 row.uid = (*itor)->processInfo_->uid;
92 row.userName = std::to_string((*itor)->processInfo_->uid);
93 row.cpuUsage = (*itor)->cpuUsageData_->cpuUsage;
94 row.pssInfo = (*itor)->pssInfo_->pssInfo;
95 row.cpuTime = (*itor)->cpuUsageData_->cpuTimeMs;
96 row.threads = (*itor)->cpuUsageData_->threadSum;
97 row.diskWrites = (*itor)->diskio_->wbytes;
98 row.diskReads = (*itor)->diskio_->rbytes;
99 traceDataCache_->GetLiveProcessData()->AppendNewData(row);
100 }
101 liveProcessData_.clear();
102 traceDataCache_->MixTraceTime(GetPluginStartTime(), GetPluginEndTime());
103 }
104 } // namespace TraceStreamer
105 } // namespace SysTuning
106