• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_cpu_data_parser.h"
16 #include "clock_filter_ex.h"
17 #include "process_filter.h"
18 #include "stat_filter.h"
19 namespace SysTuning {
20 namespace TraceStreamer {
PbreaderCpuDataParser(TraceDataCache * dataCache,const TraceStreamerFilters * ctx)21 PbreaderCpuDataParser::PbreaderCpuDataParser(TraceDataCache *dataCache, const TraceStreamerFilters *ctx)
22     : EventParserBase(dataCache, ctx)
23 {
24 }
25 
~PbreaderCpuDataParser()26 PbreaderCpuDataParser::~PbreaderCpuDataParser()
27 {
28     TS_LOGI("cpuData ts MIN:%llu, MAX:%llu", static_cast<unsigned long long>(GetPluginStartTime()),
29             static_cast<unsigned long long>(GetPluginEndTime()));
30 }
Parse(ProtoReader::BytesView tracePacket,uint64_t ts)31 void PbreaderCpuDataParser::Parse(ProtoReader::BytesView tracePacket, uint64_t ts)
32 {
33     ProtoReader::CpuData_Reader cpuData(tracePacket.data_, tracePacket.size_);
34     if (!cpuData.has_cpu_usage_info()) {
35         return;
36     }
37     auto userLoad = cpuData.user_load();
38     auto sysLoad = cpuData.sys_load();
39     auto process_num = cpuData.process_num();
40     ts = streamFilters_->clockFilter_->ToPrimaryTraceTime(TS_CLOCK_REALTIME, ts);
41     UpdatePluginTimeRange(TS_CLOCK_REALTIME, ts, ts);
42     auto cpuUsage = std::make_unique<TsCpuData>();
43     streamFilters_->statFilter_->IncreaseStat(TRACE_CPU_USAGE, STAT_EVENT_RECEIVED);
44     cpuUsage->SetCpuUsage(ts);
45     cpuUsage->SetExtInfo(cpuData.total_load(), userLoad, sysLoad, process_num);
46     cpuData_.push_back(std::move(cpuUsage));
47 }
Finish()48 void PbreaderCpuDataParser::Finish()
49 {
50     auto cmp = [](const std::unique_ptr<TsCpuData> &a, const std::unique_ptr<TsCpuData> &b) { return a->ts_ < b->ts_; };
51     std::stable_sort(cpuData_.begin(), cpuData_.end(), cmp);
52     bool firstTime = true;
53     uint64_t lastTs = 0;
54     for (auto itor = cpuData_.begin(); itor != cpuData_.end(); itor++) {
55         auto newTimeStamp = (*itor)->ts_;
56         if (firstTime) {
57             lastTs = newTimeStamp;
58             firstTime = false;
59             continue;
60         }
61         CpuUsageDetailRow row;
62         row.newTimeStamp = newTimeStamp;
63         row.dur = newTimeStamp - lastTs;
64         row.totalLoad = (*itor)->totalLoad_;
65         row.userLoad = (*itor)->userLoad_;
66         row.systemLoad = (*itor)->sysLoad_;
67         row.threads = (*itor)->processNum_;
68         traceDataCache_->GetCpuUsageInfoData()->AppendNewData(row);
69         lastTs = newTimeStamp;
70     }
71     cpuData_.clear();
72     traceDataCache_->MixTraceTime(GetPluginStartTime(), GetPluginEndTime());
73 }
74 } // namespace TraceStreamer
75 } // namespace SysTuning
76