1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
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 "htrace_process_parser.h"
16 #include "clock_filter_ex.h"
17 #include "cpu_plugin_config.pbreader.h"
18 #include "cpu_plugin_result.pbreader.h"
19 #include "diskio_plugin_config.pbreader.h"
20 #include "diskio_plugin_result.pbreader.h"
21 #include "htrace_event_parser.h"
22 #include "process_filter.h"
23 #include "process_plugin_config.pbreader.h"
24 #include "process_plugin_result.pbreader.h"
25 #include "stat_filter.h"
26 namespace SysTuning {
27 namespace TraceStreamer {
HtraceProcessParser(TraceDataCache * dataCache,const TraceStreamerFilters * ctx)28 HtraceProcessParser::HtraceProcessParser(TraceDataCache* dataCache, const TraceStreamerFilters* ctx)
29 : EventParserBase(dataCache, ctx)
30 {
31 }
32
~HtraceProcessParser()33 HtraceProcessParser::~HtraceProcessParser()
34 {
35 TS_LOGI("process ts MIN:%llu, MAX:%llu", static_cast<unsigned long long>(GetPluginStartTime()),
36 static_cast<unsigned long long>(GetPluginEndTime()));
37 TS_LOGI("process real ts MIN:%llu, MAX:%llu", static_cast<unsigned long long>(MinTs()),
38 static_cast<unsigned long long>(MaxTs()));
39 }
Parse(ProtoReader::BytesView tracePacket,uint64_t ts)40 void HtraceProcessParser::Parse(ProtoReader::BytesView tracePacket, uint64_t ts)
41 {
42 ProtoReader::ProcessData_Reader processData(tracePacket.data_, tracePacket.size_);
43 for (auto i = processData.processesinfo(); i; ++i) {
44 streamFilters_->statFilter_->IncreaseStat(TRACE_PROCESS, STAT_EVENT_START);
45 ProtoReader::ProcessInfo_Reader processInfoParser(i->ToBytes());
46 ProtoReader::CpuInfo_Reader cpuInfoParser(processInfoParser.cpuinfo());
47 ProtoReader::PssInfo_Reader pssInfoParser(processInfoParser.pssinfo());
48 ProtoReader::DiskioInfo_Reader diskioInfoParser(processInfoParser.diskinfo());
49 auto liveProcess = std::make_unique<TsLiveProcessData>();
50 auto processInfo =
51 std::make_unique<ProcessInfo>(processInfoParser.pid(), processInfoParser.name().ToStdString(),
52 processInfoParser.ppid(), processInfoParser.uid());
53 auto cpuInfo = std::make_unique<CpuInfo>(cpuInfoParser.cpu_usage(), cpuInfoParser.thread_sum(),
54 cpuInfoParser.cpu_time_ms());
55 auto pssInfo = std::make_unique<PssInfo>(pssInfoParser.pss_info());
56 auto diskioInfo = std::make_unique<DiskioInfo>(
57 diskioInfoParser.rchar(), diskioInfoParser.wchar(), diskioInfoParser.syscr(), diskioInfoParser.syscw(),
58 diskioInfoParser.rbytes(), diskioInfoParser.wbytes(), diskioInfoParser.cancelled_wbytes());
59 liveProcess->SetLiveProcess(ts, std::move(processInfo), std::move(cpuInfo), std::move(pssInfo),
60 std::move(diskioInfo));
61 liveProcessData_.push_back(std::move(liveProcess));
62 }
63 }
Finish()64 void HtraceProcessParser::Finish()
65 {
66 if (!liveProcessData_.size()) {
67 TS_LOGW("process no data");
68 return;
69 }
70 auto cmp = [](const std::unique_ptr<TsLiveProcessData>& a, const std::unique_ptr<TsLiveProcessData>& b) {
71 return a->ts_ < b->ts_;
72 };
73 std::stable_sort(liveProcessData_.begin(), liveProcessData_.end(), cmp);
74 bool first = true;
75 uint64_t lastTs = 0;
76 for (auto itor = liveProcessData_.begin(); itor != liveProcessData_.end(); itor++) {
77 auto tsOld = (*itor)->ts_;
78 (*itor)->ts_ = streamFilters_->clockFilter_->ToPrimaryTraceTime(TS_CLOCK_REALTIME, (*itor)->ts_);
79 UpdatePluginTimeRange(TS_CLOCK_REALTIME, tsOld, (*itor)->ts_);
80 if (first) {
81 lastTs = (*itor)->ts_;
82 first = false;
83 continue;
84 }
85 auto dur = (*itor)->ts_ - lastTs;
86 lastTs = (*itor)->ts_;
87 if (!(*itor)->processInfo_->pid) {
88 continue;
89 }
90 traceDataCache_->GetLiveProcessData()->AppendNewData(
91 (*itor)->ts_, dur, (*itor)->processInfo_->pid, (*itor)->processInfo_->name, (*itor)->processInfo_->ppid,
92 (*itor)->processInfo_->uid, std::to_string((*itor)->processInfo_->uid), (*itor)->cpuUsageData_->cpuUsage,
93 (*itor)->pssInfo_->pssInfo, (*itor)->cpuUsageData_->cpuTimeMs, (*itor)->cpuUsageData_->threadSum,
94 (*itor)->diskio_->wbytes, (*itor)->diskio_->rbytes);
95 }
96 liveProcessData_.clear();
97 traceDataCache_->MixTraceTime(GetPluginStartTime(), GetPluginEndTime());
98 }
99 } // namespace TraceStreamer
100 } // namespace SysTuning
101