Skip to content

Commit

Permalink
fs: reduce map usage for inode counting
Browse files Browse the repository at this point in the history
  • Loading branch information
euank committed Mar 10, 2017
1 parent 6e94871 commit 2892e34
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,11 @@ func GetDirInodeUsage(dir string, timeout time.Duration) (uint64, error) {
}
rootDevId := rootStat.Dev

inodes := map[uint64]struct{}{}
// inodes tracks the inodes that can't have duplicate hardlinks (nlink = 1);
// this allows us to save space in the inode map below
var inodes uint64
// dedupedInode stores inodes that could be duplicates (nlink > 1)
dedupedInodes := map[uint64]struct{}{}

err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if os.IsNotExist(err) {
Expand All @@ -510,11 +514,16 @@ func GetDirInodeUsage(dir string, timeout time.Duration) (uint64, error) {
// don't descend into directories on other devices
return filepath.SkipDir
}
inodes[s.Ino] = struct{}{}
if s.Nlink > 1 {
// Dedupe things that could be hardlinks
dedupedInodes[s.Ino] = struct{}{}
} else {
inodes++
}
return nil
})

return uint64(len(inodes)), err
return inodes + uint64(len(dedupedInodes)), err
}

func getVfsStats(path string) (total uint64, free uint64, avail uint64, inodes uint64, inodesFree uint64, err error) {
Expand Down

0 comments on commit 2892e34

Please sign in to comment.