aci.py 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
###############################################################################
# Copyright 2019 UChicago Argonne, LLC.
# (c.f. AUTHORS, LICENSE)
#
# This file is part of the NRM project.
# For more info, see https://xgitlab.cels.anl.gov/argo/nrm
#
# SPDX-License-Identifier: BSD-3-Clause
###############################################################################

11 12 13 14 15
"""Parse and Represent the APPC ACI specification."""
import collections
import logging
import json

16
logger = logging.getLogger('nrm')
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
spec = collections.namedtuple('Field', ['cls', 'required'])


class SpecField(object):

    """Object part of the ACI Image Manifest fields."""

    fields = {}

    def __init__(self):
        """Create empty field."""
        pass

    def load(self, data):
        """Load fields."""
        for key in self.fields:
            spec = self.fields[key]
            if key not in data:
                if spec.required:
                    logger.error("Missing key from manifest: %s", key)
                    return False
            else:
                ok, v = self.loadfield(data[key], spec.cls)
                if not ok:
                    logger.error("Error for key %s in %s", key, self.__class__)
                    return False
                setattr(self, key, v)
        return True

    def loadfield(self, data, cls):
        """load data as if from a field of the provided cls.

        Make sure the basic types are also respected.
        """
        ret = cls()
        if not hasattr(ret, 'load'):
            if not isinstance(data, cls):
                logger.error("Wrong data type %s, expected: %s", cls,
                             data.__class__)
                return (False, None)
            else:
                return (True, data)
        else:
            return (ret.load(data), ret)


class Scheduler(SpecField):

    """Scheduler information for a container."""

    classes = ['SCHED_FIFO', 'SCHED_HPC', 'SCHED_OTHER']

    fields = {"policy": spec(unicode, True),
70
              "priority": spec(unicode, False),
71
              "enabled": spec(unicode, False),
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
              }

    def __init__(self):
        """Create scheduler object."""
        pass

    def load(self, data):
        """Load configuration from json text."""
        ret = super(Scheduler, self).load(data)
        if not ret:
            return ret
        # check scheduler class & prio
        if self.policy not in self.classes:
            logger.error("Wrong scheduling class %s, not any of %r", data,
                         Scheduler.classes)
            return False
        if self.policy != "SCHED_OTHER":
            logger.warning("scheduler priority forced as 0 " +
                           "for non default policies")
            self.priority = "0"
92 93 94 95
        if getattr(self, "enabled", "1") not in ["0", "False", "1", "True"]:
            logger.error("Invalid value for scheduler enabled: %s",
                         self.enabled)
            return False
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        return True


class CPUSet(SpecField):

    """Represent a CPUSet field."""

    def __init__(self):
        """Create an empty set."""
        pass

    def load(self, data):
        """Load from json object."""
        self.value = data
        return True


class MemSet(SpecField):

    """Represent a MemSet field."""

    def __init__(self):
        """Create an empty set."""
        pass

    def load(self, data):
        """Load from json object."""
        self.value = data
        return True


class Container(SpecField):

    """Container Information."""

    fields = {"cpus": spec(CPUSet, True),
              "mems": spec(MemSet, True)
              }

    def __init__(self):
        """Create empty container."""
        pass

    def load(self, data):
        """Load container information."""
        return super(Container, self).load(data)

Swann Perarnau's avatar
Swann Perarnau committed
143

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
class PerfWrapper(SpecField):

    """Information on whether to use perf for a container."""

    fields = {"enabled": spec(unicode, False)
              }

    def __init__(self):
        """Create empty perf wrapper."""
        pass

    def load(self, data):
        """Load perf wrapper information."""
        ret = super(PerfWrapper, self).load(data)
        if not ret:
            return ret
160
        if getattr(self, "enabled", "1") not in ["0", "False", "1", "True"]:
161
            logger.error("Invalid value for perfwrapper enabled: %s",
162 163 164
                         self.enabled)
            return False
        return True
165

Swann Perarnau's avatar
Swann Perarnau committed
166

167
class Power(SpecField):
168

169
    """Power settings for a container."""
170 171 172 173

    policies = ['NONE', 'DDCM', 'DVFS', 'COMBINED']

    fields = {"enabled": spec(unicode, False),
174
              "profile": spec(unicode, False),
175 176 177
              "policy": spec(unicode, False),
              "damper": spec(unicode, False),
              "slowdown": spec(unicode, False)
178 179 180
              }

    def __init__(self):
181
        """Create empty power settings object."""
182 183 184
        pass

    def load(self, data):
185 186
        """Load power settings."""
        ret = super(Power, self).load(data)
187 188 189
        if not ret:
            return ret
        if self.enabled not in ["0", "False", "1", "True"]:
