2 Playground for resource models created by University of Paderborn.
5 from emuvim
.dcemulator
.resourcemodel
import BaseResourceModel
7 LOG
= logging
.getLogger("rm.upb.simple")
8 LOG
.setLevel(logging
.DEBUG
)
11 class UpbSimpleCloudDcRM(BaseResourceModel
):
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
19 def __init__(self
, max_cu
=32, max_mu
=1024):
22 :param max_cu: Maximum number of compute units available in this DC.
23 :param max_mu: Maximum memory of entire dc.
26 self
.dc_max_cu
= max_cu
27 self
.dc_max_mu
= max_mu
32 super(self
.__class
__, self
).__init
__()
34 def allocate(self
, name
, flavor_name
):
36 Calculate resources for container with given flavor.
37 :param name: Container name.
38 :param flavor_name: Flavor name.
41 # bookkeeping and flavor handling
42 if flavor_name
not in self
._flavors
:
43 raise Exception("Flavor %r does not exist" % flavor_name
)
44 fl
= self
._flavors
.get(flavor_name
)
45 self
.allocated_compute_instances
[name
] = flavor_name
47 return self
._allocate
_cpu
(fl
), self
._allocate
_mem
(fl
), -1.0 # return 3tuple (cpu, memory, disk)
51 Free resources of given container.
52 :param name: Container name.
55 if name
not in self
.allocated_compute_instances
:
58 self
._free
_cpu
(self
._flavors
.get(self
.allocated_compute_instances
[name
]))
59 self
._free
_mem
(self
._flavors
.get(self
.allocated_compute_instances
[name
]))
60 del self
.allocated_compute_instances
[name
]
61 # we don't have to calculate anything special here in this simple model
64 def get_state_dict(self
):
66 Return the state of the resource model as simple dict.
67 Helper method for logging functionality.
71 r
["e_cpu"] = self
.registrar
.e_cpu
72 r
["e_mem"] = self
.registrar
.e_mem
73 r
["dc_max_cu"] = self
.dc_max_cu
74 r
["dc_max_mu"] = self
.dc_max_mu
75 r
["dc_alloc_cu"] = self
.dc_alloc_cu
76 r
["dc_alloc_mu"] = self
.dc_alloc_mu
77 r
["cu_cpu_percentage"] = self
.cu
78 r
["mu_mem_percentage"] = self
.mu
79 r
["allocated_compute_instances"] = self
.allocated_compute_instances
82 def _allocate_cpu(self
, flavor
):
85 :param flavor: flavor dict
86 :return: cpu time fraction
88 fl_cu
= flavor
.get("compute")
89 # check for over provisioning
90 if self
.dc_alloc_cu
+ fl_cu
> self
.dc_max_cu
:
91 raise Exception("Not enough compute resources left.")
92 self
.dc_alloc_cu
+= fl_cu
93 # get cpu time fraction for entire emulation
94 e_cpu
= self
.registrar
.e_cpu
95 # calculate cpu time fraction of a single compute unit
96 self
.cu
= float(e_cpu
) / sum([rm
.dc_max_cu
for rm
in list(self
.registrar
.resource_models
)])
97 # calculate cpu time fraction for container with given flavor
98 return self
.cu
* fl_cu
100 def _free_cpu(self
, flavor
):
103 :param flavor: flavor dict
106 self
.dc_alloc_cu
-= flavor
.get("compute")
108 def _allocate_mem(self
, flavor
):
111 :param flavor: flavor dict
112 :return: mem limit in MB
114 fl_mu
= flavor
.get("memory")
115 # check for over provisioning
116 if self
.dc_alloc_mu
+ fl_mu
> self
.dc_max_mu
:
117 raise Exception("Not enough memory resources left.")
118 self
.dc_alloc_mu
+= fl_mu
119 # get cpu time fraction for entire emulation
120 e_mem
= self
.registrar
.e_mem
121 # calculate cpu time fraction of a single compute unit
122 self
.mu
= float(e_mem
) / sum([rm
.dc_max_mu
for rm
in list(self
.registrar
.resource_models
)])
123 # calculate cpu time fraction for container with given flavor
124 return self
.mu
* fl_mu
126 def _free_mem(self
, flavor
):
128 Free memory allocation
129 :param flavor: flavor dict
132 self
.dc_alloc_mu
-= flavor
.get("memory")