weedfs_stats.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package mount
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/hanwen/go-fuse/v2/fuse"
  6. "github.com/seaweedfs/seaweedfs/weed/glog"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  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. if wfs.option.Quota > 0 && totalDiskSize > uint64(wfs.option.Quota) {
  48. totalDiskSize = uint64(wfs.option.Quota)
  49. if usedDiskSize > totalDiskSize {
  50. totalDiskSize = usedDiskSize
  51. }
  52. }
  53. // http://man.he.net/man2/statfs
  54. /*
  55. struct statfs {
  56. __fsword_t f_type; // Type of filesystem (see below)
  57. __fsword_t f_bsize; // Optimal transfer block size
  58. fsblkcnt_t f_blocks; // Total data blocks in filesystem
  59. fsblkcnt_t f_bfree; // Free blocks in filesystem
  60. fsblkcnt_t f_bavail; // Free blocks available to
  61. unprivileged user
  62. fsfilcnt_t f_files; // Total file nodes in filesystem
  63. fsfilcnt_t f_ffree; // Free file nodes in filesystem
  64. fsid_t f_fsid; // Filesystem ID
  65. __fsword_t f_namelen; // Maximum length of filenames
  66. __fsword_t f_frsize; // Fragment size (since Linux 2.6)
  67. __fsword_t f_flags; // Mount flags of filesystem
  68. (since Linux 2.6.36)
  69. __fsword_t f_spare[xxx];
  70. // Padding bytes reserved for future use
  71. };
  72. */
  73. // Compute the total number of available blocks
  74. out.Blocks = totalDiskSize / blockSize
  75. if out.Blocks <= 0 {
  76. out.Blocks = 1
  77. }
  78. // Compute the number of used blocks
  79. numBlocks := usedDiskSize / blockSize
  80. remainingBlocks := int64(out.Blocks) - int64(numBlocks)
  81. if remainingBlocks < 0 {
  82. remainingBlocks = 0
  83. }
  84. // Report the number of free and available blocks for the block size
  85. out.Bfree = uint64(remainingBlocks)
  86. out.Bavail = uint64(remainingBlocks)
  87. out.Bsize = uint32(blockSize)
  88. // Report the total number of possible files in the file system (and those free)
  89. out.Files = math.MaxInt64
  90. out.Ffree = math.MaxInt64 - actualFileCount
  91. // Report the maximum length of a name and the minimum fragment size
  92. out.NameLen = 1024
  93. out.Frsize = uint32(blockSize)
  94. return fuse.OK
  95. }