2019-05-01 06:59:51 +00:00
|
|
|
package counters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2019-07-28 04:01:33 +00:00
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2019-05-01 06:59:51 +00:00
|
|
|
|
|
|
|
c "github.com/Azareal/Gosora/common"
|
2019-07-28 04:01:33 +00:00
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
|
|
|
"github.com/pkg/errors"
|
2019-05-01 06:59:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var MemoryCounter *DefaultMemoryCounter
|
|
|
|
|
|
|
|
type DefaultMemoryCounter struct {
|
|
|
|
insert *sql.Stmt
|
2019-05-09 06:58:55 +00:00
|
|
|
|
2019-07-28 04:01:33 +00:00
|
|
|
totMem uint64
|
|
|
|
totCount uint64
|
|
|
|
stackMem uint64
|
2019-05-09 06:58:55 +00:00
|
|
|
stackCount uint64
|
2019-07-28 04:01:33 +00:00
|
|
|
heapMem uint64
|
|
|
|
heapCount uint64
|
2019-05-09 06:58:55 +00:00
|
|
|
|
2019-05-01 23:14:07 +00:00
|
|
|
sync.Mutex
|
2019-05-01 06:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMemoryCounter(acc *qgen.Accumulator) (*DefaultMemoryCounter, error) {
|
|
|
|
co := &DefaultMemoryCounter{
|
2020-02-28 05:47:43 +00:00
|
|
|
insert: acc.Insert("memchunks").Columns("count,stack,heap,createdAt").Fields("?,?,?,UTC_TIMESTAMP()").Prepare(),
|
2019-05-01 06:59:51 +00:00
|
|
|
}
|
2021-05-03 00:36:29 +00:00
|
|
|
c.Tasks.FifteenMin.Add(co.Tick)
|
|
|
|
//c.Tasks.Sec.Add(co.Tick)
|
|
|
|
c.Tasks.Shutdown.Add(co.Tick)
|
2019-05-01 23:14:07 +00:00
|
|
|
ticker := time.NewTicker(time.Minute)
|
|
|
|
go func() {
|
2021-05-05 07:24:16 +00:00
|
|
|
defer c.EatPanics()
|
2019-05-01 23:14:07 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
var m runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&m)
|
|
|
|
co.Lock()
|
|
|
|
co.totCount++
|
|
|
|
co.totMem += m.Sys
|
2019-05-09 06:58:55 +00:00
|
|
|
co.stackCount++
|
|
|
|
co.stackMem += m.StackInuse
|
|
|
|
co.heapCount++
|
|
|
|
co.heapMem += m.HeapAlloc
|
2019-05-01 23:14:07 +00:00
|
|
|
co.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2019-05-01 06:59:51 +00:00
|
|
|
return co, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
2021-04-27 10:20:26 +00:00
|
|
|
func (co *DefaultMemoryCounter) Tick() (e error) {
|
2019-05-01 06:59:51 +00:00
|
|
|
var m runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&m)
|
2020-02-28 05:47:43 +00:00
|
|
|
var rTotMem, rTotCount, rStackMem, rStackCount, rHeapMem, rHeapCount uint64
|
2019-05-09 06:58:55 +00:00
|
|
|
|
2020-02-28 05:47:43 +00:00
|
|
|
co.Lock()
|
2019-05-09 06:58:55 +00:00
|
|
|
|
2020-02-28 05:47:43 +00:00
|
|
|
rTotMem = co.totMem
|
|
|
|
rTotCount = co.totCount
|
|
|
|
rStackMem = co.stackMem
|
|
|
|
rStackCount = co.stackCount
|
|
|
|
rHeapMem = co.heapMem
|
|
|
|
rHeapCount = co.heapCount
|
2019-05-09 06:58:55 +00:00
|
|
|
|
2019-05-01 23:14:07 +00:00
|
|
|
co.totMem = 0
|
|
|
|
co.totCount = 0
|
2019-05-09 06:58:55 +00:00
|
|
|
co.stackMem = 0
|
|
|
|
co.stackCount = 0
|
|
|
|
co.heapMem = 0
|
|
|
|
co.heapCount = 0
|
|
|
|
|
2019-05-01 23:14:07 +00:00
|
|
|
co.Unlock()
|
2019-05-09 06:58:55 +00:00
|
|
|
|
2020-02-28 05:47:43 +00:00
|
|
|
var avgMem, avgStack, avgHeap uint64
|
|
|
|
avgMem = (rTotMem + m.Sys) / (rTotCount + 1)
|
|
|
|
avgStack = (rStackMem + m.StackInuse) / (rStackCount + 1)
|
|
|
|
avgHeap = (rHeapMem + m.HeapAlloc) / (rHeapCount + 1)
|
|
|
|
|
2019-05-09 06:58:55 +00:00
|
|
|
c.DebugLogf("Inserting a memchunk with a value of %d - %d - %d", avgMem, avgStack, avgHeap)
|
2021-04-27 10:20:26 +00:00
|
|
|
_, e = co.insert.Exec(avgMem, avgStack, avgHeap)
|
|
|
|
if e != nil {
|
|
|
|
return errors.Wrap(errors.WithStack(e), "mem counter")
|
2019-07-28 04:01:33 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|