aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/actions
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/actions')
-rw-r--r--src/main/java/com/google/devtools/build/lib/actions/LocalHostCapacity.java8
-rw-r--r--src/main/java/com/google/devtools/build/lib/actions/ResourceManager.java72
-rw-r--r--src/main/java/com/google/devtools/build/lib/actions/ResourceSet.java46
3 files changed, 88 insertions, 38 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/actions/LocalHostCapacity.java b/src/main/java/com/google/devtools/build/lib/actions/LocalHostCapacity.java
index 7f375d4c25..7255b5d49e 100644
--- a/src/main/java/com/google/devtools/build/lib/actions/LocalHostCapacity.java
+++ b/src/main/java/com/google/devtools/build/lib/actions/LocalHostCapacity.java
@@ -126,7 +126,8 @@ public final class LocalHostCapacity {
static boolean isDisabled;
// If /proc/* information is not available, assume 3000 MB and 2 CPUs.
- private static ResourceSet DEFAULT_RESOURCES = ResourceSet.createWithRamCpuIo(3000.0, 2.0, 1.0);
+ private static ResourceSet DEFAULT_RESOURCES = ResourceSet.create(3000.0, 2.0, 1.0,
+ Integer.MAX_VALUE);
private LocalHostCapacity() {}
@@ -252,10 +253,11 @@ public final class LocalHostCapacity {
boolean hyperthreading = (logicalCpuCount != totalCores);
double ramMb = ProcMeminfoParser.kbToMb(memInfo.getTotalKb());
final double EFFECTIVE_CPUS_PER_HYPERTHREADED_CPU = 0.6;
- return ResourceSet.createWithRamCpuIo(
+ return ResourceSet.create(
ramMb,
logicalCpuCount * (hyperthreading ? EFFECTIVE_CPUS_PER_HYPERTHREADED_CPU : 1.0),
- 1.0);
+ 1.0,
+ Integer.MAX_VALUE);
} catch (IOException | IllegalArgumentException e) {
disableProcFsUse(e);
return DEFAULT_RESOURCES;
diff --git a/src/main/java/com/google/devtools/build/lib/actions/ResourceManager.java b/src/main/java/com/google/devtools/build/lib/actions/ResourceManager.java
index bb2aea9fcc..db6ed118f6 100644
--- a/src/main/java/com/google/devtools/build/lib/actions/ResourceManager.java
+++ b/src/main/java/com/google/devtools/build/lib/actions/ResourceManager.java
@@ -33,14 +33,14 @@ import java.util.logging.Level;
import java.util.logging.Logger;
/**
- * CPU/RAM resource manager. Used to keep track of resources consumed by the Blaze action execution
- * threads and throttle them when necessary.
+ * Used to keep track of resources consumed by the Blaze action execution threads and throttle them
+ * when necessary.
*
- * <p>Threads which are known to consume a significant amount of the local CPU or RAM resources
- * should call {@link #acquireResources} method. This method will check whether requested resources
- * are available and will either mark them as used and allow thread to proceed or will block the
- * thread until requested resources will become available. When thread completes it task, it must
- * release allocated resources by calling {@link #releaseResources} method.
+ * <p>Threads which are known to consume a significant amount of resources should call
+ * {@link #acquireResources} method. This method will check whether requested resources are
+ * available and will either mark them as used and allow the thread to proceed or will block the
+ * thread until requested resources will become available. When the thread completes its task, it
+ * must release allocated resources by calling {@link #releaseResources} method.
*
* <p>Available resources can be calculated using one of three ways:
* <ol>
@@ -100,9 +100,9 @@ public class ResourceManager {
// Please note that this value is purely empirical - we assume that generally
// requested resources are somewhat pessimistic and thread would end up
// using less than requested amount.
- private final static double MIN_NECESSARY_CPU_RATIO = 0.6;
- private final static double MIN_NECESSARY_RAM_RATIO = 1.0;
- private final static double MIN_NECESSARY_IO_RATIO = 1.0;
+ private static final double MIN_NECESSARY_CPU_RATIO = 0.6;
+ private static final double MIN_NECESSARY_RAM_RATIO = 1.0;
+ private static final double MIN_NECESSARY_IO_RATIO = 1.0;
// List of blocked threads. Associated CountDownLatch object will always
// be initialized to 1 during creation in the acquire() method.
@@ -129,6 +129,9 @@ public class ResourceManager {
// definition in the ResourceSet class.
private double usedIo;
+ // Used local test count. Corresponds to the local test count definition in the ResourceSet class.
+ private int usedLocalTestCount;
+
// Specifies how much of the RAM in staticResources we should allow to be used.
public static final int DEFAULT_RAM_UTILIZATION_PERCENTAGE = 67;
private int ramUtilizationPercentage = DEFAULT_RAM_UTILIZATION_PERCENTAGE;
@@ -138,7 +141,7 @@ public class ResourceManager {
private ResourceManager() {
FINE = LOG.isLoggable(Level.FINE);
- requestList = new LinkedList<Pair<ResourceSet, CountDownLatch>>();
+ requestList = new LinkedList<>();
}
@VisibleForTesting public static ResourceManager instanceForTestingOnly() {
@@ -154,6 +157,7 @@ public class ResourceManager {
usedCpu = 0;
usedRam = 0;
usedIo = 0;
+ usedLocalTestCount = 0;
for (Pair<ResourceSet, CountDownLatch> request : requestList) {
// CountDownLatch can be set only to 0 or 1.
request.second.countDown();
@@ -220,7 +224,7 @@ public class ResourceManager {
*/
public void acquireResources(ActionMetadata owner, ResourceSet resources)
throws InterruptedException {
- Preconditions.checkArgument(resources != null);
+ Preconditions.checkNotNull(resources);
long startTime = Profiler.nanoTimeMaybe();
CountDownLatch latch = null;
try {
@@ -231,7 +235,7 @@ public class ResourceManager {
}
} finally {
threadLocked.set(resources.getCpuUsage() != 0 || resources.getMemoryMb() != 0
- || resources.getIoUsage() != 0);
+ || resources.getIoUsage() != 0 || resources.getLocalTestCount() != 0);
acquired(owner);
// Profile acquisition only if it waited for resource to become available.
@@ -255,7 +259,8 @@ public class ResourceManager {
}
if (acquired) {
- threadLocked.set(resources.getCpuUsage() != 0 || resources.getMemoryMb() != 0);
+ threadLocked.set(resources.getCpuUsage() != 0 || resources.getMemoryMb() != 0
+ || resources.getIoUsage() != 0 || resources.getLocalTestCount() != 0);
acquired(owner);
}
@@ -266,13 +271,15 @@ public class ResourceManager {
usedCpu += resources.getCpuUsage();
usedRam += resources.getMemoryMb();
usedIo += resources.getIoUsage();
+ usedLocalTestCount += resources.getLocalTestCount();
}
/**
* Return true if any resources have been claimed through this manager.
*/
public synchronized boolean inUse() {
- return usedCpu != 0.0 || usedRam != 0.0 || usedIo != 0.0 || requestList.size() > 0;
+ return usedCpu != 0.0 || usedRam != 0.0 || usedIo != 0.0 || usedLocalTestCount != 0
+ || requestList.size() > 0;
}
@@ -353,16 +360,18 @@ public class ResourceManager {
usedCpu -= resources.getCpuUsage();
usedRam -= resources.getMemoryMb();
usedIo -= resources.getIoUsage();
+ usedLocalTestCount -= resources.getLocalTestCount();
// TODO(bazel-team): (2010) rounding error can accumulate and value below can end up being
// e.g. 1E-15. So if it is small enough, we set it to 0. But maybe there is a better solution.
- if (usedCpu < 0.0001) {
+ double epsilon = 0.0001;
+ if (usedCpu < epsilon) {
usedCpu = 0;
}
- if (usedRam < 0.0001) {
+ if (usedRam < epsilon) {
usedRam = 0;
}
- if (usedIo < 0.0001) {
+ if (usedIo < epsilon) {
usedIo = 0;
}
if (requestList.size() > 0) {
@@ -393,7 +402,7 @@ public class ResourceManager {
Preconditions.checkNotNull(availableResources);
// Comparison below is robust, since any calculation errors will be fixed
// by the release() method.
- if (usedCpu == 0.0 && usedRam == 0.0 && usedIo == 0.0) {
+ if (usedCpu == 0.0 && usedRam == 0.0 && usedIo == 0.0 && usedLocalTestCount == 0) {
return true;
}
// Use only MIN_NECESSARY_???_RATIO of the resource value to check for
@@ -404,10 +413,12 @@ public class ResourceManager {
double cpu = resources.getCpuUsage() * MIN_NECESSARY_CPU_RATIO;
double ram = resources.getMemoryMb() * MIN_NECESSARY_RAM_RATIO;
double io = resources.getIoUsage() * MIN_NECESSARY_IO_RATIO;
+ int localTestCount = resources.getLocalTestCount();
double availableCpu = availableResources.getCpuUsage();
double availableRam = availableResources.getMemoryMb();
double availableIo = availableResources.getIoUsage();
+ int availableLocalTestCount = availableResources.getLocalTestCount();
// Resources are considered available if any one of the conditions below is true:
// 1) If resource is not requested at all, it is available.
@@ -416,28 +427,33 @@ public class ResourceManager {
// ensure that at any given time, at least one thread is able to acquire
// resources even if it requests more than available.
// 3) If used resource amount is less than total available resource amount.
- return (cpu == 0.0 || usedCpu == 0.0 || usedCpu + cpu <= availableCpu) &&
- (ram == 0.0 || usedRam == 0.0 || usedRam + ram <= availableRam) &&
- (io == 0.0 || usedIo == 0.0 || usedIo + io <= availableIo);
+ boolean cpuIsAvailable = cpu == 0.0 || usedCpu == 0.0 || usedCpu + cpu <= availableCpu;
+ boolean ramIsAvailable = ram == 0.0 || usedRam == 0.0 || usedRam + ram <= availableRam;
+ boolean ioIsAvailable = io == 0.0 || usedIo == 0.0 || usedIo + io <= availableIo;
+ boolean localTestCountIsAvailable = localTestCount == 0 || usedLocalTestCount == 0
+ || usedLocalTestCount + localTestCount <= availableLocalTestCount;
+ return cpuIsAvailable && ramIsAvailable && ioIsAvailable && localTestCountIsAvailable;
}
private synchronized void updateAvailableResources(boolean useFreeReading) {
Preconditions.checkNotNull(staticResources);
if (useFreeReading && isAutoSensingEnabled()) {
- availableResources = ResourceSet.createWithRamCpuIo(
+ availableResources = ResourceSet.create(
usedRam + freeReading.getFreeMb(),
usedCpu + freeReading.getAvgFreeCpu(),
- staticResources.getIoUsage());
+ staticResources.getIoUsage(),
+ staticResources.getLocalTestCount());
if(FINE) {
LOG.fine("Free resources: " + Math.round(freeReading.getFreeMb()) + " MB,"
+ Math.round(freeReading.getAvgFreeCpu() * 100) + "% CPU");
}
processWaitingThreads();
} else {
- availableResources = ResourceSet.createWithRamCpuIo(
+ availableResources = ResourceSet.create(
staticResources.getMemoryMb() * this.ramUtilizationPercentage / 100.0,
staticResources.getCpuUsage(),
- staticResources.getIoUsage());
+ staticResources.getIoUsage(),
+ staticResources.getLocalTestCount());
processWaitingThreads();
}
}
@@ -466,7 +482,7 @@ public class ResourceManager {
}
@VisibleForTesting
- synchronized boolean isAvailable(double ram, double cpu, double io) {
- return areResourcesAvailable(ResourceSet.createWithRamCpuIo(ram, cpu, io));
+ synchronized boolean isAvailable(double ram, double cpu, double io, int localTestCount) {
+ return areResourcesAvailable(ResourceSet.create(ram, cpu, io, localTestCount));
}
}
diff --git a/src/main/java/com/google/devtools/build/lib/actions/ResourceSet.java b/src/main/java/com/google/devtools/build/lib/actions/ResourceSet.java
index 65d2d1453f..f4f1327bb1 100644
--- a/src/main/java/com/google/devtools/build/lib/actions/ResourceSet.java
+++ b/src/main/java/com/google/devtools/build/lib/actions/ResourceSet.java
@@ -33,7 +33,7 @@ import java.util.NoSuchElementException;
public class ResourceSet {
/** For actions that consume negligible resources. */
- public static final ResourceSet ZERO = new ResourceSet(0.0, 0.0, 0.0);
+ public static final ResourceSet ZERO = new ResourceSet(0.0, 0.0, 0.0, 0);
/** The amount of real memory (resident set size). */
private final double memoryMb;
@@ -41,20 +41,49 @@ public class ResourceSet {
/** The number of CPUs, or fractions thereof. */
private final double cpuUsage;
+ /** The number of local tests. */
+ private final int localTestCount;
+
/**
* Relative amount of used I/O resources (with 1.0 being total available amount on an "average"
* workstation.
*/
private final double ioUsage;
- private ResourceSet(double memoryMb, double cpuUsage, double ioUsage) {
+ private ResourceSet(double memoryMb, double cpuUsage, double ioUsage, int localTestCount) {
this.memoryMb = memoryMb;
this.cpuUsage = cpuUsage;
this.ioUsage = ioUsage;
+ this.localTestCount = localTestCount;
}
+ /**
+ * Returns a new ResourceSet with the provided values for memoryMb, cpuUsage, and ioUsage, and
+ * with 0.0 for localTestCount. Use this method in action resource definitions when they aren't
+ * local tests.
+ */
public static ResourceSet createWithRamCpuIo(double memoryMb, double cpuUsage, double ioUsage) {
- return new ResourceSet(memoryMb, cpuUsage, ioUsage);
+ return new ResourceSet(memoryMb, cpuUsage, ioUsage, 0);
+ }
+
+ /**
+ * Returns a new ResourceSet with the provided value for localTestCount, and 0.0 for memoryMb,
+ * cpuUsage, and ioUsage. Use this method in action resource definitions when they are local tests
+ * that acquire no local resources.
+ */
+ public static ResourceSet createWithLocalTestCount(int localTestCount) {
+ return new ResourceSet(0.0, 0.0, 0.0, localTestCount);
+ }
+
+ /**
+ * Returns a new ResourceSet with the provided values for memoryMb, cpuUsage, ioUsage, and
+ * localTestCount. Most action resource definitions should use
+ * {@link #createWithRamCpuIo(double, double, double)} or {@link #createWithLocalTestCount(int)}.
+ * Use this method primarily when constructing ResourceSets that represent available resources.
+ */
+ public static ResourceSet create(double memoryMb, double cpuUsage, double ioUsage,
+ int localTestCount) {
+ return new ResourceSet(memoryMb, cpuUsage, ioUsage, localTestCount);
}
/** Returns the amount of real memory (resident set size) used in MB. */
@@ -83,6 +112,11 @@ public class ResourceSet {
return ioUsage;
}
+ /** Returns the local test count used. */
+ public int getLocalTestCount() {
+ return localTestCount;
+ }
+
public static class ResourceSetConverter implements Converter<ResourceSet> {
private static final Splitter SPLITTER = Splitter.on(',');
@@ -99,11 +133,9 @@ public class ResourceSet {
if (memoryMb <= 0.0 || cpuUsage <= 0.0 || ioUsage <= 0.0) {
throw new OptionsParsingException("All resource values must be positive");
}
- return createWithRamCpuIo(memoryMb, cpuUsage, ioUsage);
- } catch (NumberFormatException nfe) {
+ return create(memoryMb, cpuUsage, ioUsage, Integer.MAX_VALUE);
+ } catch (NumberFormatException | NoSuchElementException nfe) {
throw new OptionsParsingException("Expected exactly 3 comma-separated float values", nfe);
- } catch (NoSuchElementException nsee) {
- throw new OptionsParsingException("Expected exactly 3 comma-separated float values", nsee);
}
}