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