|
1 | 1 | package cli
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | +"bufio" |
| 5 | +"bytes" |
4 | 6 | "fmt"
|
| 7 | +"os" |
| 8 | +"strconv" |
| 9 | +"strings" |
5 | 10 | "time"
|
6 | 11 |
|
7 | 12 | "github.com/shirou/gopsutil/cpu"
|
| 13 | +"golang.org/x/xerrors" |
8 | 14 |
|
9 | 15 | "github.com/coder/coder/cli/clibase"
|
| 16 | +"github.com/coder/coder/cli/cliui" |
10 | 17 | )
|
11 | 18 |
|
12 | 19 | typestatCmdstruct {
|
| 20 | +*RootCmd |
13 | 21 | watch time.Duration
|
14 | 22 | }
|
15 | 23 |
|
16 |
| -func (*RootCmd)stat()*clibase.Cmd { |
| 24 | +func (r*RootCmd)stat()*clibase.Cmd { |
17 | 25 | c:=&clibase.Cmd{
|
18 |
| -Use:"stat <type> [flags...]", |
19 |
| -Short:"Display system resource usage statistics", |
20 |
| -Long:"stat can be used as the script for agent metadata blocks.", |
21 |
| -Hidden:true, |
| 26 | +Use:"stat <type> [flags...]", |
| 27 | +Short:"Display local system resource usage statistics", |
| 28 | +Long:"stat calls can be used as the script for agent metadata blocks.", |
22 | 29 | }
|
23 |
| -varstatCmdstatCmd |
| 30 | +sc:=statCmd{RootCmd:r} |
24 | 31 | c.Options.Add(
|
25 | 32 | clibase.Option{
|
26 | 33 | Flag:"watch",
|
27 | 34 | FlagShorthand:"w",
|
28 | 35 | Description:"Continuously display the statistic on the given interval.",
|
29 |
| -Value:clibase.DurationOf(&statCmd.watch), |
| 36 | +Value:clibase.DurationOf(&sc.watch), |
30 | 37 | },
|
31 | 38 | )
|
32 | 39 | c.AddSubcommands(
|
33 |
| -statCmd.cpu(), |
| 40 | +sc.cpu(), |
34 | 41 | )
|
| 42 | +sc.setWatchLoops(c) |
35 | 43 | returnc
|
36 | 44 | }
|
37 | 45 |
|
38 |
| -func (sc*statCmd)watchLoop(fn clibase.HandlerFunc) clibase.HandlerFunc { |
39 |
| -returnfunc(inv*clibase.Invocation)error { |
40 |
| -ifsc.watch==0 { |
41 |
| -returnfn(inv) |
42 |
| -} |
| 46 | +func (sc*statCmd)setWatchLoops(c*clibase.Cmd) { |
| 47 | +for_,cmd:=rangec.Children { |
| 48 | +innerHandler:=cmd.Handler |
| 49 | +cmd.Handler=func(inv*clibase.Invocation)error { |
| 50 | +ifsc.watch==0 { |
| 51 | +returninnerHandler(inv) |
| 52 | +} |
43 | 53 |
|
44 |
| -ticker:=time.NewTicker(sc.watch) |
45 |
| -deferticker.Stop() |
| 54 | +ticker:=time.NewTicker(sc.watch) |
| 55 | +deferticker.Stop() |
46 | 56 |
|
47 |
| -forrangeticker.C { |
48 |
| -iferr:=fn(inv);err!=nil { |
49 |
| -_,_=fmt.Fprintf(inv.Stderr,"error: %v",err) |
| 57 | +forrangeticker.C { |
| 58 | +iferr:=innerHandler(inv);err!=nil { |
| 59 | +_,_=fmt.Fprintf(inv.Stderr,"error: %v",err) |
| 60 | +} |
50 | 61 | }
|
| 62 | +panic("unreachable") |
| 63 | +} |
| 64 | +} |
| 65 | +} |
| 66 | + |
| 67 | +funccpuUsageFromCgroup(interval time.Duration) (float64,error) { |
| 68 | +cgroup,err:=os.OpenFile("/proc/self/cgroup",os.O_RDONLY,0) |
| 69 | +iferr!=nil { |
| 70 | +return0,err |
| 71 | +} |
| 72 | +defercgroup.Close() |
| 73 | +sc:=bufio.NewScanner(cgroup) |
| 74 | + |
| 75 | +vargroupDirstring |
| 76 | +forsc.Scan() { |
| 77 | +fields:=strings.Split(sc.Text(),":") |
| 78 | +iflen(fields)!=3 { |
| 79 | +continue |
51 | 80 | }
|
52 |
| -panic("unreachable") |
| 81 | +iffields[1]!="cpu,cpuacct" { |
| 82 | +continue |
| 83 | +} |
| 84 | +groupDir=fields[2] |
| 85 | +break |
| 86 | +} |
| 87 | + |
| 88 | +ifgroupDir=="" { |
| 89 | +return0,xerrors.New("no cpu cgroup found") |
| 90 | +} |
| 91 | + |
| 92 | +cpuAcct:=func() (int64,error) { |
| 93 | +path:=fmt.Sprintf("/sys/fs/cgroup/cpu,cpuacct/%s/cpuacct.usage",groupDir) |
| 94 | + |
| 95 | +byt,err:=os.ReadFile( |
| 96 | +path, |
| 97 | +) |
| 98 | +iferr!=nil { |
| 99 | +return0,err |
| 100 | +} |
| 101 | + |
| 102 | +returnstrconv.ParseInt(string(bytes.TrimSpace(byt)),10,64) |
53 | 103 | }
|
| 104 | + |
| 105 | +stat1,err:=cpuAcct() |
| 106 | +iferr!=nil { |
| 107 | +return0,err |
| 108 | +} |
| 109 | + |
| 110 | +time.Sleep(interval) |
| 111 | + |
| 112 | +stat2,err:=cpuAcct() |
| 113 | +iferr!=nil { |
| 114 | +return0,err |
| 115 | +} |
| 116 | + |
| 117 | +var ( |
| 118 | +cpuTime=time.Duration(stat2-stat1) |
| 119 | +realTime=interval |
| 120 | +) |
| 121 | + |
| 122 | +ncpu,err:=cpu.Counts(true) |
| 123 | +iferr!=nil { |
| 124 | +return0,err |
| 125 | +} |
| 126 | + |
| 127 | +return (cpuTime.Seconds()/realTime.Seconds())*100/float64(ncpu),nil |
54 | 128 | }
|
55 | 129 |
|
56 | 130 | //nolint:revive
|
57 | 131 | func (sc*statCmd)cpu()*clibase.Cmd {
|
58 | 132 | varinterval time.Duration
|
59 | 133 | c:=&clibase.Cmd{
|
60 |
| -Use:"cpu", |
61 |
| -Short:"Display the system's cpu usage", |
62 |
| -Long:"Display the system's load average.", |
63 |
| -Handler:sc.watchLoop(func(inv*clibase.Invocation)error { |
64 |
| -r,err:=cpu.Percent(0,false) |
| 134 | +Use:"cpu-usage", |
| 135 | +Aliases: []string{"cu"}, |
| 136 | +Short:"Display the system's cpu usage", |
| 137 | +Long:"If inside a cgroup (e.g. docker container), the cpu usage is ", |
| 138 | +Handler:func(inv*clibase.Invocation)error { |
| 139 | +ifsc.watch!=0 { |
| 140 | +interval=sc.watch |
| 141 | +} |
| 142 | + |
| 143 | +r,err:=cpuUsageFromCgroup(interval) |
65 | 144 | iferr!=nil {
|
66 |
| -returnerr |
| 145 | +cliui.Infof(sc.verboseStderr(inv),"cgroup error: %+v",err) |
| 146 | + |
| 147 | +// Use standard methods if cgroup method fails. |
| 148 | +rs,err:=cpu.Percent(interval,false) |
| 149 | +iferr!=nil { |
| 150 | +returnerr |
| 151 | +} |
| 152 | +r=rs[0] |
67 | 153 | }
|
68 |
| -_,_=fmt.Fprintf(inv.Stdout,"%02.0f\n",r[0]) |
| 154 | + |
| 155 | +_,_=fmt.Fprintf(inv.Stdout,"%02.0f\n",r) |
| 156 | + |
69 | 157 | returnnil
|
70 |
| -}), |
| 158 | +}, |
71 | 159 | Options: []clibase.Option{
|
72 | 160 | {
|
73 | 161 | Flag:"interval",
|
74 | 162 | FlagShorthand:"i",
|
75 |
| -Description:"The sample collection interval.", |
76 |
| -Default:"1s", |
| 163 | +Description:`The sample collection interval. If --watch is set, it overrides this value.`, |
| 164 | +Default:"0s", |
77 | 165 | Value:clibase.DurationOf(&interval),
|
78 | 166 | },
|
79 | 167 | },
|
|