Skip to content

Commit b4a7753

Browse files
author
Drew Macrae
committed
fixup Implement extra resources for test actions
Addressed comments on bazelbuild#13996 Fixed issues in tests and built and tested with lowRISC/opentitan#16436 Signed-off-by: Drew Macrae <[email protected]>
1 parent dd7f980 commit b4a7753

File tree

7 files changed

+57
-37
lines changed

7 files changed

+57
-37
lines changed

src/main/java/com/google/devtools/build/lib/actions/BUILD

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ java_library(
128128
"//third_party:flogger",
129129
"//third_party:guava",
130130
"//third_party:jsr305",
131-
"//third_party:rxjava3",
132131
"//third_party/protobuf:protobuf_java",
133132
],
134133
)
@@ -312,7 +311,6 @@ java_library(
312311
"//third_party:flogger",
313312
"//third_party:guava",
314313
"//third_party:jsr305",
315-
"//third_party:rxjava3",
316314
],
317315
)
318316

src/main/java/com/google/devtools/build/lib/actions/ExecutionRequirements.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,27 +157,22 @@ public String parseIfMatches(String tag) throws ValidationException {
157157
/** How many extra resources an action requires for execution. */
158158
public static final ParseableRequirement RESOURCES =
159159
ParseableRequirement.create(
160-
"resources:<str>:<int>",
160+
"resources:<str>:<float>",
161161
Pattern.compile("resources:(.+:.+)"),
162162
s -> {
163163
Preconditions.checkNotNull(s);
164164

165165
int splitIndex = s.indexOf(":");
166166
String resourceCount = s.substring(splitIndex+1);
167-
int value;
167+
float value;
168168
try {
169-
value = Integer.parseInt(resourceCount);
169+
value = Float.parseFloat(resourceCount);
170170
} catch (NumberFormatException e) {
171-
return "can't be parsed as an integer";
172-
}
173-
174-
// De-and-reserialize & compare to only allow canonical integer formats.
175-
if (!Integer.toString(value).equals(resourceCount)) {
176-
return "must be in canonical format (e.g. '4' instead of '+04')";
171+
return "can't be parsed as a float";
177172
}
178173

179-
if (value < 1) {
180-
return "can't be zero or negative";
174+
if (value < 0) {
175+
return "can't be negative";
181176
}
182177

183178
return null;

src/main/java/com/google/devtools/build/lib/actions/ResourceSet.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.google.devtools.build.lib.worker.WorkerKey;
2424
import com.google.devtools.common.options.Converter;
2525
import com.google.devtools.common.options.OptionsParsingException;
26-
import io.reactivex.rxjava3.annotations.NonNull;
2726
import java.util.Iterator;
2827
import java.util.NoSuchElementException;
2928
import javax.annotation.Nullable;
@@ -58,11 +57,12 @@ public class ResourceSet implements ResourceSetOrBuilder {
5857
/** The workerKey of used worker. Null if no worker is used. */
5958
@Nullable private final WorkerKey workerKey;
6059

61-
private ResourceSet(double memoryMb, double cpuUsage, int localTestCount, @Nullable WorkerKey workerKey) {
60+
private ResourceSet(
61+
double memoryMb, double cpuUsage, int localTestCount, @Nullable WorkerKey workerKey) {
6262
this(memoryMb, cpuUsage, ImmutableMap.of(), localTestCount, workerKey);
6363
}
6464

65-
private ResourceSet(double memoryMb, double cpuUsage, @NonNull ImmutableMap<String, Float> extraResourceUsage, int localTestCount, @Nullable WorkerKey workerKey) {
65+
private ResourceSet(double memoryMb, double cpuUsage, @Nullable ImmutableMap<String, Float> extraResourceUsage, int localTestCount, @Nullable WorkerKey workerKey) {
6666
this.memoryMb = memoryMb;
6767
this.cpuUsage = cpuUsage;
6868
this.extraResourceUsage = extraResourceUsage;
@@ -102,7 +102,7 @@ public static ResourceSet createWithLocalTestCount(int localTestCount) {
102102
* represent available resources.
103103
*/
104104
public static ResourceSet create(double memoryMb, double cpuUsage, int localTestCount) {
105-
return ResourceSet.create(memoryMb, cpuUsage, ImmutableMap.of(), localTestCount, /* wolkerKey= */ null);
105+
return ResourceSet.createWithWorkerKey(memoryMb, cpuUsage, ImmutableMap.of(), localTestCount, /* wolkerKey= */ null);
106106
}
107107

108108
/**
@@ -112,7 +112,11 @@ public static ResourceSet create(double memoryMb, double cpuUsage, int localTest
112112
* ResourceSets that represent available resources.
113113
*/
114114
public static ResourceSet create(double memoryMb, double cpuUsage, ImmutableMap<String, Float> extraResourceUsage, int localTestCount) {
115-
return createWithWorkerKey(memoryMb, cpuUsage, extraResourceUseage, localTestCount, /* workerKey= */ null);
115+
return createWithWorkerKey(memoryMb, cpuUsage, extraResourceUsage, localTestCount, /* workerKey= */ null);
116+
}
117+
118+
public static ResourceSet createWithWorkerKey(double memoryMb, double cpuUsage, int localTestCount, WorkerKey workerKey) {
119+
return ResourceSet.createWithWorkerKey(memoryMb, cpuUsage, /* extraResourceUsage= */ImmutableMap.of(), localTestCount, workerKey);
116120
}
117121

118122
public static ResourceSet createWithWorkerKey(

src/main/java/com/google/devtools/build/lib/exec/ExecutionOptions.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,11 @@ public boolean shouldMaterializeParamFiles() {
334334
help =
335335
"Set the number of extra resources available to Bazel. "
336336
+ "Takes in a string-float pair. Can be used multiple times to specify multiple "
337-
+ "types of extra resources. Bazel will limit concurrently running test actions "
338-
+ "based on the available extra resources and the extra resources required "
339-
+ "by the test actions. Tests can declare the amount of extra resources they need "
337+
+ "types of extra resources. Bazel will limit concurrently running actions "
338+
+ "based on the available extra resources and the extra resources required. "
339+
+ "Tests can declare the amount of extra resources they need "
340340
+ "by using a tag of the \"resources:<resoucename>:<amount>\" format. "
341-
+ "Available CPU, RAM and test job resources cannot be set with this flag.",
341+
+ "Available CPU, RAM and resources cannot be set with this flag.",
342342
converter = Converters.StringToFloatAssignmentConverter.class)
343343
public List<Map.Entry<String, Float>> localExtraResources;
344344

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ WorkResponse execInWorkerWorkerAsResource(
407407
ResourceSet.createWithWorkerKey(
408408
spawn.getLocalResources().getMemoryMb(),
409409
spawn.getLocalResources().getCpuUsage(),
410+
spawn.getLocalResources().getExtraResourceUsage(),
410411
spawn.getLocalResources().getLocalTestCount(),
411412
key);
412413

src/main/java/com/google/devtools/common/options/Converters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ public String getTypeDescription() {
475475
/**
476476
* A converter for for assignments from a string value to a float value.
477477
*/
478-
public static class StringToFloatAssignmentConverter implements Converter<Map.Entry<String, Float>> {
478+
public static class StringToFloatAssignmentConverter extends Converter.Contextless<Map.Entry<String, Float>> {
479479
private static final AssignmentConverter baseConverter = new AssignmentConverter();
480480

481481
@Override

src/test/java/com/google/devtools/build/lib/actions/ResourceManagerTest.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,42 @@ private ResourceHandle acquire(double ram, double cpu, int tests)
114114
return acquire(ram, cpu, tests, ResourcePriority.LOCAL);
115115
}
116116

117-
private ResourceHandle acquire(double ram, double cpu, int tests, ImmutableMap<String, Float> extraResources, String mnemonic)
117+
private ResourceHandle acquire(double ram, double cpu, int tests, String mnemonic)
118118
throws InterruptedException, IOException {
119119

120120
return rm.acquireResources(
121121
resourceOwner,
122-
ResourceSet.createWithWorkerKey(ram, cpu, tests, extraResources, createWorkerKey(mnemonic)),
122+
ResourceSet.createWithWorkerKey(ram, cpu, tests, createWorkerKey(mnemonic)),
123+
ResourcePriority.LOCAL);
124+
}
125+
126+
private ResourceHandle acquire(double ram, double cpu, ImmutableMap<String, Float> extraResources, int tests, ResourcePriority priority)
127+
throws InterruptedException, IOException {
128+
return rm.acquireResources(resourceOwner, ResourceSet.create(ram, cpu, extraResources, tests), priority);
129+
}
130+
131+
private ResourceHandle acquire(double ram, double cpu, ImmutableMap<String, Float> extraResources, int tests)
132+
throws InterruptedException, IOException {
133+
return acquire(ram, cpu, extraResources, tests, ResourcePriority.LOCAL);
134+
}
135+
136+
private ResourceHandle acquire(double ram, double cpu, ImmutableMap<String, Float> extraResources, int tests, String mnemonic)
137+
throws InterruptedException, IOException {
138+
139+
return rm.acquireResources(
140+
resourceOwner,
141+
ResourceSet.createWithWorkerKey(ram, cpu, extraResources, tests, createWorkerKey(mnemonic)),
123142
ResourcePriority.LOCAL);
124143
}
125144

126145
private void release(double ram, double cpu, int tests) throws IOException, InterruptedException {
127-
rm.releaseResources(resourceOwner, ResourceSet.create(ram, cpu, tests), /* worker=*/ null);
146+
rm.releaseResources(resourceOwner, ResourceSet.create(ram, cpu, tests), /* worker= */ null);
128147
}
129148

130-
private void release(double ram, double cpu, int tests, ImmutableMap<String, Float> extraResources) {
131-
rm.releaseResources(resourceOwner, ResourceSet.create(ram, cpu, extraResources, tests));
149+
private void release(double ram, double cpu, ImmutableMap<String, Float> extraResources, int tests)
150+
throws InterruptedException, IOException {
151+
rm.releaseResources(resourceOwner, ResourceSet.create(ram, cpu, extraResources, tests),
152+
/* worker= */ null);
132153
}
133154

134155
private void validate(int count) {
@@ -181,9 +202,9 @@ public void testOverBudgetRequests() throws Exception {
181202

182203
// Ditto, for extra resources (even if they don't exist in the available resource set):
183204
ImmutableMap<String, Float> bigExtraResources = ImmutableMap.of("gpu", 10.0f, "fancyresource", 10.0f, "nonexisting", 10.0f);
184-
acquire(0, 0, 0, bigExtraResources);
205+
acquire(0, 0, bigExtraResources, 0);
185206
assertThat(rm.inUse()).isTrue();
186-
release(0, 0, 0, bigExtraResources);
207+
release(0, 0, bigExtraResources, 0);
187208
assertThat(rm.inUse()).isFalse();
188209
}
189210

@@ -271,20 +292,21 @@ public void testThatExtraResourcesCannotBeOverallocated() throws Exception {
271292
assertThat(rm.inUse()).isFalse();
272293

273294
// Given a partially acquired extra resources:
274-
acquire(0, 0, 1, ImmutableMap.of("gpu", 1.0f));
295+
acquire(0, 0, ImmutableMap.of("gpu", 1.0f), 1);
275296

276297
// When a request for extra resources is made that would overallocate,
277298
// Then the request fails:
278-
TestThread thread1 = new TestThread(() -> assertThat(acquireNonblocking(0, 0, 0, ImmutableMap.of("gpu", 1.1f))).isNull());
299+
TestThread thread1 = new TestThread(() -> acquire(0, 0, ImmutableMap.of("gpu", 1.1f), 0));
279300
thread1.start();
280-
thread1.joinAndAssertState(10000);
301+
AssertionError e = assertThrows(AssertionError.class, () -> thread1.joinAndAssertState(10000));
302+
assertThat(e).hasCauseThat().hasMessageThat().contains("is still alive");
281303
}
282304

283305
@Test
284306
public void testHasResources() throws Exception {
285307
assertThat(rm.inUse()).isFalse();
286308
assertThat(rm.threadHasResources()).isFalse();
287-
acquire(1, 0.1, 1, ImmutableMap.of("gpu", 1.0f));
309+
acquire(1, 0.1, ImmutableMap.of("gpu", 1.0f), 1);
288310
assertThat(rm.threadHasResources()).isTrue();
289311

290312
// We have resources in this thread - make sure other threads
@@ -305,15 +327,15 @@ public void testHasResources() throws Exception {
305327
assertThat(rm.threadHasResources()).isTrue();
306328
release(0, 0, 1);
307329
assertThat(rm.threadHasResources()).isFalse();
308-
acquire(0, 0, 0, ImmutableMap.of("gpu", 1.0f));
330+
acquire(0, 0, ImmutableMap.of("gpu", 1.0f), 0);
309331
assertThat(rm.threadHasResources()).isTrue();
310-
release(0, 0, 0, ImmutableMap.of("gpu", 1.0f));
332+
release(0, 0, ImmutableMap.of("gpu", 1.0f), 0);
311333
assertThat(rm.threadHasResources()).isFalse();
312334
});
313335
thread1.start();
314336
thread1.joinAndAssertState(10000);
315337

316-
release(1, 0.1, 1, ImmutableMap.of("gpu", 1.0f));
338+
release(1, 0.1, ImmutableMap.of("gpu", 1.0f), 1);
317339
assertThat(rm.threadHasResources()).isFalse();
318340
assertThat(rm.inUse()).isFalse();
319341
}

0 commit comments

Comments
 (0)