-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstat.go
82 lines (71 loc) · 1.59 KB
/
stat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"io"
"os"
"strings"
"sync/atomic"
"time"
"github.com/as/log"
)
func init() {
log.Service = os.Getenv("SVC")
tags := strings.Split(os.Getenv("TAGS"), ",")
for i := 0; i+1 < len(tags); i += 2 {
key, val := tags[i], tags[i+1]
if key == "" || val == "" {
continue
}
log.Tags = log.Tags.Add(key, val)
}
for _, key := range strings.Split(os.Getenv("LOGENV"), ",") {
if val := os.Getenv(key); val != "" && key != "" {
log.Tags = log.Tags.Add(key, val)
}
}
log.Tags = log.Tags.Add("ver", Version)
}
var iostat = struct {
rx, tx int64
}{}
type rx struct{ io.Reader }
func (r rx) Read(p []byte) (n int, err error) {
n, err = r.Reader.Read(p)
if n == 0 && err == nil {
quantum() // avoid spinning in a read loop
}
atomic.AddInt64(&iostat.rx, int64(n))
return n, err
}
type tx struct{ io.Writer }
func (w tx) Write(p []byte) (n int, err error) {
n, err = w.Writer.Write(p)
atomic.AddInt64(&iostat.tx, int64(n))
return n, err
}
type rxlim struct {
lim int
rx
}
func (r *rxlim) Read(p []byte) (n int, err error) {
rx := int(atomic.LoadInt64(&iostat.rx))
dur := int(time.Since(procstart) / (100 * time.Millisecond))
bpms := 0
if dur > 0 {
bpms = rx / dur
}
if bpms*10 > r.lim {
s := time.Duration((float64(bpms*10)/float64(r.lim) - 1) * float64(time.Second))
if s >= 100*time.Millisecond {
quantum()
time.Sleep(s)
}
}
return r.rx.Read(p)
}
// quantum releases the thread and prevents spinning in a loop
// it sleeps for double the actual quantum on linux, which is 2*100ms
func quantum() {
if !*spin {
time.Sleep(200 * time.Millisecond)
}
}