-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathstatus.go
More file actions
186 lines (175 loc) · 4.57 KB
/
Copy pathstatus.go
File metadata and controls
186 lines (175 loc) · 4.57 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2018 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package bigmachine
import (
"context"
"fmt"
"io"
"net/http"
"sort"
"strings"
"text/tabwriter"
"text/template"
"time"
"github.com/grailbio/base/data"
"github.com/grailbio/base/diagnostic/dump"
"golang.org/x/sync/errgroup"
)
var startTime = time.Now()
var statusTemplate = template.Must(template.New("status").
Funcs(template.FuncMap{
"roundjoindur": func(ds []time.Duration) string {
strs := make([]string, len(ds))
for i, d := range ds {
d = d - d%time.Millisecond
strs[i] = d.String()
}
return strings.Join(strs, " ")
},
"until": time.Until,
"human": func(v interface{}) string {
switch v := v.(type) {
case int:
return data.Size(v).String()
case int64:
return data.Size(v).String()
case uint64:
return data.Size(v).String()
default:
return fmt.Sprintf("(!%T)%v", v, v)
}
},
"ns": func(v interface{}) string {
switch v := v.(type) {
case int:
return time.Duration(v).String()
case int64:
return time.Duration(v).String()
case uint64:
return time.Duration(v).String()
default:
return fmt.Sprintf("(!%T)%v", v, v)
}
},
}).
Parse(`{{.machine.Addr}}
{{if .machine.Owned}} keepalive:
next: {{.info.NextKeepalive}} (in {{until .info.NextKeepalive}})
reply times: {{roundjoindur .info.KeepaliveReplyTimes}}
{{end}} memory:
total: {{human .info.MemInfo.System.Total}}
used: {{human .info.MemInfo.System.Used}}
(percent): {{printf "%.1f%%" .info.MemInfo.System.UsedPercent}}
available: {{human .info.MemInfo.System.Available}}
runtime: {{human .info.MemInfo.Runtime.Sys}}
(alloc): {{human .info.MemInfo.Runtime.Alloc}}
runtime:
uptime: {{.uptime}}
pausetime: {{ns .info.MemInfo.Runtime.PauseTotalNs}}
(last): {{ns .lastpause}}
disk:
total: {{human .info.DiskInfo.Usage.Total}}
available: {{human .info.DiskInfo.Usage.Free}}
used: {{human .info.DiskInfo.Usage.Used}}
(percent): {{printf "%.1f%%" .info.DiskInfo.Usage.UsedPercent}}
load: {{printf "%.1f %.1f %.1f" .info.LoadInfo.Averages.Load1 .info.LoadInfo.Averages.Load5 .info.LoadInfo.Averages.Load15}}
`))
func makeStatusDumpFunc(b *B) dump.Func {
return func(ctx context.Context, w io.Writer) error {
return writeStatus(ctx, b, w)
}
}
func writeStatus(ctx context.Context, b *B, w io.Writer) error {
machines := b.Machines()
sort.Slice(machines, func(i, j int) bool {
return machines[i].Addr < machines[j].Addr
})
infos := make([]machineInfo, len(machines))
g, ctx := errgroup.WithContext(ctx)
for i, m := range machines {
if state := m.State(); state != Running {
infos[i].err = fmt.Errorf("machine state %s", state)
continue
}
i, m := i, m
g.Go(func() error {
infos[i] = allInfo(ctx, m)
return nil
})
}
if err := g.Wait(); err != nil {
return err
}
var tw tabwriter.Writer
tw.Init(w, 4, 4, 1, ' ', 0)
defer tw.Flush()
for i, info := range infos {
m := machines[i]
if info.err != nil {
fmt.Fprintln(&tw, m.Addr, ":", info.err)
continue
}
err := statusTemplate.Execute(&tw, map[string]interface{}{
"machine": m,
"info": info,
"uptime": time.Since(startTime),
"lastpause": info.MemInfo.Runtime.PauseNs[(info.MemInfo.Runtime.NumGC+255)%256],
})
if err != nil {
panic(err)
}
}
return nil
}
// StatusHandler implements an HTTP handler that displays machine
// statuses.
type statusHandler struct{ *B }
func (s *statusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
if err := writeStatus(r.Context(), s.B, w); err != nil {
http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
}
}
type machineInfo struct {
err error
MemInfo
DiskInfo
LoadInfo
KeepaliveReplyTimes []time.Duration
NextKeepalive time.Time
}
func allInfo(ctx context.Context, m *Machine) machineInfo {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
g, ctx := errgroup.WithContext(ctx)
var (
mem MemInfo
disk DiskInfo
load LoadInfo
)
g.Go(func() error {
var err error
mem, err = m.MemInfo(ctx, true)
return err
})
g.Go(func() error {
var err error
disk, err = m.DiskInfo(ctx)
return err
})
g.Go(func() error {
var err error
load, err = m.LoadInfo(ctx)
return err
})
err := g.Wait()
return machineInfo{
err: err,
MemInfo: mem,
DiskInfo: disk,
LoadInfo: load,
KeepaliveReplyTimes: m.KeepaliveReplyTimes(),
NextKeepalive: m.NextKeepalive(),
}
}