gosora/common/counters/memory.go
Azareal f10fdcb40c Added the active memory analytics pane. More to come here.
Added the meta store.

Added the panel_menu_statistics_active_memory phrase.
Added the panel_statistics_active_memory_head phrase.

Added the panel_analytics_time_range_month template.

Added Go 1.12 to the Travis list.

Added the stack and heap columns to the memchunks table.
Added the createdAt column to the activity_stream table.
Added the meta table.

You will need to run the patcher / updater for this commit.
2019-05-09 16:58:55 +10:00

85 lines
1.7 KiB
Go

package counters
import (
"time"
"sync"
"runtime"
"database/sql"
c "github.com/Azareal/Gosora/common"
"github.com/Azareal/Gosora/query_gen"
)
var MemoryCounter *DefaultMemoryCounter
type DefaultMemoryCounter struct {
insert *sql.Stmt
totMem uint64
totCount uint64
stackMem uint64
stackCount uint64
heapMem uint64
heapCount uint64
sync.Mutex
}
func NewMemoryCounter(acc *qgen.Accumulator) (*DefaultMemoryCounter, error) {
co := &DefaultMemoryCounter{
insert: acc.Insert("memchunks").Columns("count, stack, heap, createdAt").Fields("?,?,?,UTC_TIMESTAMP()").Prepare(),
}
c.AddScheduledFifteenMinuteTask(co.Tick)
//c.AddScheduledSecondTask(co.Tick)
c.AddShutdownTask(co.Tick)
ticker := time.NewTicker(time.Minute)
go func() {
for {
select {
case <-ticker.C:
var m runtime.MemStats
runtime.ReadMemStats(&m)
co.Lock()
co.totCount++
co.totMem += m.Sys
co.stackCount++
co.stackMem += m.StackInuse
co.heapCount++
co.heapMem += m.HeapAlloc
co.Unlock()
}
}
}()
return co, acc.FirstError()
}
func (co *DefaultMemoryCounter) Tick() (err error) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
var avgMem, avgStack, avgHeap uint64
co.Lock()
co.totCount++
co.totMem += m.Sys
co.stackCount++
co.stackMem += m.StackInuse
co.heapCount++
co.heapMem += m.HeapAlloc
avgMem = co.totMem / co.totCount
avgStack = co.stackMem / co.stackCount
avgHeap = co.heapMem / co.heapCount
co.totMem = 0
co.totCount = 0
co.stackMem = 0
co.stackCount = 0
co.heapMem = 0
co.heapCount = 0
co.Unlock()
c.DebugLogf("Inserting a memchunk with a value of %d - %d - %d", avgMem, avgStack, avgHeap)
_, err = co.insert.Exec(avgMem, avgStack, avgHeap)
return err
}