-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restrict syscall caching to paths backed by the build's main `FileSys…
…tem`. Avoids wasteful caching of in-memory and transient `FileSystem` types. PiperOrigin-RevId: 492560997 Change-Id: I19865c33f91566fb101c98508fe8897f9ad115f5
- Loading branch information
1 parent
32fb0a2
commit de7b26a
Showing
2 changed files
with
109 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/main/java/com/google/devtools/build/lib/vfs/SingleFileSystemSyscallCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2022 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.devtools.build.lib.vfs; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* A {@link SyscallCache} that delegates to a caching implementation only for paths with a | ||
* particular {@link FileSystem}. | ||
* | ||
* <p>Any calls that pass a {@link Path} backed by a different {@link FileSystem} are routed to | ||
* {@link SyscallCache#NO_CACHE}. This can be used to ensure that only calls for the build's main | ||
* {@link FileSystem} are cached. Common alternative filesystems for which caching is wasteful | ||
* include: | ||
* | ||
* <ul> | ||
* <li>{@link | ||
* com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider.BundledFileSystem}, an | ||
* in-memory filesystem (no real filesystem ops to save). | ||
* <li>An {@linkplain com.google.devtools.build.lib.vfs.OutputService.ActionFileSystemType | ||
* action-scoped filesystem} where there is no potential for reuse outside a single action's | ||
* execution, and caching prolongs the lifetime of the instance. | ||
*/ | ||
public final class SingleFileSystemSyscallCache implements SyscallCache { | ||
|
||
private final SyscallCache delegate; | ||
private final FileSystem fs; | ||
|
||
public SingleFileSystemSyscallCache(SyscallCache delegate, FileSystem fs) { | ||
this.delegate = checkNotNull(delegate); | ||
this.fs = checkNotNull(fs); | ||
} | ||
|
||
@Override | ||
public Collection<Dirent> readdir(Path path) throws IOException { | ||
return delegateFor(path).readdir(path); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public FileStatus statIfFound(Path path, Symlinks symlinks) throws IOException { | ||
return delegateFor(path).statIfFound(path, symlinks); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public DirentTypeWithSkip getType(Path path, Symlinks symlinks) throws IOException { | ||
return delegateFor(path).getType(path, symlinks); | ||
} | ||
|
||
@Override | ||
public byte[] getFastDigest(Path path) throws IOException { | ||
return delegateFor(path).getFastDigest(path); | ||
} | ||
|
||
@Override | ||
public byte[] getxattr(Path path, String xattrName) throws IOException { | ||
return delegateFor(path).getxattr(path, xattrName); | ||
} | ||
|
||
@Override | ||
public byte[] getxattr(Path path, String xattrName, Symlinks followSymlinks) throws IOException { | ||
return delegateFor(path).getxattr(path, xattrName, followSymlinks); | ||
} | ||
|
||
@Override | ||
public void noteAnalysisPhaseEnded() { | ||
delegate.noteAnalysisPhaseEnded(); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
delegate.clear(); | ||
} | ||
|
||
/** Returns the underlying {@link SyscallCache} used for eligible paths. */ | ||
// TODO(b/245929310): This shouldn't be exposed. | ||
public SyscallCache getUnderlyingCache() { | ||
return delegate; | ||
} | ||
|
||
private SyscallCache delegateFor(Path path) { | ||
return path.getFileSystem().equals(fs) ? delegate : SyscallCache.NO_CACHE; | ||
} | ||
} |