HBASE-8267 Add some resources checker for tests

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1464918 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
nkeywal 2013-04-05 10:42:35 +00:00
parent d8d5825af7
commit 28d84ea0a8
3 changed files with 87 additions and 9 deletions

View File

@ -100,14 +100,14 @@ public class JVM
try { try {
classRef = Class.forName("com.sun.management.UnixOperatingSystemMXBean"); classRef = Class.forName("com.sun.management.UnixOperatingSystemMXBean");
if (classRef.isInstance(osMbean)) { if (classRef.isInstance(osMbean)) {
mBeanMethod = classRef.getDeclaredMethod(mBeanMethodName, mBeanMethod = classRef.getMethod(mBeanMethodName, new Class[0]);
new Class[0]);
unixos = classRef.cast(osMbean); unixos = classRef.cast(osMbean);
return (Long)mBeanMethod.invoke(unixos); return (Long)mBeanMethod.invoke(unixos);
} }
} }
catch(Exception e) { catch(Exception e) {
LOG.warn("Not able to load class or method for com.sun.managment.UnixOperatingSystemMXBean.", e); LOG.warn("Not able to load class or method for" +
" com.sun.management.UnixOperatingSystemMXBean.", e);
} }
return null; return null;
} }
@ -164,6 +164,58 @@ public class JVM
return -1; return -1;
} }
/**
* @see java.lang.management.OperatingSystemMXBean#getSystemLoadAverage
*/
public double getSystemLoadAverage() {
return osMbean.getSystemLoadAverage();
}
/**
* @return the physical free memory (not the JVM one, as it's not very useful as it depends on
* the GC), but the one from the OS as it allows a little bit more to guess if the machine is
* overloaded or not).
*/
public long getFreeMemory() {
if (ibmvendor){
return 0;
}
Long r = runUnixMXBeanMethod("getFreePhysicalMemorySize");
return (r != null ? r : -1);
}
/**
* Workaround to get the current number of process running. Approach is the one described here:
* http://stackoverflow.com/questions/54686/how-to-get-a-list-of-current-open-windows-process-with-java
*/
public int getNumberOfRunningProcess(){
if (!isUnix()){
return 0;
}
BufferedReader input = null;
try {
int count = 0;
Process p = Runtime.getRuntime().exec("ps -e");
input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (input.readLine() != null) {
count++;
}
return count - 1; // -1 because there is a headline
} catch (IOException e) {
return -1;
} finally {
if (input != null){
try {
input.close();
} catch (IOException ignored) {
}
}
}
}
/** /**
* Get the number of the maximum file descriptors the system can use. * Get the number of the maximum file descriptors the system can use.
* If Oracle java, it will use the com.sun.management interfaces. * If Oracle java, it will use the com.sun.management interfaces.

View File

@ -142,7 +142,7 @@ public class HBaseCommonTestingUtility {
* @throws IOException * @throws IOException
*/ */
boolean deleteDir(final File dir) throws IOException { boolean deleteDir(final File dir) throws IOException {
if (dir != null && !dir.exists()) { if (dir == null || !dir.exists()) {
return true; return true;
} }
try { try {
@ -153,4 +153,4 @@ public class HBaseCommonTestingUtility {
return false; return false;
} }
} }
}; }

View File

@ -20,8 +20,7 @@
package org.apache.hadoop.hbase; package org.apache.hadoop.hbase;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -89,7 +88,7 @@ public class ResourceCheckerJUnitListener extends RunListener {
static class OpenFileDescriptorResourceAnalyzer extends ResourceChecker.ResourceAnalyzer { static class OpenFileDescriptorResourceAnalyzer extends ResourceChecker.ResourceAnalyzer {
@Override @Override
public int getVal(Phase phase) { public int getVal(Phase phase) {
if (JVM.isUnix() == false) return 0; if (!JVM.isUnix()) return 0;
JVM jvm = new JVM(); JVM jvm = new JVM();
return (int)jvm.getOpenFileDescriptorCount(); return (int)jvm.getOpenFileDescriptorCount();
} }
@ -103,12 +102,36 @@ public class ResourceCheckerJUnitListener extends RunListener {
static class MaxFileDescriptorResourceAnalyzer extends ResourceChecker.ResourceAnalyzer { static class MaxFileDescriptorResourceAnalyzer extends ResourceChecker.ResourceAnalyzer {
@Override @Override
public int getVal(Phase phase) { public int getVal(Phase phase) {
if (JVM.isUnix() == false) return 0; if (!JVM.isUnix()) return 0;
JVM jvm = new JVM(); JVM jvm = new JVM();
return (int)jvm.getMaxFileDescriptorCount(); return (int)jvm.getMaxFileDescriptorCount();
} }
} }
static class SystemLoadAverageResourceAnalyzer extends ResourceChecker.ResourceAnalyzer {
@Override
public int getVal(Phase phase) {
if (!JVM.isUnix()) return 0;
return (int)(new JVM().getSystemLoadAverage()*100);
}
}
static class ProcessCountResourceAnalyzer extends ResourceChecker.ResourceAnalyzer {
@Override
public int getVal(Phase phase) {
if (!JVM.isUnix()) return 0;
return new JVM().getNumberOfRunningProcess();
}
}
static class AvailableMemoryMBResourceAnalyzer extends ResourceChecker.ResourceAnalyzer {
@Override
public int getVal(Phase phase) {
if (!JVM.isUnix()) return 0;
return (int) (new JVM().getFreeMemory() / (1024L * 1024L));
}
}
/** /**
* To be implemented by sub classes if they want to add specific ResourceAnalyzer. * To be implemented by sub classes if they want to add specific ResourceAnalyzer.
@ -122,6 +145,9 @@ public class ResourceCheckerJUnitListener extends RunListener {
rc.addResourceAnalyzer(new ThreadResourceAnalyzer()); rc.addResourceAnalyzer(new ThreadResourceAnalyzer());
rc.addResourceAnalyzer(new OpenFileDescriptorResourceAnalyzer()); rc.addResourceAnalyzer(new OpenFileDescriptorResourceAnalyzer());
rc.addResourceAnalyzer(new MaxFileDescriptorResourceAnalyzer()); rc.addResourceAnalyzer(new MaxFileDescriptorResourceAnalyzer());
rc.addResourceAnalyzer(new SystemLoadAverageResourceAnalyzer());
rc.addResourceAnalyzer(new ProcessCountResourceAnalyzer());
rc.addResourceAnalyzer(new AvailableMemoryMBResourceAnalyzer());
addResourceAnalyzer(rc); addResourceAnalyzer(rc);