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_network_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 {
HtraceNetworkParser(TraceDataCache * dataCache,const TraceStreamerFilters * ctx)22 HtraceNetworkParser::HtraceNetworkParser(TraceDataCache* dataCache, const TraceStreamerFilters* ctx)
23 : HtracePluginTimeParser(dataCache, ctx)
24 {
25 }
26
~HtraceNetworkParser()27 HtraceNetworkParser::~HtraceNetworkParser()
28 {
29 TS_LOGI("network ts MIN:%llu, MAX:%llu", static_cast<unsigned long long>(GetPluginStartTime()),
30 static_cast<unsigned long long>(GetPluginEndTime()));
31 }
32
Parse(NetworkDatas & tracePacket,uint64_t ts)33 void HtraceNetworkParser::Parse(NetworkDatas& tracePacket, uint64_t ts)
34 {
35 auto netSysInfo = tracePacket.network_system_info();
36 ts = streamFilters_->clockFilter_->ToPrimaryTraceTime(TS_CLOCK_REALTIME, ts);
37 streamFilters_->statFilter_->IncreaseStat(TRACE_NETWORK, STAT_EVENT_RECEIVED);
38 networkData_.push_back(std::move(TsNetworkData{ts, netSysInfo}));
39 }
Finish()40 void HtraceNetworkParser::Finish()
41 {
42 auto cmp = [](const TsNetworkData& a, const TsNetworkData& b) { return a.ts < b.ts; };
43 std::sort(networkData_.begin(), networkData_.end(), cmp);
44 bool firstTime = true;
45 uint64_t lastTs = 0;
46 uint64_t lastRx = 0;
47 uint64_t lastTx = 0;
48 uint64_t lastPacketIn = 0.0;
49 uint64_t lastPacketOut = 0.0;
50 for (auto itor = networkData_.begin(); itor != networkData_.end(); itor++) {
51 auto networkSysData = itor->networkSysData;
52 auto newTimeStamp = itor->ts;
53 UpdatePluginTimeRange(TS_CLOCK_REALTIME, itor->ts, newTimeStamp);
54 if (firstTime) {
55 lastTs = newTimeStamp;
56 lastRx = networkSysData.rx_bytes();
57 lastTx = networkSysData.tx_bytes();
58 lastPacketIn = networkSysData.rx_packets();
59 lastPacketOut = networkSysData.tx_packets();
60 firstTime = false;
61 continue;
62 }
63 auto dur = newTimeStamp - lastTs;
64 auto durS = 1.0 * dur / SEC_TO_NS;
65 traceDataCache_->GetNetworkData()->AppendNewNetData(
66 newTimeStamp, networkSysData.tx_bytes(), networkSysData.rx_bytes(), dur,
67 1.0 * (networkSysData.rx_bytes() - lastRx) / durS, 1.0 * (networkSysData.tx_bytes() - lastTx) / durS,
68 networkSysData.rx_packets(), 1.0 * (networkSysData.rx_packets() - lastPacketIn) / durS,
69 networkSysData.tx_packets(), 1.0 * (networkSysData.tx_packets() - lastPacketOut) / durS, "undefined");
70 lastTs = newTimeStamp;
71 lastRx = networkSysData.rx_bytes();
72 lastTx = networkSysData.tx_bytes();
73 lastPacketIn = networkSysData.rx_packets();
74 lastPacketOut = networkSysData.tx_packets();
75 }
76 networkData_.clear();
77 traceDataCache_->MixTraceTime(GetPluginStartTime(), GetPluginEndTime());
78 }
79 } // namespace TraceStreamer
80 } // namespace SysTuning
81