190 191 192 193 194
            logger.error("Invalid value for power enabled: %s",
                         self.enabled)
            return False
        if self.profile not in ["0", "False", "1", "True"]:
            logger.error("Invalid value for power profile: %s",
195 196 197
                         self.enabled)
            return False
        if self.policy not in self.policies:
198
            logger.error("Invalid value for power policy: %s",
199
                         self.policy)
200 201
            return False
        if self.damper < 0.0:
202
            logger.error("Invalid value for power policy damper: %s",
203 204 205
                         self.policy)
            return False
        if self.slowdown < 1.0:
206
            logger.error("Invalid value for power policy slowdown: %s",
207 208 209 210 211 212 213 214
                         self.policy)
            return False
        if self.damper < 0.0:
            logger.error("Invalid value of powerpolicy damper: %s",
                         self.policy)
            return False
        if self.slowdown < 1.0:
            logger.error("Invalid value of powerpolicy slowdown: %s",
215
                         self.policy)
216 217 218 219
            return False
        return True


220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
class HwBind(SpecField):

    """Hardware bindings for a container."""

    fields = {"enabled": spec(unicode, False),
              }

    def __init__(self):
        """Create empty hardware bindings  settings object."""
        pass

    def load(self, data):
        """Load hardware bindings settings."""
        ret = super(HwBind, self).load(data)
        if not ret:
            return ret
        if self.enabled not in ["0", "False", "1", "True"]:
            logger.error("Invalid value for hardware bindings enabled: %s",
                         self.enabled)
            return False
        return True


243 244 245 246 247
class Monitoring(SpecField):

    """Monitoring options (libnrm)."""

    fields = {"enabled": spec(unicode, False),
248
              "ratelimit": spec(unicode, False),
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
              }

    def __init__(self):
        """Create empty monitoring option object."""
        pass

    def load(self, data):
        """Load monitoring options."""
        ret = super(Monitoring, self).load(data)
        if not ret:
            return ret
        if self.enabled not in ["0", "False", "1", "True"]:
            logger.error("Invalid value for monitoring options enabled: %s",
                         self.enabled)
            return False
        if self.ratelimit < 0:
            logger.error("Invalid value for monitoring ratelimit: %s",
                         self.ratelimit)
            return False
        return True


271 272 273 274 275
class IsolatorList(SpecField):

    """Represent the list of isolator in a Manifest."""

    types = {"argo/scheduler": spec(Scheduler, False),
276
             "argo/container": spec(Container, True),
277
             "argo/perfwrapper": spec(PerfWrapper, False),
278
             "argo/power": spec(Power, False),
279
             "argo/hwbind": spec(HwBind, False),
280
             "argo/monitoring": spec(Monitoring, False),
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
             }

    def __init__(self):
        """Create empty list."""
        pass

    def load(self, data):
        """Load from json struct."""
        for e in data:
            name = e['name']
            if name in self.types:
                t = self.types[name]
                ok, v = super(IsolatorList, self).loadfield(e['value'], t.cls)
                if not ok:
                    logger.error("Error with %s in %s", name, self.__class__)
                    return False
                setattr(self, name.lstrip("argo/"), v)
        for k in self.types:
            if self.types[k].required:
300 301 302
                if not hasattr(self, k.lstrip("argo/")):
                    logger.error("Missing mandatory isolator: %s", k)
                    return False
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
        return True


class App(SpecField):

    """Represent the App part of an Image Manifest."""

    # attribute, subclass, required
    fields = {"environment": spec(list, False),
              "isolators": spec(IsolatorList, True),
              }

    def __init__(self):
        """Create empty container."""
        pass

    def load(self, data):
        """Load from json dict."""
        return super(App, self).load(data)


class ImageManifest(SpecField):

    """Represent an ACI Image Manifest."""

    fields = {"acKind": spec(unicode, True),
              "acVersion": spec(unicode, True),
              "name": spec(unicode, True),
331
              "app": spec(App, True),
332 333 334 335 336 337 338 339 340 341 342
              }

    def __init__(self):
        """Create empty manifest."""
        pass

    def load(self, filename):
        """Load a manifest from JSON file."""
        with open(filename, 'r') as f:
            data = json.load(f)
        return super(ImageManifest, self).load(data)
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

    def load_dict(self, data):
        """Load a manifest in dictionary form."""
        return super(ImageManifest, self).load(data)

    def is_feature_enabled(self, feature, true_values=["1", "True"]):
        """Check if a specific feature is enabled.

        Since the enabled field itself is optional, we return true if an
        isolator is present in a manifest or the enabled field is not true."""
        typename = "argo/{}".format(feature)
        assert typename in IsolatorList.types, \
            "{} in not a valid feature".format(feature)
        logger.debug(repr(self))
        if hasattr(self.app.isolators, feature):
            isolator = getattr(self.app.isolators, feature)
            if hasattr(isolator, 'enabled'):
                if isolator.enabled not in true_values:
                    return False
362 363 364
            return True
        else:
            return False