device: do atomic 64-bit add outside of vector loop

Only bother updating the rxBytes counter once we've processed a whole
vector, since additions are atomic.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2023-12-11 16:35:57 +01:00
parent 7c20311b3d
commit 542e565baa

View File

@ -447,6 +447,7 @@ func (peer *Peer) RoutineSequentialReceiver(maxBatchSize int) {
elemsContainer.Lock() elemsContainer.Lock()
validTailPacket := -1 validTailPacket := -1
dataPacketReceived := false dataPacketReceived := false
rxBytesLen := uint64(0)
for i, elem := range elemsContainer.elems { for i, elem := range elemsContainer.elems {
if elem.packet == nil { if elem.packet == nil {
// decryption failed // decryption failed
@ -463,7 +464,7 @@ func (peer *Peer) RoutineSequentialReceiver(maxBatchSize int) {
peer.timersHandshakeComplete() peer.timersHandshakeComplete()
peer.SendStagedPackets() peer.SendStagedPackets()
} }
peer.rxBytes.Add(uint64(len(elem.packet) + MinMessageSize)) rxBytesLen += uint64(len(elem.packet) + MinMessageSize)
if len(elem.packet) == 0 { if len(elem.packet) == 0 {
device.log.Verbosef("%v - Receiving keepalive packet", peer) device.log.Verbosef("%v - Receiving keepalive packet", peer)
@ -512,6 +513,8 @@ func (peer *Peer) RoutineSequentialReceiver(maxBatchSize int) {
bufs = append(bufs, elem.buffer[:MessageTransportOffsetContent+len(elem.packet)]) bufs = append(bufs, elem.buffer[:MessageTransportOffsetContent+len(elem.packet)])
} }
peer.rxBytes.Add(rxBytesLen)
if validTailPacket >= 0 { if validTailPacket >= 0 {
peer.SetEndpointFromPacket(elemsContainer.elems[validTailPacket].endpoint) peer.SetEndpointFromPacket(elemsContainer.elems[validTailPacket].endpoint)
peer.keepKeyFreshReceiving() peer.keepKeyFreshReceiving()