Clean up stats configuration

This commit is contained in:
Justin Li 2014-07-23 17:25:01 -04:00
parent cf619aa50f
commit 88d68a99a2
4 changed files with 36 additions and 38 deletions

View file

@ -71,9 +71,7 @@ func Boot() {
glog.V(1).Infof("Loaded config file: %s", configPath) glog.V(1).Infof("Loaded config file: %s", configPath)
} }
stats.DefaultBufferSize = cfg.StatsBufferSize stats.DefaultStats = stats.New(cfg.StatsConfig)
stats.DefaultIncludeMemStats = cfg.IncludeMemStats
stats.DefaultVerboseMemStats = cfg.VerboseMemStats
tkr, err := tracker.New(cfg) tkr, err := tracker.New(cfg)
if err != nil { if err != nil {

View file

@ -51,6 +51,14 @@ type NetConfig struct {
PreferredIPv6Subnet int `json:"preferred_ipv6_subnet,omitempty"` PreferredIPv6Subnet int `json:"preferred_ipv6_subnet,omitempty"`
} }
type StatsConfig struct {
BufferSize int `json:"stats_buffer_size"`
IncludeMem bool `json:"include_mem_stats"`
VerboseMem bool `json:"verbose_mem_stats"`
MemUpdateInterval Duration `json:"mem_stats_interval"`
}
// Config is a configuration for a Server. // Config is a configuration for a Server.
type Config struct { type Config struct {
Addr string `json:"addr"` Addr string `json:"addr"`
@ -66,11 +74,8 @@ type Config struct {
MinAnnounce Duration `json:"min_announce"` MinAnnounce Duration `json:"min_announce"`
RequestTimeout Duration `json:"request_timeout"` RequestTimeout Duration `json:"request_timeout"`
NumWantFallback int `json:"default_num_want"` NumWantFallback int `json:"default_num_want"`
StatsBufferSize int `json:"stats_buffer_size"`
IncludeMemStats bool `json:"include_mem_stats"`
VerboseMemStats bool `json:"verbose_mem_stats"`
MemStatInterval Duration `json:"mem_stats_interval"`
StatsConfig
NetConfig NetConfig
} }
@ -95,10 +100,14 @@ var DefaultConfig = Config{
MinAnnounce: Duration{15 * time.Minute}, MinAnnounce: Duration{15 * time.Minute},
RequestTimeout: Duration{10 * time.Second}, RequestTimeout: Duration{10 * time.Second},
NumWantFallback: 50, NumWantFallback: 50,
StatsBufferSize: 0,
IncludeMemStats: true, StatsConfig: StatsConfig{
VerboseMemStats: false, BufferSize: 0,
MemStatInterval: Duration{15 * time.Second}, IncludeMem: true,
VerboseMem: false,
MemUpdateInterval: Duration{5 * time.Second},
},
NetConfig: NetConfig{ NetConfig: NetConfig{
AllowIPSpoofing: true, AllowIPSpoofing: true,

View file

@ -32,6 +32,9 @@ type BasicMemStats struct {
PauseTotalNs uint64 PauseTotalNs uint64
} }
// 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.
type MemStatsWrapper struct { type MemStatsWrapper struct {
basic *BasicMemStats basic *BasicMemStats
full *runtime.MemStats full *runtime.MemStats
@ -57,6 +60,7 @@ func (s *MemStatsWrapper) MarshalJSON() ([]byte, error) {
} }
} }
// Update fetches the current memstats from runtime and resets the cache.
func (s *MemStatsWrapper) Update() { func (s *MemStatsWrapper) Update() {
runtime.ReadMemStats(s.full) runtime.ReadMemStats(s.full)

View file

@ -6,7 +6,11 @@
// BitTorrent tracker. // BitTorrent tracker.
package stats package stats
import "time" import (
"time"
"github.com/chihaya/chihaya/config"
)
const ( const (
Announce = iota Announce = iota
@ -36,12 +40,7 @@ const (
// DefaultStats is a default instance of stats tracking that uses an unbuffered // DefaultStats is a default instance of stats tracking that uses an unbuffered
// channel for broadcasting events unless specified otherwise via a command // channel for broadcasting events unless specified otherwise via a command
// line flag. // line flag.
var ( var DefaultStats *Stats
DefaultStats *Stats
DefaultBufferSize int
DefaultIncludeMemStats bool
DefaultVerboseMemStats bool
)
type PeerStats struct { type PeerStats struct {
// Stats for all peers. // Stats for all peers.
@ -94,14 +93,14 @@ type Stats struct {
recordMemStats <-chan time.Time recordMemStats <-chan time.Time
} }
func New(chanSize int, mem bool, verboseMem bool) *Stats { func New(cfg config.StatsConfig) *Stats {
s := &Stats{ s := &Stats{
Start: time.Now(), Start: time.Now(),
events: make(chan int, chanSize), events: make(chan int, cfg.BufferSize),
ipv4PeerEvents: make(chan int, chanSize), ipv4PeerEvents: make(chan int, cfg.BufferSize),
ipv6PeerEvents: make(chan int, chanSize), ipv6PeerEvents: make(chan int, cfg.BufferSize),
responseTimeEvents: make(chan time.Duration, chanSize), responseTimeEvents: make(chan time.Duration, cfg.BufferSize),
ResponseTime: PercentileTimes{ ResponseTime: PercentileTimes{
P50: NewPercentile(0.5), P50: NewPercentile(0.5),
@ -110,9 +109,9 @@ func New(chanSize int, mem bool, verboseMem bool) *Stats {
}, },
} }
if mem { if cfg.IncludeMem {
s.MemStats = NewMemStatsWrapper(verboseMem) s.MemStats = NewMemStatsWrapper(cfg.VerboseMem)
s.recordMemStats = time.NewTicker(time.Second * 10).C s.recordMemStats = time.NewTicker(cfg.MemUpdateInterval.Duration).C
} }
go s.handleEvents() go s.handleEvents()
@ -250,27 +249,15 @@ func (s *Stats) handlePeerEvent(ps *PeerStats, event int) {
// RecordEvent broadcasts an event to the default stats queue. // RecordEvent broadcasts an event to the default stats queue.
func RecordEvent(event int) { func RecordEvent(event int) {
if DefaultStats == nil {
DefaultStats = New(DefaultBufferSize, DefaultIncludeMemStats, DefaultVerboseMemStats)
}
DefaultStats.RecordEvent(event) DefaultStats.RecordEvent(event)
} }
// RecordPeerEvent broadcasts a peer event to the default stats queue. // RecordPeerEvent broadcasts a peer event to the default stats queue.
func RecordPeerEvent(event int, ipv6 bool) { func RecordPeerEvent(event int, ipv6 bool) {
if DefaultStats == nil {
DefaultStats = New(DefaultBufferSize, DefaultIncludeMemStats, DefaultVerboseMemStats)
}
DefaultStats.RecordPeerEvent(event, ipv6) DefaultStats.RecordPeerEvent(event, ipv6)
} }
// RecordTiming broadcasts a timing event to the default stats queue. // RecordTiming broadcasts a timing event to the default stats queue.
func RecordTiming(event int, duration time.Duration) { func RecordTiming(event int, duration time.Duration) {
if DefaultStats == nil {
DefaultStats = New(DefaultBufferSize, DefaultIncludeMemStats, DefaultVerboseMemStats)
}
DefaultStats.RecordTiming(event, duration) DefaultStats.RecordTiming(event, duration)
} }