Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a command to view block space utilization #4176

Merged
merged 1 commit into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chain/exchange/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (c *client) doRequest(
res, err := c.sendRequestToPeer(ctx, peer, req)
if err != nil {
if !xerrors.Is(err, network.ErrNoConn) {
log.Warnf("could not connect to peer %s: %s",
log.Warnf("could not send request to peer %s: %s",
peer.String(), err)
}
continue
Expand Down
115 changes: 115 additions & 0 deletions cli/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/exec"
"path"
"sort"
"strconv"
"strings"
"time"
Expand All @@ -30,6 +31,7 @@ import (
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/stmgr"
types "github.com/filecoin-project/lotus/chain/types"
)

Expand All @@ -50,6 +52,7 @@ var chainCmd = &cli.Command{
chainExportCmd,
slashConsensusFault,
chainGasPriceCmd,
chainInspectUsage,
},
}

Expand Down Expand Up @@ -375,6 +378,118 @@ var chainSetHeadCmd = &cli.Command{
},
}

var chainInspectUsage = &cli.Command{
Name: "inspect-usage",
Usage: "Inspect block space usage of a given tipset",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "tipset",
Usage: "specify tipset to view block space usage of",
Value: "@head",
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)

ts, err := LoadTipSet(ctx, cctx, api)
if err != nil {
return err
}

pmsgs, err := api.ChainGetParentMessages(ctx, ts.Blocks()[0].Cid())
if err != nil {
return err
}

codeCache := make(map[address.Address]cid.Cid)

lookupActorCode := func(a address.Address) (cid.Cid, error) {
c, ok := codeCache[a]
if ok {
return c, nil
}

act, err := api.StateGetActor(ctx, a, ts.Key())
if err != nil {
return cid.Undef, err
}

codeCache[a] = act.Code
return act.Code, nil
}

bySender := make(map[string]int64)
byDest := make(map[string]int64)
byMethod := make(map[string]int64)

var sum int64
for _, m := range pmsgs {
bySender[m.Message.From.String()] += m.Message.GasLimit
byDest[m.Message.To.String()] += m.Message.GasLimit
sum += m.Message.GasLimit

code, err := lookupActorCode(m.Message.To)
if err != nil {
return err
}

mm := stmgr.MethodsMap[code][m.Message.Method]

byMethod[mm.Name] += m.Message.GasLimit

}

type keyGasPair struct {
Key string
Gas int64
}

mapToSortedKvs := func(m map[string]int64) []keyGasPair {
var vals []keyGasPair
for k, v := range m {
vals = append(vals, keyGasPair{
Key: k,
Gas: v,
})
}
sort.Slice(vals, func(i, j int) bool {
return vals[i].Gas > vals[j].Gas
})
return vals
}

senderVals := mapToSortedKvs(bySender)
destVals := mapToSortedKvs(byDest)
methodVals := mapToSortedKvs(byMethod)

fmt.Printf("Total Gas Limit: %d\n", sum)
fmt.Printf("By Sender:\n")
for i := 0; i < 10 && i < len(senderVals); i++ {
sv := senderVals[i]
fmt.Printf("%s\t%0.2f\t(%d)\n", sv.Key, (100*float64(sv.Gas))/float64(sum), sv.Gas)
}
fmt.Println()
fmt.Printf("By Receiver:\n")
for i := 0; i < 10 && i < len(destVals); i++ {
sv := destVals[i]
fmt.Printf("%s\t%0.2f\t(%d)\n", sv.Key, (100*float64(sv.Gas))/float64(sum), sv.Gas)
}
fmt.Println()
fmt.Printf("By Method:\n")
for i := 0; i < 10 && i < len(methodVals); i++ {
sv := methodVals[i]
fmt.Printf("%s\t%0.2f\t(%d)\n", sv.Key, (100*float64(sv.Gas))/float64(sum), sv.Gas)
}

return nil
},
}

var chainListCmd = &cli.Command{
Name: "list",
Usage: "View a segment of the chain",
Expand Down