2014-07-21 09:58:56 +02:00
|
|
|
// Copyright 2014 The Chihaya Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package stats implements a means of tracking processing statistics for a
|
|
|
|
// BitTorrent tracker.
|
|
|
|
package stats
|
|
|
|
|
2014-07-23 22:14:50 +02:00
|
|
|
import "time"
|
2014-07-21 09:58:56 +02:00
|
|
|
|
|
|
|
const (
|
|
|
|
Announce = iota
|
|
|
|
Scrape
|
2014-07-21 17:42:05 +02:00
|
|
|
|
2014-07-23 05:41:20 +02:00
|
|
|
Completed
|
|
|
|
NewLeech
|
|
|
|
DeletedLeech
|
|
|
|
ReapedLeech
|
|
|
|
NewSeed
|
|
|
|
DeletedSeed
|
|
|
|
ReapedSeed
|
2014-07-21 17:42:05 +02:00
|
|
|
|
2014-07-21 09:58:56 +02:00
|
|
|
NewTorrent
|
|
|
|
DeletedTorrent
|
2014-07-21 17:42:05 +02:00
|
|
|
ReapedTorrent
|
|
|
|
|
|
|
|
AcceptedConnection
|
|
|
|
ClosedConnection
|
|
|
|
|
|
|
|
HandledRequest
|
|
|
|
ErroredRequest
|
2014-07-22 20:57:36 +02:00
|
|
|
|
|
|
|
ResponseTime
|
2014-07-21 09:58:56 +02:00
|
|
|
)
|
|
|
|
|
2014-07-22 01:08:08 +02:00
|
|
|
// DefaultStats is a default instance of stats tracking that uses an unbuffered
|
2014-07-23 09:59:23 +02:00
|
|
|
// channel for broadcasting events unless specified otherwise via a command
|
|
|
|
// line flag.
|
|
|
|
var (
|
2014-07-23 22:14:50 +02:00
|
|
|
DefaultStats *Stats
|
|
|
|
DefaultBufferSize int
|
2014-07-23 09:59:23 +02:00
|
|
|
)
|
2014-07-22 01:08:08 +02:00
|
|
|
|
2014-07-21 17:42:05 +02:00
|
|
|
type PeerStats struct {
|
|
|
|
// Stats for all peers.
|
2014-07-23 08:25:35 +02:00
|
|
|
Current uint64 `json:"current"` // Current total peer count.
|
2014-07-23 05:29:30 +02:00
|
|
|
Joined uint64 `json:"joined"` // Total peers that announced.
|
|
|
|
Left uint64 `json:"left"` // Total peers that paused or stopped.
|
|
|
|
Reaped uint64 `json:"reaped"` // Total peers cleaned up after inactivity.
|
2014-07-23 08:25:35 +02:00
|
|
|
Completed uint64 `json:"completed"` // Number of transitions from leech to seed.
|
2014-07-21 17:42:05 +02:00
|
|
|
|
|
|
|
// Stats for seeds only (subset of total).
|
2014-07-23 08:25:35 +02:00
|
|
|
SeedsCurrent uint64 `json:"seeds_current"` // Current seed count.
|
2014-07-23 05:29:30 +02:00
|
|
|
SeedsJoined uint64 `json:"seeds_joined"` // Seeds that announced (does not included leechers that completed).
|
|
|
|
SeedsLeft uint64 `json:"seeds_left"` // Seeds that paused or stopped.
|
|
|
|
SeedsReaped uint64 `json:"seeds_reaped"` // Seeds cleaned up after inactivity.
|
2014-07-21 17:42:05 +02:00
|
|
|
}
|
|
|
|
|
2014-07-22 20:57:36 +02:00
|
|
|
type PercentileTimes struct {
|
|
|
|
P50 *Percentile
|
|
|
|
P90 *Percentile
|
|
|
|
P95 *Percentile
|
|
|
|
}
|
|
|
|
|
2014-07-21 09:58:56 +02:00
|
|
|
type Stats struct {
|
2014-07-23 05:29:30 +02:00
|
|
|
Start time.Time `json:"start_time"` // Time at which Chihaya was booted.
|
2014-07-21 17:42:05 +02:00
|
|
|
|
2014-07-23 05:29:30 +02:00
|
|
|
Announces uint64 `json:"announces"` // Total number of announces.
|
|
|
|
Scrapes uint64 `json:"scrapes"` // Total number of scrapes.
|
2014-07-21 17:42:05 +02:00
|
|
|
|
2014-07-22 07:12:41 +02:00
|
|
|
IPv4Peers PeerStats `json:"ipv4_peers"`
|
|
|
|
IPv6Peers PeerStats `json:"ipv6_peers"`
|
2014-07-21 17:42:05 +02:00
|
|
|
|
2014-07-22 07:12:41 +02:00
|
|
|
TorrentsAdded uint64 `json:"torrents_added"`
|
|
|
|
TorrentsRemoved uint64 `json:"torrents_removed"`
|
|
|
|
TorrentsReaped uint64 `json:"torrents_reaped"`
|
2014-07-21 17:42:05 +02:00
|
|
|
|
2014-07-22 07:38:07 +02:00
|
|
|
OpenConnections uint64 `json:"open_connections"`
|
2014-07-22 07:12:41 +02:00
|
|
|
ConnectionsAccepted uint64 `json:"connections_accepted"`
|
|
|
|
BytesTransmitted uint64 `json:"bytes_transmitted"`
|
2014-07-21 17:42:05 +02:00
|
|
|
|
2014-07-22 07:12:41 +02:00
|
|
|
RequestsHandled uint64 `json:"requests_handled"`
|
|
|
|
RequestsErrored uint64 `json:"requests_errored"`
|
2014-07-21 09:58:56 +02:00
|
|
|
|
2014-07-22 20:57:36 +02:00
|
|
|
ResponseTime PercentileTimes `json:"response_time"`
|
|
|
|
|
|
|
|
events chan int
|
2014-07-23 05:41:20 +02:00
|
|
|
ipv4PeerEvents chan int
|
|
|
|
ipv6PeerEvents chan int
|
2014-07-22 20:57:36 +02:00
|
|
|
responseTimeEvents chan time.Duration
|
2014-07-21 09:58:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(chanSize int) *Stats {
|
|
|
|
s := &Stats{
|
|
|
|
Start: time.Now(),
|
|
|
|
events: make(chan int, chanSize),
|
2014-07-22 20:57:36 +02:00
|
|
|
|
2014-07-23 05:41:20 +02:00
|
|
|
ipv4PeerEvents: make(chan int, chanSize),
|
|
|
|
ipv6PeerEvents: make(chan int, chanSize),
|
2014-07-22 20:57:36 +02:00
|
|
|
responseTimeEvents: make(chan time.Duration, chanSize),
|
2014-07-23 05:41:20 +02:00
|
|
|
|
2014-07-22 20:57:36 +02:00
|
|
|
ResponseTime: PercentileTimes{
|
2014-07-23 05:13:59 +02:00
|
|
|
P50: NewPercentile(0.5),
|
|
|
|
P90: NewPercentile(0.9),
|
|
|
|
P95: NewPercentile(0.95),
|
2014-07-22 20:57:36 +02:00
|
|
|
},
|
2014-07-21 09:58:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
go s.handleEvents()
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stats) Close() {
|
|
|
|
close(s.events)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stats) Uptime() time.Duration {
|
|
|
|
return time.Since(s.Start)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stats) RecordEvent(event int) {
|
|
|
|
s.events <- event
|
|
|
|
}
|
|
|
|
|
2014-07-23 05:41:20 +02:00
|
|
|
func (s *Stats) RecordPeerEvent(event int, ipv6 bool) {
|
|
|
|
if ipv6 {
|
|
|
|
s.ipv6PeerEvents <- event
|
|
|
|
} else {
|
|
|
|
s.ipv4PeerEvents <- event
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-22 18:42:56 +02:00
|
|
|
func (s *Stats) RecordTiming(event int, duration time.Duration) {
|
2014-07-22 20:57:36 +02:00
|
|
|
switch event {
|
|
|
|
case ResponseTime:
|
|
|
|
s.responseTimeEvents <- duration
|
|
|
|
default:
|
|
|
|
panic("stats: RecordTiming called with an unknown event")
|
|
|
|
}
|
2014-07-22 18:42:56 +02:00
|
|
|
}
|
|
|
|
|
2014-07-21 09:58:56 +02:00
|
|
|
func (s *Stats) handleEvents() {
|
2014-07-23 05:49:00 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-s.events:
|
|
|
|
s.handleEvent(event)
|
2014-07-23 05:41:20 +02:00
|
|
|
|
2014-07-23 05:49:00 +02:00
|
|
|
case event := <-s.ipv4PeerEvents:
|
|
|
|
s.handlePeerEvent(&s.IPv4Peers, event)
|
2014-07-21 09:58:56 +02:00
|
|
|
|
2014-07-23 05:49:00 +02:00
|
|
|
case event := <-s.ipv6PeerEvents:
|
|
|
|
s.handlePeerEvent(&s.IPv6Peers, event)
|
2014-07-23 05:41:20 +02:00
|
|
|
|
2014-07-23 05:49:00 +02:00
|
|
|
case duration := <-s.responseTimeEvents:
|
|
|
|
f := float64(duration) / float64(time.Millisecond)
|
|
|
|
s.ResponseTime.P50.AddSample(f)
|
|
|
|
s.ResponseTime.P90.AddSample(f)
|
|
|
|
s.ResponseTime.P95.AddSample(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-23 05:41:20 +02:00
|
|
|
|
2014-07-23 05:49:00 +02:00
|
|
|
func (s *Stats) handleEvent(event int) {
|
|
|
|
switch event {
|
|
|
|
case Announce:
|
|
|
|
s.Announces++
|
2014-07-21 09:58:56 +02:00
|
|
|
|
2014-07-23 05:49:00 +02:00
|
|
|
case Scrape:
|
|
|
|
s.Scrapes++
|
2014-07-21 09:58:56 +02:00
|
|
|
|
2014-07-23 05:49:00 +02:00
|
|
|
case NewTorrent:
|
|
|
|
s.TorrentsAdded++
|
2014-07-21 09:58:56 +02:00
|
|
|
|
2014-07-23 05:49:00 +02:00
|
|
|
case DeletedTorrent:
|
|
|
|
s.TorrentsRemoved++
|
2014-07-21 09:58:56 +02:00
|
|
|
|
2014-07-23 05:49:00 +02:00
|
|
|
case ReapedTorrent:
|
|
|
|
s.TorrentsReaped++
|
2014-07-21 09:58:56 +02:00
|
|
|
|
2014-07-23 05:49:00 +02:00
|
|
|
case AcceptedConnection:
|
|
|
|
s.ConnectionsAccepted++
|
|
|
|
s.OpenConnections++
|
2014-07-22 01:08:08 +02:00
|
|
|
|
2014-07-23 05:49:00 +02:00
|
|
|
case ClosedConnection:
|
|
|
|
s.OpenConnections--
|
2014-07-23 05:41:20 +02:00
|
|
|
|
2014-07-23 05:49:00 +02:00
|
|
|
case HandledRequest:
|
|
|
|
s.RequestsHandled++
|
|
|
|
|
|
|
|
case ErroredRequest:
|
|
|
|
s.RequestsErrored++
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic("stats: RecordEvent called with an unknown event")
|
2014-07-23 05:41:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stats) handlePeerEvent(ps *PeerStats, event int) {
|
|
|
|
switch event {
|
|
|
|
case Completed:
|
|
|
|
ps.Completed++
|
|
|
|
ps.SeedsCurrent++
|
|
|
|
|
|
|
|
case NewLeech:
|
|
|
|
ps.Joined++
|
|
|
|
ps.Current++
|
|
|
|
|
|
|
|
case DeletedLeech:
|
|
|
|
ps.Left++
|
|
|
|
ps.Current--
|
|
|
|
|
|
|
|
case ReapedLeech:
|
|
|
|
ps.Reaped++
|
|
|
|
ps.Current--
|
|
|
|
|
|
|
|
case NewSeed:
|
|
|
|
ps.SeedsJoined++
|
|
|
|
ps.SeedsCurrent++
|
|
|
|
ps.Joined++
|
|
|
|
ps.Current++
|
|
|
|
|
|
|
|
case DeletedSeed:
|
|
|
|
ps.SeedsLeft++
|
|
|
|
ps.SeedsCurrent--
|
|
|
|
ps.Left++
|
|
|
|
ps.Current--
|
|
|
|
|
|
|
|
case ReapedSeed:
|
|
|
|
ps.SeedsReaped++
|
|
|
|
ps.SeedsCurrent--
|
|
|
|
ps.Reaped++
|
|
|
|
ps.Current--
|
|
|
|
|
2014-07-23 05:49:00 +02:00
|
|
|
default:
|
|
|
|
panic("stats: RecordPeerEvent called with an unknown event")
|
2014-07-22 20:57:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-22 18:42:56 +02:00
|
|
|
// RecordEvent broadcasts an event to the default stats queue.
|
2014-07-22 01:08:08 +02:00
|
|
|
func RecordEvent(event int) {
|
2014-07-23 09:59:23 +02:00
|
|
|
if DefaultStats == nil {
|
2014-07-23 22:14:50 +02:00
|
|
|
DefaultStats = New(DefaultBufferSize)
|
2014-07-23 09:59:23 +02:00
|
|
|
}
|
|
|
|
|
2014-07-22 01:08:08 +02:00
|
|
|
DefaultStats.RecordEvent(event)
|
|
|
|
}
|
2014-07-22 18:42:56 +02:00
|
|
|
|
2014-07-23 05:41:20 +02:00
|
|
|
// RecordPeerEvent broadcasts a peer event to the default stats queue.
|
|
|
|
func RecordPeerEvent(event int, ipv6 bool) {
|
2014-07-23 09:59:23 +02:00
|
|
|
if DefaultStats == nil {
|
2014-07-23 22:14:50 +02:00
|
|
|
DefaultStats = New(DefaultBufferSize)
|
2014-07-23 09:59:23 +02:00
|
|
|
}
|
|
|
|
|
2014-07-23 05:41:20 +02:00
|
|
|
DefaultStats.RecordPeerEvent(event, ipv6)
|
|
|
|
}
|
|
|
|
|
2014-07-22 18:42:56 +02:00
|
|
|
// RecordTiming broadcasts a timing event to the default stats queue.
|
|
|
|
func RecordTiming(event int, duration time.Duration) {
|
2014-07-23 09:59:23 +02:00
|
|
|
if DefaultStats == nil {
|
2014-07-23 22:14:50 +02:00
|
|
|
DefaultStats = New(DefaultBufferSize)
|
2014-07-23 09:59:23 +02:00
|
|
|
}
|
|
|
|
|
2014-07-22 18:42:56 +02:00
|
|
|
DefaultStats.RecordTiming(event, duration)
|
|
|
|
}
|