From 88d68a99a23aa9bf14cbfc2267d2aac2787790fc Mon Sep 17 00:00:00 2001 From: Justin Li Date: Wed, 23 Jul 2014 17:25:01 -0400 Subject: [PATCH] Clean up stats configuration --- chihaya.go | 4 +--- config/config.go | 25 +++++++++++++++++-------- stats/mem.go | 4 ++++ stats/stats.go | 41 ++++++++++++++--------------------------- 4 files changed, 36 insertions(+), 38 deletions(-) diff --git a/chihaya.go b/chihaya.go index 84535a6..2d5dd2a 100644 --- a/chihaya.go +++ b/chihaya.go @@ -71,9 +71,7 @@ func Boot() { glog.V(1).Infof("Loaded config file: %s", configPath) } - stats.DefaultBufferSize = cfg.StatsBufferSize - stats.DefaultIncludeMemStats = cfg.IncludeMemStats - stats.DefaultVerboseMemStats = cfg.VerboseMemStats + stats.DefaultStats = stats.New(cfg.StatsConfig) tkr, err := tracker.New(cfg) if err != nil { diff --git a/config/config.go b/config/config.go index 5bc78c7..536d165 100644 --- a/config/config.go +++ b/config/config.go @@ -51,6 +51,14 @@ type NetConfig struct { 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. type Config struct { Addr string `json:"addr"` @@ -66,11 +74,8 @@ type Config struct { MinAnnounce Duration `json:"min_announce"` RequestTimeout Duration `json:"request_timeout"` 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 } @@ -95,10 +100,14 @@ var DefaultConfig = Config{ MinAnnounce: Duration{15 * time.Minute}, RequestTimeout: Duration{10 * time.Second}, NumWantFallback: 50, - StatsBufferSize: 0, - IncludeMemStats: true, - VerboseMemStats: false, - MemStatInterval: Duration{15 * time.Second}, + + StatsConfig: StatsConfig{ + BufferSize: 0, + IncludeMem: true, + VerboseMem: false, + + MemUpdateInterval: Duration{5 * time.Second}, + }, NetConfig: NetConfig{ AllowIPSpoofing: true, diff --git a/stats/mem.go b/stats/mem.go index c507cb9..413a78c 100644 --- a/stats/mem.go +++ b/stats/mem.go @@ -32,6 +32,9 @@ type BasicMemStats struct { 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 { basic *BasicMemStats 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() { runtime.ReadMemStats(s.full) diff --git a/stats/stats.go b/stats/stats.go index ff9a5bc..48941c6 100644 --- a/stats/stats.go +++ b/stats/stats.go @@ -6,7 +6,11 @@ // BitTorrent tracker. package stats -import "time" +import ( + "time" + + "github.com/chihaya/chihaya/config" +) const ( Announce = iota @@ -36,12 +40,7 @@ const ( // DefaultStats is a default instance of stats tracking that uses an unbuffered // channel for broadcasting events unless specified otherwise via a command // line flag. -var ( - DefaultStats *Stats - DefaultBufferSize int - DefaultIncludeMemStats bool - DefaultVerboseMemStats bool -) +var DefaultStats *Stats type PeerStats struct { // Stats for all peers. @@ -94,14 +93,14 @@ type Stats struct { recordMemStats <-chan time.Time } -func New(chanSize int, mem bool, verboseMem bool) *Stats { +func New(cfg config.StatsConfig) *Stats { s := &Stats{ Start: time.Now(), - events: make(chan int, chanSize), + events: make(chan int, cfg.BufferSize), - ipv4PeerEvents: make(chan int, chanSize), - ipv6PeerEvents: make(chan int, chanSize), - responseTimeEvents: make(chan time.Duration, chanSize), + ipv4PeerEvents: make(chan int, cfg.BufferSize), + ipv6PeerEvents: make(chan int, cfg.BufferSize), + responseTimeEvents: make(chan time.Duration, cfg.BufferSize), ResponseTime: PercentileTimes{ P50: NewPercentile(0.5), @@ -110,9 +109,9 @@ func New(chanSize int, mem bool, verboseMem bool) *Stats { }, } - if mem { - s.MemStats = NewMemStatsWrapper(verboseMem) - s.recordMemStats = time.NewTicker(time.Second * 10).C + if cfg.IncludeMem { + s.MemStats = NewMemStatsWrapper(cfg.VerboseMem) + s.recordMemStats = time.NewTicker(cfg.MemUpdateInterval.Duration).C } go s.handleEvents() @@ -250,27 +249,15 @@ func (s *Stats) handlePeerEvent(ps *PeerStats, event int) { // RecordEvent broadcasts an event to the default stats queue. func RecordEvent(event int) { - if DefaultStats == nil { - DefaultStats = New(DefaultBufferSize, DefaultIncludeMemStats, DefaultVerboseMemStats) - } - DefaultStats.RecordEvent(event) } // RecordPeerEvent broadcasts a peer event to the default stats queue. func RecordPeerEvent(event int, ipv6 bool) { - if DefaultStats == nil { - DefaultStats = New(DefaultBufferSize, DefaultIncludeMemStats, DefaultVerboseMemStats) - } - DefaultStats.RecordPeerEvent(event, ipv6) } // RecordTiming broadcasts a timing event to the default stats queue. func RecordTiming(event int, duration time.Duration) { - if DefaultStats == nil { - DefaultStats = New(DefaultBufferSize, DefaultIncludeMemStats, DefaultVerboseMemStats) - } - DefaultStats.RecordTiming(event, duration) }