Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
sfu
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alain Takoudjou
sfu
Commits
2454e33d
Commit
2454e33d
authored
Jun 09, 2020
by
Juliusz Chroboczek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use jiffies in rate estimator.
This avoids locking in estimate.
parent
f277b42e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
34 deletions
+31
-34
estimator/estimator.go
estimator/estimator.go
+24
-29
estimator/estimator_test.go
estimator/estimator_test.go
+7
-5
No files found.
estimator/estimator.go
View file @
2454e33d
package
estimator
import
(
"sync"
"sync/atomic"
"time"
"sfu/rtptime"
)
type
Estimator
struct
{
interval
time
.
Duration
bytes
uint32
packets
uint32
mu
sync
.
Mutex
interval
uint64
time
uint64
bytes
uint32
packets
uint32
totalBytes
uint32
totalPackets
uint32
rate
uint32
packetRate
uint32
time
time
.
Time
}
func
New
(
interval
time
.
Duration
)
*
Estimator
{
return
&
Estimator
{
interval
:
interval
,
time
:
time
.
Now
(
),
interval
:
rtptime
.
FromDuration
(
interval
,
rtptime
.
JiffiesPerSec
)
,
time
:
rtptime
.
Now
(
rtptime
.
JiffiesPerSec
),
}
}
func
(
e
*
Estimator
)
swap
(
now
time
.
Time
)
{
interval
:=
now
.
Sub
(
e
.
time
)
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
)
if
interval
<
time
.
Millisecond
{
e
.
rate
=
0
e
.
packetRate
=
0
}
else
{
e
.
rate
=
uint32
(
uint64
(
bytes
*
1000
)
/
uint64
(
interval
/
time
.
Millisecond
))
e
.
packetRate
=
uint32
(
uint64
(
packets
*
1000
)
/
uint64
(
interval
/
time
.
Millisecond
))
var
rate
,
packetRate
uint32
if
jiffies
>=
rtptime
.
JiffiesPerSec
/
1000
{
rate
=
uint32
(
uint64
(
bytes
)
*
rtptime
.
JiffiesPerSec
/
jiffies
)
packetRate
=
uint32
(
uint64
(
packets
)
*
rtptime
.
JiffiesPerSec
/
jiffies
)
}
e
.
time
=
now
atomic
.
StoreUint32
(
&
e
.
rate
,
rate
)
atomic
.
StoreUint32
(
&
e
.
packetRate
,
packetRate
)
atomic
.
StoreUint64
(
&
e
.
time
,
now
)
}
func
(
e
*
Estimator
)
Accumulate
(
count
uint32
)
{
...
...
@@ -51,20 +49,17 @@ func (e *Estimator) Accumulate(count uint32) {
atomic
.
AddUint32
(
&
e
.
packets
,
1
)
}
func
(
e
*
Estimator
)
estimate
(
now
time
.
Time
)
(
uint32
,
uint32
)
{
if
now
.
Sub
(
e
.
time
)
>
e
.
interval
{
func
(
e
*
Estimator
)
estimate
(
now
uint64
)
(
uint32
,
uint32
)
{
tm
:=
atomic
.
LoadUint64
(
&
e
.
time
)
if
now
<
tm
||
now
-
tm
>
e
.
interval
{
e
.
swap
(
now
)
}
return
e
.
rate
,
e
.
packetRate
return
atomic
.
LoadUint32
(
&
e
.
rate
),
atomic
.
LoadUint32
(
&
e
.
packetRate
)
}
func
(
e
*
Estimator
)
Estimate
()
(
uint32
,
uint32
)
{
now
:=
time
.
Now
()
e
.
mu
.
Lock
()
defer
e
.
mu
.
Unlock
()
return
e
.
estimate
(
now
)
return
e
.
estimate
(
rtptime
.
Now
(
rtptime
.
JiffiesPerSec
))
}
func
(
e
*
Estimator
)
Totals
()
(
uint32
,
uint32
)
{
...
...
estimator/estimator_test.go
View file @
2454e33d
...
...
@@ -2,18 +2,20 @@ package estimator
import
(
"testing"
"time"
"sfu/rtptime"
)
func
TestEstimator
(
t
*
testing
.
T
)
{
now
:=
time
.
Now
()
e
:=
New
(
time
.
Second
)
now
:=
rtptime
.
Jiffies
()
e
:=
New
(
rtptime
.
JiffiesPerSec
)
e
.
estimate
(
now
)
e
.
Accumulate
(
42
)
e
.
Accumulate
(
128
)
e
.
estimate
(
now
.
Add
(
time
.
Second
))
rate
,
packetRate
:=
e
.
estimate
(
now
.
Add
(
time
.
Second
+
time
.
Millisecond
))
e
.
estimate
(
now
+
rtptime
.
JiffiesPerSec
)
rate
,
packetRate
:=
e
.
estimate
(
now
+
(
rtptime
.
JiffiesPerSec
*
1001
)
/
1000
)
if
rate
!=
42
+
128
{
t
.
Errorf
(
"Expected %v, got %v"
,
42
+
128
,
rate
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment