gosora/common/counters/memory.go
Azareal a8e1076f7c Do proper averaging and collect more samples for the memory analytics pane.
Added an ETag for the alerts endpoint when you're not logged to save bandwidth.
The Page Manager now uses dyntmpl.
The Setting Manager now uses dyntmpl.
The Word Filter Manager now uses dyntmpl.
Fixed the padding on the noavatar alerts for Nox.

Tweaked the panel_word_filters_to phrase.
Tweaked the panel_statistics_memory_head phrase.
Added the panel_statistics_memory_chart_aria phrase.
Added the panel_statistics_memory_table_aria phrase.
Added the panel_statistics_memory_no_memory phrase.
2019-05-02 09:14:07 +10:00

60 lines
1.2 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
sync.Mutex
}
func NewMemoryCounter(acc *qgen.Accumulator) (*DefaultMemoryCounter, error) {
co := &DefaultMemoryCounter{
insert: acc.Insert("memchunks").Columns("count, 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.Unlock()
}
}
}()
return co, acc.FirstError()
}
func (co *DefaultMemoryCounter) Tick() (err error) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
var avgMem uint64
co.Lock()
co.totCount++
co.totMem += m.Sys
avgMem = co.totMem / co.totCount
co.totMem = 0
co.totCount = 0
co.Unlock()
c.DebugLogf("Inserting a memchunk with a value of %d", avgMem)
_, err = co.insert.Exec(avgMem)
return err
}