From 36c070cd7a1c4081d42df3b31576b39d127bc9ae Mon Sep 17 00:00:00 2001 From: peusterm Date: Sat, 16 Apr 2016 17:39:01 +0200 Subject: [PATCH] Re-enabled logging of resource model state --- src/emuvim/dcemulator/node.py | 39 ++---------------- .../dcemulator/resourcemodel/__init__.py | 28 +++++++++++++ .../dcemulator/resourcemodel/upb/simple.py | 40 ++++++++++++++++--- 3 files changed, 66 insertions(+), 41 deletions(-) diff --git a/src/emuvim/dcemulator/node.py b/src/emuvim/dcemulator/node.py index 94d047b..1229b8d 100755 --- a/src/emuvim/dcemulator/node.py +++ b/src/emuvim/dcemulator/node.py @@ -49,6 +49,7 @@ class EmulatorCompute(Docker): status["name"] = self.name status["network"] = self.getNetworkStatus() status["image"] = self.dimage + status["flavor_name"] = self.flavor_name status["cpu_quota"] = self.cpu_quota status["cpu_period"] = self.cpu_period status["cpu_shares"] = self.cpu_shares @@ -152,6 +153,7 @@ class Datacenter(object): # apply resource limits to container if a resource model is defined if self._resource_model is not None: self._resource_model.allocate(d) + self._resource_model.write_allocation_log(d, self.resource_log_path) # connect all given networks # if no --net option is given, network = [{}], so 1 empty dict in the list @@ -161,25 +163,6 @@ class Datacenter(object): self.net.addLink(d, self.switch, params1=nw, cls=Link) # do bookkeeping self.containers[name] = d - - # TODO re-enable logging - """ - # write resource log if a path is given - if self.resource_log_path is not None: - l = dict() - l["t"] = time.time() - l["name"] = name - l["compute"] = d.getStatus() - l["flavor_name"] = flavor_name - l["action"] = "allocate" - l["cpu_limit"] = cpu_limit - l["mem_limit"] = mem_limit - l["disk_limit"] = disk_limit - l["rm_state"] = None if self._resource_model is None else self._resource_model.get_state_dict() - # append to logfile - with open(self.resource_log_path, "a") as f: - f.write("%s\n" % json.dumps(l)) - """ return d # we might use UUIDs for naming later on def stopCompute(self, name): @@ -194,6 +177,7 @@ class Datacenter(object): # call resource model and free resources if self._resource_model is not None: self._resource_model.free(self.containers[name]) + self._resource_model.write_free_log(self.containers[name], self.resource_log_path) # remove links self.net.removeLink( @@ -203,23 +187,6 @@ class Datacenter(object): self.net.removeDocker("%s" % (name)) del self.containers[name] - # TODO re-enable logging - """ - # write resource log if a path is given - if self.resource_log_path is not None: - l = dict() - l["t"] = time.time() - l["name"] = name - l["flavor_name"] = None - l["action"] = "free" - l["cpu_limit"] = -1 - l["mem_limit"] = -1 - l["disk_limit"] = -1 - l["rm_state"] = None if self._resource_model is None else self._resource_model.get_state_dict() - # append to logfile - with open(self.resource_log_path, "a") as f: - f.write("%s\n" % json.dumps(l)) - """ return True def listCompute(self): diff --git a/src/emuvim/dcemulator/resourcemodel/__init__.py b/src/emuvim/dcemulator/resourcemodel/__init__.py index 3df414f..21d0868 100644 --- a/src/emuvim/dcemulator/resourcemodel/__init__.py +++ b/src/emuvim/dcemulator/resourcemodel/__init__.py @@ -130,3 +130,31 @@ class BaseResourceModel(object): :return: """ return dict() + + def write_allocation_log(self, d, path): + """ + Helper to log RM info for experiments. + :param d: container + :param path: log path + :return: + """ + self._write_log(d, path, "allocate") + + def write_free_log(self, d, path): + """ + Helper to log RM info for experiments. + :param d: container + :param path: log path + :return: + """ + self._write_log(d, path, "free") + + 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: + """ + pass \ No newline at end of file diff --git a/src/emuvim/dcemulator/resourcemodel/upb/simple.py b/src/emuvim/dcemulator/resourcemodel/upb/simple.py index ff0c852..9d5df06 100644 --- a/src/emuvim/dcemulator/resourcemodel/upb/simple.py +++ b/src/emuvim/dcemulator/resourcemodel/upb/simple.py @@ -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 @@ -157,7 +159,7 @@ class UpbSimpleCloudDcRM(BaseResourceModel): # 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 +173,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 +191,7 @@ 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["allocation_state"] = allocation_state return r def _get_flavor(self, d): @@ -194,3 +204,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)) -- 2.25.1