f8b8b9406057102402b166e68934783b0f7ee9c6
[osm/vim-emu.git] / src / emuvim / dcemulator / resourcemodel / upb / simple.py
1 """
2 Playground for resource models created by University of Paderborn.
3 """
4 import logging
5 from emuvim.dcemulator.resourcemodel import BaseResourceModel
6
7 LOG = logging.getLogger("rm.upb.simple")
8 LOG.setLevel(logging.DEBUG)
9
10
11 class UpbSimpleCloudDcRM(BaseResourceModel):
12 """
13 This will be an example resource model that limits the overall
14 resources that can be deployed per data center.
15 No over provisioning. Resources are fixed throughout entire container
16 lifetime.
17 """
18
19 def __init__(self, max_cu=32, max_mu=1024):
20 """
21 Initialize model.
22 :param max_cu: Maximum number of compute units available in this DC.
23 :param max_mu: Maximum memory of entire dc.
24 :return:
25 """
26 self.dc_max_cu = max_cu
27 self.dc_max_mu = max_mu
28 self.dc_alloc_cu = 0
29 super(self.__class__, self).__init__()
30
31 def allocate(self, name, flavor_name):
32 """
33 Calculate resources for container with given flavor.
34 :param name: Container name.
35 :param flavor_name: Flavor name.
36 :return:
37 """
38 # TODO Add memory model calculation (split in private methods for each tuple component)
39 # bookkeeping and flavor handling
40 if flavor_name not in self._flavors:
41 raise Exception("Flavor %r does not exist" % flavor_name)
42 fl = self._flavors.get(flavor_name)
43 fl_cu = fl.get("compute")
44 self.allocated_compute_instances[name] = flavor_name
45 # check for over provisioning
46 if self.dc_alloc_cu + fl_cu > self.dc_max_cu:
47 raise Exception("Not enough compute resources left.")
48 self.dc_alloc_cu += fl_cu
49 #
50 # calculate cpu limitation:
51 #
52 # get cpu time fraction for entire emulation
53 e_cpu = self.registrar.e_cpu
54 # calculate cpu time fraction of a single compute unit
55 cu = e_cpu / sum([rm.dc_max_cu for rm in list(self.registrar.resource_models)])
56 # calculate cpu time fraction for container with given flavor
57 c_ct = cu * fl_cu
58 return c_ct, -1.0, -1.0 # return 3tuple (cpu, memory, disk)
59
60 def free(self, name):
61 """
62 Free resources of given container.
63 :param name: Container name.
64 :return:
65 """
66 if name not in self.allocated_compute_instances:
67 return False
68 # bookkeeping
69 self.dc_alloc_cu -= self._flavors.get(self.allocated_compute_instances[name]).get("compute")
70 del self.allocated_compute_instances[name]
71 # we don't have to calculate anything special here in this simple model
72 return True