feat(lotus-sim): print info on SIGUSR1

This commit is contained in:
Steven Allen 2021-06-09 21:27:48 -07:00
parent dcdb0abe27
commit 936659d087
2 changed files with 64 additions and 40 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"io"
"text/tabwriter"
"time"
@ -13,6 +14,7 @@ import (
"github.com/filecoin-project/lotus/chain/actors/builtin/power"
"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/cmd/lotus-sim/simulation"
)
func getTotalPower(ctx context.Context, sm *stmgr.StateManager, ts *types.TipSet) (power.Claim, error) {
@ -27,32 +29,18 @@ func getTotalPower(ctx context.Context, sm *stmgr.StateManager, ts *types.TipSet
return state.TotalPower()
}
var infoSimCommand = &cli.Command{
Name: "info",
Description: "Output information about the simulation.",
Action: func(cctx *cli.Context) error {
node, err := open(cctx)
func printInfo(ctx context.Context, sim *simulation.Simulation, out io.Writer) error {
powerNow, err := getTotalPower(ctx, sim.StateManager, sim.GetHead())
if err != nil {
return err
}
defer node.Close()
sim, err := node.LoadSim(cctx.Context, cctx.String("simulation"))
if err != nil {
return err
}
powerNow, err := getTotalPower(cctx.Context, sim.StateManager, sim.GetHead())
if err != nil {
return err
}
powerStart, err := getTotalPower(cctx.Context, sim.StateManager, sim.GetStart())
powerStart, err := getTotalPower(ctx, sim.StateManager, sim.GetStart())
if err != nil {
return err
}
powerGrowth := big.Sub(powerNow.RawBytePower, powerStart.RawBytePower)
tw := tabwriter.NewWriter(cctx.App.Writer, 8, 8, 0, ' ', 0)
tw := tabwriter.NewWriter(out, 8, 8, 1, ' ', 0)
head := sim.GetHead()
start := sim.GetStart()
@ -81,5 +69,22 @@ var infoSimCommand = &cli.Command{
fmt.Fprintf(tw, "Power Growth Rate:\t%s/day\n", types.SizeStr(growthRate))
fmt.Fprintf(tw, "Network Version:\t%d\n", sim.GetNetworkVersion())
return tw.Flush()
}
var infoSimCommand = &cli.Command{
Name: "info",
Description: "Output information about the simulation.",
Action: func(cctx *cli.Context) error {
node, err := open(cctx)
if err != nil {
return err
}
defer node.Close()
sim, err := node.LoadSim(cctx.Context, cctx.String("simulation"))
if err != nil {
return err
}
return printInfo(cctx.Context, sim, cctx.App.Writer)
},
}

View File

@ -2,6 +2,8 @@ package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/urfave/cli/v2"
@ -36,12 +38,29 @@ var runSimCommand = &cli.Command{
}
fmt.Fprintln(cctx.App.Writer, "running simulation")
targetEpochs := cctx.Int("epochs")
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGUSR1)
defer signal.Stop(ch)
for i := 0; targetEpochs == 0 || i < targetEpochs; i++ {
ts, err := sim.Step(cctx.Context)
if err != nil {
return err
}
fmt.Fprintf(cctx.App.Writer, "advanced to %d %s\n", ts.Height(), ts.Key())
// Print
select {
case <-ch:
if err := printInfo(cctx.Context, sim, cctx.App.Writer); err != nil {
fmt.Fprintf(cctx.App.ErrWriter, "ERROR: failed to print info: %s\n", err)
}
case <-cctx.Context.Done():
return cctx.Err()
default:
}
}
fmt.Fprintln(cctx.App.Writer, "simulation done")
return err