chore: use time.Since instead of time.Now().Sub (#12801)

This commit is contained in:
asamuj
2024-12-30 11:13:53 +08:00
committed by GitHub
parent ac85fd87c6
commit bbd53a6af7
13 changed files with 38 additions and 42 deletions

View File

@ -30,8 +30,10 @@ import (
"github.com/filecoin-project/lotus/metrics" "github.com/filecoin-project/lotus/metrics"
) )
var log = logging.Logger("sub") var (
var DefaultHashFunction = uint64(mh.BLAKE2B_MIN + 31) log = logging.Logger("sub")
DefaultHashFunction = uint64(mh.BLAKE2B_MIN + 31)
)
var msgCidPrefix = cid.Prefix{ var msgCidPrefix = cid.Prefix{
Version: 1, Version: 1,
@ -169,7 +171,6 @@ func fetchCids(
cids []cid.Cid, cids []cid.Cid,
cb func(int, blocks.Block) error, cb func(int, blocks.Block) error,
) error { ) error {
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
@ -327,7 +328,7 @@ func (mv *MessageValidator) Validate(ctx context.Context, pid peer.ID, msg *pubs
start := time.Now() start := time.Now()
defer func() { defer func() {
ms := time.Now().Sub(start).Microseconds() ms := time.Since(start).Microseconds()
stats.Record(ctx, metrics.MessageValidationDuration.M(float64(ms)/1000)) stats.Record(ctx, metrics.MessageValidationDuration.M(float64(ms)/1000))
}() }()
@ -394,7 +395,7 @@ func (mv *MessageValidator) validateLocalMessage(ctx context.Context, msg *pubsu
start := time.Now() start := time.Now()
defer func() { defer func() {
ms := time.Now().Sub(start).Microseconds() ms := time.Since(start).Microseconds()
stats.Record(ctx, metrics.MessageValidationDuration.M(float64(ms)/1000)) stats.Record(ctx, metrics.MessageValidationDuration.M(float64(ms)/1000))
}() }()

View File

@ -45,7 +45,7 @@ func infoCmdAct(cctx *cli.Context) error {
} }
fmt.Printf("Network: %s\n", network.NetworkName) fmt.Printf("Network: %s\n", network.NetworkName)
fmt.Printf("StartTime: %s (started at %s)\n", time.Now().Sub(start).Truncate(time.Second), start.Truncate(time.Second)) fmt.Printf("StartTime: %s (started at %s)\n", time.Since(start).Truncate(time.Second), start.Truncate(time.Second))
fmt.Print("Chain: ") fmt.Print("Chain: ")
err = SyncBasefeeCheck(ctx, fullapi) err = SyncBasefeeCheck(ctx, fullapi)
if err != nil { if err != nil {
@ -75,7 +75,7 @@ func infoCmdAct(cctx *cli.Context) error {
fmt.Printf("%s (check %s)\n", color.RedString("⚠ %d Active alerts", len(activeAlerts)), color.YellowString("lotus log alerts")) fmt.Printf("%s (check %s)\n", color.RedString("⚠ %d Active alerts", len(activeAlerts)), color.YellowString("lotus log alerts"))
} }
//Chain health calculated as percentage: amount of blocks in last finality / very healthy amount of blocks in a finality (900 epochs * 5 blocks per tipset) // Chain health calculated as percentage: amount of blocks in last finality / very healthy amount of blocks in a finality (900 epochs * 5 blocks per tipset)
health := (100 * (900 * status.ChainStatus.BlocksPerTipsetLastFinality) / (900 * 5)) health := (100 * (900 * status.ChainStatus.BlocksPerTipsetLastFinality) / (900 * 5))
switch { switch {
case health > 85: case health > 85:
@ -173,7 +173,6 @@ func infoCmdAct(cctx *cli.Context) error {
return err return err
} }
return tw.Flush() return tw.Flush()
} }
func SyncBasefeeCheck(ctx context.Context, fullapi v1api.FullNode) error { func SyncBasefeeCheck(ctx context.Context, fullapi v1api.FullNode) error {
@ -186,9 +185,9 @@ func SyncBasefeeCheck(ctx context.Context, fullapi v1api.FullNode) error {
case time.Now().Unix()-int64(head.MinTimestamp()) < int64(buildconstants.BlockDelaySecs*3/2): // within 1.5 epochs case time.Now().Unix()-int64(head.MinTimestamp()) < int64(buildconstants.BlockDelaySecs*3/2): // within 1.5 epochs
fmt.Printf("[%s]", color.GreenString("sync ok")) fmt.Printf("[%s]", color.GreenString("sync ok"))
case time.Now().Unix()-int64(head.MinTimestamp()) < int64(buildconstants.BlockDelaySecs*5): // within 5 epochs case time.Now().Unix()-int64(head.MinTimestamp()) < int64(buildconstants.BlockDelaySecs*5): // within 5 epochs
fmt.Printf("[%s]", color.YellowString("sync slow (%s behind)", time.Now().Sub(time.Unix(int64(head.MinTimestamp()), 0)).Truncate(time.Second))) fmt.Printf("[%s]", color.YellowString("sync slow (%s behind)", time.Since(time.Unix(int64(head.MinTimestamp()), 0)).Truncate(time.Second)))
default: default:
fmt.Printf("[%s]", color.RedString("sync behind! (%s behind)", time.Now().Sub(time.Unix(int64(head.MinTimestamp()), 0)).Truncate(time.Second))) fmt.Printf("[%s]", color.RedString("sync behind! (%s behind)", time.Since(time.Unix(int64(head.MinTimestamp()), 0)).Truncate(time.Second)))
} }
basefee := head.MinTicketBlock().ParentBaseFee basefee := head.MinTicketBlock().ParentBaseFee
gasCol := []color.Attribute{color.FgBlue} gasCol := []color.Attribute{color.FgBlue}

View File

@ -81,7 +81,7 @@ func infoCmdAct(cctx *cli.Context) error {
if err != nil { if err != nil {
return err return err
} }
fmt.Printf("StartTime: %s (started at %s)\n", time.Now().Sub(start).Truncate(time.Second), start.Truncate(time.Second)) fmt.Printf("StartTime: %s (started at %s)\n", time.Since(start).Truncate(time.Second), start.Truncate(time.Second))
fmt.Print("Chain: ") fmt.Print("Chain: ")
@ -135,7 +135,6 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v1api.Full
// Sector size // Sector size
mi, err := fullapi.StateMinerInfo(ctx, maddr, types.EmptyTSK) mi, err := fullapi.StateMinerInfo(ctx, maddr, types.EmptyTSK)
if err != nil { if err != nil {
return err return err
} }
@ -166,7 +165,6 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v1api.Full
), ),
) )
secCounts, err := fullapi.StateMinerSectorCount(ctx, maddr, types.EmptyTSK) secCounts, err := fullapi.StateMinerSectorCount(ctx, maddr, types.EmptyTSK)
if err != nil { if err != nil {
return err return err
} }
@ -267,7 +265,6 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v1api.Full
colorTokenAmount(" Available: %s\n", availBalance) colorTokenAmount(" Available: %s\n", availBalance)
mb, err := fullapi.StateMarketBalance(ctx, maddr, types.EmptyTSK) mb, err := fullapi.StateMarketBalance(ctx, maddr, types.EmptyTSK)
if err != nil { if err != nil {
return xerrors.Errorf("getting market balance: %w", err) return xerrors.Errorf("getting market balance: %w", err)
} }
@ -278,7 +275,6 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v1api.Full
colorTokenAmount(" Available: %s\n", big.Sub(mb.Escrow, mb.Locked)) colorTokenAmount(" Available: %s\n", big.Sub(mb.Escrow, mb.Locked))
wb, err := fullapi.WalletBalance(ctx, mi.Worker) wb, err := fullapi.WalletBalance(ctx, mi.Worker)
if err != nil { if err != nil {
return xerrors.Errorf("getting worker balance: %w", err) return xerrors.Errorf("getting worker balance: %w", err)
} }

View File

@ -239,12 +239,12 @@ It will not send any messages to the chain.`,
start := time.Now() start := time.Now()
res, err := minerApi.ComputeWindowPoSt(ctx, dlIdx, types.EmptyTSK) res, err := minerApi.ComputeWindowPoSt(ctx, dlIdx, types.EmptyTSK)
fmt.Printf("Took %s\n", time.Now().Sub(start)) fmt.Printf("Took %s\n", time.Since(start))
if err != nil { if err != nil {
return err return err
} }
//convert sector information into easily readable information // convert sector information into easily readable information
type PoStPartition struct { type PoStPartition struct {
Index uint64 Index uint64
Skipped []uint64 Skipped []uint64

View File

@ -286,7 +286,7 @@ var sealingJobsCmd = &cli.Command{
} }
dur := "n/a" dur := "n/a"
if !l.Start.IsZero() { if !l.Start.IsZero() {
dur = time.Now().Sub(l.Start).Truncate(time.Millisecond * 100).String() dur = time.Since(l.Start).Truncate(time.Millisecond * 100).String()
} }
hostname, ok := workerHostnames[l.wid] hostname, ok := workerHostnames[l.wid]

View File

@ -351,7 +351,7 @@ var storageListCmd = &cli.Command{
fmt.Printf("\t%s: %s:\n", color.RedString("Error"), err) fmt.Printf("\t%s: %s:\n", color.RedString("Error"), err)
continue continue
} }
ping := time.Now().Sub(pingStart) ping := time.Since(pingStart)
safeRepeat := func(s string, count int) string { safeRepeat := func(s string, count int) string {
if count < 0 { if count < 0 {
@ -360,7 +360,7 @@ var storageListCmd = &cli.Command{
return strings.Repeat(s, count) return strings.Repeat(s, count)
} }
var barCols = int64(50) barCols := int64(50)
// filesystem use bar // filesystem use bar
{ {
@ -738,7 +738,6 @@ var storageListSectorsCmd = &cli.Command{
} }
for _, info := range si { for _, info := range si {
list = append(list, entry{ list = append(list, entry{
id: sector, id: sector,
storage: info.ID, storage: info.ID,

View File

@ -84,7 +84,7 @@ out:
return xerrors.Errorf("failed to compute proof: %w", err) return xerrors.Errorf("failed to compute proof: %w", err)
} }
log.Infow("winning PoSt warmup successful", "took", time.Now().Sub(start)) log.Infow("winning PoSt warmup successful", "took", time.Since(start))
return nil return nil
} }

View File

@ -37,7 +37,7 @@ func fetch(ctx context.Context, url, outname string, header http.Header) (rerr e
start := time.Now() start := time.Now()
var bytes int64 var bytes int64
defer func() { defer func() {
took := time.Now().Sub(start) took := time.Since(start)
mibps := float64(bytes) / 1024 / 1024 * float64(time.Second) / float64(took) mibps := float64(bytes) / 1024 / 1024 * float64(time.Second) / float64(took)
log.Infow("Fetch done", "url", url, "out", outname, "took", took.Round(time.Millisecond), "bytes", bytes, "MiB/s", mibps, "err", rerr) log.Infow("Fetch done", "url", url, "out", outname, "took", took.Round(time.Millisecond), "bytes", bytes, "MiB/s", mibps, "err", rerr)
}() }()

View File

@ -144,7 +144,7 @@ func (p *path) stat(ls LocalStorage, newReserve ...statExistingSectorForReservat
} }
if stat.Reserved < 0 { if stat.Reserved < 0 {
//log.Warnf("negative reserved storage: p.reserved=%d, reserved: %d", p.reserved, stat.Reserved) // log.Warnf("negative reserved storage: p.reserved=%d, reserved: %d", p.reserved, stat.Reserved)
var jsonReservations []map[string]interface{} var jsonReservations []map[string]interface{}
for id, res := range p.reservations { for id, res := range p.reservations {
jsonReservations = append(jsonReservations, map[string]interface{}{ jsonReservations = append(jsonReservations, map[string]interface{}{
@ -182,8 +182,8 @@ func (p *path) stat(ls LocalStorage, newReserve ...statExistingSectorForReservat
} }
} }
if time.Now().Sub(start) > 5*time.Second { if took := time.Since(start); took > 5*time.Second {
log.Warnw("slow storage stat", "took", time.Now().Sub(start), "reservations", len(p.reservations)) log.Warnw("slow storage stat", "took", took, "reservations", len(p.reservations))
} }
return stat, newReserveOnDisk, err return stat, newReserveOnDisk, err
@ -463,7 +463,8 @@ func (st *Local) reportStorage(ctx context.Context) {
} }
func (st *Local) Reserve(ctx context.Context, sid storiface.SectorRef, ft storiface.SectorFileType, func (st *Local) Reserve(ctx context.Context, sid storiface.SectorRef, ft storiface.SectorFileType,
storageIDs storiface.SectorPaths, overheadTab map[storiface.SectorFileType]int, minFreePercentage float64) (func(), error) { storageIDs storiface.SectorPaths, overheadTab map[storiface.SectorFileType]int, minFreePercentage float64,
) (func(), error) {
ssize, err := sid.ProofType.SectorSize() ssize, err := sid.ProofType.SectorSize()
if err != nil { if err != nil {
return nil, err return nil, err
@ -661,7 +662,6 @@ func (st *Local) AcquireSector(ctx context.Context, sid storiface.SectorRef, exi
} }
alloc, err := allocPathOk(si.CanSeal, si.CanStore, si.AllowTypes, si.DenyTypes, si.AllowMiners, si.DenyMiners, fileType, sid.ID.Miner) alloc, err := allocPathOk(si.CanSeal, si.CanStore, si.AllowTypes, si.DenyTypes, si.AllowMiners, si.DenyMiners, fileType, sid.ID.Miner)
if err != nil { if err != nil {
log.Debug(err) log.Debug(err)
continue continue
@ -950,7 +950,7 @@ func (st *Local) GenerateSingleVanillaProof(ctx context.Context, minerID abi.Act
case r := <-resCh: case r := <-resCh:
return r.Unwrap() return r.Unwrap()
case <-ctx.Done(): case <-ctx.Done():
log.Errorw("failed to generate valilla PoSt proof before context cancellation", "err", ctx.Err(), "duration", time.Now().Sub(start), "cache-id", cacheID, "sealed-id", sealedID, "cache", cache, "sealed", sealed) log.Errorw("failed to generate valilla PoSt proof before context cancellation", "err", ctx.Err(), "duration", time.Since(start), "cache-id", cacheID, "sealed-id", sealedID, "cache", cache, "sealed", sealed)
// this will leave the GenerateSingleVanillaProof goroutine hanging, but that's still less bad than failing PoSt // this will leave the GenerateSingleVanillaProof goroutine hanging, but that's still less bad than failing PoSt
return nil, xerrors.Errorf("failed to generate vanilla proof before context cancellation: %w", ctx.Err()) return nil, xerrors.Errorf("failed to generate vanilla proof before context cancellation: %w", ctx.Err())

View File

@ -11,8 +11,10 @@ import (
"github.com/filecoin-project/lotus/storage/sealer/storiface" "github.com/filecoin-project/lotus/storage/sealer/storiface"
) )
var StatTimeout = 5 * time.Second var (
var MaxDiskUsageDuration = time.Second StatTimeout = 5 * time.Second
MaxDiskUsageDuration = time.Second
)
type cachedLocalStorage struct { type cachedLocalStorage struct {
base LocalStorage base LocalStorage
@ -61,7 +63,7 @@ func (c *cachedLocalStorage) Stat(path string) (fsutil.FsStat, error) {
c.statLk.Lock() c.statLk.Lock()
defer c.statLk.Unlock() defer c.statLk.Unlock()
if v, ok := c.stats.Get(path); ok && time.Now().Sub(v.time) < StatTimeout { if v, ok := c.stats.Get(path); ok && time.Since(v.time) < StatTimeout {
return v.stat, nil return v.stat, nil
} }
@ -87,7 +89,7 @@ func (c *cachedLocalStorage) DiskUsage(path string) (int64, error) {
entry = v entry = v
// if we have recent cached entry, use that // if we have recent cached entry, use that
if time.Now().Sub(entry.last.time) < StatTimeout { if time.Since(entry.last.time) < StatTimeout {
return entry.last.usage, nil return entry.last.usage, nil
} }
} else { } else {
@ -126,7 +128,7 @@ func (c *cachedLocalStorage) DiskUsage(path string) (int64, error) {
log.Warnw("getting usage is slow, falling back to previous usage", log.Warnw("getting usage is slow, falling back to previous usage",
"path", path, "path", path,
"fallback", entry.last.usage, "fallback", entry.last.usage,
"age", time.Now().Sub(entry.last.time)) "age", time.Since(entry.last.time))
} }
return entry.last.usage, nil return entry.last.usage, nil

View File

@ -39,8 +39,8 @@ func FileSize(path string) (SizeInfo, error) {
return err return err
}) })
if time.Now().Sub(start) >= 3*time.Second { if took := time.Since(start); took >= 3*time.Second {
log.Warnw("very slow file size check", "took", time.Now().Sub(start), "path", path) log.Warnw("very slow file size check", "took", took, "path", path)
} }
if err != nil { if err != nil {

View File

@ -117,7 +117,6 @@ func (s *WindowPoStScheduler) startSubmitPoST(
posts []miner.SubmitWindowedPoStParams, posts []miner.SubmitWindowedPoStParams,
completeSubmitPoST CompleteSubmitPoSTCb, completeSubmitPoST CompleteSubmitPoSTCb,
) context.CancelFunc { ) context.CancelFunc {
ctx, abort := context.WithCancel(ctx) ctx, abort := context.WithCancel(ctx)
go func() { go func() {
defer abort() defer abort()
@ -276,7 +275,7 @@ func (s *WindowPoStScheduler) runPoStCycle(ctx context.Context, manual bool, di
log := log.WithOptions(zap.Fields(zap.Time("cycle", start))) log := log.WithOptions(zap.Fields(zap.Time("cycle", start)))
log.Infow("starting PoSt cycle", "manual", manual, "ts", ts, "deadline", di.Index) log.Infow("starting PoSt cycle", "manual", manual, "ts", ts, "deadline", di.Index)
defer func() { defer func() {
log.Infow("post cycle done", "took", time.Now().Sub(start)) log.Infow("post cycle done", "took", time.Since(start))
}() }()
if !manual { if !manual {

View File

@ -81,7 +81,7 @@ func (c *ChainPointCollector) Collect(ctx context.Context, tipset *types.TipSet)
start := time.Now() start := time.Now()
done := metrics.Timer(ctx, metrics.TipsetCollectionDuration) done := metrics.Timer(ctx, metrics.TipsetCollectionDuration)
defer func() { defer func() {
log.Infow("record tipset", "elapsed", time.Now().Sub(start).Seconds()) log.Infow("record tipset", "elapsed", time.Since(start).Seconds())
done() done()
}() }()
@ -126,7 +126,7 @@ func (c *ChainPointCollector) collectBlockheaderPoints(ctx context.Context, pl *
start := time.Now() start := time.Now()
done := metrics.Timer(ctx, metrics.TipsetCollectionBlockHeaderDuration) done := metrics.Timer(ctx, metrics.TipsetCollectionBlockHeaderDuration)
defer func() { defer func() {
log.Infow("collect blockheader points", "elapsed", time.Now().Sub(start).Seconds()) log.Infow("collect blockheader points", "elapsed", time.Since(start).Seconds())
done() done()
}() }()
@ -218,7 +218,7 @@ func (c *ChainPointCollector) collectStaterootPoints(ctx context.Context, pl *in
start := time.Now() start := time.Now()
done := metrics.Timer(ctx, metrics.TipsetCollectionStaterootDuration) done := metrics.Timer(ctx, metrics.TipsetCollectionStaterootDuration)
defer func() { defer func() {
log.Infow("collect stateroot points", "elapsed", time.Now().Sub(start).Seconds()) log.Infow("collect stateroot points", "elapsed", time.Since(start).Seconds())
done() done()
}() }()
@ -280,7 +280,7 @@ func (c *ChainPointCollector) collectMessagePoints(ctx context.Context, pl *infl
start := time.Now() start := time.Now()
done := metrics.Timer(ctx, metrics.TipsetCollectionMessageDuration) done := metrics.Timer(ctx, metrics.TipsetCollectionMessageDuration)
defer func() { defer func() {
log.Infow("collect message points", "elapsed", time.Now().Sub(start).Seconds()) log.Infow("collect message points", "elapsed", time.Since(start).Seconds())
done() done()
}() }()