optimization: optimized locking and op atomicity for the packets queue and network statistics view

This commit is contained in:
evilsocket 2018-02-05 21:04:25 +01:00
commit 305e22ee2e
3 changed files with 33 additions and 20 deletions

View file

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"sort"
"sync/atomic"
"time"
"github.com/evilsocket/bettercap-ng/core"
@ -112,9 +113,6 @@ func (d *Discovery) showTable(header []string, rows [][]string) {
}
func (d *Discovery) Show(by string) error {
d.Session.Queue.Lock()
defer d.Session.Queue.Unlock()
targets := d.Session.Targets.List()
if by == "seen" {
sort.Sort(BySeenSorter(targets))
@ -146,11 +144,11 @@ func (d *Discovery) Show(by string) error {
fmt.Printf("\n%s %s / %s %s / %d pkts / %d errs\n\n",
core.Red("↑"),
humanize.Bytes(d.Session.Queue.Sent),
humanize.Bytes(atomic.LoadUint64(&d.Session.Queue.Sent)),
core.Green("↓"),
humanize.Bytes(d.Session.Queue.Received),
d.Session.Queue.PktReceived,
d.Session.Queue.Errors)
humanize.Bytes(atomic.LoadUint64(&d.Session.Queue.Received)),
atomic.LoadUint64(&d.Session.Queue.PktReceived),
atomic.LoadUint64(&d.Session.Queue.Errors))
s := EventsStream{}
events := d.Session.Events.Sorted()

View file

@ -28,6 +28,9 @@ type BySentSorter []*net.Endpoint
func (a BySentSorter) Len() int { return len(a) }
func (a BySentSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a BySentSorter) Less(i, j int) bool {
session.I.Queue.Lock()
defer session.I.Queue.Unlock()
var found bool = false
var aTraffic *packets.Traffic = nil
var bTraffic *packets.Traffic = nil
@ -48,6 +51,9 @@ type ByRcvdSorter []*net.Endpoint
func (a ByRcvdSorter) Len() int { return len(a) }
func (a ByRcvdSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByRcvdSorter) Less(i, j int) bool {
session.I.Queue.Lock()
defer session.I.Queue.Unlock()
var found bool = false
var aTraffic *packets.Traffic = nil
var bTraffic *packets.Traffic = nil