|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math" |
| 6 | + "runtime" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/elastic/go-sysinfo" |
| 12 | + |
| 13 | + "github.com/coder/coder/cli/clibase" |
| 14 | + "github.com/coder/coder/cli/cliui" |
| 15 | +) |
| 16 | + |
| 17 | +func (*RootCmd) stat() *clibase.Cmd { |
| 18 | + var ( |
| 19 | + sampleInterval time.Duration |
| 20 | + formatter = cliui.NewOutputFormatter( |
| 21 | + cliui.TextFormat(), |
| 22 | + cliui.JSONFormat(), |
| 23 | + ) |
| 24 | + ) |
| 25 | + |
| 26 | + cmd := &clibase.Cmd{ |
| 27 | + Use: "stat", |
| 28 | + Short: "Show workspace resource usage.", |
| 29 | + Options: clibase.OptionSet{ |
| 30 | + { |
| 31 | + Description: "Configure the sample interval.", |
| 32 | + Flag: "sample-interval", |
| 33 | + Value: clibase.DurationOf(&sampleInterval), |
| 34 | + Default: "100ms", |
| 35 | + }, |
| 36 | + }, |
| 37 | + Handler: func(inv *clibase.Invocation) error { |
| 38 | + stats, err := newStats(sampleInterval) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + out, err := formatter.Format(inv.Context(), stats) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + _, err = fmt.Fprintln(inv.Stdout, out) |
| 47 | + return err |
| 48 | + }, |
| 49 | + } |
| 50 | + formatter.AttachOptions(&cmd.Options) |
| 51 | + return cmd |
| 52 | +} |
| 53 | + |
| 54 | +type stats struct { |
| 55 | + HostCPU stat `json:"cpu_host"` |
| 56 | + HostMemory stat `json:"mem_host"` |
| 57 | + Disk stat `json:"disk"` |
| 58 | + InContainer bool `json:"in_container,omitempty"` |
| 59 | + ContainerCPU stat `json:"cpu_container,omitempty"` |
| 60 | + ContainerMemory stat `json:"mem_container,omitempty"` |
| 61 | +} |
| 62 | + |
| 63 | +func (s *stats) String() string { |
| 64 | + var sb strings.Builder |
| 65 | + sb.WriteString(s.HostCPU.String()) |
| 66 | + sb.WriteString("\n") |
| 67 | + sb.WriteString(s.HostMemory.String()) |
| 68 | + sb.WriteString("\n") |
| 69 | + sb.WriteString(s.Disk.String()) |
| 70 | + sb.WriteString("\n") |
| 71 | + if s.InContainer { |
| 72 | + sb.WriteString(s.ContainerCPU.String()) |
| 73 | + sb.WriteString("\n") |
| 74 | + sb.WriteString(s.ContainerMemory.String()) |
| 75 | + sb.WriteString("\n") |
| 76 | + } |
| 77 | + return sb.String() |
| 78 | +} |
| 79 | + |
| 80 | +func newStats(dur time.Duration) (stats, error) { |
| 81 | + var s stats |
| 82 | + nproc := float64(runtime.NumCPU()) |
| 83 | + // start := time.Now() |
| 84 | + // ticksPerDur := dur / tickInterval |
| 85 | + h1, err := sysinfo.Host() |
| 86 | + if err != nil { |
| 87 | + return s, err |
| 88 | + } |
| 89 | + <-time.After(dur) |
| 90 | + h2, err := sysinfo.Host() |
| 91 | + if err != nil { |
| 92 | + return s, err |
| 93 | + } |
| 94 | + // elapsed := time.Since(start) |
| 95 | + // numTicks := elapsed / tickInterval |
| 96 | + cts1, err := h1.CPUTime() |
| 97 | + if err != nil { |
| 98 | + return s, err |
| 99 | + } |
| 100 | + cts2, err := h2.CPUTime() |
| 101 | + if err != nil { |
| 102 | + return s, err |
| 103 | + } |
| 104 | + // Assuming the total measured should add up to $(nproc) "cores", |
| 105 | + // we determine a scaling factor such that scaleFactor * total = nproc. |
| 106 | + // We then calculate used as the total time spent idle, and multiply |
| 107 | + // that by scaleFactor to give a rough approximation of how busy the |
| 108 | + // CPU(s) were. |
| 109 | + s.HostCPU.Total = nproc |
| 110 | + total := (cts2.Total() - cts1.Total()) |
| 111 | + idle := (cts2.Idle - cts1.Idle) |
| 112 | + used := total - idle |
| 113 | + scaleFactor := nproc / total.Seconds() |
| 114 | + s.HostCPU.Used = used.Seconds() * scaleFactor |
| 115 | + s.HostCPU.Unit = "cores" |
| 116 | + |
| 117 | + return s, nil |
| 118 | +} |
| 119 | + |
| 120 | +type stat struct { |
| 121 | + Used float64 `json:"used"` |
| 122 | + Total float64 `json:"total"` |
| 123 | + Unit string `json:"unit"` |
| 124 | +} |
| 125 | + |
| 126 | +func (s *stat) String() string { |
| 127 | + var sb strings.Builder |
| 128 | + _, _ = sb.WriteString(strconv.FormatFloat(s.Used, 'f', 1, 64)) |
| 129 | + _, _ = sb.WriteString("/") |
| 130 | + _, _ = sb.WriteString(strconv.FormatFloat(s.Total, 'f', 1, 64)) |
| 131 | + _, _ = sb.WriteString(" ") |
| 132 | + if s.Unit != "" { |
| 133 | + _, _ = sb.WriteString(s.Unit) |
| 134 | + _, _ = sb.WriteString(" ") |
| 135 | + } |
| 136 | + _, _ = sb.WriteString("(") |
| 137 | + var pct float64 |
| 138 | + if s.Total == 0 { |
| 139 | + pct = math.NaN() |
| 140 | + } else { |
| 141 | + pct = s.Used / s.Total * 100 |
| 142 | + } |
| 143 | + _, _ = sb.WriteString(strconv.FormatFloat(pct, 'f', 1, 64)) |
| 144 | + _, _ = sb.WriteString("%)") |
| 145 | + return sb.String() |
| 146 | +} |
0 commit comments