• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.h"
17 #include "htrace_event_parser.h"
18 #include "process_filter.h"
19 #include "stat_filter.h"
20 namespace SysTuning {
21 namespace TraceStreamer {
HtraceProcessParser(TraceDataCache * dataCache,const TraceStreamerFilters * ctx)22 HtraceProcessParser::HtraceProcessParser(TraceDataCache* dataCache, const TraceStreamerFilters* ctx)
23     : HtracePluginTimeParser(dataCache, ctx)
24 {
25 }
26 
~HtraceProcessParser()27 HtraceProcessParser::~HtraceProcessParser()
28 {
29     TS_LOGI("process ts MIN:%llu, MAX:%llu", static_cast<unsigned long long>(GetPluginStartTime()),
30             static_cast<unsigned long long>(GetPluginEndTime()));
31     TS_LOGI("process real ts MIN:%llu, MAX:%llu", static_cast<unsigned long long>(MinTs()),
32             static_cast<unsigned long long>(MaxTs()));
33 }
Parse(ProcessData & tracePacket,uint64_t ts)34 void HtraceProcessParser::Parse(ProcessData& tracePacket, uint64_t ts)
35 {
36     for (int i = 0; i < tracePacket.processesinfo_size(); ++i) {
37         streamFilters_->statFilter_->IncreaseStat(TRACE_PROCESS, STAT_EVENT_START);
38         auto processesInfo = tracePacket.processesinfo(i);
39         auto pssInfo = processesInfo.pssinfo();
40         auto cpuInfo = CpuInfo();
41         if (processesInfo.has_cpuinfo()) {
42             cpuInfo = processesInfo.cpuinfo();
43         }
44         auto liveProcess = std::make_unique<TsLiveProcessData>();
45         auto diskio = processesInfo.diskinfo();
46         liveProcess->SetLiveProcess(ts, processesInfo, cpuInfo, pssInfo, diskio);
47         liveProcessData_.push_back(std::move(liveProcess));
48     }
49 }
Finish()50 void HtraceProcessParser::Finish()
51 {
52     if (!liveProcessData_.size()) {
53         TS_LOGW("process no data");
54         return;
55     }
56     auto cmp = [](const std::unique_ptr<TsLiveProcessData>& a, const std::unique_ptr<TsLiveProcessData>& b) {
57         return a->ts_ < b->ts_;
58     };
59     std::sort(liveProcessData_.begin(), liveProcessData_.end(), cmp);
60     bool first = true;
61     uint64_t lastTs = 0;
62     for (auto itor = liveProcessData_.begin(); itor != liveProcessData_.end(); itor++) {
63         auto tsOld = (*itor)->ts_;
64         (*itor)->ts_ = streamFilters_->clockFilter_->ToPrimaryTraceTime(TS_CLOCK_REALTIME, (*itor)->ts_);
65         UpdatePluginTimeRange(TS_CLOCK_REALTIME, tsOld, (*itor)->ts_);
66         if (first) {
67             lastTs = (*itor)->ts_;
68             first = false;
69             continue;
70         }
71         auto dur = (*itor)->ts_ - lastTs;
72         lastTs = (*itor)->ts_;
73         if (!(*itor)->processInfo_->pid()) {
74             continue;
75         }
76         traceDataCache_->GetLiveProcessData()->AppendNewData(
77             (*itor)->ts_, dur, (*itor)->processInfo_->pid(), (*itor)->processInfo_->name(),
78             (*itor)->processInfo_->ppid(), (*itor)->processInfo_->uid(), std::to_string((*itor)->processInfo_->uid()),
79             (*itor)->cpuUsageData_->cpu_usage(), (*itor)->pssInfo_->pss_info(), (*itor)->cpuUsageData_->cpu_time_ms(),
80             (*itor)->cpuUsageData_->thread_sum(), (*itor)->diskio_->wbytes(), (*itor)->diskio_->rbytes());
81     }
82     liveProcessData_.clear();
83     traceDataCache_->MixTraceTime(GetPluginStartTime(), GetPluginEndTime());
84 }
85 } // namespace TraceStreamer
86 } // namespace SysTuning
87