diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/CpuTimeTracker.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/CpuTimeTracker.java index d36848e..b09a4b6 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/CpuTimeTracker.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/CpuTimeTracker.java @@ -26,7 +26,8 @@ @InterfaceAudience.Private @InterfaceStability.Unstable public class CpuTimeTracker { - public static final int UNAVAILABLE = -1; + public static final int UNAVAILABLE = + ResourceCalculatorProcessTree.UNAVAILABLE; final long MINIMUM_UPDATE_INTERVAL; // CPU used time since system is on (ms) diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/ProcfsBasedProcessTree.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/ProcfsBasedProcessTree.java index 134cec2..1bd8fa4 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/ProcfsBasedProcessTree.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/ProcfsBasedProcessTree.java @@ -140,7 +140,7 @@ public static MemInfo getMemInfoByName(String name) { static private String deadPid = "-1"; private String pid = deadPid; static private Pattern numberPattern = Pattern.compile("[1-9][0-9]*"); - private Long cpuTime = 0L; + private long cpuTime = UNAVAILABLE; protected Map processTree = new HashMap(); @@ -340,51 +340,37 @@ public String getProcessTreeDump() { return ret.toString(); } - /** - * Get the cumulative virtual memory used by all the processes in the - * process-tree that are older than the passed in age. - * - * @param olderThanAge processes above this age are included in the - * memory addition - * @return cumulative virtual memory used by the process-tree in bytes, - * for processes older than this age. - */ @Override public long getCumulativeVmem(int olderThanAge) { - long total = 0; + long total = UNAVAILABLE; for (ProcessInfo p : processTree.values()) { if ((p != null) && (p.getAge() > olderThanAge)) { + if (total == UNAVAILABLE ) { + total = 0; + } total += p.getVmem(); } } return total; } - /** - * Get the cumulative resident set size (rss) memory used by all the processes - * in the process-tree that are older than the passed in age. - * - * @param olderThanAge processes above this age are included in the - * memory addition - * @return cumulative rss memory used by the process-tree in bytes, - * for processes older than this age. return 0 if it cannot be - * calculated - */ @Override public long getCumulativeRssmem(int olderThanAge) { if (PAGE_SIZE < 0) { - return 0; + return UNAVAILABLE; } if (smapsEnabled) { return getSmapBasedCumulativeRssmem(olderThanAge); } + boolean isAvailable = false; long totalPages = 0; for (ProcessInfo p : processTree.values()) { if ((p != null) && (p.getAge() > olderThanAge)) { totalPages += p.getRssmemPage(); + isAvailable = true; } } - return totalPages * PAGE_SIZE; // convert # pages to byte + return isAvailable ? totalPages * PAGE_SIZE : UNAVAILABLE; // convert # pages to byte } /** @@ -396,10 +382,11 @@ public long getCumulativeRssmem(int olderThanAge) { * @param olderThanAge * processes above this age are included in the memory addition * @return cumulative rss memory used by the process-tree in bytes, for - * processes older than this age. return 0 if it cannot be calculated + * processes older than this age. return {@link #UNAVAILABLE} if it cannot + * be calculated. */ private long getSmapBasedCumulativeRssmem(int olderThanAge) { - long total = 0; + long total = UNAVAILABLE; for (ProcessInfo p : processTree.values()) { if ((p != null) && (p.getAge() > olderThanAge)) { ProcessTreeSmapMemInfo procMemInfo = processSMAPTree.get(p.getPid()); @@ -412,6 +399,9 @@ private long getSmapBasedCumulativeRssmem(int olderThanAge) { .equalsIgnoreCase(READ_EXECUTE_WITH_SHARED_PERMISSION)) { continue; } + if (total == UNAVAILABLE){ + total = 0; + } total += Math.min(info.sharedDirty, info.pss) + info.privateDirty + info.privateClean; @@ -429,22 +419,17 @@ private long getSmapBasedCumulativeRssmem(int olderThanAge) { } } } - total = (total * KB_TO_BYTES); // convert to bytes + if (total > 0) { + total *= KB_TO_BYTES; // convert to bytes + } LOG.info("SmapBasedCumulativeRssmem (bytes) : " + total); return total; // size } - /** - * Get the CPU time in millisecond used by all the processes in the - * process-tree since the process-tree created - * - * @return cumulative CPU time in millisecond since the process-tree created - * return 0 if it cannot be calculated - */ @Override public long getCumulativeCpuTime() { if (JIFFY_LENGTH_IN_MILLIS < 0) { - return 0; + return UNAVAILABLE; } long incJiffies = 0; for (ProcessInfo p : processTree.values()) { @@ -452,6 +437,9 @@ public long getCumulativeCpuTime() { incJiffies += p.getDtime(); } } + if (incJiffies > 0 && cpuTime == UNAVAILABLE) { + cpuTime = 0L; + } cpuTime += incJiffies * JIFFY_LENGTH_IN_MILLIS; return cpuTime; } diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/ResourceCalculatorProcessTree.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/ResourceCalculatorProcessTree.java index 6ee8834..ad74ddb 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/ResourceCalculatorProcessTree.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/ResourceCalculatorProcessTree.java @@ -23,19 +23,23 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.classification.InterfaceAudience.Public; +import org.apache.hadoop.classification.InterfaceAudience.Private; import org.apache.hadoop.classification.InterfaceStability.Evolving; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; /** * Interface class to obtain process resource usage - * + * NOTE: This class should not be used by external users, but only by external + * developers to extend and include their own process-tree implementation, + * especially for platforms other than Linux and Windows. */ @Public @Evolving public abstract class ResourceCalculatorProcessTree extends Configured { static final Log LOG = LogFactory .getLog(ResourceCalculatorProcessTree.class); + public static final int UNAVAILABLE = -1; /** * Create process-tree instance with specified root process. @@ -68,7 +72,8 @@ public ResourceCalculatorProcessTree(String root) { * Get the cumulative virtual memory used by all the processes in the * process-tree. * - * @return cumulative virtual memory used by the process-tree in bytes. + * @return cumulative virtual memory used by the process-tree in bytes, + * {@link #UNAVAILABLE} if it cannot be calculated. */ public long getCumulativeVmem() { return getCumulativeVmem(0); @@ -78,8 +83,8 @@ public long getCumulativeVmem() { * Get the cumulative resident set size (rss) memory used by all the processes * in the process-tree. * - * @return cumulative rss memory used by the process-tree in bytes. return 0 - * if it cannot be calculated + * @return cumulative rss memory used by the process-tree in bytes, + * {@link #UNAVAILABLE} if it cannot be calculated. */ public long getCumulativeRssmem() { return getCumulativeRssmem(0); @@ -90,13 +95,13 @@ public long getCumulativeRssmem() { * process-tree that are older than the passed in age. * * @param olderThanAge processes above this age are included in the - * memory addition - * @return cumulative virtual memory used by the process-tree in bytes, - * for processes older than this age. return 0 if it cannot be - * calculated + * memory addition + * @return cumulative virtual memory used by the process-tree in bytes for + * processes older than the specified age, {@link #UNAVAILABLE} if it + * cannot be calculated. */ public long getCumulativeVmem(int olderThanAge) { - return 0; + return UNAVAILABLE; } /** @@ -104,24 +109,24 @@ public long getCumulativeVmem(int olderThanAge) { * in the process-tree that are older than the passed in age. * * @param olderThanAge processes above this age are included in the - * memory addition - * @return cumulative rss memory used by the process-tree in bytes, - * for processes older than this age. return 0 if it cannot be - * calculated + * memory addition + * @return cumulative rss memory used by the process-tree in bytes for + * processes older than specified age, {@link #UNAVAILABLE} if it cannot be + * calculated. */ public long getCumulativeRssmem(int olderThanAge) { - return 0; + return UNAVAILABLE; } /** * Get the CPU time in millisecond used by all the processes in the * process-tree since the process-tree was created * - * @return cumulative CPU time in millisecond since the process-tree created - * return 0 if it cannot be calculated + * @return cumulative CPU time in millisecond since the process-tree + * created, {@link #UNAVAILABLE} if it cannot be calculated. */ public long getCumulativeCpuTime() { - return 0; + return UNAVAILABLE; } /** @@ -129,11 +134,11 @@ public long getCumulativeCpuTime() { * average between samples as a ratio of overall CPU cycles similar to top. * Thus, if 2 out of 4 cores are used this should return 200.0. * - * @return percentage CPU usage since the process-tree was created - * return {@link CpuTimeTracker#UNAVAILABLE} if it cannot be calculated + * @return percentage CPU usage since the process-tree was created, + * {@link #UNAVAILABLE} if it cannot be calculated. */ public float getCpuUsagePercent() { - return -1; + return UNAVAILABLE; } /** Verify that the tree process id is same as its process group id. @@ -153,6 +158,7 @@ public float getCpuUsagePercent() { * @return ResourceCalculatorProcessTree or null if ResourceCalculatorPluginTree * is not available for this system. */ + @Private public static ResourceCalculatorProcessTree getResourceCalculatorProcessTree( String pid, Class clazz, Configuration conf) { diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/WindowsBasedProcessTree.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/WindowsBasedProcessTree.java index 5c3251f..d8f37d3 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/WindowsBasedProcessTree.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/WindowsBasedProcessTree.java @@ -45,8 +45,7 @@ } private String taskProcessId = null; - private long cpuTimeMs = 0; - private Map processTree = + private Map processTree = new HashMap(); public static boolean isAvailable() { @@ -174,9 +173,12 @@ public String getProcessTreeDump() { @Override public long getCumulativeVmem(int olderThanAge) { - long total = 0; + long total = UNAVAILABLE; for (ProcessInfo p : processTree.values()) { if ((p != null) && (p.age > olderThanAge)) { + if (total == UNAVAILABLE) { + total = 0; + } total += p.vmem; } } @@ -185,9 +187,12 @@ public long getCumulativeVmem(int olderThanAge) { @Override public long getCumulativeRssmem(int olderThanAge) { - long total = 0; + long total = UNAVAILABLE; for (ProcessInfo p : processTree.values()) { if ((p != null) && (p.age > olderThanAge)) { + if (total == UNAVAILABLE) { + total = 0; + } total += p.workingSet; } } @@ -196,7 +201,11 @@ public long getCumulativeRssmem(int olderThanAge) { @Override public long getCumulativeCpuTime() { + long cpuTimeMs = UNAVAILABLE; for (ProcessInfo p : processTree.values()) { + if (cpuTimeMs == UNAVAILABLE) { + cpuTimeMs = 0; + } cpuTimeMs += p.cpuTimeMsDelta; } return cpuTimeMs; diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/TestProcfsBasedProcessTree.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/TestProcfsBasedProcessTree.java index d62e21d..616775e 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/TestProcfsBasedProcessTree.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/TestProcfsBasedProcessTree.java @@ -19,6 +19,7 @@ package org.apache.hadoop.yarn.util; import static org.apache.hadoop.yarn.util.ProcfsBasedProcessTree.KB_TO_BYTES; +import static org.apache.hadoop.yarn.util.ResourceCalculatorProcessTree.UNAVAILABLE; import static org.junit.Assert.fail; import static org.junit.Assume.assumeTrue; @@ -662,16 +663,16 @@ private void testMemForOlderProcesses(boolean smapEnabled) throws IOException { 100 * KB_TO_BYTES * 4, processTree.getCumulativeRssmem(1)); } - // no processes older than 3 iterations, this should be 0 + // no processes older than 3 iterations Assert.assertEquals( - "Getting non-zero vmem for processes older than 3 iterations", 0L, - processTree.getCumulativeVmem(3)); + "Getting non-zero vmem for processes older than 3 iterations", + UNAVAILABLE, processTree.getCumulativeVmem(3)); Assert.assertEquals( - "Getting non-zero rssmem for processes older than 3 iterations", 0L, - processTree.getCumulativeRssmem(3)); + "Getting non-zero rssmem for processes older than 3 iterations", + UNAVAILABLE, processTree.getCumulativeRssmem(3)); Assert.assertEquals( - "Getting non-zero rssmem for processes older than 3 iterations", 0L, - processTree.getCumulativeRssmem(3)); + "Getting non-zero rssmem for processes older than 3 iterations", + UNAVAILABLE, processTree.getCumulativeRssmem(3)); } finally { FileUtil.fullyDelete(procfsRootDir); } diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/monitor/ContainerMetrics.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/monitor/ContainerMetrics.java index 1375da8..ffa72a4 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/monitor/ContainerMetrics.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/monitor/ContainerMetrics.java @@ -188,13 +188,19 @@ public synchronized void finished() { } public void recordMemoryUsage(int memoryMBs) { - this.pMemMBsStat.add(memoryMBs); + if (memoryMBs >= 0) { + this.pMemMBsStat.add(memoryMBs); + } } public void recordCpuUsage( int totalPhysicalCpuPercent, int milliVcoresUsed) { - this.cpuCoreUsagePercent.add(totalPhysicalCpuPercent); - this.milliVcoresUsed.add(milliVcoresUsed); + if (totalPhysicalCpuPercent >=0) { + this.cpuCoreUsagePercent.add(totalPhysicalCpuPercent); + } + if (milliVcoresUsed >= 0) { + this.milliVcoresUsed.add(milliVcoresUsed); + } } public void recordProcessId(String processId) {