mirror of
https://github.com/livekit/livekit.git
synced 2026-08-04 19:48:40 +00:00
Consolidate getMemoryStats (#1122)
* Consolidate getMemoryStats * Avoid divide-by-0
This commit is contained in:
@@ -2,6 +2,7 @@ package routing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -171,6 +172,7 @@ func (r *LocalRouter) Start() error {
|
||||
return nil
|
||||
}
|
||||
go r.statsWorker()
|
||||
go r.memStatsWorker()
|
||||
// on local routers, Start doesn't do anything, websocket connections initiate the connections
|
||||
go r.rtcMessageWorker()
|
||||
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() {
|
||||
// is a new channel available? if so swap to that one
|
||||
if !r.isStarted.Load() {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/mackerelio/go-osstat/loadavg"
|
||||
"github.com/mackerelio/go-osstat/memory"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"go.uber.org/atomic"
|
||||
|
||||
@@ -84,6 +85,18 @@ func Init(nodeID string) {
|
||||
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) {
|
||||
loadAvg, err := loadavg.Get()
|
||||
if err != nil {
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
|
||||
"github.com/florianl/go-tc"
|
||||
"github.com/mackerelio/go-osstat/cpu"
|
||||
"github.com/mackerelio/go-osstat/memory"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -37,16 +36,6 @@ func getCPUStats() (cpuLoad float32, numCPUs uint32, err error) {
|
||||
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) {
|
||||
rtnl, err := tc.Open(&tc.Config{})
|
||||
if err != nil {
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/mackerelio/go-osstat/cpu"
|
||||
"github.com/mackerelio/go-osstat/memory"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -35,16 +34,6 @@ func getCPUStats() (cpuLoad float32, numCPUs uint32, err error) {
|
||||
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) {
|
||||
// linux only
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user