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:
parent
d8d5825af7
commit
28d84ea0a8
|
@ -100,14 +100,14 @@ public class JVM
|
|||
try {
|
||||
classRef = Class.forName("com.sun.management.UnixOperatingSystemMXBean");
|
||||
if (classRef.isInstance(osMbean)) {
|
||||
mBeanMethod = classRef.getDeclaredMethod(mBeanMethodName,
|
||||
new Class[0]);
|
||||
mBeanMethod = classRef.getMethod(mBeanMethodName, new Class[0]);
|
||||
unixos = classRef.cast(osMbean);
|
||||
return (Long)mBeanMethod.invoke(unixos);
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
@ -164,6 +164,58 @@ public class JVM
|
|||
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.
|
||||
* If Oracle java, it will use the com.sun.management interfaces.
|
||||
|
|
|
@ -142,7 +142,7 @@ public class HBaseCommonTestingUtility {
|
|||
* @throws IOException
|
||||
*/
|
||||
boolean deleteDir(final File dir) throws IOException {
|
||||
if (dir != null && !dir.exists()) {
|
||||
if (dir == null || !dir.exists()) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
|
@ -153,4 +153,4 @@ public class HBaseCommonTestingUtility {
|
|||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -20,8 +20,7 @@
|
|||
package org.apache.hadoop.hbase;
|
||||
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.management.OperatingSystemMXBean;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
@ -89,7 +88,7 @@ public class ResourceCheckerJUnitListener extends RunListener {
|
|||
static class OpenFileDescriptorResourceAnalyzer extends ResourceChecker.ResourceAnalyzer {
|
||||
@Override
|
||||
public int getVal(Phase phase) {
|
||||
if (JVM.isUnix() == false) return 0;
|
||||
if (!JVM.isUnix()) return 0;
|
||||
JVM jvm = new JVM();
|
||||
return (int)jvm.getOpenFileDescriptorCount();
|
||||
}
|
||||
|
@ -103,12 +102,36 @@ public class ResourceCheckerJUnitListener extends RunListener {
|
|||
static class MaxFileDescriptorResourceAnalyzer extends ResourceChecker.ResourceAnalyzer {
|
||||
@Override
|
||||
public int getVal(Phase phase) {
|
||||
if (JVM.isUnix() == false) return 0;
|
||||
if (!JVM.isUnix()) return 0;
|
||||
JVM jvm = new JVM();
|
||||
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.
|
||||
|
@ -122,6 +145,9 @@ public class ResourceCheckerJUnitListener extends RunListener {
|
|||
rc.addResourceAnalyzer(new ThreadResourceAnalyzer());
|
||||
rc.addResourceAnalyzer(new OpenFileDescriptorResourceAnalyzer());
|
||||
rc.addResourceAnalyzer(new MaxFileDescriptorResourceAnalyzer());
|
||||
rc.addResourceAnalyzer(new SystemLoadAverageResourceAnalyzer());
|
||||
rc.addResourceAnalyzer(new ProcessCountResourceAnalyzer());
|
||||
rc.addResourceAnalyzer(new AvailableMemoryMBResourceAnalyzer());
|
||||
|
||||
addResourceAnalyzer(rc);
|
||||
|
||||
|
|
Loading…
Reference in New Issue