self.rm_registrar = ResourceModelRegistrar(
dc_emulation_max_cpu, dc_emulation_max_mem)
- def addDatacenter(self, label, metadata={}):
+ def addDatacenter(self, label, metadata={}, resource_log_path=None):
"""
Create and add a logical cloud data center to the network.
"""
if label in self.dcs:
raise Exception("Data center label already exists: %s" % label)
- dc = Datacenter(label, metadata=metadata)
+ dc = Datacenter(label, metadata=metadata, resource_log_path=resource_log_path)
dc.net = self # set reference to network
self.dcs[label] = dc
dc.create() # finally create the data center in our Mininet instance
from mininet.node import Docker
from mininet.link import Link
import logging
+import time
+import json
LOG = logging.getLogger("dcemulator")
LOG.setLevel(logging.DEBUG)
DC_COUNTER = 1
- def __init__(self, label, metadata={}):
+ def __init__(self, label, metadata={}, resource_log_path=None):
self.net = None # DCNetwork to which we belong
# each node (DC) has a short internal name used by Mininet
# this is caused by Mininets naming limitations for swtiches etc.
self.label = label
# dict to store arbitrary metadata (e.g. latitude and longitude)
self.metadata = metadata
+ # path to which resource information should be logged (e.g. for experiments). None = no logging
+ self.resource_log_path = resource_log_path
# first prototype assumes one "bigswitch" per DC
self.switch = None
# keep track of running containers
self.net.addLink(d, self.switch, params1=nw, cls=Link)
# do bookkeeping
self.containers[name] = d
+
+ # 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):
assert name is not None
if name not in self.containers:
raise Exception("Container with name %s not found." % name)
+ LOG.debug("Stopping compute instance %r in data center %r" % (name, str(self)))
self.net.removeLink(
link=None, node1=self.containers[name], node2=self.switch)
self.net.removeDocker("%s" % (name))
# call resource model and free resources
if self._resource_model is not None:
self._resource_model.free(name)
+
+ # 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):
initialize some default flavours (naming/sizes inspired by OpenStack)
"""
self.addFlavour(ResourceFlavor(
- "tiny", {"compute": 1, "memory": 32, "disk": 1}))
+ "tiny", {"compute": 0.5, "memory": 32, "disk": 1}))
self.addFlavour(ResourceFlavor(
- "small", {"compute": 4, "memory": 128, "disk": 20}))
+ "small", {"compute": 1.0, "memory": 128, "disk": 20}))
self.addFlavour(ResourceFlavor(
- "medium", {"compute": 8, "memory": 256, "disk": 40}))
+ "medium", {"compute": 4.0, "memory": 256, "disk": 40}))
self.addFlavour(ResourceFlavor(
- "large", {"compute": 16, "memory": 512, "disk": 80}))
+ "large", {"compute": 8.0, "memory": 512, "disk": 80}))
self.addFlavour(ResourceFlavor(
- "xlarge", {"compute": 32, "memory": 1024, "disk": 160}))
+ "xlarge", {"compute": 16.0, "memory": 1024, "disk": 160}))
def addFlavour(self, fl):
"""
LOG.warning("Free in BaseResourceModel: %r" % name)
del self.allocated_compute_instances[name]
return True
+
+ def get_state_dict(self):
+ """
+ Return the state of the resource model as simple dict.
+ Helper method for logging functionality.
+ :return:
+ """
+ return dict()
# we don't have to calculate anything special here in this simple model
return True
+ def get_state_dict(self):
+ """
+ Return the state of the resource model as simple dict.
+ Helper method for logging functionality.
+ :return:
+ """
+ r = dict()
+ r["e_cpu"] = self.registrar.e_cpu
+ r["e_mem"] = self.registrar.e_mem
+ r["dc_max_cu"] = self.dc_max_cu
+ 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["allocated_compute_instances"] = self.allocated_compute_instances
+ return r
+
def _allocate_cpu(self, flavor):
"""
Allocate CPU time.
import logging
import time
+import os
from mininet.log import setLogLevel
from mininet.node import Controller
from emuvim.dcemulator.net import DCNetwork
logging.basicConfig(level=logging.INFO)
+RESOURCE_LOG_PATH = "resource.log"
+
+
def create_topology1():
+ cleanup()
# create topology
# use a maximum of 50% cpu time for containers added to data centers
net = DCNetwork(dc_emulation_max_cpu=0.5, controller=Controller)
# add some data centers and create a topology
- dc1 = net.addDatacenter("dc1")
- dc2 = net.addDatacenter("dc2")
+ dc1 = net.addDatacenter("dc1", resource_log_path=RESOURCE_LOG_PATH)
+ dc2 = net.addDatacenter("dc2", resource_log_path=RESOURCE_LOG_PATH)
s1 = net.addSwitch("s1")
net.addLink(dc1, s1, delay="10ms")
net.addLink(dc2, s1, delay="20ms")
# create and assign resource models for each DC
- rm1 = UpbSimpleCloudDcRM(max_cu=10, max_mu=1024)
- rm2 = UpbSimpleCloudDcRM(max_cu=20)
+ rm1 = UpbSimpleCloudDcRM(max_cu=4, max_mu=1024)
+ rm2 = UpbSimpleCloudDcRM(max_cu=6)
dc1.assignResourceModel(rm1)
dc2.assignResourceModel(rm2)
dc1.startCompute("vnf3", flavor_name="small")
dc2.startCompute("vnf4", flavor_name="medium")
print "... done."
- time.sleep(2)
+ time.sleep(5)
+ print "Removing instances ..."
+ dc1.stopCompute("vnf1")
+ dc2.stopCompute("vnf4")
+ print "... done"
net.CLI()
net.stop()
+def cleanup():
+ try:
+ os.remove(RESOURCE_LOG_PATH)
+ except OSError:
+ pass
+
+
def main():
setLogLevel('info') # set Mininet loglevel
create_topology1()
reg.register("test_dc", rm)
res = rm.allocate("c1", "tiny") # calculate allocation
- self.assertEqual(res[0], E_CPU / MAX_CU * 1) # validate compute result
+ self.assertEqual(res[0], E_CPU / MAX_CU * 0.5) # validate compute result
self.assertEqual(res[1], float(E_MEM) / MAX_MU * 32) # validate memory result
self.assertTrue(res[2] < 0) # validate disk result
res = rm.allocate("c2", "small") # calculate allocation
- self.assertEqual(res[0], E_CPU / MAX_CU * 4) # validate compute result
+ self.assertEqual(res[0], E_CPU / MAX_CU * 1) # validate compute result
self.assertEqual(res[1], float(E_MEM) / MAX_MU * 128) # validate memory result
self.assertTrue(res[2] < 0) # validate disk result
res = rm.allocate("c3", "medium") # calculate allocation
- self.assertEqual(res[0], E_CPU / MAX_CU * 8) # validate compute result
+ self.assertEqual(res[0], E_CPU / MAX_CU * 4) # validate compute result
self.assertEqual(res[1], float(E_MEM) / MAX_MU * 256) # validate memory result
self.assertTrue(res[2] < 0) # validate disk result
res = rm.allocate("c4", "large") # calculate allocation
- self.assertEqual(res[0], E_CPU / MAX_CU * 16) # validate compute result
+ self.assertEqual(res[0], E_CPU / MAX_CU * 8) # validate compute result
self.assertEqual(res[1], float(E_MEM) / MAX_MU * 512) # validate memory result
self.assertTrue(res[2] < 0) # validate disk result
res = rm.allocate("c5", "xlarge") # calculate allocation
- self.assertEqual(res[0], E_CPU / MAX_CU * 32) # validate compute result
+ self.assertEqual(res[0], E_CPU / MAX_CU * 16) # validate compute result
self.assertEqual(res[1], float(E_MEM) / MAX_MU * 1024) # validate memory result
self.assertTrue(res[2] < 0) # validate disk result
"""
# config
E_CPU = 1.0
- MAX_CU = 100
+ MAX_CU = 40
E_MEM = 512
MAX_MU = 4096
# create dummy resource model environment
rm = UpbSimpleCloudDcRM(max_cu=100, max_mu=100)
reg.register("test_dc", rm)
rm.allocate("c1", "tiny") # calculate allocation
- self.assertTrue(rm.dc_alloc_cu == 1)
+ self.assertTrue(rm.dc_alloc_cu == 0.5)
rm.free("c1")
self.assertTrue(rm.dc_alloc_cu == 0)