How to use cgroupspy - 10 common examples

To help you get started, we’ve selected a few cgroupspy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cloudsigma / cgroupspy / cgroupspy / interfaces.py View on Github external
return ",".join(str(x) for x in value)


class MultiLineIntegerFile(BaseFileInterface):

    def sanitize_get(self, value):
        int_list = [int(val) for val in value.strip().split("\n") if val]
        return int_list

    def sanitize_set(self, value):
        if value is None:
            value = -1
        return int(value)


class SplitValueFile(BaseFileInterface):
    """
    Example: Getting int(10) for file with value 'Total 10'. Readonly.
    """
    readonly = True

    def __init__(self, filename, position, restype=None, splitchar=" "):
        super(SplitValueFile, self).__init__(filename)
        self.position = position
        self.splitchar = splitchar
        self.restype = restype

    def sanitize_get(self, value):
        res = value.strip().split(self.splitchar)[self.position]
        if self.restype and not isinstance(res, self.restype):
            return self.restype(res)
        return res
github cloudsigma / cgroupspy / cgroupspy / interfaces.py View on Github external
"""
    ex: 253237230463342 317756630269369 247294096796305 289833051422078
    """

    def sanitize_get(self, value):
        value_list = super(IntegerListFile, self).sanitize_get(value)
        return list(map(int, value_list))

    def sanitize_set(self, value):
        if value is None:
            value = -1
        return int(value)


class CommaDashSetFile(BaseFileInterface):

    """
    Builds a set from files containing the following data format 'cpuset.cpus: 1-3,6,11-15',
    returning {1,2,3,5,11,12,13,14,15}
    """

    def sanitize_get(self, value):
        elems = []
        for el_group in value.strip().split(','):
            if "-" in el_group:
                start, end = el_group.split("-")
                for el in range(int(start), int(end) + 1):
                    elems.append(el)
            else:
                if el_group != '':
                    elems.append(int(el_group))
github cloudsigma / cgroupspy / cgroupspy / interfaces.py View on Github external
elems.append(int(el_group))
        return set(elems)

    def sanitize_set(self, value):
        if len(value) == 0:
            return ' '
        try:
            value = value.encode()
        except AttributeError:
            pass
        if isinstance(value, bytes) or not isinstance(value, Iterable):
            value = [str(value)]
        return ",".join(str(x) for x in value)


class MultiLineIntegerFile(BaseFileInterface):

    def sanitize_get(self, value):
        int_list = [int(val) for val in value.strip().split("\n") if val]
        return int_list

    def sanitize_set(self, value):
        if value is None:
            value = -1
        return int(value)


class SplitValueFile(BaseFileInterface):
    """
    Example: Getting int(10) for file with value 'Total 10'. Readonly.
    """
    readonly = True
