Skip to content

Commit

Permalink
[DATA-2672] simplify diskusage (viamrobotics#4300)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksanford authored Aug 20, 2024
1 parent e0fe63f commit 35120a3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 34 deletions.
12 changes: 8 additions & 4 deletions services/datamanager/builtin/file_deletion.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,24 @@ var (
var errAtSizeThreshold = errors.New("capture directory has reached or exceeded disk usage threshold for deletion")

func shouldDeleteBasedOnDiskUsage(ctx context.Context, captureDirPath string, logger logging.Logger) (bool, error) {
usage := diskusage.NewDiskUsage(captureDirPath)
statfs, err := diskusage.Statfs(captureDirPath)
if err != nil {
logger.Debugf("diskusage.Statfs returned error: %s", err.Error())
return false, nil
}
// we get usage this way to ensure we get the amount of remaining space in the partition.
// calling usage.Usage() returns the usage of the whole disk, not the user partition
usedSpace := 1.0 - float64(usage.Available())/float64(usage.Size())
usedSpace := 1.0 - statfs.AvailablePercent()
if math.IsNaN(usedSpace) {
return false, nil
}
if usedSpace < fsThresholdToTriggerDeletion {
logger.Debugf("disk not full enough, exiting. Used space: %f, available space: %d, size: %d",
usedSpace, usage.Available(), usage.Size())
usedSpace, statfs.AvailableBytes, statfs.SizeBytes)
return false, nil
}
// Walk the dir to get capture stats
shouldDelete, err := exceedsDeletionThreshold(ctx, captureDirPath, float64(usage.Size()), logger)
shouldDelete, err := exceedsDeletionThreshold(ctx, captureDirPath, float64(statfs.SizeBytes), logger)
if err != nil && !shouldDelete {
logger.Warnf("Disk nearing capacity but data capture directory is below %f of that size, file deletion will not run",
captureDirToFSUsageRatio)
Expand Down
48 changes: 18 additions & 30 deletions utils/diskusage/disk_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,27 @@ import "syscall"

// DiskUsage contains usage data and provides user-friendly access methods.
type DiskUsage struct {
stat *syscall.Statfs_t
// AvailableBytes is the total available bytes on file system to an unprivileged user.
AvailableBytes uint64
// SizeBytes is the total size of the file system in bytes.
SizeBytes uint64
}

// NewDiskUsage returns an object holding the disk usage of volumePath
// or nil in case of error (invalid path, etc).
func NewDiskUsage(volumePath string) *DiskUsage {
// Statfs returns file system statistics.
func Statfs(volumePath string) (DiskUsage, error) {
var stat syscall.Statfs_t
//nolint:errcheck,gosec
syscall.Statfs(volumePath, &stat)
return &DiskUsage{&stat}
if err := syscall.Statfs(volumePath, &stat); err != nil {
return DiskUsage{}, err
}
return DiskUsage{
AvailableBytes: stat.Bavail * uint64(stat.Bsize),
SizeBytes: stat.Blocks * uint64(stat.Bsize),
}, nil
}

// Free returns total free bytes on file system.
func (du *DiskUsage) Free() uint64 {
return du.stat.Bfree * uint64(du.stat.Bsize)
}

// Available return total available bytes on file system to an unprivileged user.
func (du *DiskUsage) Available() uint64 {
return du.stat.Bavail * uint64(du.stat.Bsize)
}

// Size returns total size of the file system.
func (du *DiskUsage) Size() uint64 {
return du.stat.Blocks * uint64(du.stat.Bsize)
}

// Used returns total bytes used in file system.
func (du *DiskUsage) Used() uint64 {
return du.Size() - du.Free()
}

// Usage returns percentage of use on the file system.
func (du *DiskUsage) Usage() float32 {
return float32(du.Used()) / float32(du.Size())
// AvailablePercent returns the percentage (0.0-1.0) of the disk available
// to an unprivileged user
// see `man statfs` for how the underlying values are derived on your platform.
func (du DiskUsage) AvailablePercent() float64 {
return float64(du.AvailableBytes) / float64(du.SizeBytes)
}

0 comments on commit 35120a3

Please sign in to comment.