lotus/cmd/lotus-sim/run.go

75 lines
1.7 KiB
Go
Raw Normal View History

2021-05-19 00:01:30 +00:00
package main
import (
"fmt"
2021-06-10 04:27:48 +00:00
"os"
"os/signal"
2021-06-10 04:27:33 +00:00
"syscall"
2021-05-19 00:01:30 +00:00
"github.com/urfave/cli/v2"
)
var runSimCommand = &cli.Command{
2021-06-10 04:39:18 +00:00
Name: "run",
Description: `Run the simulation.
Signals:
- SIGUSR1: Print information about the current simulation (equivalent to 'lotus-sim info').
- SIGUSR2: Write pprof profiles to ./pprof-simulation-$DATE/`,
2021-05-19 00:01:30 +00:00
Flags: []cli.Flag{
&cli.IntFlag{
Name: "epochs",
Usage: "Advance the given number of epochs then stop.",
2021-05-19 00:01:30 +00:00
},
},
Action: func(cctx *cli.Context) error {
node, err := open(cctx)
if err != nil {
return err
}
defer node.Close()
2021-06-10 04:27:33 +00:00
go profileOnSignal(cctx, syscall.SIGUSR2)
2021-05-19 00:01:30 +00:00
sim, err := node.LoadSim(cctx.Context, cctx.String("simulation"))
if err != nil {
return err
}
fmt.Fprintln(cctx.App.Writer, "loading simulation")
err = sim.Load(cctx.Context)
if err != nil {
return err
}
fmt.Fprintln(cctx.App.Writer, "running simulation")
targetEpochs := cctx.Int("epochs")
2021-06-10 04:27:48 +00:00
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGUSR1)
defer signal.Stop(ch)
for i := 0; targetEpochs == 0 || i < targetEpochs; i++ {
2021-05-19 00:01:30 +00:00
ts, err := sim.Step(cctx.Context)
if err != nil {
return err
}
2021-06-10 04:27:48 +00:00
2021-05-19 00:01:30 +00:00
fmt.Fprintf(cctx.App.Writer, "advanced to %d %s\n", ts.Height(), ts.Key())
2021-06-10 04:27:48 +00:00
// Print
select {
case <-ch:
2021-06-10 04:31:32 +00:00
fmt.Fprintln(cctx.App.Writer, "---------------------")
2021-06-10 04:27:48 +00:00
if err := printInfo(cctx.Context, sim, cctx.App.Writer); err != nil {
fmt.Fprintf(cctx.App.ErrWriter, "ERROR: failed to print info: %s\n", err)
}
2021-06-10 04:31:32 +00:00
fmt.Fprintln(cctx.App.Writer, "---------------------")
2021-06-10 04:27:48 +00:00
case <-cctx.Context.Done():
return cctx.Err()
default:
}
2021-05-19 00:01:30 +00:00
}
fmt.Fprintln(cctx.App.Writer, "simulation done")
return err
},
}