Author: jm
Date: 2012-10-25 14:51:46 -0700 (Thu, 25 Oct 2012)
New Revision: 30709
Added:
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/SystemDetails.java
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/SystemDetailsImpl.java
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/SystemInformation.java
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/AbstractHeuristic.java
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/Heuristic.java
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/ProcHeuristic.java
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/Sw_VersHeuristic.java
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/SysctlHeuristic.java
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/UnameHeuristic.java
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/Vm_StatHeuristic.java
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/WmicHeuristic.java
Modified:
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/Activator.java
Log:
References #1529: Added cross-platform utilities for obtaining information
about a running system's OS, CPU, memory, and swap configuration.
Added:
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/SystemDetails.java
===================================================================
---
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/SystemDetails.java
(rev 0)
+++
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/SystemDetails.java
2012-10-25 21:51:46 UTC (rev 30709)
@@ -0,0 +1,17 @@
+package org.cytoscape.diagnostics;
+
+public interface SystemDetails {
+
+ Long getFreeSwap();
+
+ Long getTotalSwap();
+
+ Long getFreeMemory();
+
+ Long getTotalMemory();
+
+ String getProcessorName();
+
+ String getOSName();
+
+}
Modified:
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/Activator.java
===================================================================
---
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/Activator.java
2012-10-25 19:45:33 UTC (rev 30708)
+++
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/Activator.java
2012-10-25 21:51:46 UTC (rev 30709)
@@ -3,6 +3,7 @@
import java.util.Properties;
import org.cytoscape.diagnostics.PerformanceDetails;
+import org.cytoscape.diagnostics.SystemDetails;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
@@ -16,12 +17,15 @@
public void start(BundleContext context) throws Exception {
applyStartLevelHack(context);
- PerformanceDetailsBuilder details = new
PerformanceDetailsBuilder();
- context.registerService(PerformanceDetails.class.getName(),
details, new Properties());
+ PerformanceDetailsBuilder performanceDetails = new
PerformanceDetailsBuilder();
+ context.registerService(PerformanceDetails.class.getName(),
performanceDetails, new Properties());
- PerformanceTracker performanceTracker = new
PerformanceTracker(context, details);
+ PerformanceTracker performanceTracker = new
PerformanceTracker(context, performanceDetails);
context.addFrameworkListener(performanceTracker);
context.addBundleListener(performanceTracker);
+
+ SystemDetailsImpl systemDetails = new SystemDetailsImpl();
+ context.registerService(SystemDetails.class.getName(),
systemDetails, new Properties());
}
private void applyStartLevelHack(BundleContext context) {
Added:
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/SystemDetailsImpl.java
===================================================================
---
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/SystemDetailsImpl.java
(rev 0)
+++
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/SystemDetailsImpl.java
2012-10-25 21:51:46 UTC (rev 30709)
@@ -0,0 +1,119 @@
+package org.cytoscape.diagnostics.internal;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.cytoscape.diagnostics.SystemDetails;
+import org.cytoscape.diagnostics.internal.heuristics.Heuristic;
+import org.cytoscape.diagnostics.internal.heuristics.ProcHeuristic;
+import org.cytoscape.diagnostics.internal.heuristics.Sw_VersHeuristic;
+import org.cytoscape.diagnostics.internal.heuristics.SysctlHeuristic;
+import org.cytoscape.diagnostics.internal.heuristics.UnameHeuristic;
+import org.cytoscape.diagnostics.internal.heuristics.Vm_StatHeuristic;
+import org.cytoscape.diagnostics.internal.heuristics.WmicHeuristic;
+
+public class SystemDetailsImpl implements SystemDetails {
+ Map<Class<? extends Heuristic<?>>, Heuristic<?>> singletonCache;
+
+ public SystemDetailsImpl() {
+ singletonCache = new HashMap<Class<? extends Heuristic<?>>,
Heuristic<?>>();
+ }
+
+ public static void main(String[] args) {
+ SystemDetails details = new SystemDetailsImpl();
+ System.out.println(details.getOSName());
+ System.out.println(details.getProcessorName());
+ System.out.println(details.getTotalMemory());
+ System.out.println(details.getFreeMemory());
+ System.out.println(details.getTotalSwap());
+ System.out.println(details.getFreeSwap());
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Long getFreeSwap() {
+ return tryHeuristics(
+ WmicHeuristic.FreeSwap.class,
+ SysctlHeuristic.FreeSwap.class,
+ ProcHeuristic.FreeSwap.class
+ );
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Long getTotalSwap() {
+ return tryHeuristics(
+ WmicHeuristic.TotalSwap.class,
+ SysctlHeuristic.TotalSwap.class,
+ ProcHeuristic.TotalSwap.class
+ );
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Long getFreeMemory() {
+ return tryHeuristics(
+ WmicHeuristic.FreeMemory.class,
+ Vm_StatHeuristic.FreeMemory.class,
+ ProcHeuristic.FreeMemory.class
+ );
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Long getTotalMemory() {
+ return tryHeuristics(
+ WmicHeuristic.TotalMemory.class,
+ SysctlHeuristic.TotalMemory.class,
+ ProcHeuristic.TotalMemory.class
+ );
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public String getProcessorName() {
+ return tryHeuristics(
+ WmicHeuristic.ProcessorName.class,
+ SysctlHeuristic.ProcessorName.class,
+ ProcHeuristic.ProcessorName.class
+ );
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public String getOSName() {
+ return tryHeuristics(
+ WmicHeuristic.OSName.class,
+ Sw_VersHeuristic.class,
+ UnameHeuristic.class
+ );
+ }
+
+ @SuppressWarnings("unchecked")
+ private <T> T tryHeuristics(Class<? extends
Heuristic<T>>...heuristicTypes) {
+ for (Class<? extends Heuristic<T>> type : heuristicTypes) {
+ try {
+ // Lazily instantiate singletons
+ Heuristic<T> heuristic = (Heuristic<T>)
singletonCache.get(type);
+ if (heuristic == null) {
+ try {
+ heuristic = type.newInstance();
+ } catch (InstantiationException e) {
+ throw new RuntimeException(e);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ }
+ singletonCache.put(type, heuristic);
+ }
+
+ T value = heuristic.computeValue();
+ if (value != null) {
+ return value;
+ }
+ } catch (IOException e) {
+ }
+ }
+ return null;
+ }
+}
Added:
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/SystemInformation.java
===================================================================
---
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/SystemInformation.java
(rev 0)
+++
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/SystemInformation.java
2012-10-25 21:51:46 UTC (rev 30709)
@@ -0,0 +1,5 @@
+package org.cytoscape.diagnostics.internal;
+
+public class SystemInformation {
+
+}
Added:
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/AbstractHeuristic.java
===================================================================
---
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/AbstractHeuristic.java
(rev 0)
+++
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/AbstractHeuristic.java
2012-10-25 21:51:46 UTC (rev 30709)
@@ -0,0 +1,127 @@
+package org.cytoscape.diagnostics.internal.heuristics;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.StringWriter;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+public abstract class AbstractHeuristic<T> implements Heuristic<T> {
+ static boolean isWindows = isWindows();
+
+ private static boolean isWindows() {
+ return System.getProperty("os.name").startsWith("Windows");
+ }
+
+ protected boolean commandExists(String commandName) throws IOException {
+ String command = isWindows ? "where" : "which";
+ Process process = createProcess(command, commandName);
+ if (isWindows) {
+ process.getInputStream().close();
+ }
+ try {
+ return process.waitFor() == 0;
+ } catch (InterruptedException e) {
+ throw new IOException(e);
+ }
+ }
+
+
+ protected String runAndGetOutput(String...arguments) throws IOException
{
+ Process process = createProcess(arguments);
+ InputStream stream = process.getInputStream();
+ String result;
+ BufferedReader reader = new BufferedReader(new
InputStreamReader(stream));
+ try {
+ StringWriter writer = new StringWriter();
+ String line = reader.readLine();
+
+ boolean firstLine = true;
+ while (line != null) {
+ if (!firstLine) {
+ writer.write("\n");
+ } else {
+ firstLine = false;
+ }
+ writer.write(line);
+ line = reader.readLine();
+ }
+ result = writer.toString();
+ if (result != null && result.isEmpty()) {
+ return null;
+ }
+ try {
+ if (process.waitFor() != 0) {
+ return null;
+ }
+ } catch (InterruptedException e) {
+ throw new IOException(e);
+ }
+ if (process.exitValue() != 0) {
+ return null;
+ }
+ return result;
+ } finally {
+ reader.close();
+ }
+ }
+
+ private Process createProcess(String...arguments) throws IOException {
+ ProcessBuilder builder = new ProcessBuilder(arguments);
+ Process process = builder.start();
+ if (isWindows) {
+ process.getErrorStream().close();
+ process.getOutputStream().close();
+ }
+ return process;
+ }
+
+
+ @SuppressWarnings("unchecked")
+ protected <S> S convert(String value, Class<S> type) {
+ if (value == null || value.trim().isEmpty()) {
+ return null;
+ }
+
+ if (type.equals(String.class)) {
+ return (S) value;
+ }
+ try {
+ Method method = type.getMethod("valueOf", String.class);
+ return (S) method.invoke(null, value.trim());
+ } catch (SecurityException e) {
+ e.printStackTrace();
+ return null;
+ } catch (NoSuchMethodException e) {
+ e.printStackTrace();
+ return null;
+ } catch (IllegalArgumentException e) {
+ e.printStackTrace();
+ return null;
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
+ return null;
+ } catch (InvocationTargetException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+
+ protected String join(String delimiter, String...items) {
+ StringBuilder builder = new StringBuilder();
+ boolean first = true;
+
+ for (String item : items) {
+ if (!first) {
+ builder.append(delimiter);
+ } else {
+ first = false;
+ }
+ builder.append(item);
+ }
+ return builder.toString();
+ }
+}
Added:
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/Heuristic.java
===================================================================
---
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/Heuristic.java
(rev 0)
+++
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/Heuristic.java
2012-10-25 21:51:46 UTC (rev 30709)
@@ -0,0 +1,16 @@
+package org.cytoscape.diagnostics.internal.heuristics;
+
+import java.io.IOException;
+
+/**
+ * A heuristic that computes a value of type T.
+ */
+public interface Heuristic<T> {
+ /**
+ * Returns the value computed by this heuristic, or null, if this
+ * heuristic cannot compute the value successfully.
+ * @return
+ * @throws IOException
+ */
+ T computeValue() throws IOException;
+}
\ No newline at end of file
Added:
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/ProcHeuristic.java
===================================================================
---
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/ProcHeuristic.java
(rev 0)
+++
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/ProcHeuristic.java
2012-10-25 21:51:46 UTC (rev 30709)
@@ -0,0 +1,92 @@
+package org.cytoscape.diagnostics.internal.heuristics;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Heuristics for obtaining information via the "/proc"
+ * pseudo-filesystem on Linux systems.
+ */
+public abstract class ProcHeuristic<T> extends AbstractHeuristic<T> {
+ static final Pattern CPUINFO_MODEL_NAME_PATTERN =
Pattern.compile("model name\\s*[:]\\s+(.*)\\s*");
+
+ static final Pattern MEMINFO_MEMTOTAL_PATTERN =
Pattern.compile("MemTotal:\\s+(.*)\\s*");
+ static final Pattern MEMINFO_MEMFREE_PATTERN =
Pattern.compile("MemFree:\\s+(.*)\\s*");
+ static final Pattern MEMINFO_SWAPTOTAL_PATTERN =
Pattern.compile("SwapTotal:\\s+(.*)\\s*");
+ static final Pattern MEMINFO_SWAPFREE_PATTERN =
Pattern.compile("SwapFree:\\s+(.*)\\s*");
+
+ protected Long parseMemTotal(String value) {
+ if (value == null) {
+ return null;
+ }
+ String[] values = value.split(" ");
+ long baseValue = Long.parseLong(values[0]);
+ if (values.length > 1) {
+ if ("kB".equals(values[1])) {
+ baseValue *= 1024;
+ }
+ }
+ return baseValue;
+ }
+
+ protected <S> S getValueFromProc(String fileName, Pattern pattern,
Class<S> type) throws IOException {
+ File file = new File("/proc/" + fileName);
+ if (!file.exists()) {
+ return null;
+ }
+
+ BufferedReader reader = new BufferedReader(new
FileReader(file));
+ try {
+ String line = reader.readLine();
+ while (line != null) {
+ Matcher matcher = pattern.matcher(line);
+ if (matcher.matches()) {
+ return convert(matcher.group(1), type);
+ }
+ line = reader.readLine();
+ }
+ return null;
+ } finally {
+ reader.close();
+ }
+ }
+
+ public static class TotalMemory extends ProcHeuristic<Long> {
+ @Override
+ public Long computeValue() throws IOException {
+ return parseMemTotal(getValueFromProc("meminfo",
MEMINFO_MEMTOTAL_PATTERN, String.class));
+ }
+ }
+
+ public static class FreeMemory extends ProcHeuristic<Long> {
+ @Override
+ public Long computeValue() throws IOException {
+ return parseMemTotal(getValueFromProc("meminfo",
MEMINFO_MEMFREE_PATTERN, String.class));
+ }
+ }
+
+ public static class ProcessorName extends ProcHeuristic<String> {
+ @Override
+ public String computeValue() throws IOException {
+ return getValueFromProc("cpuinfo",
CPUINFO_MODEL_NAME_PATTERN, String.class);
+ }
+ }
+
+ public static class TotalSwap extends ProcHeuristic<Long> {
+ @Override
+ public Long computeValue() throws IOException {
+ return parseMemTotal(getValueFromProc("meminfo",
MEMINFO_SWAPTOTAL_PATTERN, String.class));
+ }
+ }
+
+ public static class FreeSwap extends ProcHeuristic<Long> {
+ @Override
+ public Long computeValue() throws IOException {
+ return parseMemTotal(getValueFromProc("meminfo",
MEMINFO_SWAPFREE_PATTERN, String.class));
+ }
+ }
+}
Added:
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/Sw_VersHeuristic.java
===================================================================
---
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/Sw_VersHeuristic.java
(rev 0)
+++
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/Sw_VersHeuristic.java
2012-10-25 21:51:46 UTC (rev 30709)
@@ -0,0 +1,38 @@
+package org.cytoscape.diagnostics.internal.heuristics;
+
+import java.io.IOException;
+
+/**
+ * Heuristics for obtaining information via the "sw_vers" command
+ * typically found on Mac OS X.
+ */
+public class Sw_VersHeuristic extends AbstractHeuristic<String> {
+ @Override
+ public String computeValue() throws IOException {
+ if (!commandExists("sw_vers")) {
+ return null;
+ }
+
+ String output = runAndGetOutput("sw_vers");
+
+ StringBuilder builder = new StringBuilder();
+ boolean firstLine = true;
+ for (String line : output.split("\n")) {
+ String[] parts = line.split("[:]", 2);
+ if (parts.length < 2) {
+ continue;
+ }
+ if (!firstLine) {
+ builder.append(" ");
+ } else {
+ firstLine = false;
+ }
+ builder.append(parts[1].trim());
+ }
+ String result = builder.toString().trim();
+ if (result.isEmpty()) {
+ return null;
+ }
+ return result;
+ }
+}
Added:
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/SysctlHeuristic.java
===================================================================
---
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/SysctlHeuristic.java
(rev 0)
+++
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/SysctlHeuristic.java
2012-10-25 21:51:46 UTC (rev 30709)
@@ -0,0 +1,73 @@
+package org.cytoscape.diagnostics.internal.heuristics;
+
+import java.io.IOException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Heuristics for obtaining information via the "sysctl" command
+ * typically found on POSIX systems.
+ */
+public abstract class SysctlHeuristic<T> extends AbstractHeuristic<T> {
+ static final Pattern VM_SWAPUSAGE_PATTERN =
Pattern.compile(".*total\\s*=\\s*([0-9.]+)M\\s*used\\s*=\\s*([0-9.]+)M\\s*free\\s*=\\s*([0-9.]+)M.*");
+
+ public static class ProcessorName extends SysctlHeuristic<String> {
+ @Override
+ public String computeValue() throws IOException {
+ if (!commandExists("sysctl")) {
+ return null;
+ }
+
+ return runAndGetOutput("sysctl", "-n",
"machdep.cpu.brand_string");
+ }
+ }
+
+ protected Long parseVmSwapUsage(String data, int field) {
+ Matcher matcher = VM_SWAPUSAGE_PATTERN.matcher(data);
+ if (!matcher.matches()) {
+ return null;
+ }
+ double megaBytes = Double.parseDouble(matcher.group(field));
+ return (long) (megaBytes * 1048576);
+ }
+
+ protected Long getVmSwapUsage(int field) throws IOException {
+ if (!commandExists("sysctl")) {
+ return null;
+ }
+
+ String output = runAndGetOutput("sysctl", "-n", "vm.swapusage");
+ if (output == null) {
+ return null;
+ }
+
+ return parseVmSwapUsage(output, field);
+ }
+
+ public static class TotalMemory extends SysctlHeuristic<Long> {
+ @Override
+ public Long computeValue() throws IOException {
+ if (!commandExists("sysctl")) {
+ return null;
+ }
+
+ String output = runAndGetOutput("sysctl", "-n",
"hw.memsize");
+ if (output == null) {
+ return null;
+ }
+ return Long.parseLong(output);
+ }
+ }
+
+ public static class FreeSwap extends SysctlHeuristic<Long> {
+ public Long computeValue() throws IOException {
+ return getVmSwapUsage(3);
+ }
+ }
+
+ public static class TotalSwap extends SysctlHeuristic<Long> {
+ public Long computeValue() throws IOException {
+ return getVmSwapUsage(1);
+ }
+ }
+}
Added:
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/UnameHeuristic.java
===================================================================
---
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/UnameHeuristic.java
(rev 0)
+++
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/UnameHeuristic.java
2012-10-25 21:51:46 UTC (rev 30709)
@@ -0,0 +1,18 @@
+package org.cytoscape.diagnostics.internal.heuristics;
+
+import java.io.IOException;
+
+/**
+ * Heuristics for obtaining information via the "uname" command
+ * typically found on POSIX systems.
+ */
+public class UnameHeuristic extends AbstractHeuristic<String> {
+ @Override
+ public String computeValue() throws IOException {
+ if (!commandExists("uname")) {
+ return null;
+ }
+
+ return runAndGetOutput("uname", "-a");
+ }
+}
Added:
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/Vm_StatHeuristic.java
===================================================================
---
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/Vm_StatHeuristic.java
(rev 0)
+++
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/Vm_StatHeuristic.java
2012-10-25 21:51:46 UTC (rev 30709)
@@ -0,0 +1,72 @@
+package org.cytoscape.diagnostics.internal.heuristics;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Heuristics for obtaining information via the "vm_stat" command
+ * typically found on BSD systems running the Mach kernel (e.g. Mac OS X).
+ */
+public abstract class Vm_StatHeuristic<T> extends AbstractHeuristic<T> {
+ static final Pattern VMSTAT_PAGE_SIZE_PATTERN =
Pattern.compile(".*\\(page size of (\\d+) bytes\\).*");
+ static final Pattern VMSTAT_ENTRY_PATTERN =
Pattern.compile("([^:]+):\\s+(\\d+)[.]");
+
+ protected Map<String, Long> getValuesFromVmstat() throws IOException {
+ if (!commandExists("vm_stat")) {
+ return Collections.emptyMap();
+ }
+
+ Map<String, Long> values = new HashMap<String, Long>();
+
+ Process process = new ProcessBuilder("vm_stat").start();
+ BufferedReader reader = new BufferedReader(new
InputStreamReader(process.getInputStream()));
+ try {
+ String line = reader.readLine();
+ while (line != null) {
+ try {
+ Matcher matcher =
VMSTAT_PAGE_SIZE_PATTERN.matcher(line);
+ if (matcher.matches()) {
+ Long pageSize =
convert(matcher.group(1), Long.class);
+ values.put("page size",
pageSize);
+ continue;
+ }
+ matcher =
VMSTAT_ENTRY_PATTERN.matcher(line);
+ if (matcher.matches()) {
+ String key = matcher.group(1);
+ Long value =
convert(matcher.group(2), Long.class);
+ values.put(key, value);
+ continue;
+ }
+ } finally {
+ line = reader.readLine();
+ }
+ }
+ return values;
+ } finally {
+ reader.close();
+ }
+ }
+
+ public static class FreeMemory extends Vm_StatHeuristic<Long> {
+ public Long computeValue() throws IOException {
+ Map<String, Long> values = getValuesFromVmstat();
+ Long pageSize = values.get("page size");
+ if (pageSize == null) {
+ return null;
+ }
+
+ Long freeMemory1 = values.get("Pages free");
+ Long freeMemory2 = values.get("Pages speculative");
+ if (freeMemory1 == null || freeMemory2 == null) {
+ return null;
+ }
+ return (freeMemory1 + freeMemory2) * pageSize;
+ }
+ }
+}
Added:
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/WmicHeuristic.java
===================================================================
---
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/WmicHeuristic.java
(rev 0)
+++
core3/gui-distribution/trunk/diagnostics/src/main/java/org/cytoscape/diagnostics/internal/heuristics/WmicHeuristic.java
2012-10-25 21:51:46 UTC (rev 30709)
@@ -0,0 +1,112 @@
+package org.cytoscape.diagnostics.internal.heuristics;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Heuristics for obtaining information via the "wmic" command
+ * typically found on Windows systems since Windows 2000.
+ */
+public abstract class WmicHeuristic<T> extends AbstractHeuristic<T> {
+ protected String getValue(String alias, String key) throws IOException {
+ if (!commandExists("wmic")) {
+ return null;
+ }
+ String output = runAndGetOutput("wmic", alias, "get", key);
+ String[] lines = output.split("\n");
+ if (lines.length < 3) {
+ return null;
+ }
+ return lines[2];
+ }
+
+ protected Map<String, String> getValues(String alias, String...keys)
throws IOException {
+ if (!commandExists("wmic")) {
+ return Collections.emptyMap();
+ }
+
+ List<String> arguments = new ArrayList<String>();
+ arguments.add("wmic");
+ arguments.add(alias);
+
+ if (keys.length > 0) {
+ arguments.add("get");
+ arguments.add(join(",", keys));
+ }
+
+ Map<String, String> result = new HashMap<String, String>();
+ String output = runAndGetOutput(arguments.toArray(new
String[arguments.size()]));
+ if (output == null) {
+ return Collections.emptyMap();
+ }
+
+ String[] lines = output.split("\n");
+ if (lines.length < 3) {
+ return Collections.emptyMap();
+ }
+
+ String[] keyNames = lines[0].split("\t");
+ String[] values = lines[2].split("\t");
+
+ for (int i = 0; i < keyNames.length; i++) {
+ result.put(keyNames[i], values[i]);
+ }
+ return result;
+ }
+
+ protected Long parseMemory(String data) {
+ Long value = convert(data, Long.class);
+ if (value == null) {
+ return null;
+ }
+ return value * 1024;
+ }
+
+ public static class ProcessorName extends WmicHeuristic<String> {
+ @Override
+ public String computeValue() throws IOException {
+ return getValue("cpu", "name");
+ }
+ }
+
+ public static class TotalMemory extends WmicHeuristic<Long> {
+ @Override
+ public Long computeValue() throws IOException {
+ return parseMemory(getValue("os",
"TotalVisibleMemorySize"));
+ }
+ }
+
+ public static class FreeMemory extends WmicHeuristic<Long> {
+ @Override
+ public Long computeValue() throws IOException {
+ return parseMemory(getValue("os",
"FreePhysicalMemory"));
+ }
+ }
+
+ public static class TotalSwap extends WmicHeuristic<Long> {
+ @Override
+ public Long computeValue() throws IOException {
+ return parseMemory(getValue("os",
"TotalVirtualMemorySize"));
+ }
+ }
+
+ public static class FreeSwap extends WmicHeuristic<Long> {
+ @Override
+ public Long computeValue() throws IOException {
+ return parseMemory(getValue("os", "FreeVirtualMemory"));
+ }
+ }
+
+ public static class OSName extends WmicHeuristic<String> {
+ @Override
+ public String computeValue() throws IOException {
+ return getValue("os", "caption");
+ }
+ }
+
+}
+
--
You received this message because you are subscribed to the Google Groups
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/cytoscape-cvs?hl=en.