Added logging functionality to resource models.
[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 self.dc_alloc_mu = 0
30 super(self.__class__, self).__init__()
31
32 def allocate(self, name, flavor_name):
33 """
34 Calculate resources for container with given flavor.
35 :param name: Container name.
36 :param flavor_name: Flavor name.
37 :return:
38 """
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 self.allocated_compute_instances[name] = flavor_name
44 # calc and return
45 return self._allocate_cpu(fl), self._allocate_mem(fl), -1.0 # return 3tuple (cpu, memory, disk)
46
47 def free(self, name):
48 """
49 Free resources of given container.
50 :param name: Container name.
51 :return:
52 """
53 if name not in self.allocated_compute_instances:
54 return False
55 # bookkeeping
56 self._free_cpu(self._flavors.get(self.allocated_compute_instances[name]))
57 self._free_mem(self._flavors.get(self.allocated_compute_instances[name]))
58 del self.allocated_compute_instances[name]
59 # we don't have to calculate anything special here in this simple model
60 return True
61
62 def get_state_dict(self):
63 """
64 Return the state of the resource model as simple dict.
65 Helper method for logging functionality.
66 :return:
67 """
68 r = dict()
69 r["e_cpu"] = self.registrar.e_cpu
70 r["e_mem"] = self.registrar.e_mem
71 r["dc_max_cu"] = self.dc_max_cu
72 r["dc_max_mu"] = self.dc_max_mu
73 r["dc_alloc_cu"] = self.dc_alloc_cu
74 r["dc_alloc_mu"] = self.dc_alloc_mu
75 r["allocated_compute_instances"] = self.allocated_compute_instances
76 return r
77
78 def _allocate_cpu(self, flavor):
79 """
80 Allocate CPU time.
81 :param flavor: flavor dict
82 :return: cpu time fraction
83 """
84 fl_cu = flavor.get("compute")
85 # check for over provisioning
86 if self.dc_alloc_cu + fl_cu > self.dc_max_cu:
87 raise Exception("Not enough compute resources left.")
88 self.dc_alloc_cu += fl_cu
89 # get cpu time fraction for entire emulation
90 e_cpu = self.registrar.e_cpu
91 # calculate cpu time fraction of a single compute unit
92 cu = float(e_cpu) / sum([rm.dc_max_cu for rm in list(self.registrar.resource_models)])
93 # calculate cpu time fraction for container with given flavor
94 return cu * fl_cu
95
96 def _free_cpu(self, flavor):
97 """
98 Free CPU allocation.
99 :param flavor: flavor dict
100 :return:
101 """
102 self.dc_alloc_cu -= flavor.get("compute")
103
104 def _allocate_mem(self, flavor):
105 """
106 Allocate mem.
107 :param flavor: flavor dict
108 :return: mem limit in MB
109 """
110 fl_mu = flavor.get("memory")
111 # check for over provisioning
112 if self.dc_alloc_mu + fl_mu > self.dc_max_mu:
113 raise Exception("Not enough memory resources left.")
114 self.dc_alloc_mu += fl_mu
115 # get cpu time fraction for entire emulation
116 e_mem = self.registrar.e_mem
117 # calculate cpu time fraction of a single compute unit
118 mu = float(e_mem) / sum([rm.dc_max_mu for rm in list(self.registrar.resource_models)])
119 # calculate cpu time fraction for container with given flavor
120 return mu * fl_mu
121
122 def _free_mem(self, flavor):
123 """
124 Free memory allocation
125 :param flavor: flavor dict
126 :return:
127 """
128 self.dc_alloc_mu -= flavor.get("memory")