changeset 19:cb8e7d5966f9

Resolve some FIXME's and some TODO's
author Omair Majid <omajid@redhat.com>
date Tue, 13 Dec 2011 11:56:12 -0500
parents 568cabac9d09
children 51fb566e27dc
files src/com/redhat/thermostat/agent/config/StartupConfiguration.java src/com/redhat/thermostat/backend/system/HostInfoBuilder.java src/com/redhat/thermostat/common/utils/StringUtils.java
diffstat 3 files changed, 35 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/com/redhat/thermostat/agent/config/StartupConfiguration.java	Fri Dec 09 13:02:16 2011 -0500
+++ b/src/com/redhat/thermostat/agent/config/StartupConfiguration.java	Tue Dec 13 11:56:12 2011 -0500
@@ -23,19 +23,11 @@
 
 public final class StartupConfiguration {
 
-    /* FIXME
-     *
-     * This class needs some love.  It mixes up startup configuration with runtime configuration,
-     * while each is handled in very different ways.  It probably should be split into separate
-     * classes, but it makes very little sense to do that before we have a Storage abstraction
-     * hiding implementation details (ie Mongo API stuff).
-     */
     private static Logger logger = LoggingUtils.getLogger(StartupConfiguration.class);
 
     private final long startTimestamp;
 
     private Properties props;
-
     private Level logLevel;
     private boolean localMode;
     private int mongodPort;
@@ -113,8 +105,6 @@
     // TODO move this into Storage as well
     public DBObject toDBObject() {
         BasicDBObject result = new BasicDBObject();
-        // TODO explicit exception if agent not yet set.
-        result.put(StorageConstants.KEY_AGENT_ID, agent.getId().toString());
         result.put(StorageConstants.KEY_AGENT_CONFIG_AGENT_START_TIME, startTimestamp);
         // TODO create nested backend config parts
         return result;
--- a/src/com/redhat/thermostat/backend/system/HostInfoBuilder.java	Fri Dec 09 13:02:16 2011 -0500
+++ b/src/com/redhat/thermostat/backend/system/HostInfoBuilder.java	Tue Dec 13 11:56:12 2011 -0500
@@ -20,9 +20,13 @@
 import com.redhat.thermostat.common.Constants;
 import com.redhat.thermostat.common.HostInfo;
 import com.redhat.thermostat.common.utils.LoggingUtils;
+import com.redhat.thermostat.common.utils.StringUtils;
 
 public class HostInfoBuilder {
 
+    private static final String MEMINFO_FILE = "/proc/meminfo";
+    private static final String CPUINFO_FILE = "/proc/cpuinfo";
+
     private static final Logger logger = LoggingUtils.getLogger(HostInfoBuilder.class);
 
     public HostInfo build() {
@@ -42,9 +46,7 @@
         String osKernel = System.getProperty("os.name") + " " + System.getProperty("os.version");
         logger.log(Level.FINEST, "osKernel: " + osKernel);
 
-        // FIXME replace with a real implementation. This is the number of
-        // processors available to the _JVM_
-        int cpuCount = Runtime.getRuntime().availableProcessors();
+        int cpuCount = getProcessorCountFromProc();
         logger.log(Level.FINEST, "cpuCount: " + cpuCount);
 
         long totalMemory = -1;
@@ -85,6 +87,32 @@
 
     }
 
+    private int getProcessorCountFromProc() {
+        final String KEY_PROCESSOR_ID = "processor";
+        int totalCpus = 0;
+        BufferedReader reader = null;
+        try {
+            reader = new BufferedReader(new FileReader(CPUINFO_FILE));
+            String line = null;
+            while ((line = reader.readLine()) != null) {
+                if (line.startsWith(KEY_PROCESSOR_ID)) {
+                    totalCpus++;
+                }
+            }
+        } catch (IOException e) {
+            logger.log(Level.WARNING, "unable to read " + CPUINFO_FILE);
+        } finally {
+            if (reader != null) {
+                try {
+                    reader.close();
+                } catch (IOException e) {
+                    logger.log(Level.WARNING, "unable to close " + CPUINFO_FILE);
+                }
+            }
+        }
+        return totalCpus;
+    }
+
     /**
      * Removes the "hostname/" and the "%scope_id" parts from the
      * {@link InetAddress#toString()} output.
--- a/src/com/redhat/thermostat/common/utils/StringUtils.java	Fri Dec 09 13:02:16 2011 -0500
+++ b/src/com/redhat/thermostat/common/utils/StringUtils.java	Tue Dec 13 11:56:12 2011 -0500
@@ -17,4 +17,8 @@
             throw new IllegalStateException("UTF-8 not supported");
         }
     }
+
+    public static String quote(String toQuote) {
+        return new String("\"" + toQuote + "\"");
+    }
 }