diff options
Diffstat (limited to 'src/expvar')
-rw-r--r-- | src/expvar/expvar.go | 367 | ||||
-rw-r--r-- | src/expvar/expvar_test.go | 601 |
2 files changed, 968 insertions, 0 deletions
diff --git a/src/expvar/expvar.go b/src/expvar/expvar.go new file mode 100644 index 0000000..13b5c99 --- /dev/null +++ b/src/expvar/expvar.go @@ -0,0 +1,367 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package expvar provides a standardized interface to public variables, such +// as operation counters in servers. It exposes these variables via HTTP at +// /debug/vars in JSON format. +// +// Operations to set or modify these public variables are atomic. +// +// In addition to adding the HTTP handler, this package registers the +// following variables: +// +// cmdline os.Args +// memstats runtime.Memstats +// +// The package is sometimes only imported for the side effect of +// registering its HTTP handler and the above variables. To use it +// this way, link this package into your program: +// import _ "expvar" +// +package expvar + +import ( + "encoding/json" + "fmt" + "log" + "math" + "net/http" + "os" + "runtime" + "sort" + "strconv" + "strings" + "sync" + "sync/atomic" +) + +// Var is an abstract type for all exported variables. +type Var interface { + // String returns a valid JSON value for the variable. + // Types with String methods that do not return valid JSON + // (such as time.Time) must not be used as a Var. + String() string +} + +// Int is a 64-bit integer variable that satisfies the Var interface. +type Int struct { + i int64 +} + +func (v *Int) Value() int64 { + return atomic.LoadInt64(&v.i) +} + +func (v *Int) String() string { + return strconv.FormatInt(atomic.LoadInt64(&v.i), 10) +} + +func (v *Int) Add(delta int64) { + atomic.AddInt64(&v.i, delta) +} + +func (v *Int) Set(value int64) { + atomic.StoreInt64(&v.i, value) +} + +// Float is a 64-bit float variable that satisfies the Var interface. +type Float struct { + f uint64 +} + +func (v *Float) Value() float64 { + return math.Float64frombits(atomic.LoadUint64(&v.f)) +} + +func (v *Float) String() string { + return strconv.FormatFloat( + math.Float64frombits(atomic.LoadUint64(&v.f)), 'g', -1, 64) +} + +// Add adds delta to v. +func (v *Float) Add(delta float64) { + for { + cur := atomic.LoadUint64(&v.f) + curVal := math.Float64frombits(cur) + nxtVal := curVal + delta + nxt := math.Float64bits(nxtVal) + if atomic.CompareAndSwapUint64(&v.f, cur, nxt) { + return + } + } +} + +// Set sets v to value. +func (v *Float) Set(value float64) { + atomic.StoreUint64(&v.f, math.Float64bits(value)) +} + +// Map is a string-to-Var map variable that satisfies the Var interface. +type Map struct { + m sync.Map // map[string]Var + keysMu sync.RWMutex + keys []string // sorted +} + +// KeyValue represents a single entry in a Map. +type KeyValue struct { + Key string + Value Var +} + +func (v *Map) String() string { + var b strings.Builder + fmt.Fprintf(&b, "{") + first := true + v.Do(func(kv KeyValue) { + if !first { + fmt.Fprintf(&b, ", ") + } + fmt.Fprintf(&b, "%q: %v", kv.Key, kv.Value) + first = false + }) + fmt.Fprintf(&b, "}") + return b.String() +} + +// Init removes all keys from the map. +func (v *Map) Init() *Map { + v.keysMu.Lock() + defer v.keysMu.Unlock() + v.keys = v.keys[:0] + v.m.Range(func(k, _ interface{}) bool { + v.m.Delete(k) + return true + }) + return v +} + +// addKey updates the sorted list of keys in v.keys. +func (v *Map) addKey(key string) { + v.keysMu.Lock() + defer v.keysMu.Unlock() + // Using insertion sort to place key into the already-sorted v.keys. + if i := sort.SearchStrings(v.keys, key); i >= len(v.keys) { + v.keys = append(v.keys, key) + } else if v.keys[i] != key { + v.keys = append(v.keys, "") + copy(v.keys[i+1:], v.keys[i:]) + v.keys[i] = key + } +} + +func (v *Map) Get(key string) Var { + i, _ := v.m.Load(key) + av, _ := i.(Var) + return av +} + +func (v *Map) Set(key string, av Var) { + // Before we store the value, check to see whether the key is new. Try a Load + // before LoadOrStore: LoadOrStore causes the key interface to escape even on + // the Load path. + if _, ok := v.m.Load(key); !ok { + if _, dup := v.m.LoadOrStore(key, av); !dup { + v.addKey(key) + return + } + } + + v.m.Store(key, av) +} + +// Add adds delta to the *Int value stored under the given map key. +func (v *Map) Add(key string, delta int64) { + i, ok := v.m.Load(key) + if !ok { + var dup bool + i, dup = v.m.LoadOrStore(key, new(Int)) + if !dup { + v.addKey(key) + } + } + + // Add to Int; ignore otherwise. + if iv, ok := i.(*Int); ok { + iv.Add(delta) + } +} + +// AddFloat adds delta to the *Float value stored under the given map key. +func (v *Map) AddFloat(key string, delta float64) { + i, ok := v.m.Load(key) + if !ok { + var dup bool + i, dup = v.m.LoadOrStore(key, new(Float)) + if !dup { + v.addKey(key) + } + } + + // Add to Float; ignore otherwise. + if iv, ok := i.(*Float); ok { + iv.Add(delta) + } +} + +// Delete deletes the given key from the map. +func (v *Map) Delete(key string) { + v.keysMu.Lock() + defer v.keysMu.Unlock() + i := sort.SearchStrings(v.keys, key) + if i < len(v.keys) && key == v.keys[i] { + v.keys = append(v.keys[:i], v.keys[i+1:]...) + v.m.Delete(key) + } +} + +// Do calls f for each entry in the map. +// The map is locked during the iteration, +// but existing entries may be concurrently updated. +func (v *Map) Do(f func(KeyValue)) { + v.keysMu.RLock() + defer v.keysMu.RUnlock() + for _, k := range v.keys { + i, _ := v.m.Load(k) + f(KeyValue{k, i.(Var)}) + } +} + +// String is a string variable, and satisfies the Var interface. +type String struct { + s atomic.Value // string +} + +func (v *String) Value() string { + p, _ := v.s.Load().(string) + return p +} + +// String implements the Var interface. To get the unquoted string +// use Value. +func (v *String) String() string { + s := v.Value() + b, _ := json.Marshal(s) + return string(b) +} + +func (v *String) Set(value string) { + v.s.Store(value) +} + +// Func implements Var by calling the function +// and formatting the returned value using JSON. +type Func func() interface{} + +func (f Func) Value() interface{} { + return f() +} + +func (f Func) String() string { + v, _ := json.Marshal(f()) + return string(v) +} + +// All published variables. +var ( + vars sync.Map // map[string]Var + varKeysMu sync.RWMutex + varKeys []string // sorted +) + +// Publish declares a named exported variable. This should be called from a +// package's init function when it creates its Vars. If the name is already +// registered then this will log.Panic. +func Publish(name string, v Var) { + if _, dup := vars.LoadOrStore(name, v); dup { + log.Panicln("Reuse of exported var name:", name) + } + varKeysMu.Lock() + defer varKeysMu.Unlock() + varKeys = append(varKeys, name) + sort.Strings(varKeys) +} + +// Get retrieves a named exported variable. It returns nil if the name has +// not been registered. +func Get(name string) Var { + i, _ := vars.Load(name) + v, _ := i.(Var) + return v +} + +// Convenience functions for creating new exported variables. + +func NewInt(name string) *Int { + v := new(Int) + Publish(name, v) + return v +} + +func NewFloat(name string) *Float { + v := new(Float) + Publish(name, v) + return v +} + +func NewMap(name string) *Map { + v := new(Map).Init() + Publish(name, v) + return v +} + +func NewString(name string) *String { + v := new(String) + Publish(name, v) + return v +} + +// Do calls f for each exported variable. +// The global variable map is locked during the iteration, +// but existing entries may be concurrently updated. +func Do(f func(KeyValue)) { + varKeysMu.RLock() + defer varKeysMu.RUnlock() + for _, k := range varKeys { + val, _ := vars.Load(k) + f(KeyValue{k, val.(Var)}) + } +} + +func expvarHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + fmt.Fprintf(w, "{\n") + first := true + Do(func(kv KeyValue) { + if !first { + fmt.Fprintf(w, ",\n") + } + first = false + fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value) + }) + fmt.Fprintf(w, "\n}\n") +} + +// Handler returns the expvar HTTP Handler. +// +// This is only needed to install the handler in a non-standard location. +func Handler() http.Handler { + return http.HandlerFunc(expvarHandler) +} + +func cmdline() interface{} { + return os.Args +} + +func memstats() interface{} { + stats := new(runtime.MemStats) + runtime.ReadMemStats(stats) + return *stats +} + +func init() { + http.HandleFunc("/debug/vars", expvarHandler) + Publish("cmdline", Func(cmdline)) + Publish("memstats", Func(memstats)) +} diff --git a/src/expvar/expvar_test.go b/src/expvar/expvar_test.go new file mode 100644 index 0000000..69b0a76 --- /dev/null +++ b/src/expvar/expvar_test.go @@ -0,0 +1,601 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package expvar + +import ( + "bytes" + "crypto/sha1" + "encoding/json" + "fmt" + "net" + "net/http/httptest" + "reflect" + "runtime" + "strconv" + "sync" + "sync/atomic" + "testing" +) + +// RemoveAll removes all exported variables. +// This is for tests only. +func RemoveAll() { + varKeysMu.Lock() + defer varKeysMu.Unlock() + for _, k := range varKeys { + vars.Delete(k) + } + varKeys = nil +} + +func TestNil(t *testing.T) { + RemoveAll() + val := Get("missing") + if val != nil { + t.Errorf("got %v, want nil", val) + } +} + +func TestInt(t *testing.T) { + RemoveAll() + reqs := NewInt("requests") + if i := reqs.Value(); i != 0 { + t.Errorf("reqs.Value() = %v, want 0", i) + } + if reqs != Get("requests").(*Int) { + t.Errorf("Get() failed.") + } + + reqs.Add(1) + reqs.Add(3) + if i := reqs.Value(); i != 4 { + t.Errorf("reqs.Value() = %v, want 4", i) + } + + if s := reqs.String(); s != "4" { + t.Errorf("reqs.String() = %q, want \"4\"", s) + } + + reqs.Set(-2) + if i := reqs.Value(); i != -2 { + t.Errorf("reqs.Value() = %v, want -2", i) + } +} + +func BenchmarkIntAdd(b *testing.B) { + var v Int + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + v.Add(1) + } + }) +} + +func BenchmarkIntSet(b *testing.B) { + var v Int + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + v.Set(1) + } + }) +} + +func TestFloat(t *testing.T) { + RemoveAll() + reqs := NewFloat("requests-float") + if reqs.f != 0.0 { + t.Errorf("reqs.f = %v, want 0", reqs.f) + } + if reqs != Get("requests-float").(*Float) { + t.Errorf("Get() failed.") + } + + reqs.Add(1.5) + reqs.Add(1.25) + if v := reqs.Value(); v != 2.75 { + t.Errorf("reqs.Value() = %v, want 2.75", v) + } + + if s := reqs.String(); s != "2.75" { + t.Errorf("reqs.String() = %q, want \"4.64\"", s) + } + + reqs.Add(-2) + if v := reqs.Value(); v != 0.75 { + t.Errorf("reqs.Value() = %v, want 0.75", v) + } +} + +func BenchmarkFloatAdd(b *testing.B) { + var f Float + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + f.Add(1.0) + } + }) +} + +func BenchmarkFloatSet(b *testing.B) { + var f Float + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + f.Set(1.0) + } + }) +} + +func TestString(t *testing.T) { + RemoveAll() + name := NewString("my-name") + if s := name.Value(); s != "" { + t.Errorf(`NewString("my-name").Value() = %q, want ""`, s) + } + + name.Set("Mike") + if s, want := name.String(), `"Mike"`; s != want { + t.Errorf(`after name.Set("Mike"), name.String() = %q, want %q`, s, want) + } + if s, want := name.Value(), "Mike"; s != want { + t.Errorf(`after name.Set("Mike"), name.Value() = %q, want %q`, s, want) + } + + // Make sure we produce safe JSON output. + name.Set("<") + if s, want := name.String(), "\"\\u003c\""; s != want { + t.Errorf(`after name.Set("<"), name.String() = %q, want %q`, s, want) + } +} + +func BenchmarkStringSet(b *testing.B) { + var s String + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + s.Set("red") + } + }) +} + +func TestMapInit(t *testing.T) { + RemoveAll() + colors := NewMap("bike-shed-colors") + colors.Add("red", 1) + colors.Add("blue", 1) + colors.Add("chartreuse", 1) + + n := 0 + colors.Do(func(KeyValue) { n++ }) + if n != 3 { + t.Errorf("after three Add calls with distinct keys, Do should invoke f 3 times; got %v", n) + } + + colors.Init() + + n = 0 + colors.Do(func(KeyValue) { n++ }) + if n != 0 { + t.Errorf("after Init, Do should invoke f 0 times; got %v", n) + } +} + +func TestMapDelete(t *testing.T) { + RemoveAll() + colors := NewMap("bike-shed-colors") + + colors.Add("red", 1) + colors.Add("red", 2) + colors.Add("blue", 4) + + n := 0 + colors.Do(func(KeyValue) { n++ }) + if n != 2 { + t.Errorf("after two Add calls with distinct keys, Do should invoke f 2 times; got %v", n) + } + + colors.Delete("red") + n = 0 + colors.Do(func(KeyValue) { n++ }) + if n != 1 { + t.Errorf("removed red, Do should invoke f 1 times; got %v", n) + } + + colors.Delete("notfound") + n = 0 + colors.Do(func(KeyValue) { n++ }) + if n != 1 { + t.Errorf("attempted to remove notfound, Do should invoke f 1 times; got %v", n) + } + + colors.Delete("blue") + colors.Delete("blue") + n = 0 + colors.Do(func(KeyValue) { n++ }) + if n != 0 { + t.Errorf("all keys removed, Do should invoke f 0 times; got %v", n) + } +} + +func TestMapCounter(t *testing.T) { + RemoveAll() + colors := NewMap("bike-shed-colors") + + colors.Add("red", 1) + colors.Add("red", 2) + colors.Add("blue", 4) + colors.AddFloat(`green "midori"`, 4.125) + if x := colors.Get("red").(*Int).Value(); x != 3 { + t.Errorf("colors.m[\"red\"] = %v, want 3", x) + } + if x := colors.Get("blue").(*Int).Value(); x != 4 { + t.Errorf("colors.m[\"blue\"] = %v, want 4", x) + } + if x := colors.Get(`green "midori"`).(*Float).Value(); x != 4.125 { + t.Errorf("colors.m[`green \"midori\"] = %v, want 4.125", x) + } + + // colors.String() should be '{"red":3, "blue":4}', + // though the order of red and blue could vary. + s := colors.String() + var j interface{} + err := json.Unmarshal([]byte(s), &j) + if err != nil { + t.Errorf("colors.String() isn't valid JSON: %v", err) + } + m, ok := j.(map[string]interface{}) + if !ok { + t.Error("colors.String() didn't produce a map.") + } + red := m["red"] + x, ok := red.(float64) + if !ok { + t.Error("red.Kind() is not a number.") + } + if x != 3 { + t.Errorf("red = %v, want 3", x) + } +} + +func BenchmarkMapSet(b *testing.B) { + m := new(Map).Init() + + v := new(Int) + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + m.Set("red", v) + } + }) +} + +func BenchmarkMapSetDifferent(b *testing.B) { + procKeys := make([][]string, runtime.GOMAXPROCS(0)) + for i := range procKeys { + keys := make([]string, 4) + for j := range keys { + keys[j] = fmt.Sprint(i, j) + } + procKeys[i] = keys + } + + m := new(Map).Init() + v := new(Int) + b.ResetTimer() + + var n int32 + b.RunParallel(func(pb *testing.PB) { + i := int(atomic.AddInt32(&n, 1)-1) % len(procKeys) + keys := procKeys[i] + + for pb.Next() { + for _, k := range keys { + m.Set(k, v) + } + } + }) +} + +// BenchmarkMapSetDifferentRandom simulates such a case where the concerned +// keys of Map.Set are generated dynamically and as a result insertion is +// out of order and the number of the keys may be large. +func BenchmarkMapSetDifferentRandom(b *testing.B) { + keys := make([]string, 100) + for i := range keys { + keys[i] = fmt.Sprintf("%x", sha1.Sum([]byte(fmt.Sprint(i)))) + } + + v := new(Int) + b.ResetTimer() + + for i := 0; i < b.N; i++ { + m := new(Map).Init() + for _, k := range keys { + m.Set(k, v) + } + } +} + +func BenchmarkMapSetString(b *testing.B) { + m := new(Map).Init() + + v := new(String) + v.Set("Hello, !") + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + m.Set("red", v) + } + }) +} + +func BenchmarkMapAddSame(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + m := new(Map).Init() + m.Add("red", 1) + m.Add("red", 1) + m.Add("red", 1) + m.Add("red", 1) + } + }) +} + +func BenchmarkMapAddDifferent(b *testing.B) { + procKeys := make([][]string, runtime.GOMAXPROCS(0)) + for i := range procKeys { + keys := make([]string, 4) + for j := range keys { + keys[j] = fmt.Sprint(i, j) + } + procKeys[i] = keys + } + + b.ResetTimer() + + var n int32 + b.RunParallel(func(pb *testing.PB) { + i := int(atomic.AddInt32(&n, 1)-1) % len(procKeys) + keys := procKeys[i] + + for pb.Next() { + m := new(Map).Init() + for _, k := range keys { + m.Add(k, 1) + } + } + }) +} + +// BenchmarkMapAddDifferentRandom simulates such a case where that the concerned +// keys of Map.Add are generated dynamically and as a result insertion is out of +// order and the number of the keys may be large. +func BenchmarkMapAddDifferentRandom(b *testing.B) { + keys := make([]string, 100) + for i := range keys { + keys[i] = fmt.Sprintf("%x", sha1.Sum([]byte(fmt.Sprint(i)))) + } + + b.ResetTimer() + + for i := 0; i < b.N; i++ { + m := new(Map).Init() + for _, k := range keys { + m.Add(k, 1) + } + } +} + +func BenchmarkMapAddSameSteadyState(b *testing.B) { + m := new(Map).Init() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + m.Add("red", 1) + } + }) +} + +func BenchmarkMapAddDifferentSteadyState(b *testing.B) { + procKeys := make([][]string, runtime.GOMAXPROCS(0)) + for i := range procKeys { + keys := make([]string, 4) + for j := range keys { + keys[j] = fmt.Sprint(i, j) + } + procKeys[i] = keys + } + + m := new(Map).Init() + b.ResetTimer() + + var n int32 + b.RunParallel(func(pb *testing.PB) { + i := int(atomic.AddInt32(&n, 1)-1) % len(procKeys) + keys := procKeys[i] + + for pb.Next() { + for _, k := range keys { + m.Add(k, 1) + } + } + }) +} + +func TestFunc(t *testing.T) { + RemoveAll() + var x interface{} = []string{"a", "b"} + f := Func(func() interface{} { return x }) + if s, exp := f.String(), `["a","b"]`; s != exp { + t.Errorf(`f.String() = %q, want %q`, s, exp) + } + if v := f.Value(); !reflect.DeepEqual(v, x) { + t.Errorf(`f.Value() = %q, want %q`, v, x) + } + + x = 17 + if s, exp := f.String(), `17`; s != exp { + t.Errorf(`f.String() = %q, want %q`, s, exp) + } +} + +func TestHandler(t *testing.T) { + RemoveAll() + m := NewMap("map1") + m.Add("a", 1) + m.Add("z", 2) + m2 := NewMap("map2") + for i := 0; i < 9; i++ { + m2.Add(strconv.Itoa(i), int64(i)) + } + rr := httptest.NewRecorder() + rr.Body = new(bytes.Buffer) + expvarHandler(rr, nil) + want := `{ +"map1": {"a": 1, "z": 2}, +"map2": {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8} +} +` + if got := rr.Body.String(); got != want { + t.Errorf("HTTP handler wrote:\n%s\nWant:\n%s", got, want) + } +} + +func BenchmarkRealworldExpvarUsage(b *testing.B) { + var ( + bytesSent Int + bytesRead Int + ) + + // The benchmark creates GOMAXPROCS client/server pairs. + // Each pair creates 4 goroutines: client reader/writer and server reader/writer. + // The benchmark stresses concurrent reading and writing to the same connection. + // Such pattern is used in net/http and net/rpc. + + b.StopTimer() + + P := runtime.GOMAXPROCS(0) + N := b.N / P + W := 1000 + + // Setup P client/server connections. + clients := make([]net.Conn, P) + servers := make([]net.Conn, P) + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + b.Fatalf("Listen failed: %v", err) + } + defer ln.Close() + done := make(chan bool, 1) + go func() { + for p := 0; p < P; p++ { + s, err := ln.Accept() + if err != nil { + b.Errorf("Accept failed: %v", err) + done <- false + return + } + servers[p] = s + } + done <- true + }() + for p := 0; p < P; p++ { + c, err := net.Dial("tcp", ln.Addr().String()) + if err != nil { + <-done + b.Fatalf("Dial failed: %v", err) + } + clients[p] = c + } + if !<-done { + b.FailNow() + } + + b.StartTimer() + + var wg sync.WaitGroup + wg.Add(4 * P) + for p := 0; p < P; p++ { + // Client writer. + go func(c net.Conn) { + defer wg.Done() + var buf [1]byte + for i := 0; i < N; i++ { + v := byte(i) + for w := 0; w < W; w++ { + v *= v + } + buf[0] = v + n, err := c.Write(buf[:]) + if err != nil { + b.Errorf("Write failed: %v", err) + return + } + + bytesSent.Add(int64(n)) + } + }(clients[p]) + + // Pipe between server reader and server writer. + pipe := make(chan byte, 128) + + // Server reader. + go func(s net.Conn) { + defer wg.Done() + var buf [1]byte + for i := 0; i < N; i++ { + n, err := s.Read(buf[:]) + + if err != nil { + b.Errorf("Read failed: %v", err) + return + } + + bytesRead.Add(int64(n)) + pipe <- buf[0] + } + }(servers[p]) + + // Server writer. + go func(s net.Conn) { + defer wg.Done() + var buf [1]byte + for i := 0; i < N; i++ { + v := <-pipe + for w := 0; w < W; w++ { + v *= v + } + buf[0] = v + n, err := s.Write(buf[:]) + if err != nil { + b.Errorf("Write failed: %v", err) + return + } + + bytesSent.Add(int64(n)) + } + s.Close() + }(servers[p]) + + // Client reader. + go func(c net.Conn) { + defer wg.Done() + var buf [1]byte + for i := 0; i < N; i++ { + n, err := c.Read(buf[:]) + + if err != nil { + b.Errorf("Read failed: %v", err) + return + } + + bytesRead.Add(int64(n)) + } + c.Close() + }(clients[p]) + } + wg.Wait() +} |