Modify the vmstat plugin as it was returning only the summary previously.

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@417341 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrian T. Co 2006-06-27 05:15:30 +00:00
parent a6f93512b4
commit cc1986ff12
5 changed files with 103 additions and 40 deletions

View File

@ -72,9 +72,6 @@ public abstract class AbstractPerformanceSampler extends AbstractObjectPropertie
public void run() {
try {
// Compute for the actual duration window of the sampler
long endTime = System.currentTimeMillis() + duration - rampDownTime;
onRampUpStart();
if (perfEventListener != null) {
perfEventListener.onRampUpStart(this);
@ -91,15 +88,7 @@ public abstract class AbstractPerformanceSampler extends AbstractObjectPropertie
perfEventListener.onSamplerStart(this);
}
while (System.currentTimeMillis() < endTime) {
try {
Thread.sleep(interval);
} catch (InterruptedException e) {
e.printStackTrace();
}
sampleData();
sampleIndex++;
}
sample();
onSamplerEnd();
if (perfEventListener != null) {
@ -124,7 +113,22 @@ public abstract class AbstractPerformanceSampler extends AbstractObjectPropertie
}
}
public abstract void sampleData();
protected void sample() {
// Compute for the actual duration window of the sampler
long endTime = System.currentTimeMillis() + duration - rampDownTime - rampUpTime;
while (System.currentTimeMillis() < endTime) {
try {
Thread.sleep(interval);
} catch (InterruptedException e) {
e.printStackTrace();
}
sampleData();
sampleIndex++;
}
}
public abstract void sampleData();
public boolean isRunning() {
return isRunning.get();

View File

@ -16,11 +16,11 @@ public class CpuSamplerTask extends AbstractPerformanceSampler {
public void createPlugin(String osName) throws IOException {
if (osName == null) {
throw new IOException("No defined OS name found. Foound: " + osName);
throw new IOException("No defined OS name found. Found: " + osName);
}
if (osName.equalsIgnoreCase(CpuSamplerPlugin.LINUX)) {
plugin = new LinuxCpuSamplerPlugin();
plugin = new LinuxCpuSamplerPlugin(getInterval());
} else {
throw new IOException("No CPU Sampler Plugin found for OS: " + osName + ". CPU Sampler will not be started.");
}
@ -31,4 +31,18 @@ public class CpuSamplerTask extends AbstractPerformanceSampler {
perfReportWriter.writeCsvData(ReportPlugin.REPORT_PLUGIN_CPU, plugin.getCpuUtilizationStats());
}
}
protected void onRampUpStart() {
super.onRampUpStart();
if (plugin != null) {
plugin.start();
}
}
protected void onRampDownEnd() {
super.onRampDownEnd();
if (plugin != null) {
plugin.stop();
}
}
}

View File

@ -11,6 +11,12 @@ public class ThroughputSamplerTask extends AbstractPerformanceSampler {
private final Object mutex = new Object();
private List clients = new ArrayList();
public void registerClient(MeasurableClient client) {
synchronized (mutex) {
clients.add(client);
}
}
public void sampleData() {
for (Iterator i = clients.iterator(); i.hasNext();) {
MeasurableClient client = (MeasurableClient) i.next();
@ -23,12 +29,6 @@ public class ThroughputSamplerTask extends AbstractPerformanceSampler {
}
}
public void registerClient(MeasurableClient client) {
synchronized (mutex) {
clients.add(client);
}
}
protected void onSamplerStart() {
// Reset the throughput of the clients
for (Iterator i = clients.iterator(); i.hasNext();) {

View File

@ -14,6 +14,8 @@ public interface CpuSamplerPlugin {
public final static String MAC_OS_X = "Mac OS X";
public final static String POWERPC = "PowerPC";
public final static String OS_2 = "OS/2";
public String getCpuUtilizationStats();
public void start();
public void stop();
}

View File

@ -1,32 +1,75 @@
package org.apache.activemq.tool.sampler.plugins;
import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class LinuxCpuSamplerPlugin implements CpuSamplerPlugin {
public class LinuxCpuSamplerPlugin implements CpuSamplerPlugin, Runnable {
private String vmstat = "vmstat";
private Process vmstatProcess;
private String vmstat;
private String result = "";
private final Object mutex = new Object();
private AtomicBoolean stop = new AtomicBoolean(false);
public String getCpuUtilizationStats() {
try {
Process p = Runtime.getRuntime().exec(vmstat);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
public LinuxCpuSamplerPlugin(long intervalInMs) {
vmstat = "vmstat -n " + (int)(intervalInMs / 1000);
}
br.readLine(); // throw away the first line
public void start() {
stop.set(false);
Thread t = new Thread(this);
t.start();
}
String header = br.readLine();
String data = br.readLine();
public void stop() {
stop.set(true);
try {
vmstatProcess.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
br.close();
public void run() {
// Convert to CSV of key=value pair
return convertToCSV(header, data);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
try {
vmstatProcess = Runtime.getRuntime().exec(vmstat);
BufferedReader br = new BufferedReader(new InputStreamReader(vmstatProcess.getInputStream()), 1024);
br.readLine(); // throw away the first line
String header = br.readLine();
String data;
while (!stop.get()) {
data = br.readLine();
if (data != null) {
String csvData = convertToCSV(header, data);
synchronized (mutex) {
result = csvData;
}
}
}
br.close();
vmstatProcess.destroy();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public String getCpuUtilizationStats() {
String data;
synchronized (mutex) {
data = result;
result = "";
}
return data;
}
public String getVmstat() {
return vmstat;
@ -36,7 +79,7 @@ public class LinuxCpuSamplerPlugin implements CpuSamplerPlugin {
this.vmstat = vmstat;
}
protected String convertToCSV(String header, String data) {
protected String convertToCSV(String header, String data) {
StringTokenizer headerTokens = new StringTokenizer(header, " ");
StringTokenizer dataTokens = new StringTokenizer(data, " ");