2018-12-03 04:39:13 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Copyright (C) Microsoft. All rights reserved.
|
|
|
|
// Licensed under the MIT license.
|
|
|
|
// See LICENSE.txt file in the project root for full license information.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"time"
|
2020-06-19 01:07:05 +08:00
|
|
|
)
|
2018-12-03 04:39:13 +08:00
|
|
|
|
2020-06-19 01:07:05 +08:00
|
|
|
type ethrNetStat struct {
|
|
|
|
netDevStats []ethrNetDevStat
|
|
|
|
tcpStats ethrTCPStat
|
|
|
|
}
|
|
|
|
|
|
|
|
type ethrNetDevStat struct {
|
|
|
|
interfaceName string
|
|
|
|
rxBytes uint64
|
|
|
|
txBytes uint64
|
|
|
|
rxPkts uint64
|
|
|
|
txPkts uint64
|
|
|
|
}
|
2018-12-03 04:39:13 +08:00
|
|
|
|
2020-06-19 01:07:05 +08:00
|
|
|
type ethrTCPStat struct {
|
|
|
|
segRetrans uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNetworkStats() ethrNetStat {
|
|
|
|
stats := ðrNetStat{}
|
|
|
|
getNetDevStats(stats)
|
|
|
|
/*
|
2020-11-10 21:37:52 +08:00
|
|
|
devStats, err := osStats.GetNetDevStats()
|
|
|
|
if err != nil {
|
|
|
|
return stats.EthrNetStats{}, errors.Wrap(err, "getNetworkStats: could not get net device stats")
|
|
|
|
}
|
2020-06-19 01:07:05 +08:00
|
|
|
*/
|
|
|
|
sort.SliceStable(stats.netDevStats, func(i, j int) bool {
|
|
|
|
return stats.netDevStats[i].interfaceName < stats.netDevStats[j].interfaceName
|
2018-12-03 04:39:13 +08:00
|
|
|
})
|
2020-06-19 01:07:05 +08:00
|
|
|
getTCPStats(stats)
|
2018-12-03 04:39:13 +08:00
|
|
|
|
2020-06-19 01:07:05 +08:00
|
|
|
/*
|
2020-11-10 21:37:52 +08:00
|
|
|
tcpStats, err := osStats.GetTCPStats()
|
|
|
|
if err != nil {
|
|
|
|
return stats.EthrNetStats{}, errors.Wrap(err, "getNetworkStats: could not get net TCP stats")
|
|
|
|
}
|
2019-07-23 11:08:04 +08:00
|
|
|
|
2020-11-10 21:37:52 +08:00
|
|
|
return stats.EthrNetStats{NetDevStats: devStats, TCPStats: tcpStats}, nil
|
2020-06-19 01:07:05 +08:00
|
|
|
*/
|
|
|
|
return *stats
|
2018-12-03 04:39:13 +08:00
|
|
|
}
|
|
|
|
|
2020-06-19 01:07:05 +08:00
|
|
|
func getNetDevStatDiff(curStats ethrNetDevStat, prevNetStats ethrNetStat, seconds uint64) ethrNetDevStat {
|
|
|
|
for _, prevStats := range prevNetStats.netDevStats {
|
|
|
|
if prevStats.interfaceName != curStats.interfaceName {
|
2018-12-03 04:39:13 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-06-19 01:07:05 +08:00
|
|
|
if curStats.rxBytes >= prevStats.rxBytes {
|
|
|
|
curStats.rxBytes -= prevStats.rxBytes
|
2018-12-03 04:39:13 +08:00
|
|
|
} else {
|
2020-06-19 01:07:05 +08:00
|
|
|
curStats.rxBytes += (^uint64(0) - prevStats.rxBytes)
|
2018-12-03 04:39:13 +08:00
|
|
|
}
|
|
|
|
|
2020-06-19 01:07:05 +08:00
|
|
|
if curStats.txBytes >= prevStats.txBytes {
|
|
|
|
curStats.txBytes -= prevStats.txBytes
|
2018-12-03 04:39:13 +08:00
|
|
|
} else {
|
2020-06-19 01:07:05 +08:00
|
|
|
curStats.txBytes += (^uint64(0) - prevStats.txBytes)
|
2018-12-03 04:39:13 +08:00
|
|
|
}
|
|
|
|
|
2020-06-19 01:07:05 +08:00
|
|
|
if curStats.rxPkts >= prevStats.rxPkts {
|
|
|
|
curStats.rxPkts -= prevStats.rxPkts
|
2018-12-03 04:39:13 +08:00
|
|
|
} else {
|
2020-06-19 01:07:05 +08:00
|
|
|
curStats.rxPkts += (^uint64(0) - prevStats.rxPkts)
|
2018-12-03 04:39:13 +08:00
|
|
|
}
|
|
|
|
|
2020-06-19 01:07:05 +08:00
|
|
|
if curStats.txPkts >= prevStats.txPkts {
|
|
|
|
curStats.txPkts -= prevStats.txPkts
|
2018-12-03 04:39:13 +08:00
|
|
|
} else {
|
2020-06-19 01:07:05 +08:00
|
|
|
curStats.txPkts += (^uint64(0) - prevStats.txPkts)
|
2018-12-03 04:39:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
2020-06-19 01:07:05 +08:00
|
|
|
curStats.rxBytes /= seconds
|
|
|
|
curStats.txBytes /= seconds
|
|
|
|
curStats.rxPkts /= seconds
|
|
|
|
curStats.txPkts /= seconds
|
2018-12-03 04:39:13 +08:00
|
|
|
return curStats
|
|
|
|
}
|
|
|
|
|
|
|
|
var statsEnabled bool
|
|
|
|
|
|
|
|
func startStatsTimer() {
|
|
|
|
if statsEnabled {
|
|
|
|
return
|
|
|
|
}
|
2020-11-10 21:37:52 +08:00
|
|
|
|
|
|
|
// In an ideal setup, client and server should print stats at the same time.
|
|
|
|
// However, instead of building a whole time synchronization mechanism, a
|
|
|
|
// hack is used that starts stat at a second granularity. This is done on
|
|
|
|
// both client and sever, and as long as both client & server have time
|
|
|
|
// synchronized e.g. with a time server, both would print stats of the running
|
|
|
|
// test at _almost_ the same time.
|
|
|
|
SleepUntilNextWholeSecond()
|
|
|
|
|
|
|
|
lastStatsTime = time.Now()
|
2018-12-03 04:39:13 +08:00
|
|
|
ticker := time.NewTicker(time.Second)
|
|
|
|
statsEnabled = true
|
|
|
|
go func() {
|
|
|
|
for statsEnabled {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
emitStats()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopStatsTimer() {
|
|
|
|
statsEnabled = false
|
|
|
|
}
|
|
|
|
|
2019-01-21 01:13:47 +08:00
|
|
|
var lastStatsTime time.Time = time.Now()
|
|
|
|
|
|
|
|
func timeToNextTick() time.Duration {
|
|
|
|
nextTick := lastStatsTime.Add(time.Second)
|
|
|
|
return time.Until(nextTick)
|
|
|
|
}
|
|
|
|
|
2018-12-03 04:39:13 +08:00
|
|
|
func emitStats() {
|
2019-01-21 01:13:47 +08:00
|
|
|
d := time.Since(lastStatsTime)
|
|
|
|
lastStatsTime = time.Now()
|
|
|
|
seconds := int64(d.Seconds())
|
|
|
|
if seconds < 1 {
|
|
|
|
seconds = 1
|
|
|
|
}
|
2018-12-03 04:39:13 +08:00
|
|
|
ui.emitTestResultBegin()
|
2019-01-21 01:13:47 +08:00
|
|
|
emitTestResults(uint64(seconds))
|
2018-12-03 04:39:13 +08:00
|
|
|
ui.emitTestResultEnd()
|
2020-06-19 01:07:05 +08:00
|
|
|
ui.emitStats(getNetworkStats())
|
2019-01-21 01:13:47 +08:00
|
|
|
ui.paint(uint64(seconds))
|
2018-12-03 04:39:13 +08:00
|
|
|
}
|
|
|
|
|
2019-01-21 01:13:47 +08:00
|
|
|
func emitTestResults(s uint64) {
|
2018-12-03 04:39:13 +08:00
|
|
|
gSessionLock.RLock()
|
|
|
|
defer gSessionLock.RUnlock()
|
|
|
|
for _, k := range gSessionKeys {
|
|
|
|
v := gSessions[k]
|
2019-01-21 01:13:47 +08:00
|
|
|
ui.emitTestResult(v, TCP, s)
|
|
|
|
ui.emitTestResult(v, UDP, s)
|
|
|
|
ui.emitTestResult(v, ICMP, s)
|
2018-12-03 04:39:13 +08:00
|
|
|
}
|
|
|
|
}
|