1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-12-23 07:45:46 +01:00
galene/estimator/estimator.go

79 lines
2.2 KiB
Go
Raw Normal View History

2021-01-19 20:40:19 +01:00
// Package estimator implements a packet and byte rate estimator.
2020-04-30 20:15:52 +02:00
package estimator
import (
"sync/atomic"
"time"
2020-12-19 17:37:48 +01:00
"github.com/jech/galene/rtptime"
2020-04-30 20:15:52 +02:00
)
type Estimator struct {
interval uint64
time uint64
bytes uint32
packets uint32
2020-05-31 23:54:55 +02:00
totalBytes uint32
totalPackets uint32
rate uint32
packetRate uint32
2020-04-30 20:15:52 +02:00
}
2021-01-19 20:40:19 +01:00
// New creates a new estimator that estimates rate over the last interval.
2020-04-30 20:15:52 +02:00
func New(interval time.Duration) *Estimator {
return &Estimator{
interval: rtptime.FromDuration(interval, rtptime.JiffiesPerSec),
time: rtptime.Now(rtptime.JiffiesPerSec),
2020-04-30 20:15:52 +02:00
}
}
func (e *Estimator) swap(now uint64) {
tm := atomic.LoadUint64(&e.time)
jiffies := now - tm
bytes := atomic.SwapUint32(&e.bytes, 0)
packets := atomic.SwapUint32(&e.packets, 0)
atomic.AddUint32(&e.totalBytes, bytes)
atomic.AddUint32(&e.totalPackets, packets)
var rate, packetRate uint32
if jiffies >= rtptime.JiffiesPerSec/1000 {
rate = uint32(uint64(bytes) * rtptime.JiffiesPerSec / jiffies)
packetRate =
uint32(uint64(packets) * rtptime.JiffiesPerSec / jiffies)
2020-04-30 20:15:52 +02:00
}
atomic.StoreUint32(&e.rate, rate)
atomic.StoreUint32(&e.packetRate, packetRate)
atomic.StoreUint64(&e.time, now)
2020-04-30 20:15:52 +02:00
}
2021-01-19 20:40:19 +01:00
// Accumulate records one packet of size bytes
func (e *Estimator) Accumulate(bytes uint32) {
atomic.AddUint32(&e.bytes, bytes)
atomic.AddUint32(&e.packets, 1)
2020-04-30 20:15:52 +02:00
}
func (e *Estimator) estimate(now uint64) (uint32, uint32) {
tm := atomic.LoadUint64(&e.time)
if now < tm || now-tm > e.interval {
2020-04-30 20:15:52 +02:00
e.swap(now)
}
return atomic.LoadUint32(&e.rate), atomic.LoadUint32(&e.packetRate)
2020-04-30 20:15:52 +02:00
}
2021-01-19 20:40:19 +01:00
// Estimate returns an estimate of the rate over the last interval.
// It starts a new interval if the last interval is larger than the value
// passed to New. It returns the byte rate and the packet rate, in units
// per second.
func (e *Estimator) Estimate() (uint32, uint32) {
return e.estimate(rtptime.Now(rtptime.JiffiesPerSec))
2020-04-30 20:15:52 +02:00
}
2020-05-31 23:54:55 +02:00
2021-01-19 20:40:19 +01:00
// Totals returns the total number of bytes and packets accumulated.
2020-05-31 23:54:55 +02:00
func (e *Estimator) Totals() (uint32, uint32) {
b := atomic.LoadUint32(&e.totalBytes) + atomic.LoadUint32(&e.bytes)
p := atomic.LoadUint32(&e.totalPackets) + atomic.LoadUint32(&e.packets)
2020-05-31 23:54:55 +02:00
return p, b
}