github cloudsigma / cgroupspy / cgroupspy / interfaces.py View on Github external
Get/set single integer values.
    """

    def sanitize_get(self, value):
        val = int(value)
        if val == -1:
            val = None
        return val

    def sanitize_set(self, value):
        if value is None:
            value = -1
        return int(value)


class DictFile(BaseFileInterface):

    def sanitize_get(self, value):
        res = {}
        for el in value.split("\n"):
            key, val = el.split()
            res[key] = int(val)
        return res

    def sanitize_set(self, value):
        if not isinstance(value, dict):
            raise ValueError("Value {} must be a dict".format(value))
        return ",".join(str(x) for x in value)


class ListFile(BaseFileInterface):
    readonly = True
github cloudsigma / cgroupspy / cgroupspy / interfaces.py View on Github external
class DictFile(BaseFileInterface):

    def sanitize_get(self, value):
        res = {}
        for el in value.split("\n"):
            key, val = el.split()
            res[key] = int(val)
        return res

    def sanitize_set(self, value):
        if not isinstance(value, dict):
            raise ValueError("Value {} must be a dict".format(value))
        return ",".join(str(x) for x in value)


class ListFile(BaseFileInterface):
    readonly = True

    def sanitize_get(self, value):
        return value.split()


class IntegerListFile(ListFile):

    """
    ex: 253237230463342 317756630269369 247294096796305 289833051422078
    """

    def sanitize_get(self, value):
        value_list = super(IntegerListFile, self).sanitize_get(value)
        return list(map(int, value_list))
github cloudsigma / cgroupspy / cgroupspy / interfaces.py View on Github external
readonly = True

    def __init__(self, filename, position, restype=None, splitchar=" "):
        super(SplitValueFile, self).__init__(filename)
        self.position = position
        self.splitchar = splitchar
        self.restype = restype

    def sanitize_get(self, value):
        res = value.strip().split(self.splitchar)[self.position]
        if self.restype and not isinstance(res, self.restype):
            return self.restype(res)
        return res


class TypedFile(BaseFileInterface):

    def __init__(self, filename, contenttype, readonly=None, writeonly=None, many=False):
        if not issubclass(contenttype, BaseContentType):
            raise RuntimeError("Contenttype should be a class inheriting "
                               "from BaseContentType, not {}".format(contenttype))

        self.contenttype = contenttype
        self.many = many
        super(TypedFile, self).__init__(filename, readonly=readonly, writeonly=writeonly)

    def sanitize_set(self, value):
        if isinstance(value, self.contenttype):
            return value

        return self.contenttype.from_string(value)
github apache / airflow / airflow / task / task_runner / cgroup_task_runner.py View on Github external
def _create_cgroup(self, path):
        """
        Create the specified cgroup.

        :param path: The path of the cgroup to create.
        E.g. cpu/mygroup/mysubgroup
        :return: the Node associated with the created cgroup.
        :rtype: cgroupspy.nodes.Node
        """
        node = trees.Tree().root
        path_split = path.split(os.sep)
        for path_element in path_split:
            # node.name is encoded to bytes:
            # https://github.com/cloudsigma/cgroupspy/blob/e705ac4ccdfe33d8ecc700e9a35a9556084449ca/cgroupspy/nodes.py#L64
            name_to_node = {x.name.decode(): x for x in node.children}
            if path_element not in name_to_node:
                self.log.debug("Creating cgroup %s in %s", path_element, node.path.decode())
                node = node.create_cgroup(path_element)
            else:
                self.log.debug(
                    "Not creating cgroup %s in %s since it already exists",
                    path_element, node.path.decode()
                )
                node = name_to_node[path_element]
        return node
github apache / airflow / airflow / task / task_runner / cgroup_task_runner.py View on Github external
def _delete_cgroup(self, path):
        """
        Delete the specified cgroup.

        :param path: The path of the cgroup to delete.
        E.g. cpu/mygroup/mysubgroup
        """
        node = trees.Tree().root
        path_split = path.split("/")
        for path_element in path_split:
            name_to_node = {x.name.decode(): x for x in node.children}
            if path_element not in name_to_node:
                self.log.warning("Cgroup does not exist: %s", path)
                return
            else:
                node = name_to_node[path_element]
        # node is now the leaf node
        parent = node.parent
        self.log.debug("Deleting cgroup %s/%s", parent, node.name)
        parent.delete_cgroup(node.name.decode())
github cloudsigma / cgroupspy / cgroupspy / contenttypes.py View on Github external
    @classmethod
    def from_string(cls, value):
        dev_type, major_minor, access_string = value.split()
        major, minor = major_minor.split(":")
        major = int(major) if major != "*" else None
        minor == int(minor) if minor != "*" else None

        access_mode = 0
        for idx, char in enumerate("rwm"):
            if char in access_string:
                access_mode |= (1 << idx)
        return cls(dev_type, major, minor, access_mode)


class DeviceThrottle(BaseContentType):

    def __init__(self, limit, major=None, minor=None, ):
        self.limit = limit
        self.major = major or "*"
        self.minor = minor or "*"

    def __str__(self):
        return "{self.major}:{self.minor} {self.limit}".format(self=self)

    @classmethod
    def from_string(cls, value):
        if not value:
            return None

        try:
            major_minor, limit = value.split()
github cloudsigma / cgroupspy / cgroupspy / controllers.py View on Github external
memory.memsw.usage_in_bytes
    memory.move_charge_at_immigrate
    memory.numa_stat
    memory.oom_control
    memory.pressure_level
    memory.soft_limit_in_bytes
    memory.stat
    memory.swappiness
    memory.usage_in_bytes
    memory.use_hierarchy
    """

    failcnt = IntegerFile("memory.failcnt")
    memsw_failcnt = IntegerFile("memory.memsw.failcnt")

    limit_in_bytes = IntegerFile("memory.limit_in_bytes")
    soft_limit_in_bytes = IntegerFile("memory.soft_limit_in_bytes")
    usage_in_bytes = IntegerFile("memory.usage_in_bytes")
    max_usage_in_bytes = IntegerFile("memory.max_usage_in_bytes")

    memsw_limit_in_bytes = IntegerFile("memory.memsw.limit_in_bytes")
    memsw_usage_in_bytes = IntegerFile("memory.memsw.usage_in_bytes")
    memsw_max_usage_in_bytes = IntegerFile("memory.memsw.max_usage_in_bytes")
    swappiness = IntegerFile("memory.swappiness")

    stat = DictFile("memory.stat", readonly=True)

    use_hierarchy = FlagFile("memory.use_hierarchy")
    force_empty = FlagFile("memory.force_empty")
    oom_control = FlagFile("memory.oom_control")

    move_charge_at_immigrate = BitFieldFile("memory.move_charge_at_immigrate")