Commit d5f88a14 authored by Swann Perarnau's avatar Swann Perarnau

[refactor] Allow updates in resource tracking

Implement an update allocation function to be able to update resource
tracking when containers are created and deleted.

The commit should make it easier to improve the resource manager later
on.
parent 848a9755
...@@ -89,6 +89,7 @@ class ContainerManager(object): ...@@ -89,6 +89,7 @@ class ContainerManager(object):
def delete(self, uuid): def delete(self, uuid):
"""Delete a container and kill all related processes.""" """Delete a container and kill all related processes."""
self.nodeos.delete(uuid, kill=True) self.nodeos.delete(uuid, kill=True)
self.resourcemanager.update(uuid)
c = self.containers[uuid] c = self.containers[uuid]
del self.containers[uuid] del self.containers[uuid]
del self.pids[c.pid] del self.pids[c.pid]
...@@ -17,6 +17,7 @@ class ResourceManager(object): ...@@ -17,6 +17,7 @@ class ResourceManager(object):
self.allresources = self.hwloc.info() self.allresources = self.hwloc.info()
self.logger.debug("resource info: %r", self.allresources) self.logger.debug("resource info: %r", self.allresources)
self.available = self.allresources self.available = self.allresources
self.allocations = {}
def schedule(self, uuid, request): def schedule(self, uuid, request):
"""Schedule a resource request on the available resources. """Schedule a resource request on the available resources.
...@@ -27,43 +28,37 @@ class ResourceManager(object): ...@@ -27,43 +28,37 @@ class ResourceManager(object):
# - memories exclusive if more than one left # - memories exclusive if more than one left
if len(self.available.cpus) >= request.cpus: if len(self.available.cpus) >= request.cpus:
retcpus = self.available.cpus[:request.cpus] retcpus = self.available.cpus[:request.cpus]
availcpus = self.available.cpus[request.cpus:]
else: else:
retcpus = [] retcpus = []
availcpus = self.available.cpus
if len(self.available.mems) > 1: if len(self.available.mems) > 1:
retmems = self.available.mems[:request.mems] retmems = self.available.mems[:request.mems]
availmems = self.available.mems[request.mems:]
else: else:
retmems = self.available.mems retmems = self.available.mems
availmems = self.available.mems ret = resources(retcpus, retmems)
self.available = resources(availcpus, availmems) # make sure we don't remember an error
return resources(retcpus, retmems) if ret.cpus:
self.update(uuid, ret)
return ret
def remove(self, uuid): def update(self, uuid, allocation=resources([], [])):
"""Free the resources associated with request uuid.""" """Update resource tracking according to new allocation.
pass
# def oldcode(self): The new allocation is saved, and available resources updated."""
# numcpus = int(manifest.app.isolators.container.cpus.value) added = {}
# freed = {}
# allresources = hwloc.info() prev = self.allocations.get(uuid, resources([], []))
# self.logger.debug("resource info: %r", allresources) for attr, val in prev._asdict().items():
# ncontainers = len(allresources.cpus) // numcpus added[attr] = set(getattr(allocation, attr)) - set(val)
# self.logger.debug("will support %s containers", ncontainers) freed[attr] = set(val) - set(getattr(allocation, attr))
# cur = nodeos.getavailable() if allocation != resources([], []):
# self.logger.debug("%r are available", cur) self.allocations[uuid] = allocation
# sets = hwloc.distrib(ncontainers, restrict=cur, fake=allresources) self.logger.info("updated allocation for %r: %r", uuid,
# self.logger.info("asking for %s cores", numcpus) self.available)
# self.logger.debug("will search in one of these: %r", sets) else:
# # find a free set del self.allocations[uuid]
# avail = set(cur.cpus) self.logger.info("deleted allocation for %r", uuid)
# for s in sets: new = {}
# cpuset = set(s.cpus) for attr, val in self.available._asdict().items():
# if cpuset.issubset(avail): new[attr] = list(set(val) - set(added[attr]) | set(freed[attr]))
# alloc = s self.available = resources(**new)
# break self.logger.info("updated available resources: %r", self.available)
# else:
# self.logger.error("no exclusive cpuset found among %r", avail)
# return -2
#
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment