github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/cmd/smc/monitorcmd.go (about) 1 // Copyright 2015 The Spectrum Authors 2 // This file is part of Spectrum. 3 // 4 // Spectrum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Spectrum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Spectrum. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 /* add by liangc : 220128 20 import ( 21 "fmt" 22 "math" 23 "reflect" 24 "runtime" 25 "sort" 26 "strings" 27 "time" 28 29 "github.com/SmartMeshFoundation/Spectrum/cmd/utils" 30 "github.com/SmartMeshFoundation/Spectrum/node" 31 "github.com/SmartMeshFoundation/Spectrum/rpc" 32 "github.com/gizak/termui" 33 "gopkg.in/urfave/cli.v1" 34 ) 35 36 var ( 37 monitorCommandAttachFlag = cli.StringFlag{ 38 Name: "attach", 39 Value: node.DefaultIPCEndpoint(clientIdentifier), 40 Usage: "API endpoint to attach to", 41 } 42 monitorCommandRowsFlag = cli.IntFlag{ 43 Name: "rows", 44 Value: 5, 45 Usage: "Maximum rows in the chart grid", 46 } 47 monitorCommandRefreshFlag = cli.IntFlag{ 48 Name: "refresh", 49 Value: 3, 50 Usage: "Refresh interval in seconds", 51 } 52 monitorCommand = cli.Command{ 53 Action: utils.MigrateFlags(monitor), // keep track of migration progress 54 Name: "monitor", 55 Usage: "Monitor and visualize node metrics", 56 ArgsUsage: " ", 57 Category: "MONITOR COMMANDS", 58 Description: ` 59 The Geth monitor is a tool to collect and visualize various internal metrics 60 gathered by the node, supporting different chart types as well as the capacity 61 to display multiple metrics simultaneously. 62 `, 63 Flags: []cli.Flag{ 64 monitorCommandAttachFlag, 65 monitorCommandRowsFlag, 66 monitorCommandRefreshFlag, 67 }, 68 } 69 ) 70 71 // monitor starts a terminal UI based monitoring tool for the requested metrics. 72 func monitor(ctx *cli.Context) error { 73 var ( 74 client *rpc.Client 75 err error 76 ) 77 // Attach to an Ethereum node over IPC or RPC 78 endpoint := ctx.String(monitorCommandAttachFlag.Name) 79 if client, err = dialRPC(endpoint); err != nil { 80 utils.Fatalf("Unable to attach to geth node: %v", err) 81 } 82 defer client.Close() 83 84 // Retrieve all the available metrics and resolve the user pattens 85 metrics, err := retrieveMetrics(client) 86 if err != nil { 87 utils.Fatalf("Failed to retrieve system metrics: %v", err) 88 } 89 monitored := resolveMetrics(metrics, ctx.Args()) 90 if len(monitored) == 0 { 91 list := expandMetrics(metrics, "") 92 sort.Strings(list) 93 94 if len(list) > 0 { 95 utils.Fatalf("No metrics specified.\n\nAvailable:\n - %s", strings.Join(list, "\n - ")) 96 } else { 97 utils.Fatalf("No metrics collected by geth (--%s).\n", utils.MetricsEnabledFlag.Name) 98 } 99 } 100 sort.Strings(monitored) 101 if cols := len(monitored) / ctx.Int(monitorCommandRowsFlag.Name); cols > 6 { 102 utils.Fatalf("Requested metrics (%d) spans more that 6 columns:\n - %s", len(monitored), strings.Join(monitored, "\n - ")) 103 } 104 // Create and configure the chart UI defaults 105 if err := termui.Init(); err != nil { 106 utils.Fatalf("Unable to initialize terminal UI: %v", err) 107 } 108 defer termui.Close() 109 110 rows := len(monitored) 111 if max := ctx.Int(monitorCommandRowsFlag.Name); rows > max { 112 rows = max 113 } 114 cols := (len(monitored) + rows - 1) / rows 115 for i := 0; i < rows; i++ { 116 termui.Body.AddRows(termui.NewRow()) 117 } 118 // Create each individual data chart 119 footer := termui.NewPar("") 120 footer.Block.Border = true 121 footer.Height = 3 122 123 charts := make([]*termui.LineChart, len(monitored)) 124 units := make([]int, len(monitored)) 125 data := make([][]float64, len(monitored)) 126 for i := 0; i < len(monitored); i++ { 127 charts[i] = createChart((termui.TermHeight() - footer.Height) / rows) 128 row := termui.Body.Rows[i%rows] 129 row.Cols = append(row.Cols, termui.NewCol(12/cols, 0, charts[i])) 130 } 131 termui.Body.AddRows(termui.NewRow(termui.NewCol(12, 0, footer))) 132 133 refreshCharts(client, monitored, data, units, charts, ctx, footer) 134 termui.Body.Align() 135 termui.Render(termui.Body) 136 137 // Watch for various system events, and periodically refresh the charts 138 termui.Handle("/sys/kbd/C-c", func(termui.Event) { 139 termui.StopLoop() 140 }) 141 termui.Handle("/sys/wnd/resize", func(termui.Event) { 142 termui.Body.Width = termui.TermWidth() 143 for _, chart := range charts { 144 chart.Height = (termui.TermHeight() - footer.Height) / rows 145 } 146 termui.Body.Align() 147 termui.Render(termui.Body) 148 }) 149 go func() { 150 tick := time.NewTicker(time.Duration(ctx.Int(monitorCommandRefreshFlag.Name)) * time.Second) 151 for range tick.C { 152 if refreshCharts(client, monitored, data, units, charts, ctx, footer) { 153 termui.Body.Align() 154 } 155 termui.Render(termui.Body) 156 } 157 }() 158 termui.Loop() 159 return nil 160 } 161 162 // retrieveMetrics contacts the attached geth node and retrieves the entire set 163 // of collected system metrics. 164 func retrieveMetrics(client *rpc.Client) (map[string]interface{}, error) { 165 var metrics map[string]interface{} 166 err := client.Call(&metrics, "debug_metrics", true) 167 return metrics, err 168 } 169 170 // resolveMetrics takes a list of input metric patterns, and resolves each to one 171 // or more canonical metric names. 172 func resolveMetrics(metrics map[string]interface{}, patterns []string) []string { 173 res := []string{} 174 for _, pattern := range patterns { 175 res = append(res, resolveMetric(metrics, pattern, "")...) 176 } 177 return res 178 } 179 180 // resolveMetrics takes a single of input metric pattern, and resolves it to one 181 // or more canonical metric names. 182 func resolveMetric(metrics map[string]interface{}, pattern string, path string) []string { 183 results := []string{} 184 185 // If a nested metric was requested, recurse optionally branching (via comma) 186 parts := strings.SplitN(pattern, "/", 2) 187 if len(parts) > 1 { 188 for _, variation := range strings.Split(parts[0], ",") { 189 if submetrics, ok := metrics[variation].(map[string]interface{}); !ok { 190 utils.Fatalf("Failed to retrieve system metrics: %s", path+variation) 191 return nil 192 } else { 193 results = append(results, resolveMetric(submetrics, parts[1], path+variation+"/")...) 194 } 195 } 196 return results 197 } 198 // Depending what the last link is, return or expand 199 for _, variation := range strings.Split(pattern, ",") { 200 switch metric := metrics[variation].(type) { 201 case float64: 202 // Final metric value found, return as singleton 203 results = append(results, path+variation) 204 205 case map[string]interface{}: 206 results = append(results, expandMetrics(metric, path+variation+"/")...) 207 208 default: 209 utils.Fatalf("Metric pattern resolved to unexpected type: %v", reflect.TypeOf(metric)) 210 return nil 211 } 212 } 213 return results 214 } 215 216 // expandMetrics expands the entire tree of metrics into a flat list of paths. 217 func expandMetrics(metrics map[string]interface{}, path string) []string { 218 // Iterate over all fields and expand individually 219 list := []string{} 220 for name, metric := range metrics { 221 switch metric := metric.(type) { 222 case float64: 223 // Final metric value found, append to list 224 list = append(list, path+name) 225 226 case map[string]interface{}: 227 // Tree of metrics found, expand recursively 228 list = append(list, expandMetrics(metric, path+name+"/")...) 229 230 default: 231 utils.Fatalf("Metric pattern %s resolved to unexpected type: %v", path+name, reflect.TypeOf(metric)) 232 return nil 233 } 234 } 235 return list 236 } 237 238 // fetchMetric iterates over the metrics map and retrieves a specific one. 239 func fetchMetric(metrics map[string]interface{}, metric string) float64 { 240 parts := strings.Split(metric, "/") 241 for _, part := range parts[:len(parts)-1] { 242 var found bool 243 metrics, found = metrics[part].(map[string]interface{}) 244 if !found { 245 return 0 246 } 247 } 248 if v, ok := metrics[parts[len(parts)-1]].(float64); ok { 249 return v 250 } 251 return 0 252 } 253 254 // refreshCharts retrieves a next batch of metrics, and inserts all the new 255 // values into the active datasets and charts 256 func refreshCharts(client *rpc.Client, metrics []string, data [][]float64, units []int, charts []*termui.LineChart, ctx *cli.Context, footer *termui.Par) (realign bool) { 257 values, err := retrieveMetrics(client) 258 for i, metric := range metrics { 259 if len(data) < 512 { 260 data[i] = append([]float64{fetchMetric(values, metric)}, data[i]...) 261 } else { 262 data[i] = append([]float64{fetchMetric(values, metric)}, data[i][:len(data[i])-1]...) 263 } 264 if updateChart(metric, data[i], &units[i], charts[i], err) { 265 realign = true 266 } 267 } 268 updateFooter(ctx, err, footer) 269 return 270 } 271 272 // updateChart inserts a dataset into a line chart, scaling appropriately as to 273 // not display weird labels, also updating the chart label accordingly. 274 func updateChart(metric string, data []float64, base *int, chart *termui.LineChart, err error) (realign bool) { 275 dataUnits := []string{"", "K", "M", "G", "T", "E"} 276 timeUnits := []string{"ns", "µs", "ms", "s", "ks", "ms"} 277 colors := []termui.Attribute{termui.ColorBlue, termui.ColorCyan, termui.ColorGreen, termui.ColorYellow, termui.ColorRed, termui.ColorRed} 278 279 // Extract only part of the data that's actually visible 280 if chart.Width*2 < len(data) { 281 data = data[:chart.Width*2] 282 } 283 // Find the maximum value and scale under 1K 284 high := 0.0 285 if len(data) > 0 { 286 high = data[0] 287 for _, value := range data[1:] { 288 high = math.Max(high, value) 289 } 290 } 291 unit, scale := 0, 1.0 292 for high >= 1000 && unit+1 < len(dataUnits) { 293 high, unit, scale = high/1000, unit+1, scale*1000 294 } 295 // If the unit changes, re-create the chart (hack to set max height...) 296 if unit != *base { 297 realign, *base, *chart = true, unit, *createChart(chart.Height) 298 } 299 // Update the chart's data points with the scaled values 300 if cap(chart.Data) < len(data) { 301 chart.Data = make([]float64, len(data)) 302 } 303 chart.Data = chart.Data[:len(data)] 304 for i, value := range data { 305 chart.Data[i] = value / scale 306 } 307 // Update the chart's label with the scale units 308 units := dataUnits 309 if strings.Contains(metric, "/Percentiles/") || strings.Contains(metric, "/pauses/") || strings.Contains(metric, "/time/") { 310 units = timeUnits 311 } 312 chart.BorderLabel = metric 313 if len(units[unit]) > 0 { 314 chart.BorderLabel += " [" + units[unit] + "]" 315 } 316 chart.LineColor = colors[unit] | termui.AttrBold 317 if err != nil { 318 chart.LineColor = termui.ColorRed | termui.AttrBold 319 } 320 return 321 } 322 323 // createChart creates an empty line chart with the default configs. 324 func createChart(height int) *termui.LineChart { 325 chart := termui.NewLineChart() 326 if runtime.GOOS == "windows" { 327 chart.Mode = "dot" 328 } 329 chart.DataLabels = []string{""} 330 chart.Height = height 331 chart.AxesColor = termui.ColorWhite 332 chart.PaddingBottom = -2 333 334 chart.BorderLabelFg = chart.BorderFg | termui.AttrBold 335 chart.BorderFg = chart.BorderBg 336 337 return chart 338 } 339 340 // updateFooter updates the footer contents based on any encountered errors. 341 func updateFooter(ctx *cli.Context, err error, footer *termui.Par) { 342 // Generate the basic footer 343 refresh := time.Duration(ctx.Int(monitorCommandRefreshFlag.Name)) * time.Second 344 footer.Text = fmt.Sprintf("Press Ctrl+C to quit. Refresh interval: %v.", refresh) 345 footer.TextFgColor = termui.ThemeAttr("par.fg") | termui.AttrBold 346 347 // Append any encountered errors 348 if err != nil { 349 footer.Text = fmt.Sprintf("Error: %v.", err) 350 footer.TextFgColor = termui.ColorRed | termui.AttrBold 351 } 352 } 353 */