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