Skip to content

Commit

Permalink
add scan flag and fsstat
Browse files Browse the repository at this point in the history
Signed-off-by: xixi <[email protected]>
  • Loading branch information
Hexilee committed Nov 15, 2022
1 parent d2f44cf commit cf1e65b
Showing 1 changed file with 68 additions and 12 deletions.
80 changes: 68 additions & 12 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ package cmd
import (
"encoding/json"
"fmt"
"syscall"

"github.com/juicedata/juicefs/pkg/meta"
"github.com/juicedata/juicefs/pkg/utils"

"github.com/dustin/go-humanize"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"
)

func cmdStatus() *cli.Command {
Expand All @@ -46,22 +50,48 @@ $ juicefs status redis://localhost`,
Aliases: []string{"s"},
Usage: "show detailed information (sustained inodes, locks) of the specified session (sid)",
},
&cli.StringSliceFlag{
Name: "scan",
Usage: "scanning the meta engine for more information, may take a long time",
},
},
}
}

type sections struct {
Setting *meta.Format
Sessions []*meta.Session
Slices *sliceStat
FsStat *fsStat
Slices *sliceStat `json:",omitempty"`
}

type fsStat struct {
UsedSpace string
AvailableSpace string
UsedInodes uint64
AvailableInodes uint64

totalSpace uint64
availableSpace uint64
}

type sliceStat struct {
Delayed statAggr
}
type statAggr struct {
Count uint64
TotalSize uint64
TotalSize string

totalSize uint64
}

func (s *fsStat) Format() {
s.AvailableSpace = humanize.IBytes(s.availableSpace)
s.UsedSpace = humanize.IBytes(s.totalSpace - s.availableSpace)
}

func (s *statAggr) Format() {
s.TotalSize = humanize.IBytes(s.totalSize)
}

func printJson(v interface{}) {
Expand Down Expand Up @@ -96,10 +126,38 @@ func status(ctx *cli.Context) error {
logger.Fatalf("list sessions: %s", err)
}

progress := utils.NewProgress(false, false)
slicesSpinner := progress.AddDoubleSpinner("Delayed Slices")
var fs fsStat
if err = m.StatFS(meta.Background, &fs.totalSpace, &fs.availableSpace, &fs.UsedInodes, &fs.AvailableInodes); err != syscall.Errno(0) {
logger.Fatalf("stat fs: %s", err)
}
fs.Format()

sec := &sections{format, sessions, &fs, nil}
if scan := ctx.StringSlice("scan"); len(scan) > 0 {
progress := utils.NewProgress(false, false)
eg := &errgroup.Group{}
for _, s := range scan {
switch s {
case "slices":
eg.Go(func() (err error) {
sec.Slices, err = scanSlices(ctx, m, progress)
return err
})
default:
logger.Warnf("unknown scan option: %s, ignored", s)
}
}
if err := eg.Wait(); err != nil {
logger.Fatalf("scan: %s", err)
}
}
printJson(sec)
return nil
}

err = m.ScanDelayedSlices(ctx.Context, func(s meta.Slice) error {
func scanSlices(ctx *cli.Context, m meta.Meta, progress *utils.Progress) (*sliceStat, error) {
slicesSpinner := progress.AddDoubleSpinner("Delayed Slices")
err := m.ScanDelayedSlices(ctx.Context, func(s meta.Slice) error {
slicesSpinner.IncrInt64(int64(s.Size))
return nil
})
Expand All @@ -109,12 +167,10 @@ func status(ctx *cli.Context) error {
slicesSpinner.Done()

count, size := slicesSpinner.Current()
slices := &sliceStat{
Delayed: statAggr{
Count: uint64(count),
TotalSize: uint64(size),
},
aggr := statAggr{
Count: uint64(count),
totalSize: uint64(size),
}
printJson(&sections{format, sessions, slices})
return nil
aggr.Format()
return &sliceStat{aggr}, nil
}

0 comments on commit cf1e65b

Please sign in to comment.