2020-02-23 09:08:47 +00:00
|
|
|
package counters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"math"
|
2020-02-27 01:45:07 +00:00
|
|
|
"time"
|
2020-02-23 09:08:47 +00:00
|
|
|
|
2022-02-21 03:53:13 +00:00
|
|
|
c "git.tuxpa.in/a/gosora/common"
|
|
|
|
qgen "git.tuxpa.in/a/gosora/query_gen"
|
2020-02-23 09:08:47 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var PerfCounter *DefaultPerfCounter
|
|
|
|
|
|
|
|
type PerfCounterBucket struct {
|
2020-02-27 01:45:07 +00:00
|
|
|
low *MutexCounter64Bucket
|
2020-02-23 09:08:47 +00:00
|
|
|
high *MutexCounter64Bucket
|
2020-02-27 01:45:07 +00:00
|
|
|
avg *MutexCounter64Bucket
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Track perf on a per route basis
|
|
|
|
type DefaultPerfCounter struct {
|
2020-02-28 01:56:06 +00:00
|
|
|
buckets []*PerfCounterBucket
|
2020-02-23 09:08:47 +00:00
|
|
|
|
|
|
|
insert *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultPerfCounter(acc *qgen.Accumulator) (*DefaultPerfCounter, error) {
|
|
|
|
co := &DefaultPerfCounter{
|
2020-02-28 01:56:06 +00:00
|
|
|
buckets: []*PerfCounterBucket{
|
2021-01-06 06:41:08 +00:00
|
|
|
{
|
2020-02-27 01:45:07 +00:00
|
|
|
low: &MutexCounter64Bucket{counter: math.MaxInt64},
|
2020-02-23 09:08:47 +00:00
|
|
|
high: &MutexCounter64Bucket{counter: 0},
|
2020-02-27 01:45:07 +00:00
|
|
|
avg: &MutexCounter64Bucket{counter: 0},
|
2020-02-23 09:08:47 +00:00
|
|
|
},
|
|
|
|
},
|
2020-02-27 01:45:07 +00:00
|
|
|
insert: acc.Insert("perfchunks").Columns("low,high,avg,createdAt").Fields("?,?,?,UTC_TIMESTAMP()").Prepare(),
|
2020-02-23 09:08:47 +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)
|
2020-02-23 09:08:47 +00:00
|
|
|
return co, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (co *DefaultPerfCounter) Tick() error {
|
2020-02-27 01:45:07 +00:00
|
|
|
getCounter := func(b *MutexCounter64Bucket) (c int64) {
|
|
|
|
b.Lock()
|
|
|
|
c = b.counter
|
|
|
|
b.counter = 0
|
|
|
|
b.Unlock()
|
|
|
|
return c
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|
2021-04-07 14:23:11 +00:00
|
|
|
var low int64
|
|
|
|
hTbl := c.GetHookTable()
|
2020-02-23 09:08:47 +00:00
|
|
|
for _, b := range co.buckets {
|
2020-02-27 01:45:07 +00:00
|
|
|
b.low.Lock()
|
2021-04-07 14:23:11 +00:00
|
|
|
low, b.low.counter = b.low.counter, math.MaxInt64
|
2020-02-27 01:45:07 +00:00
|
|
|
b.low.Unlock()
|
2020-02-23 09:08:47 +00:00
|
|
|
if low == math.MaxInt64 {
|
|
|
|
low = 0
|
|
|
|
}
|
|
|
|
high := getCounter(b.high)
|
|
|
|
avg := getCounter(b.avg)
|
2021-04-07 14:23:11 +00:00
|
|
|
c.H_counters_perf_tick_row_hook(hTbl, low, high, avg)
|
2021-05-04 11:01:21 +00:00
|
|
|
if e := co.insertChunk(low, high, avg); e != nil { // TODO: Bulk insert for speed?
|
2021-04-07 14:23:11 +00:00
|
|
|
return errors.Wrap(errors.WithStack(e), "perf counter")
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (co *DefaultPerfCounter) insertChunk(low, high, avg int64) error {
|
|
|
|
if low == 0 && high == 0 && avg == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.DebugLogf("Inserting a pchunk with low %d, high %d, avg %d", low, high, avg)
|
2021-05-05 07:24:16 +00:00
|
|
|
if c.Dev.LogNewLongRoute && high > (5*1000*1000) {
|
2021-05-04 11:01:21 +00:00
|
|
|
c.Logf("pchunk high %d", high)
|
|
|
|
}
|
2021-04-07 14:23:11 +00:00
|
|
|
_, e := co.insert.Exec(low, high, avg)
|
|
|
|
return e
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 23:21:38 +00:00
|
|
|
func (co *DefaultPerfCounter) Push(dur time.Duration /*,_ bool*/) {
|
2020-02-23 09:08:47 +00:00
|
|
|
id := 0
|
|
|
|
b := co.buckets[id]
|
2021-05-04 11:01:21 +00:00
|
|
|
//c.DebugDetail("buckets ", id, ": ", b)
|
2020-02-23 09:08:47 +00:00
|
|
|
micro := dur.Microseconds()
|
2020-02-28 01:41:16 +00:00
|
|
|
if micro >= math.MaxInt32 {
|
|
|
|
c.LogWarning(errors.New("dur should not be int32 max or higher"))
|
2020-02-27 23:21:38 +00:00
|
|
|
}
|
2020-02-23 09:08:47 +00:00
|
|
|
|
|
|
|
low := b.low
|
2020-02-27 01:45:07 +00:00
|
|
|
low.Lock()
|
2020-02-23 09:08:47 +00:00
|
|
|
if micro < low.counter {
|
2020-02-27 01:45:07 +00:00
|
|
|
low.counter = micro
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|
2020-02-27 01:45:07 +00:00
|
|
|
low.Unlock()
|
2020-02-23 09:08:47 +00:00
|
|
|
|
|
|
|
high := b.high
|
2020-02-27 01:45:07 +00:00
|
|
|
high.Lock()
|
2020-02-23 09:08:47 +00:00
|
|
|
if micro > high.counter {
|
2020-02-27 01:45:07 +00:00
|
|
|
high.counter = micro
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|
2020-02-27 01:45:07 +00:00
|
|
|
high.Unlock()
|
2020-02-23 09:08:47 +00:00
|
|
|
|
|
|
|
avg := b.avg
|
2020-02-27 01:45:07 +00:00
|
|
|
avg.Lock()
|
2020-02-23 09:08:47 +00:00
|
|
|
if micro != avg.counter {
|
|
|
|
if avg.counter == 0 {
|
2020-02-27 01:45:07 +00:00
|
|
|
avg.counter = micro
|
|
|
|
} else {
|
|
|
|
avg.counter = (micro + avg.counter) / 2
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-27 01:45:07 +00:00
|
|
|
avg.Unlock()
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|