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)
}
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 {

View file

@ -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,

View file

@ -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)

View file

@ -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)
}