tracker/stats/mem.go

79 lines
2.5 KiB
Go
Raw Normal View History

2015-01-01 18:02:25 +01:00
// Copyright 2015 The Chihaya Authors. All rights reserved.
2014-07-23 23:08:06 +02:00
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package stats
2014-07-25 02:27:20 +02:00
import "runtime"
2014-07-23 23:08:06 +02:00
// BasicMemStats includes a few of the fields from runtime.MemStats suitable for
// general logging.
type BasicMemStats struct {
// General statistics.
Alloc uint64 // bytes allocated and still in use
TotalAlloc uint64 // bytes allocated (even if freed)
2014-07-23 23:58:44 +02:00
Sys uint64 // bytes obtained from system (sum of XxxSys in runtime)
2014-07-23 23:08:06 +02:00
Lookups uint64 // number of pointer lookups
Mallocs uint64 // number of mallocs
Frees uint64 // number of frees
// Main allocation heap statistics.
HeapAlloc uint64 // bytes allocated and still in use
HeapSys uint64 // bytes obtained from system
HeapIdle uint64 // bytes in idle spans
HeapInuse uint64 // bytes in non-idle span
HeapReleased uint64 // bytes released to the OS
HeapObjects uint64 // total number of allocated objects
// Garbage collector statistics.
2015-08-20 21:06:57 +02:00
PauseTotalNs uint64
LatestPauseNs uint64
2014-07-23 23:08:06 +02:00
}
2014-08-01 22:16:38 +02:00
type memStatsPlaceholder interface{}
2014-07-25 02:27:20 +02:00
2014-07-23 23:25:01 +02:00
// MemStatsWrapper wraps runtime.MemStats with an optionally less verbose JSON
// representation. The JSON field names correspond exactly to the runtime field
// names to avoid reimplementing the entire struct.
2014-07-23 23:08:06 +02:00
type MemStatsWrapper struct {
2014-08-01 22:16:38 +02:00
memStatsPlaceholder `json:"Memory"`
2014-07-25 02:27:20 +02:00
basic *BasicMemStats
cache *runtime.MemStats
2014-07-23 23:08:06 +02:00
}
func NewMemStatsWrapper(verbose bool) *MemStatsWrapper {
2014-07-25 02:27:20 +02:00
stats := &MemStatsWrapper{cache: &runtime.MemStats{}}
2014-07-23 23:08:06 +02:00
2014-07-25 02:27:20 +02:00
if verbose {
2014-08-01 22:16:38 +02:00
stats.memStatsPlaceholder = stats.cache
2014-07-23 23:08:06 +02:00
} else {
2014-07-25 02:27:20 +02:00
stats.basic = &BasicMemStats{}
2014-08-01 22:16:38 +02:00
stats.memStatsPlaceholder = stats.basic
2014-07-23 23:08:06 +02:00
}
2014-07-25 02:27:20 +02:00
return stats
2014-07-23 23:08:06 +02:00
}
2014-07-23 23:25:01 +02:00
// Update fetches the current memstats from runtime and resets the cache.
2014-07-23 23:08:06 +02:00
func (s *MemStatsWrapper) Update() {
2014-07-25 02:27:20 +02:00
runtime.ReadMemStats(s.cache)
2014-07-23 23:08:06 +02:00
2014-07-25 02:27:20 +02:00
if s.basic != nil {
2014-07-23 23:08:06 +02:00
// Gross, but any decent editor can generate this in a couple commands.
2014-07-25 02:27:20 +02:00
s.basic.Alloc = s.cache.Alloc
s.basic.TotalAlloc = s.cache.TotalAlloc
s.basic.Sys = s.cache.Sys
s.basic.Lookups = s.cache.Lookups
s.basic.Mallocs = s.cache.Mallocs
s.basic.Frees = s.cache.Frees
s.basic.HeapAlloc = s.cache.HeapAlloc
s.basic.HeapSys = s.cache.HeapSys
s.basic.HeapIdle = s.cache.HeapIdle
s.basic.HeapInuse = s.cache.HeapInuse
s.basic.HeapReleased = s.cache.HeapReleased
s.basic.HeapObjects = s.cache.HeapObjects
s.basic.PauseTotalNs = s.cache.PauseTotalNs
2015-08-20 21:06:57 +02:00
s.basic.LatestPauseNs = s.cache.PauseNs[(s.cache.NumGC+255)%256]
2014-07-23 23:08:06 +02:00
}
}