Connected respurce model outputs for CPU limits to Dockernet API. Closes #47
[osm/vim-emu.git] / src / emuvim / dcemulator / node.py
1 """
2 Distributed Cloud Emulator (dcemulator)
3 (c) 2015 by Manuel Peuster <manuel.peuster@upb.de>
4 """
5 from mininet.node import Docker
6 from mininet.link import Link
7 import logging
8
9 LOG = logging.getLogger("dcemulator")
10 LOG.setLevel(logging.DEBUG)
11
12
13 DCDPID_BASE = 1000 # start of switch dpid's used for data center switches
14
15
16 class EmulatorCompute(Docker):
17 """
18 Emulator specific compute node class.
19 Inherits from Dockernet's Docker host class.
20 Represents a single container connected to a (logical)
21 data center.
22 We can add emulator specific helper functions to it.
23 """
24
25 def __init__(
26 self, name, dimage, **kwargs):
27 self.datacenter = kwargs.get("datacenter") # pointer to current DC
28 self.flavor_name = kwargs.get("flavor_name")
29 LOG.debug("Starting compute instance %r in data center %r" % (name, str(self.datacenter)))
30 # call original Docker.__init__
31 Docker.__init__(self, name, dimage, **kwargs)
32
33 def getNetworkStatus(self):
34 """
35 Helper method to receive information about the virtual networks
36 this compute instance is connected to.
37 """
38 # format list of tuples (name, Ip, MAC, isUp, status)
39 return [(str(i), i.IP(), i.MAC(), i.isUp(), i.status())
40 for i in self.intfList()]
41
42 def getStatus(self):
43 """
44 Helper method to receive information about this compute instance.
45 """
46 status = {}
47 status["name"] = self.name
48 status["network"] = self.getNetworkStatus()
49 status["image"] = self.dimage
50 status["cpu_quota"] = self.cpu_quota
51 status["cpu_period"] = self.cpu_period
52 status["cpu_shares"] = self.cpu_shares
53 status["cpuset"] = self.cpuset
54 status["mem_limit"] = self.mem_limit
55 status["memswap_limit"] = self.memswap_limit
56 status["state"] = self.dcli.inspect_container(self.dc)["State"]
57 status["id"] = self.dcli.inspect_container(self.dc)["Id"]
58 status["datacenter"] = (None if self.datacenter is None
59 else self.datacenter.label)
60 return status
61
62
63 class Datacenter(object):
64 """
65 Represents a logical data center to which compute resources
66 (Docker containers) can be added at runtime.
67
68 Will also implement resource bookkeeping in later versions.
69 """
70
71 DC_COUNTER = 1
72
73 def __init__(self, label, metadata={}):
74 self.net = None # DCNetwork to which we belong
75 # each node (DC) has a short internal name used by Mininet
76 # this is caused by Mininets naming limitations for swtiches etc.
77 self.name = "dc%d" % Datacenter.DC_COUNTER
78 Datacenter.DC_COUNTER += 1
79 # use this for user defined names that can be longer than self.name
80 self.label = label
81 # dict to store arbitrary metadata (e.g. latitude and longitude)
82 self.metadata = metadata
83 # first prototype assumes one "bigswitch" per DC
84 self.switch = None
85 # keep track of running containers
86 self.containers = {}
87 # pointer to assigned resource model
88 self._resource_model = None
89
90 def __repr__(self):
91 return self.label
92
93 def _get_next_dc_dpid(self):
94 global DCDPID_BASE
95 DCDPID_BASE += 1
96 return DCDPID_BASE
97
98 def create(self):
99 """
100 Each data center is represented by a single switch to which
101 compute resources can be connected at run time.
102
103 TODO: This will be changed in the future to support multiple networks
104 per data center
105 """
106 self.switch = self.net.addSwitch(
107 "%s.s1" % self.name, dpid=hex(self._get_next_dc_dpid())[2:])
108 LOG.debug("created data center switch: %s" % str(self.switch))
109
110 def start(self):
111 pass
112
113 def startCompute(self, name, image=None, command=None, network=None, flavor_name="tiny"):
114 """
115 Create a new container as compute resource and connect it to this
116 data center.
117 :param name: name (string)
118 :param image: image name (string)
119 :param command: command (string)
120 :param network: networks list({"ip": "10.0.0.254/8"}, {"ip": "11.0.0.254/24"})
121 :param flavor_name: name of the flavor for this compute container
122 :return:
123 """
124 assert name is not None
125 # no duplications
126 if name in [c.name for c in self.net.getAllContainers()]:
127 raise Exception("Container with name %s already exists." % name)
128 # set default parameter
129 if image is None:
130 image = "ubuntu"
131 if network is None:
132 network = {} # {"ip": "10.0.0.254/8"}
133 if isinstance(network, dict):
134 network = [network] # if we have only one network, put it in a list
135 if isinstance(network, list):
136 if len(network) < 1:
137 network.append({})
138
139 # allocate in resource resource model and compute resource limits for new container
140 cpu_limit = mem_limit = disk_limit = -1
141 cpu_period = cpu_quota = None
142 if self._resource_model is not None:
143 # call allocate in resource model to calculate resource limit for this container
144 (cpu_limit, mem_limit, disk_limit) = alloc = self._resource_model.allocate(name, flavor_name)
145 LOG.debug("Allocation result: %r" % str(alloc))
146 # check if we have a cpu_limit given by the used resource model
147 if cpu_limit > 0:
148 # calculate cpu period and quota for CFS
149 # (see: https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt)
150 # TODO consider multi core machines etc! non trivial!
151 # Attention minimum cpu_quota is 1ms (micro)
152 cpu_period = 100000 # lets consider a fixed period of 100000 microseconds for now
153 cpu_quota = cpu_period * cpu_limit # calculate the fraction of cpu time for this container
154 LOG.debug(
155 "CPU limit: cpu_quota = cpu_period * cpu_limit = %f * %f = %f" % (cpu_period, cpu_limit, cpu_quota))
156 # ATTENTION >= 1000 to avoid a invalid argument system error ... no idea why
157 if cpu_quota < 1000:
158 cpu_quota = 1000
159 LOG.warning("Increased CPU quota for %d to avoid system error." % name)
160 # TODO add memory and disc limitations
161 # create the container
162 d = self.net.addDocker(
163 "%s" % (name),
164 dimage=image,
165 dcmd=command,
166 datacenter=self,
167 flavor_name=flavor_name,
168 cpu_period=int(cpu_period) if cpu_limit > 0 else None, # set cpu limits if needed
169 cpu_quota=int(cpu_quota) if cpu_limit > 0 else None,
170 )
171 # connect all given networks
172 for nw in network:
173 # TODO we cannot use TCLink here (see: https://github.com/mpeuster/dockernet/issues/3)
174 self.net.addLink(d, self.switch, params1=nw, cls=Link)
175 # do bookkeeping
176 self.containers[name] = d
177 return d # we might use UUIDs for naming later on
178
179 def stopCompute(self, name):
180 """
181 Stop and remove a container from this data center.
182 """
183 assert name is not None
184 if name not in self.containers:
185 raise Exception("Container with name %s not found." % name)
186 self.net.removeLink(
187 link=None, node1=self.containers[name], node2=self.switch)
188 self.net.removeDocker("%s" % (name))
189 del self.containers[name]
190 # call resource model and free resources
191 if self._resource_model is not None:
192 self._resource_model.free(name)
193 return True
194
195 def listCompute(self):
196 """
197 Return a list of all running containers assigned to this
198 data center.
199 """
200 return list(self.containers.itervalues())
201
202 def getStatus(self):
203 """
204 Return a dict with status information about this DC.
205 """
206 return {
207 "label": self.label,
208 "internalname": self.name,
209 "switch": self.switch.name,
210 "n_running_containers": len(self.containers),
211 "metadata": self.metadata
212 }
213
214 def assignResourceModel(self, rm):
215 """
216 Assign a resource model to this DC.
217 :param rm: a BaseResourceModel object
218 :return:
219 """
220 if self._resource_model is not None:
221 raise Exception("There is already an resource model assigned to this DC.")
222 self._resource_model = rm
223 self.net.rm_registrar.register(self, rm)
224 LOG.info("Assigned RM: %r to DC: %r" % (rm, self))
225