Skip to content

Commit

Permalink
fix: gc policy for windows to use 10% of disk space
Browse files Browse the repository at this point in the history
Initially we had the GC Policy for Windows use only
2 GB (2e9 bytes) of disk space and this was limiting
for some build scenarios that need more than that,
especially ServerCore images.

This commit makes the policy to use percentages
as it is on Linux. Also going for 10% as it is
on Linux. For instance, after the change, on a
dev machine, the configured space was
74694367232 / (1 << 30) = 69.5 GiB.

fixes #4858 docker/buildx#2411

Also, refactors the diskSize logic to simplify
it by bringing the `d.AsByte` function back from
platform specific files to `gcpolicy.go`.

Signed-off-by: Anthony Nandaa <profnandaa@gmail.com>
  • Loading branch information
profnandaa committed Apr 22, 2024
1 parent bbd262a commit 2b9917d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
16 changes: 16 additions & 0 deletions cmd/buildkitd/config/gcpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,19 @@ func stripQuotes(s string) string {
}
return s
}

func DetectDefaultGCCap() DiskSpace {
return DiskSpace{Percentage: DiskSpacePercentage}
}

func (d DiskSpace) AsBytes(root string) int64 {
if d.Bytes != 0 {
return d.Bytes
}
if d.Percentage == 0 {
return 0
}

diskSize := getDiskSize(root)
return diskSize * d.Percentage / 100
}
19 changes: 6 additions & 13 deletions cmd/buildkitd/config/gcpolicy_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,18 @@ package config

import (
"syscall"
)

func DetectDefaultGCCap() DiskSpace {
return DiskSpace{Percentage: 10}
}
"github.com/moby/buildkit/util/bklog"
)

func (d DiskSpace) AsBytes(root string) int64 {
if d.Bytes != 0 {
return d.Bytes
}
if d.Percentage == 0 {
return 0
}
var DiskSpacePercentage int64 = 20

func getDiskSize(root string) int64 {
var st syscall.Statfs_t
if err := syscall.Statfs(root, &st); err != nil {
bklog.L.Warnf("failed to get disk size: %v", err)
return defaultCap
}
diskSize := int64(st.Bsize) * int64(st.Blocks)
avail := diskSize * d.Percentage / 100
return (avail/(1<<30) + 1) * 1e9 // round up
return diskSize
}
32 changes: 27 additions & 5 deletions cmd/buildkitd/config/gcpolicy_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,32 @@

package config

func DetectDefaultGCCap() DiskSpace {
return DiskSpace{Bytes: defaultCap}
}
import (
"github.com/moby/buildkit/util/bklog"
"golang.org/x/sys/windows"
)

// set as double that for Linux since
// Windows images are generally larger.
var DiskSpacePercentage int64 = 20

func getDiskSize(root string) int64 {
rootUTF16, err := windows.UTF16FromString(root)
if err != nil {
bklog.L.Warnf("failed to convert root dir to UTF16: %v", err)
return defaultCap
}
var freeAvailableBytes uint64
var totalBytes uint64
var totalFreeBytes uint64

func (d DiskSpace) AsBytes(root string) int64 {
return d.Bytes
if err := windows.GetDiskFreeSpaceEx(
&rootUTF16[0],
&freeAvailableBytes,
&totalBytes,
&totalFreeBytes); err != nil {
bklog.L.Warnf("failed to get disk size: %v", err)
return defaultCap
}
return int64(totalBytes)
}

0 comments on commit 2b9917d

Please sign in to comment.