Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / expvar / expvar.go
blob6068fbb4ded38a314692a94dd4e58b5523d35bd3
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // The expvar package provides a standardized interface to public variables,
6 // such as operation counters in servers. It exposes these variables via
7 // HTTP at /debug/vars in JSON format.
8 //
9 // Operations to set or modify these public variables are atomic.
11 // In addition to adding the HTTP handler, this package registers the
12 // following variables:
14 // cmdline os.Args
15 // memstats runtime.Memstats
17 // The package is sometimes only imported for the side effect of
18 // registering its HTTP handler and the above variables. To use it
19 // this way, simply link this package into your program:
20 // import _ "expvar"
22 package expvar
24 import (
25 "bytes"
26 "fmt"
27 "http"
28 "json"
29 "log"
30 "os"
31 "runtime"
32 "strconv"
33 "sync"
36 // Var is an abstract type for all exported variables.
37 type Var interface {
38 String() string
41 // Int is a 64-bit integer variable, and satisfies the Var interface.
42 type Int struct {
43 i int64
44 mu sync.Mutex
47 func (v *Int) String() string { return strconv.Itoa64(v.i) }
49 func (v *Int) Add(delta int64) {
50 v.mu.Lock()
51 defer v.mu.Unlock()
52 v.i += delta
55 func (v *Int) Set(value int64) {
56 v.mu.Lock()
57 defer v.mu.Unlock()
58 v.i = value
61 // Map is a string-to-Var map variable, and satisfies the Var interface.
62 type Map struct {
63 m map[string]Var
64 mu sync.Mutex
67 // KeyValue represents a single entry in a Map.
68 type KeyValue struct {
69 Key string
70 Value Var
73 func (v *Map) String() string {
74 v.mu.Lock()
75 defer v.mu.Unlock()
76 b := new(bytes.Buffer)
77 fmt.Fprintf(b, "{")
78 first := true
79 for key, val := range v.m {
80 if !first {
81 fmt.Fprintf(b, ", ")
83 fmt.Fprintf(b, "\"%s\": %v", key, val.String())
84 first = false
86 fmt.Fprintf(b, "}")
87 return b.String()
90 func (v *Map) Init() *Map {
91 v.m = make(map[string]Var)
92 return v
95 func (v *Map) Get(key string) Var {
96 v.mu.Lock()
97 defer v.mu.Unlock()
98 return v.m[key]
101 func (v *Map) Set(key string, av Var) {
102 v.mu.Lock()
103 defer v.mu.Unlock()
104 v.m[key] = av
107 func (v *Map) Add(key string, delta int64) {
108 v.mu.Lock()
109 defer v.mu.Unlock()
110 av, ok := v.m[key]
111 if !ok {
112 av = new(Int)
113 v.m[key] = av
116 // Add to Int; ignore otherwise.
117 if iv, ok := av.(*Int); ok {
118 iv.Add(delta)
122 // TODO(rsc): Make sure map access in separate thread is safe.
123 func (v *Map) iterate(c chan<- KeyValue) {
124 for k, v := range v.m {
125 c <- KeyValue{k, v}
127 close(c)
130 func (v *Map) Iter() <-chan KeyValue {
131 c := make(chan KeyValue)
132 go v.iterate(c)
133 return c
136 // String is a string variable, and satisfies the Var interface.
137 type String struct {
138 s string
141 func (v *String) String() string { return strconv.Quote(v.s) }
143 func (v *String) Set(value string) { v.s = value }
145 // IntFunc wraps a func() int64 to create a value that satisfies the Var interface.
146 // The function will be called each time the Var is evaluated.
147 type IntFunc func() int64
149 func (v IntFunc) String() string { return strconv.Itoa64(v()) }
151 // StringFunc wraps a func() string to create value that satisfies the Var interface.
152 // The function will be called each time the Var is evaluated.
153 type StringFunc func() string
155 func (f StringFunc) String() string { return f() }
158 // All published variables.
159 var vars map[string]Var = make(map[string]Var)
160 var mutex sync.Mutex
162 // Publish declares an named exported variable. This should be called from a
163 // package's init function when it creates its Vars. If the name is already
164 // registered then this will log.Panic.
165 func Publish(name string, v Var) {
166 mutex.Lock()
167 defer mutex.Unlock()
168 if _, existing := vars[name]; existing {
169 log.Panicln("Reuse of exported var name:", name)
171 vars[name] = v
174 // Get retrieves a named exported variable.
175 func Get(name string) Var {
176 return vars[name]
179 // RemoveAll removes all exported variables.
180 // This is for tests; don't call this on a real server.
181 func RemoveAll() {
182 mutex.Lock()
183 defer mutex.Unlock()
184 vars = make(map[string]Var)
187 // Convenience functions for creating new exported variables.
189 func NewInt(name string) *Int {
190 v := new(Int)
191 Publish(name, v)
192 return v
195 func NewMap(name string) *Map {
196 v := new(Map).Init()
197 Publish(name, v)
198 return v
201 func NewString(name string) *String {
202 v := new(String)
203 Publish(name, v)
204 return v
207 // TODO(rsc): Make sure map access in separate thread is safe.
208 func iterate(c chan<- KeyValue) {
209 for k, v := range vars {
210 c <- KeyValue{k, v}
212 close(c)
215 func Iter() <-chan KeyValue {
216 c := make(chan KeyValue)
217 go iterate(c)
218 return c
221 func expvarHandler(w http.ResponseWriter, r *http.Request) {
222 w.SetHeader("content-type", "application/json; charset=utf-8")
223 fmt.Fprintf(w, "{\n")
224 first := true
225 for name, value := range vars {
226 if !first {
227 fmt.Fprintf(w, ",\n")
229 first = false
230 fmt.Fprintf(w, "%q: %s", name, value)
232 fmt.Fprintf(w, "\n}\n")
235 func memstats() string {
236 b, _ := json.MarshalIndent(&runtime.MemStats, "", "\t")
237 return string(b)
240 func cmdline() string {
241 b, _ := json.Marshal(os.Args)
242 return string(b)
245 func init() {
246 http.Handle("/debug/vars", http.HandlerFunc(expvarHandler))
247 Publish("cmdline", StringFunc(cmdline))
248 Publish("memstats", StringFunc(memstats))