changeset 18:568cabac9d09

Obtain timestamps as late as possible
author Omair Majid <omajid@redhat.com>
date Fri, 09 Dec 2011 13:02:16 -0500
parents 29ae24fc783d
children cb8e7d5966f9
files src/com/redhat/thermostat/backend/system/JvmStatDataExtractor.java src/com/redhat/thermostat/backend/system/JvmStatHostListener.java src/com/redhat/thermostat/backend/system/JvmStatVmListener.java
diffstat 3 files changed, 14 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/com/redhat/thermostat/backend/system/JvmStatDataExtractor.java	Thu Dec 08 12:50:52 2011 -0500
+++ b/src/com/redhat/thermostat/backend/system/JvmStatDataExtractor.java	Fri Dec 09 13:02:16 2011 -0500
@@ -7,6 +7,13 @@
 
 /**
  * A helper class to provide type-safe access to commonly used jvmstat monitors
+ * <p>
+ * Implementation details: For local vms, jvmstat uses a ByteBuffer
+ * corresponding to mmap()ed hsperfdata file. The hsperfdata file is updated
+ * asynchronously by the vm that created the file. The polling that jvmstat api
+ * provides is merely an abstraction over this (possibly always up-to-date)
+ * ByteBuffer. So the data this class extracts is as current as possible, and
+ * does not correspond to when the jvmstat update events fired.
  */
 public class JvmStatDataExtractor {
 
--- a/src/com/redhat/thermostat/backend/system/JvmStatHostListener.java	Thu Dec 08 12:50:52 2011 -0500
+++ b/src/com/redhat/thermostat/backend/system/JvmStatHostListener.java	Fri Dec 09 13:02:16 2011 -0500
@@ -46,7 +46,6 @@
         if (storage == null) {
             throw new NullPointerException("null");
         }
-        long currentTime = System.currentTimeMillis();
 
         MonitoredHost host = event.getMonitoredHost();
 
@@ -55,7 +54,7 @@
             Integer newVm = newActive.next();
             try {
                 logger.fine("New vm: " + newVm);
-                sendNewVM(currentTime, newVm, host);
+                sendNewVM(newVm, host);
             } catch (MonitorException e) {
                 logger.log(Level.WARNING, "error getting info for new vm" + newVm, e);
             } catch (URISyntaxException e) {
@@ -68,7 +67,7 @@
             Integer stoppedVm = newStopped.next();
             try {
                 logger.fine("stopped vm: " + stoppedVm);
-                sendStoppedVM(currentTime, stoppedVm, host);
+                sendStoppedVM(stoppedVm, host);
             } catch (URISyntaxException e) {
                 logger.log(Level.WARNING, "error getting info for stopped vm" + stoppedVm, e);
             } catch (MonitorException e) {
@@ -77,20 +76,20 @@
         }
     }
 
-    private void sendNewVM(long timestamp, Integer vmId, MonitoredHost host)
+    private void sendNewVM(Integer vmId, MonitoredHost host)
             throws MonitorException, URISyntaxException {
         MonitoredVm vm = host.getMonitoredVm(host.getHostIdentifier().resolve(
                 new VmIdentifier(vmId.toString())));
         if (vm != null) {
-
             VmInfo info = null;
             try {
+                long startTime = System.currentTimeMillis();
                 long stopTime = Long.MIN_VALUE;
                 JvmStatDataExtractor extractor = new JvmStatDataExtractor(vm);
                 Map<String, String> properties = new HashMap<String, String>();
                 Map<String, String> environment = new HashMap<String, String>();
                 List<String> loadedNativeLibraries = new ArrayList<String>();
-                info = new VmInfo(vmId, timestamp, stopTime,
+                info = new VmInfo(vmId, startTime, stopTime,
                         extractor.getJavaVersion(), extractor.getJavaHome(), extractor.getCommandLine(),
                         extractor.getVmName(), extractor.getVmInfo(), extractor.getVmVersion(), extractor.getVmArguments(),
                         properties, environment, loadedNativeLibraries);
@@ -106,7 +105,7 @@
         }
     }
 
-    private void sendStoppedVM(long timestamp, Integer vmId, MonitoredHost host)
+    private void sendStoppedVM(Integer vmId, MonitoredHost host)
             throws URISyntaxException, MonitorException {
         VmIdentifier resolvedVmID = host.getHostIdentifier().resolve(
                 new VmIdentifier(vmId.toString()));
--- a/src/com/redhat/thermostat/backend/system/JvmStatVmListener.java	Thu Dec 08 12:50:52 2011 -0500
+++ b/src/com/redhat/thermostat/backend/system/JvmStatVmListener.java	Fri Dec 09 13:02:16 2011 -0500
@@ -52,10 +52,10 @@
 
     private void recordGcStat(MonitoredVm vm) {
         try {
-            long timestamp = System.currentTimeMillis();
             JvmStatDataExtractor extractor = new JvmStatDataExtractor(vm);
             long collectors = extractor.getTotalCollectors();
             for (int i = 0; i < collectors; i++) {
+                long timestamp = System.currentTimeMillis();
                 VmGcStat stat = new VmGcStat(vmId, timestamp,
                         extractor.getCollectorName(i),
                         extractor.getCollectorInvocations(i),