Skip to content

Commit b048282

Browse files
larsrc-googlecopybara-github
authored andcommitted
Use readdir for cleanExisting in WorkerExecRoot.
Consistently reduces system time spent under Linux, leaves the other metrics alone. May be even better under Mac, if it has a good implementation. RELNOTES: None. PiperOrigin-RevId: 359296744
1 parent 37ea3d5 commit b048282

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/main/java/com/google/devtools/build/lib/worker/WorkerExecRoot.java

+17-12
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
// limitations under the License.
1414
package com.google.devtools.build.lib.worker;
1515

16+
import static com.google.devtools.build.lib.vfs.Dirent.Type.DIRECTORY;
17+
import static com.google.devtools.build.lib.vfs.Dirent.Type.SYMLINK;
18+
1619
import com.google.common.collect.Iterables;
1720
import com.google.devtools.build.lib.cmdline.LabelConstants;
1821
import com.google.devtools.build.lib.sandbox.SandboxHelpers;
1922
import com.google.devtools.build.lib.sandbox.SandboxHelpers.SandboxInputs;
2023
import com.google.devtools.build.lib.sandbox.SandboxHelpers.SandboxOutputs;
21-
import com.google.devtools.build.lib.vfs.FileStatus;
24+
import com.google.devtools.build.lib.vfs.Dirent;
2225
import com.google.devtools.build.lib.vfs.FileSystemUtils;
2326
import com.google.devtools.build.lib.vfs.Path;
2427
import com.google.devtools.build.lib.vfs.PathFragment;
@@ -109,36 +112,38 @@ private void cleanExisting(
109112
Set<PathFragment> dirsToCreate)
110113
throws IOException {
111114
Path execroot = workDir.getParentDirectory();
112-
for (Path path : root.getDirectoryEntries()) {
113-
FileStatus stat = path.stat(Symlinks.NOFOLLOW);
115+
for (Dirent dirent : root.readdir(Symlinks.NOFOLLOW)) {
116+
Path absPath = root.getChild(dirent.getName());
114117
PathFragment pathRelativeToWorkDir;
115-
if (path.startsWith(workDir)) {
118+
if (absPath.startsWith(workDir)) {
116119
// path is under workDir, i.e. execroot/<workspace name>. Simply get the relative path.
117-
pathRelativeToWorkDir = path.relativeTo(workDir);
120+
pathRelativeToWorkDir = absPath.relativeTo(workDir);
118121
} else {
119122
// path is not under workDir, which means it belongs to one of external repositories
120123
// symlinked directly under execroot. Get the relative path based on there and prepend it
121124
// with the designated prefix, '../', so that it's still a valid relative path to workDir.
122125
pathRelativeToWorkDir =
123-
LabelConstants.EXPERIMENTAL_EXTERNAL_PATH_PREFIX.getRelative(path.relativeTo(execroot));
126+
LabelConstants.EXPERIMENTAL_EXTERNAL_PATH_PREFIX.getRelative(
127+
absPath.relativeTo(execroot));
124128
}
125129
Optional<PathFragment> destination =
126130
getExpectedSymlinkDestination(pathRelativeToWorkDir, inputs);
127131
if (destination.isPresent()) {
128-
if (stat.isSymbolicLink() && path.readSymbolicLink().equals(destination.get())) {
132+
if (SYMLINK.equals(dirent.getType())
133+
&& absPath.readSymbolicLink().equals(destination.get())) {
129134
inputsToCreate.remove(pathRelativeToWorkDir);
130135
} else {
131-
path.delete();
136+
absPath.delete();
132137
}
133-
} else if (stat.isDirectory()) {
138+
} else if (DIRECTORY.equals(dirent.getType())) {
134139
if (dirsToCreate.contains(pathRelativeToWorkDir)) {
135-
cleanExisting(path, inputs, inputsToCreate, dirsToCreate);
140+
cleanExisting(absPath, inputs, inputsToCreate, dirsToCreate);
136141
dirsToCreate.remove(pathRelativeToWorkDir);
137142
} else {
138-
path.deleteTree();
143+
absPath.deleteTree();
139144
}
140145
} else if (!inputsToCreate.contains(pathRelativeToWorkDir)) {
141-
path.delete();
146+
absPath.delete();
142147
}
143148
}
144149
}

0 commit comments

Comments
 (0)