Consolidate getMemoryStats (#1122)
* Consolidate getMemoryStats * Avoid divide-by-0
This commit is contained in:
@@ -2,6 +2,7 @@ package routing
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -171,6 +172,7 @@ func (r *LocalRouter) Start() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
go r.statsWorker()
|
go r.statsWorker()
|
||||||
|
go r.memStatsWorker()
|
||||||
// on local routers, Start doesn't do anything, websocket connections initiate the connections
|
// on local routers, Start doesn't do anything, websocket connections initiate the connections
|
||||||
go r.rtcMessageWorker()
|
go r.rtcMessageWorker()
|
||||||
return nil
|
return nil
|
||||||
@@ -201,6 +203,24 @@ func (r *LocalRouter) statsWorker() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *LocalRouter) memStatsWorker() {
|
||||||
|
ticker := time.NewTicker(time.Second * 30)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
<-ticker.C
|
||||||
|
|
||||||
|
var m runtime.MemStats
|
||||||
|
runtime.ReadMemStats(&m)
|
||||||
|
logger.Infow("memstats",
|
||||||
|
"mallocs", m.Mallocs, "frees", m.Frees, "m-f", m.Mallocs-m.Frees,
|
||||||
|
"hinuse", m.HeapInuse, "halloc", m.HeapAlloc, "frag", m.HeapInuse-m.HeapAlloc,
|
||||||
|
)
|
||||||
|
|
||||||
|
runtime.GC()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (r *LocalRouter) rtcMessageWorker() {
|
func (r *LocalRouter) rtcMessageWorker() {
|
||||||
// is a new channel available? if so swap to that one
|
// is a new channel available? if so swap to that one
|
||||||
if !r.isStarted.Load() {
|
if !r.isStarted.Load() {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mackerelio/go-osstat/loadavg"
|
"github.com/mackerelio/go-osstat/loadavg"
|
||||||
|
"github.com/mackerelio/go-osstat/memory"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"go.uber.org/atomic"
|
"go.uber.org/atomic"
|
||||||
|
|
||||||
@@ -84,6 +85,18 @@ func Init(nodeID string) {
|
|||||||
initRoomStats(nodeID)
|
initRoomStats(nodeID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getMemoryStats() (memoryLoad float32, err error) {
|
||||||
|
memInfo, err := memory.Get()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if memInfo.Total != 0 {
|
||||||
|
memoryLoad = float32(memInfo.Used) / float32(memInfo.Total)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func GetUpdatedNodeStats(prev *livekit.NodeStats, prevAverage *livekit.NodeStats) (*livekit.NodeStats, bool, error) {
|
func GetUpdatedNodeStats(prev *livekit.NodeStats, prevAverage *livekit.NodeStats) (*livekit.NodeStats, bool, error) {
|
||||||
loadAvg, err := loadavg.Get()
|
loadAvg, err := loadavg.Get()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
|
|
||||||
"github.com/florianl/go-tc"
|
"github.com/florianl/go-tc"
|
||||||
"github.com/mackerelio/go-osstat/cpu"
|
"github.com/mackerelio/go-osstat/cpu"
|
||||||
"github.com/mackerelio/go-osstat/memory"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -37,16 +36,6 @@ func getCPUStats() (cpuLoad float32, numCPUs uint32, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMemoryStats() (memoryLoad float32, err error) {
|
|
||||||
memInfo, err := memory.Get()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
memoryLoad = float32(memInfo.Used) / float32(memInfo.Total)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func getTCStats() (packets, drops uint32, err error) {
|
func getTCStats() (packets, drops uint32, err error) {
|
||||||
rtnl, err := tc.Open(&tc.Config{})
|
rtnl, err := tc.Open(&tc.Config{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/mackerelio/go-osstat/cpu"
|
"github.com/mackerelio/go-osstat/cpu"
|
||||||
"github.com/mackerelio/go-osstat/memory"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -35,16 +34,6 @@ func getCPUStats() (cpuLoad float32, numCPUs uint32, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMemoryStats() (memoryLoad float32, err error) {
|
|
||||||
memInfo, err := memory.Get()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
memoryLoad = float32(memInfo.Used) / float32(memInfo.Total)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func getTCStats() (packets, drops uint32, err error) {
|
func getTCStats() (packets, drops uint32, err error) {
|
||||||
// linux only
|
// linux only
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user