lotus/cmd/lotus-storage-miner/workers.go

110 lines
3.0 KiB
Go
Raw Normal View History

2020-03-23 12:29:24 +00:00
package main
2020-03-23 14:56:22 +00:00
import (
"fmt"
"sort"
2020-05-01 12:06:19 +00:00
"strings"
"github.com/fatih/color"
"gopkg.in/urfave/cli.v2"
2020-03-23 14:56:22 +00:00
2020-05-01 12:06:19 +00:00
"github.com/filecoin-project/sector-storage/storiface"
"github.com/filecoin-project/lotus/chain/types"
2020-03-23 14:56:22 +00:00
lcli "github.com/filecoin-project/lotus/cli"
)
var workersCmd = &cli.Command{
Name: "workers",
Usage: "interact with workers",
Subcommands: []*cli.Command{
workersListCmd,
},
}
var workersListCmd = &cli.Command{
Name: "list",
Usage: "list workers",
2020-05-01 12:06:19 +00:00
Flags: []cli.Flag{
&cli.BoolFlag{Name: "color"},
},
2020-03-23 14:56:22 +00:00
Action: func(cctx *cli.Context) error {
2020-05-01 12:06:19 +00:00
color.NoColor = !cctx.Bool("color")
2020-03-23 14:56:22 +00:00
nodeApi, closer, err := lcli.GetStorageMinerAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
stats, err := nodeApi.WorkerStats(ctx)
if err != nil {
return err
}
2020-03-24 23:49:45 +00:00
type sortableStat struct {
id uint64
2020-04-23 22:23:20 +00:00
storiface.WorkerStats
2020-03-24 23:49:45 +00:00
}
st := make([]sortableStat, 0, len(stats))
2020-03-23 14:56:22 +00:00
for id, stat := range stats {
2020-03-24 23:49:45 +00:00
st = append(st, sortableStat{id, stat})
2020-03-23 14:56:22 +00:00
}
sort.Slice(st, func(i, j int) bool {
return st[i].id < st[j].id
})
for _, stat := range st {
gpuUse := "not "
2020-05-01 12:06:19 +00:00
gpuCol := color.FgBlue
2020-03-23 14:56:22 +00:00
if stat.GpuUsed {
2020-05-01 12:06:19 +00:00
gpuCol = color.FgGreen
2020-03-23 14:56:22 +00:00
gpuUse = ""
}
2020-05-01 12:06:19 +00:00
fmt.Printf("Worker %d, host %s\n", stat.id, color.MagentaString(stat.Info.Hostname))
var barCols = uint64(64)
cpuBars := int(stat.CpuUse * barCols / stat.Info.Resources.CPUs)
cpuBar := strings.Repeat("|", cpuBars) + strings.Repeat(" ", int(barCols) - cpuBars)
fmt.Printf("\tCPU: [%s] %d core(s) in use\n", color.GreenString(cpuBar), stat.CpuUse)
ramBarsRes := int(stat.Info.Resources.MemReserved*barCols/stat.Info.Resources.MemPhysical)
ramBarsUsed := int(stat.MemUsedMin*barCols/stat.Info.Resources.MemPhysical)
ramBar := color.YellowString(strings.Repeat("|", ramBarsRes)) +
color.GreenString(strings.Repeat("|", ramBarsUsed)) +
strings.Repeat(" ", int(barCols) - ramBarsUsed - ramBarsRes)
vmem := stat.Info.Resources.MemPhysical+stat.Info.Resources.MemSwap
vmemBarsRes := int(stat.Info.Resources.MemReserved*barCols/vmem)
vmemBarsUsed := int(stat.MemUsedMax*barCols/vmem)
vmemBar := color.YellowString(strings.Repeat("|", vmemBarsRes)) +
color.GreenString(strings.Repeat("|", vmemBarsUsed)) +
strings.Repeat(" ", int(barCols) - vmemBarsUsed - vmemBarsRes)
fmt.Printf("\tRAM: [%s] %d%% %s/%s\n", ramBar,
(stat.Info.Resources.MemReserved + stat.MemUsedMin)*100/stat.Info.Resources.MemPhysical,
types.SizeStr(types.NewInt(stat.Info.Resources.MemReserved + stat.MemUsedMin)),
types.SizeStr(types.NewInt(stat.Info.Resources.MemPhysical)))
fmt.Printf("\tVMEM: [%s] %d%% %s/%s\n", vmemBar,
(stat.Info.Resources.MemReserved + stat.MemUsedMax)*100/vmem,
types.SizeStr(types.NewInt(stat.Info.Resources.MemReserved + stat.MemUsedMax)),
types.SizeStr(types.NewInt(vmem)))
2020-03-23 14:56:22 +00:00
for _, gpu := range stat.Info.Resources.GPUs {
2020-05-01 12:06:19 +00:00
fmt.Printf("\tGPU: %s\n", color.New(gpuCol).Sprintf("%s, %sused", gpu, gpuUse))
2020-03-23 14:56:22 +00:00
}
}
return nil
},
}