improved resource model logging
[osm/vim-emu.git] / src / emuvim / dcemulator / resourcemodel / upb / simple.py
index ff0c852..6cc1687 100644 (file)
@@ -1,6 +1,8 @@
 """
 Playground for resource models created by University of Paderborn.
 """
+import time
+import json
 import logging
 from emuvim.dcemulator.resourcemodel import BaseResourceModel
 
@@ -31,6 +33,8 @@ class UpbSimpleCloudDcRM(BaseResourceModel):
         self.dc_alloc_mu = 0
         self.deactivate_cpu_limit = deactivate_cpu_limit
         self.deactivate_mem_limit = deactivate_mem_limit
+        self.single_cu = 0  # current value for a single cu
+        self.single_mu = 0  # current value for a single mu
         super(self.__class__, self).__init__()
 
     def allocate(self, d):
@@ -123,9 +127,9 @@ class UpbSimpleCloudDcRM(BaseResourceModel):
         # get cpu time fraction for entire emulation
         e_cpu = self.registrar.e_cpu
         # calculate cpu time fraction of a single compute unit
-        single_cu = float(e_cpu) / sum([rm.dc_max_cu for rm in list(self.registrar.resource_models)])
+        self.single_cu = float(e_cpu) / sum([rm.dc_max_cu for rm in list(self.registrar.resource_models)])
         # calculate cpu time fraction for container with given flavor
-        cpu_time_percentage = single_cu * number_cu
+        cpu_time_percentage = self.single_cu * number_cu
         # calculate cpu period and quota for CFS
         # (see: https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt)
         # Attention minimum cpu_quota is 1ms (micro)
@@ -151,13 +155,13 @@ class UpbSimpleCloudDcRM(BaseResourceModel):
         # get memory amount for entire emulation
         e_mem = self.registrar.e_mem
         # calculate amount of memory for a single mu
-        single_mu = float(e_mem) / sum([rm.dc_max_mu for rm in list(self.registrar.resource_models)])
+        self.single_mu = float(e_mem) / sum([rm.dc_max_mu for rm in list(self.registrar.resource_models)])
         # calculate mem for given flavor
-        mem_limit = single_mu * number_mu
+        mem_limit = self.single_mu * number_mu
         # ATTENTION minimum mem_limit per container is 4MB
         if mem_limit < 4:
             mem_limit = 4
-            LOG.warning("Increased MEM limit for %r because it was less than 4.0 MB." % name)
+            LOG.warning("Increased MEM limit for %r because it was less than 4.0 MB." % d.name)
         # to byte!
         mem_limit = int(mem_limit*1024*1024)
         # apply to container if changed
@@ -171,7 +175,17 @@ class UpbSimpleCloudDcRM(BaseResourceModel):
         Helper method for logging functionality.
         :return:
         """
-        # TODO update
+        # collect info about all allocated instances
+        allocation_state = dict()
+        for k, d in self._allocated_compute_instances.iteritems():
+            s = dict()
+            s["cpu_period"] = d.cpu_period
+            s["cpu_quota"] = d.cpu_quota
+            s["cpu_shares"] = d.cpu_shares
+            s["mem_limit"] = d.mem_limit
+            s["memswap_limit"] = d.memswap_limit
+            allocation_state[k] = s
+        # final result
         r = dict()
         r["e_cpu"] = self.registrar.e_cpu
         r["e_mem"] = self.registrar.e_mem
@@ -179,9 +193,9 @@ class UpbSimpleCloudDcRM(BaseResourceModel):
         r["dc_max_mu"] = self.dc_max_mu
         r["dc_alloc_cu"] = self.dc_alloc_cu
         r["dc_alloc_mu"] = self.dc_alloc_mu
-        r["cu_cpu_percentage"] = -1
-        r["mu_mem_percentage"] = -1
-        r["allocated_compute_instances"] = None #self._allocated_compute_instances
+        r["single_cu_percentage"] = self.single_cu
+        r["single_mu_percentage"] = self.single_mu
+        r["allocation_state"] = allocation_state
         return r
 
     def _get_flavor(self, d):
@@ -194,3 +208,23 @@ class UpbSimpleCloudDcRM(BaseResourceModel):
         if d.flavor_name not in self._flavors:
             raise Exception("Flavor %r does not exist" % d.flavor_name)
         return self._flavors.get(d.flavor_name)
+
+    def _write_log(self, d, path, action):
+        """
+        Helper to log RM info for experiments.
+        :param d: container
+        :param path: log path
+        :param action: allocate or free
+        :return:
+        """
+        if path is None:
+            return
+        # we have a path: write out RM info
+        l = dict()
+        l["t"] = time.time()
+        l["container_state"] = d.getStatus()
+        l["action"] = action
+        l["rm_state"] = self.get_state_dict()
+        # append to logfile
+        with open(path, "a") as f:
+            f.write("%s\n" % json.dumps(l))