lotus/cmd/lotus-sim/run.go

73 lines
1.6 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
},
},
2021-06-18 18:17:35 +00:00
Action: func(cctx *cli.Context) (err error) {
2021-05-19 00:01:30 +00:00
node, err := open(cctx)
if err != nil {
return err
}
2021-06-18 18:17:35 +00:00
defer func() {
if cerr := node.Close(); err == nil {
err = cerr
}
}()
2021-05-19 00:01:30 +00:00
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
}
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
_, _ = 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:
_, _ = 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:27:48 +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")
2021-05-19 00:01:30 +00:00
return err
},
}