Skip to content

Commit

Permalink
Push the Linux implementation detail of user/system time being record…
Browse files Browse the repository at this point in the history
…ed in jiffies as far in as possible.

Also tests properly for whether /proc/self/stat can be read rather than throwing an exception.

PiperOrigin-RevId: 511169673
Change-Id: I098005ceca78fa2c2074adb5ce611bd588ec52c3
  • Loading branch information
larsrc-google authored and copybara-github committed Feb 21, 2023
1 parent 47fe4fb commit 9b26a11
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/main/java/com/google/devtools/build/lib/util/ResourceUsage.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,27 +123,29 @@ public static Measurement measureCurrentResourceUsage() {
readPressureStallIndicator("memory"),
readPressureStallIndicator("io"),
getAvailableMemory(),
getCurrentCpuUtilizationInJiffies());
getCurrentCpuUtilizationInMs());
}

/**
* Returns the current cpu utilization of the current process with the given id in jiffies. The
* returned array contains the following information: The 1st entry is the number of jiffies that
* the process has executed in user mode, and the 2nd entry is the number of jiffies that the
* process has executed in kernel mode. Reads /proc/self/stat to obtain this information.
* Returns the current cpu utilization of the current process with the given id in ms. The
* returned array contains the following information: The 1st entry is the number of ms that the
* process has executed in user mode, and the 2nd entry is the number of ms that the process has
* executed in kernel mode. Reads /proc/self/stat to obtain this information. The values may not
* have millisecond accuracy.
*/
private static long[] getCurrentCpuUtilizationInJiffies() {
private static long[] getCurrentCpuUtilizationInMs() {
try {
File file = new File("/proc/self/stat");
if (file.isDirectory()) {
if (file.isDirectory() || !file.canRead()) {
return new long[2];
}
List<String> stat =
WHITESPACE_SPLITTER.splitToList(Files.asCharSource(file, US_ASCII).read());
if (stat.size() < 15) {
return new long[2]; // Tolerate malformed input.
}
return new long[] {Long.parseLong(stat.get(13)), Long.parseLong(stat.get(14))};
// /proc/self/stat contains values in jiffies, which are 10 ms.
return new long[] {Long.parseLong(stat.get(13)) * 10, Long.parseLong(stat.get(14)) * 10};
} catch (NumberFormatException | IOException e) {
return new long[2];
}
Expand Down Expand Up @@ -206,7 +208,7 @@ public static final class Measurement {
private final float memoryPressureLast10Sec;
private final float ioPressureLast10Sec;
private final long freePhysicalMemory;
private final long[] cpuUtilizationInJiffies;
private final long[] cpuUtilizationInMs;

public Measurement(
long heapMemoryUsed,
Expand All @@ -217,7 +219,7 @@ public Measurement(
float memoryPressureLast10Sec1,
float ioPressureLast10Sec1,
long freePhysicalMemory,
long[] cpuUtilizationInJiffies) {
long[] cpuUtilizationInMs) {
super();
timeInNanos = System.nanoTime();
this.heapMemoryUsed = heapMemoryUsed;
Expand All @@ -228,7 +230,7 @@ public Measurement(
this.memoryPressureLast10Sec = memoryPressureLast10Sec1;
this.ioPressureLast10Sec = ioPressureLast10Sec1;
this.freePhysicalMemory = freePhysicalMemory;
this.cpuUtilizationInJiffies = cpuUtilizationInJiffies;
this.cpuUtilizationInMs = cpuUtilizationInMs;
}

/** Returns the time of the measurement in ms. */
Expand Down Expand Up @@ -311,7 +313,7 @@ public long getFreePhysicalMemory() {
* mode. Reads /proc/self/stat to obtain this information.
*/
public long[] getCpuUtilizationInMs() {
return new long[] {cpuUtilizationInJiffies[0] * 10, cpuUtilizationInJiffies[1] * 10};
return new long[] {cpuUtilizationInMs[0], cpuUtilizationInMs[1]};
}
}
}

0 comments on commit 9b26a11

Please sign in to comment.