weedfs_stats.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package mount
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/hanwen/go-fuse/v2/fuse"
  8. "math"
  9. "time"
  10. )
  11. const blockSize = 512
  12. type statsCache struct {
  13. filer_pb.StatisticsResponse
  14. lastChecked int64 // unix time in seconds
  15. }
  16. func (wfs *WFS) StatFs(cancel <-chan struct{}, in *fuse.InHeader, out *fuse.StatfsOut) (code fuse.Status) {
  17. // glog.V(4).Infof("reading fs stats")
  18. if wfs.stats.lastChecked < time.Now().Unix()-20 {
  19. err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  20. request := &filer_pb.StatisticsRequest{
  21. Collection: wfs.option.Collection,
  22. Replication: wfs.option.Replication,
  23. Ttl: fmt.Sprintf("%ds", wfs.option.TtlSec),
  24. DiskType: string(wfs.option.DiskType),
  25. }
  26. glog.V(4).Infof("reading filer stats: %+v", request)
  27. resp, err := client.Statistics(context.Background(), request)
  28. if err != nil {
  29. glog.V(0).Infof("reading filer stats %v: %v", request, err)
  30. return err
  31. }
  32. glog.V(4).Infof("read filer stats: %+v", resp)
  33. wfs.stats.TotalSize = resp.TotalSize
  34. wfs.stats.UsedSize = resp.UsedSize
  35. wfs.stats.FileCount = resp.FileCount
  36. wfs.stats.lastChecked = time.Now().Unix()
  37. return nil
  38. })
  39. if err != nil {
  40. glog.V(0).Infof("filer Statistics: %v", err)
  41. return fuse.OK
  42. }
  43. }
  44. totalDiskSize := wfs.stats.TotalSize
  45. usedDiskSize := wfs.stats.UsedSize
  46. actualFileCount := wfs.stats.FileCount
  47. // Compute the total number of available blocks
  48. out.Blocks = totalDiskSize / blockSize
  49. // Compute the number of used blocks
  50. numBlocks := uint64(usedDiskSize / blockSize)
  51. // Report the number of free and available blocks for the block size
  52. out.Bfree = out.Blocks - numBlocks
  53. out.Bavail = out.Blocks - numBlocks
  54. out.Bsize = uint32(blockSize)
  55. // Report the total number of possible files in the file system (and those free)
  56. out.Files = math.MaxInt64
  57. out.Ffree = math.MaxInt64 - actualFileCount
  58. // Report the maximum length of a name and the minimum fragment size
  59. out.NameLen = 1024
  60. out.Frsize = uint32(blockSize)
  61. return fuse.OK
  62. }