From 34d73e6af1b170178f9b9923637ce789d7985161 Mon Sep 17 00:00:00 2001
From: Quentin Perret <quentin.perret@arm.com>
Date: Thu, 17 Aug 2017 10:40:10 +0100
Subject: [PATCH] utils/gem5: try to cast statistics dump values

All values in the gem5 statistics log file are numeric. This commit adds a
cast on the strings read from the stats file to native numeric values when
and logs a warning in case of a malformed entry.
---
 devlib/instrument/gem5power.py |  2 +-
 devlib/utils/gem5.py           | 14 ++++++++++++--
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/devlib/instrument/gem5power.py b/devlib/instrument/gem5power.py
index d265440..4b145d9 100644
--- a/devlib/instrument/gem5power.py
+++ b/devlib/instrument/gem5power.py
@@ -72,7 +72,7 @@ class Gem5PowerInstrument(Instrument):
             sites_to_match = [self.site_mapping.get(s, s) for s in active_sites]
             for rec, rois in self.target.gem5stats.match_iter(sites_to_match,
                     [self.roi_label], self._base_stats_dump):
-                writer.writerow([float(rec[s]) for s in active_sites])
+                writer.writerow([rec[s] for s in active_sites])
         return MeasurementsCsv(outfile, self.active_channels, self.sample_rate_hz)
 
     def reset(self, sites=None, kinds=None, channels=None):
diff --git a/devlib/utils/gem5.py b/devlib/utils/gem5.py
index c609d70..0ca42ec 100644
--- a/devlib/utils/gem5.py
+++ b/devlib/utils/gem5.py
@@ -13,6 +13,9 @@
 # limitations under the License.
 
 import re
+import logging
+
+from devlib.utils.types import numeric
 
 
 GEM5STATS_FIELD_REGEX = re.compile("^(?P<key>[^- ]\S*) +(?P<value>[^#]+).+$")
@@ -20,6 +23,8 @@ GEM5STATS_DUMP_HEAD = '---------- Begin Simulation Statistics ----------'
 GEM5STATS_DUMP_TAIL = '---------- End Simulation Statistics   ----------'
 GEM5STATS_ROI_NUMBER = 8
 
+logger = logging.getLogger('gem5')
+
 
 def iter_statistics_dump(stats_file):
     '''
@@ -38,6 +43,11 @@ def iter_statistics_dump(stats_file):
             res = GEM5STATS_FIELD_REGEX.match(line) 
             if res:
                 k = res.group("key")
-                v = res.group("value").split()
-                cur_dump[k] = v[0] if len(v)==1 else set(v)
+                vtext = res.group("value")
+                try:
+                    v = map(numeric, vtext.split())
+                    cur_dump[k] = v[0] if len(v)==1 else set(v)
+                except ValueError:
+                    msg = 'Found non-numeric entry in gem5 stats ({}: {})'
+                    logger.warning(msg.format(k, vtext))