Skip to content

Commit b94e48c

Browse files
readahead tuning (#25)
* readahead tuning * wip
1 parent 0ea3fba commit b94e48c

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/knadh/koanf/providers/file v0.1.0
1717
github.com/knadh/koanf/providers/rawbytes v0.1.0
1818
github.com/knadh/koanf/v2 v2.0.1
19+
github.com/moby/sys/mountinfo v0.7.2
1920
github.com/redis/go-redis/v9 v9.5.1
2021
github.com/shirou/gopsutil v2.21.11+incompatible
2122
go.uber.org/zap v1.27.0

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,9 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
180180
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
181181
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
182182
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
183-
github.com/moby/sys/mountinfo v0.6.2 h1:BzJjoreD5BMFNmD9Rus6gdd1pLuecOFPt8wC+Vygl78=
184183
github.com/moby/sys/mountinfo v0.6.2/go.mod h1:IJb6JQeOklcdMU9F5xQ8ZALD+CUr5VlGpwtX+VE0rpI=
184+
github.com/moby/sys/mountinfo v0.7.2 h1:1shs6aH5s4o5H2zQLn796ADW1wMrIwHsyJ2v9KouLrg=
185+
github.com/moby/sys/mountinfo v0.7.2/go.mod h1:1YOa8w8Ih7uW0wALDUgT1dTTSBrZ+HiBLGws92L2RU4=
185186
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
186187
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
187188
github.com/pierrec/lz4/v4 v4.1.14/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=

pkg/blobfs.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/hanwen/go-fuse/v2/fs"
1515
"github.com/hanwen/go-fuse/v2/fuse"
16+
"github.com/moby/sys/mountinfo"
1617
)
1718

1819
type BlobFsMetadata struct {
@@ -158,6 +159,36 @@ func Mount(ctx context.Context, opts BlobFsSystemOpts) (func() error, <-chan err
158159
return startServer, serverError, server, nil
159160
}
160161

162+
func updateReadAheadKB(mountPoint string, valueKB int) error {
163+
mounts, err := mountinfo.GetMounts(nil)
164+
if err != nil {
165+
return fmt.Errorf("failed to get mount info: %w", err)
166+
}
167+
168+
var deviceID string
169+
for _, mount := range mounts {
170+
if mount.Mountpoint == mountPoint {
171+
deviceID = fmt.Sprintf("%d:%d", mount.Major, mount.Minor)
172+
break
173+
}
174+
}
175+
176+
if deviceID == "" {
177+
return fmt.Errorf("mount point %s not found", mountPoint)
178+
}
179+
180+
// Construct path to read_ahead_kb
181+
readAheadPath := fmt.Sprintf("/sys/class/bdi/%s/read_ahead_kb", deviceID)
182+
183+
// Update read_ahead_kb
184+
cmd := exec.Command("sh", "-c", fmt.Sprintf("echo %d > %s", valueKB, readAheadPath))
185+
if err := cmd.Run(); err != nil {
186+
return fmt.Errorf("failed to update read_ahead_kb: %w", err)
187+
}
188+
189+
return nil
190+
}
191+
161192
// NewFileSystem initializes a new BlobFs with root metadata.
162193
func NewFileSystem(ctx context.Context, opts BlobFsSystemOpts) (*BlobFs, error) {
163194
metadata := opts.Metadata

pkg/blobfs_prefetch.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ func (pb *PrefetchBuffer) GetRange(offset uint64, dst []byte) error {
269269
}
270270

271271
func (pb *PrefetchBuffer) tryGetRange(offset, length uint64) ([]byte, bool, bool) {
272+
pb.mu.Lock()
273+
defer pb.mu.Unlock()
274+
272275
windowIndex := offset / pb.windowSize
273276

274277
var w *window
@@ -287,10 +290,8 @@ func (pb *PrefetchBuffer) tryGetRange(offset, length uint64) ([]byte, bool, bool
287290
return nil, false, false
288291
}
289292

290-
if w.fetching {
291-
w.mu.Lock()
292-
defer w.mu.Unlock()
293-
}
293+
w.mu.Lock()
294+
defer w.mu.Unlock()
294295

295296
windowOffset := offset - (windowIndex * pb.windowSize)
296297
windowHead := (windowIndex * pb.windowSize) + w.readLength

pkg/client.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const (
2828
closestHostTimeout = 30 * time.Second
2929
localClientCacheCleanupInterval = 5 * time.Second
3030
localClientCacheTTL = 300 * time.Second
31+
32+
// NOTE: This value for readAheadKB is separate from the blobfs config since the FUSE library does
33+
// weird stuff with the other read_ahead_kb value internally
34+
readAheadKB = 32768
3135
)
3236

3337
func AuthInterceptor(token string) grpc.UnaryClientInterceptor {
@@ -126,6 +130,11 @@ func NewBlobCacheClient(ctx context.Context, cfg BlobCacheConfig) (*BlobCacheCli
126130
return nil, err
127131
}
128132

133+
err = updateReadAheadKB(cfg.BlobFs.MountPoint, readAheadKB)
134+
if err != nil {
135+
Logger.Errorf("Failed to update read_ahead_kb: %v", err)
136+
}
137+
129138
bc.blobfsServer = server
130139
}
131140

0 commit comments

Comments
 (0)