Skip to content

Commit

Permalink
chore: use time.Since instead of time.Now().Sub (#12801)
Browse files Browse the repository at this point in the history
  • Loading branch information
asamuj authored Dec 30, 2024
1 parent ac85fd8 commit bbd53a6
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 42 deletions.
11 changes: 6 additions & 5 deletions chain/sub/incoming.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ import (
"github.com/filecoin-project/lotus/metrics"
)

var log = logging.Logger("sub")
var DefaultHashFunction = uint64(mh.BLAKE2B_MIN + 31)
var (
log = logging.Logger("sub")
DefaultHashFunction = uint64(mh.BLAKE2B_MIN + 31)
)

var msgCidPrefix = cid.Prefix{
Version: 1,
Expand Down Expand Up @@ -169,7 +171,6 @@ func fetchCids(
cids []cid.Cid,
cb func(int, blocks.Block) error,
) error {

ctx, cancel := context.WithCancel(ctx)
defer cancel()

Expand Down Expand Up @@ -327,7 +328,7 @@ func (mv *MessageValidator) Validate(ctx context.Context, pid peer.ID, msg *pubs

start := time.Now()
defer func() {
ms := time.Now().Sub(start).Microseconds()
ms := time.Since(start).Microseconds()
stats.Record(ctx, metrics.MessageValidationDuration.M(float64(ms)/1000))
}()

Expand Down Expand Up @@ -394,7 +395,7 @@ func (mv *MessageValidator) validateLocalMessage(ctx context.Context, msg *pubsu

start := time.Now()
defer func() {
ms := time.Now().Sub(start).Microseconds()
ms := time.Since(start).Microseconds()
stats.Record(ctx, metrics.MessageValidationDuration.M(float64(ms)/1000))
}()

Expand Down
9 changes: 4 additions & 5 deletions cli/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func infoCmdAct(cctx *cli.Context) error {
}

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: ")
err = SyncBasefeeCheck(ctx, fullapi)
if err != nil {
Expand Down Expand Up @@ -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"))
}

//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))
switch {
case health > 85:
Expand Down Expand Up @@ -173,7 +173,6 @@ func infoCmdAct(cctx *cli.Context) error {
return err
}
return tw.Flush()

}

func SyncBasefeeCheck(ctx context.Context, fullapi v1api.FullNode) error {
Expand All @@ -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
fmt.Printf("[%s]", color.GreenString("sync ok"))
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:
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
gasCol := []color.Attribute{color.FgBlue}
Expand Down
6 changes: 1 addition & 5 deletions cmd/lotus-miner/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func infoCmdAct(cctx *cli.Context) error {
if err != nil {
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: ")

Expand Down Expand Up @@ -135,7 +135,6 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v1api.Full

// Sector size
mi, err := fullapi.StateMinerInfo(ctx, maddr, types.EmptyTSK)

if err != nil {
return err
}
Expand Down Expand Up @@ -166,7 +165,6 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v1api.Full
),
)
secCounts, err := fullapi.StateMinerSectorCount(ctx, maddr, types.EmptyTSK)

if err != nil {
return err
}
Expand Down Expand Up @@ -267,7 +265,6 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v1api.Full
colorTokenAmount(" Available: %s\n", availBalance)

mb, err := fullapi.StateMarketBalance(ctx, maddr, types.EmptyTSK)

if err != nil {
return xerrors.Errorf("getting market balance: %w", err)
}
Expand All @@ -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))

wb, err := fullapi.WalletBalance(ctx, mi.Worker)

if err != nil {
return xerrors.Errorf("getting worker balance: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/lotus-miner/proving.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ It will not send any messages to the chain.`,

start := time.Now()
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 {
return err
}

//convert sector information into easily readable information
// convert sector information into easily readable information
type PoStPartition struct {
Index uint64
Skipped []uint64
Expand Down
2 changes: 1 addition & 1 deletion cmd/lotus-miner/sealing.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ var sealingJobsCmd = &cli.Command{
}
dur := "n/a"
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]
Expand Down
5 changes: 2 additions & 3 deletions cmd/lotus-miner/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ var storageListCmd = &cli.Command{
fmt.Printf("\t%s: %s:\n", color.RedString("Error"), err)
continue
}
ping := time.Now().Sub(pingStart)
ping := time.Since(pingStart)

safeRepeat := func(s string, count int) string {
if count < 0 {
Expand All @@ -360,7 +360,7 @@ var storageListCmd = &cli.Command{
return strings.Repeat(s, count)
}

var barCols = int64(50)
barCols := int64(50)

// filesystem use bar
{
Expand Down Expand Up @@ -738,7 +738,6 @@ var storageListSectorsCmd = &cli.Command{
}

for _, info := range si {

list = append(list, entry{
id: sector,
storage: info.ID,
Expand Down
2 changes: 1 addition & 1 deletion miner/warmup.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ out:
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
}

Expand Down
2 changes: 1 addition & 1 deletion storage/paths/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func fetch(ctx context.Context, url, outname string, header http.Header) (rerr e
start := time.Now()
var bytes int64
defer func() {
took := time.Now().Sub(start)
took := time.Since(start)
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)
}()
Expand Down
12 changes: 6 additions & 6 deletions storage/paths/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (p *path) stat(ls LocalStorage, newReserve ...statExistingSectorForReservat
}

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{}
for id, res := range p.reservations {
jsonReservations = append(jsonReservations, map[string]interface{}{
Expand Down Expand Up @@ -182,8 +182,8 @@ func (p *path) stat(ls LocalStorage, newReserve ...statExistingSectorForReservat
}
}

if time.Now().Sub(start) > 5*time.Second {
log.Warnw("slow storage stat", "took", time.Now().Sub(start), "reservations", len(p.reservations))
if took := time.Since(start); took > 5*time.Second {
log.Warnw("slow storage stat", "took", took, "reservations", len(p.reservations))
}

return stat, newReserveOnDisk, err
Expand Down Expand Up @@ -463,7 +463,8 @@ func (st *Local) reportStorage(ctx context.Context) {
}

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()
if err != nil {
return nil, err
Expand Down Expand Up @@ -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)

if err != nil {
log.Debug(err)
continue
Expand Down Expand Up @@ -950,7 +950,7 @@ func (st *Local) GenerateSingleVanillaProof(ctx context.Context, minerID abi.Act
case r := <-resCh:
return r.Unwrap()
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
return nil, xerrors.Errorf("failed to generate vanilla proof before context cancellation: %w", ctx.Err())
Expand Down
12 changes: 7 additions & 5 deletions storage/paths/localstorage_cached.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"github.com/filecoin-project/lotus/storage/sealer/storiface"
)

var StatTimeout = 5 * time.Second
var MaxDiskUsageDuration = time.Second
var (
StatTimeout = 5 * time.Second
MaxDiskUsageDuration = time.Second
)

type cachedLocalStorage struct {
base LocalStorage
Expand Down Expand Up @@ -61,7 +63,7 @@ func (c *cachedLocalStorage) Stat(path string) (fsutil.FsStat, error) {
c.statLk.Lock()
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
}

Expand All @@ -87,7 +89,7 @@ func (c *cachedLocalStorage) DiskUsage(path string) (int64, error) {
entry = v

// 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
}
} else {
Expand Down Expand Up @@ -126,7 +128,7 @@ func (c *cachedLocalStorage) DiskUsage(path string) (int64, error) {
log.Warnw("getting usage is slow, falling back to previous usage",
"path", path,
"fallback", entry.last.usage,
"age", time.Now().Sub(entry.last.time))
"age", time.Since(entry.last.time))
}

return entry.last.usage, nil
Expand Down
4 changes: 2 additions & 2 deletions storage/sealer/fsutil/filesize_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func FileSize(path string) (SizeInfo, error) {
return err
})

if time.Now().Sub(start) >= 3*time.Second {
log.Warnw("very slow file size check", "took", time.Now().Sub(start), "path", path)
if took := time.Since(start); took >= 3*time.Second {
log.Warnw("very slow file size check", "took", took, "path", path)
}

if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions storage/wdpost/wdpost_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ func (s *WindowPoStScheduler) startSubmitPoST(
posts []miner.SubmitWindowedPoStParams,
completeSubmitPoST CompleteSubmitPoSTCb,
) context.CancelFunc {

ctx, abort := context.WithCancel(ctx)
go func() {
defer abort()
Expand Down Expand Up @@ -276,7 +275,7 @@ func (s *WindowPoStScheduler) runPoStCycle(ctx context.Context, manual bool, di
log := log.WithOptions(zap.Fields(zap.Time("cycle", start)))
log.Infow("starting PoSt cycle", "manual", manual, "ts", ts, "deadline", di.Index)
defer func() {
log.Infow("post cycle done", "took", time.Now().Sub(start))
log.Infow("post cycle done", "took", time.Since(start))
}()

if !manual {
Expand Down
8 changes: 4 additions & 4 deletions tools/stats/points/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (c *ChainPointCollector) Collect(ctx context.Context, tipset *types.TipSet)
start := time.Now()
done := metrics.Timer(ctx, metrics.TipsetCollectionDuration)
defer func() {
log.Infow("record tipset", "elapsed", time.Now().Sub(start).Seconds())
log.Infow("record tipset", "elapsed", time.Since(start).Seconds())
done()
}()

Expand Down Expand Up @@ -126,7 +126,7 @@ func (c *ChainPointCollector) collectBlockheaderPoints(ctx context.Context, pl *
start := time.Now()
done := metrics.Timer(ctx, metrics.TipsetCollectionBlockHeaderDuration)
defer func() {
log.Infow("collect blockheader points", "elapsed", time.Now().Sub(start).Seconds())
log.Infow("collect blockheader points", "elapsed", time.Since(start).Seconds())
done()
}()

Expand Down Expand Up @@ -218,7 +218,7 @@ func (c *ChainPointCollector) collectStaterootPoints(ctx context.Context, pl *in
start := time.Now()
done := metrics.Timer(ctx, metrics.TipsetCollectionStaterootDuration)
defer func() {
log.Infow("collect stateroot points", "elapsed", time.Now().Sub(start).Seconds())
log.Infow("collect stateroot points", "elapsed", time.Since(start).Seconds())
done()
}()

Expand Down Expand Up @@ -280,7 +280,7 @@ func (c *ChainPointCollector) collectMessagePoints(ctx context.Context, pl *infl
start := time.Now()
done := metrics.Timer(ctx, metrics.TipsetCollectionMessageDuration)
defer func() {
log.Infow("collect message points", "elapsed", time.Now().Sub(start).Seconds())
log.Infow("collect message points", "elapsed", time.Since(start).Seconds())
done()
}()

Expand Down

0 comments on commit bbd53a6

Please sign in to comment.