Beispiel #1
0
    def run(self, args):
        if len(args) == 0:
            self.parser.error('Less arguments: ' + ' '.join(args))

        if self.options.debug:
            print args

        target_dir = args[0]

        parent_path = os.path.dirname(target_dir)
        new = os.path.basename(target_dir)
        if self.options.parents and not os.path.exists(parent_path):
            self.parser.error('%s not found' % parent_path)

        parent = cgroup.get_cgroup(parent_path)

        if not self.options.apply_all:
            parent.mkdir(new)
        else:
            status = cgroup.SubsystemStatus()
            enabled = status.get_enabled()
            enabled = [
                s for s in enabled if not (s == 'perf_event' or s == 'debug')
            ]

            parents = []
            for name in enabled:
                mount_point = status.get_path(name)
                path = os.path.join(mount_point, parent.fullname.lstrip('/'))
                parents.append(cgroup.get_cgroup(path))

            # Check directory existence first
            to_be_created = []
            for _parent in parents:
                new_path = os.path.join(_parent.fullpath, new)
                if self.options.debug:
                    print(new_path)
                if os.path.exists(new_path):
                    if not self.options.parents:
                        print("%s exists" % new_path)
                        sys.exit(1)
                    else:
                        to_be_created.append(_parent)
                else:
                    to_be_created.append(_parent)

            if self.options.debug:
                print(to_be_created)

            for _parent in to_be_created:
                if self.options.debug:
                    new_path = os.path.join(_parent.fullpath, new)
                    print("mkdir %s" % new_path)
                new_path = os.path.join(_parent.fullpath, new)
                if os.path.exists(new_path):
                    # XXX: this may happen when systemd creates
                    # a cpuacct,cpu group and links cpu and cpuacct
                    # to it.
                    continue
                _parent.mkdir(new)
Beispiel #2
0
    def run(self, args):
        if len(args) == 0:
            self.parser.error('Less arguments: ' + ' '.join(args))

        if self.options.debug:
            print args

        target_dir = args[0]

        if not self.options.apply_all:
            if not os.path.exists(target_dir):
                print("Error: %s not found" % target_dir)
                sys.exit(1)

            if not os.path.isdir(target_dir):
                print("Error: %s is not a directory" % target_dir)
                sys.exit(1)

            cg = cgroup.get_cgroup(target_dir)

            if cg.depth == 0:
                print("Error: %s is a root cgroup" % target_dir)
                sys.exit(1)

            cg.rmdir()
        else:
            target = cgroup.get_cgroup(target_dir)

            status = cgroup.SubsystemStatus()
            enabled = status.get_enabled()
            enabled = [s for s in enabled if not (s == 'perf_event' or s == 'debug')]

            targets = []
            for name in enabled:
                mount_point = status.get_path(name)
                path = os.path.join(mount_point, target.fullname.lstrip('/'))
                targets.append(cgroup.get_cgroup(path))

            # Check directory existence first
            for _target in targets:
                if self.options.debug:
                    print(_target.fullpath)
                if not os.path.exists(_target.fullpath):
                    print("Error: %s not found" % _target.fullpath)
                    sys.exit(1)
                if not os.path.isdir(_target.fullpath):
                    print("Error: %s is not a directory" % _target.fullpath)
                    sys.exit(1)

            for _target in targets:
                if self.options.debug:
                    print("rmdir %s" % _target.fullpath)
                if not os.path.exists(_target.fullpath):
                    # XXX: this may happen when systemd creates
                    # a cpuacct,cpu group and links cpu and cpuacct
                    # to it.
                    continue
                _target.rmdir()
Beispiel #3
0
    def run(self, args):
        if len(args) == 0:
            self.parser.error('Less arguments: ' + ' '.join(args))

        if self.options.debug:
            print args

        target_dir = args[0]

        parent_path = os.path.dirname(target_dir)
        new = os.path.basename(target_dir)
        if self.options.parents and not os.path.exists(parent_path):
            self.parser.error('%s not found' % parent_path)

        parent = cgroup.get_cgroup(parent_path)

        if not self.options.apply_all:
            parent.mkdir(new)
        else:
            status = cgroup.SubsystemStatus()
            enabled = status.get_enabled()
            enabled = [s for s in enabled if not (s == 'perf_event' or s == 'debug')]

            parents = []
            for name in enabled:
                mount_point = status.get_path(name)
                path = os.path.join(mount_point, parent.fullname.lstrip('/'))
                parents.append(cgroup.get_cgroup(path))

            # Check directory existence first
            to_be_created = []
            for _parent in parents:
                new_path = os.path.join(_parent.fullpath, new)
                if self.options.debug:
                    print(new_path)
                if os.path.exists(new_path):
                    if not self.options.parents:
                        print("%s exists" % new_path)
                        sys.exit(1)
                    else:
                        to_be_created.append(_parent)
                else:
                    to_be_created.append(_parent)

            if self.options.debug:
                print(to_be_created)

            for _parent in to_be_created:
                if self.options.debug:
                    new_path = os.path.join(_parent.fullpath, new)
                    print("mkdir %s" % new_path)
                new_path = os.path.join(_parent.fullpath, new)
                if os.path.exists(new_path):
                    # XXX: this may happen when systemd creates
                    # a cpuacct,cpu group and links cpu and cpuacct
                    # to it.
                    continue
                _parent.mkdir(new)
Beispiel #4
0
    def run(self, args):
        if len(args) == 0:
            self.parser.error("Less arguments: " + " ".join(args))

        if self.options.debug:
            print args

        target_file = args[0]

        if not os.path.exists(target_file):
            print "File not found: %s" % target_file
            sys.exit(1)

        target_name = os.path.basename(target_file)

        arguments = []
        if target_name in ["memory.usage_in_bytes", "memory.memsw.usage_in_bytes"]:
            self.parser.usage = "%%prog %s [options] <target_file> <threshold>" % self.NAME
            if len(args) < 2:
                self.parser.error("Less arguments: " + " ".join(args))

            if len(args) > 2:
                self.parser.error("Too many arguments: " + " ".join(args))
            threshold = args[1]

            if threshold[0] == "+":
                cur = long(fileops.read(target_file))
                threshold = threshold.replace("+", "")
                threshold = cur + self._parse_value(threshold)
            elif threshold[0] == "-":
                cur = long(fileops.read(target_file))
                threshold = threshold.replace("-", "")
                threshold = cur - self._parse_value(threshold)
            else:
                threshold = self._parse_value(threshold)

            if self.options.verbose:
                print "Threshold: %d (%s)" % (threshold, formatter.byte(threshold))

            arguments.append(threshold)

        elif target_name == "memory.oom_control":
            self.parser.usage = "%%prog %s [options] <target_file>" % self.NAME
            if len(args) != 1:
                self.parser.error("Too many arguments: " + " ".join(args))

        else:
            files = ", ".join(cgroup.EventListener.SUPPORTED_FILES)
            message = "Target file not supported: %s\n" % target_name
            message += "(Supported files: %s)" % files
            self.parser.error(message)

        cg = cgroup.get_cgroup(os.path.dirname(target_file))

        if self.options.verbose:
            self._show_memory_usage("Before", cg)

        pid = os.fork()
        if pid == 0:
            listener = cgroup.EventListener(cg, target_name)
            listener.register(arguments)

            # ret = listener.wait()
            listener.wait()
            os._exit(0)

        timed_out = False
        if self.options.timeout_seconds:
            remained = self.options.timeout_seconds
            while remained > 0:
                ret = os.waitpid(pid, os.WNOHANG)

                if ret != (0, 0):
                    break
                time.sleep(0.1)
                remained -= 0.1
            else:
                timed_out = True
                os.kill(pid, signal.SIGTERM)
        else:
            os.waitpid(pid, 0)

        if not os.path.exists(cg.fullpath):
            print ("The cgroup seems to have been removed.")
            sys.exit(1)

        if self.options.verbose:
            self._show_memory_usage("After", cg)

        if timed_out:
            if self.options.verbose:
                print ("Timed out")
            sys.exit(2)
        else:
            sys.exit(0)
Beispiel #5
0
    def run(self):
        if not os.path.exists(self.args.target_file):
            print("File not found: %s" % self.args.target_file)
            sys.exit(1)
        target_file = self.args.target_file

        target_name = os.path.basename(target_file)

        arguments = []
        if target_name in [
                'memory.usage_in_bytes', 'memory.memsw.usage_in_bytes'
        ]:
            if not self.args.threshold:
                self.parser.error('Less arguments: ' + ' '.join(self.args))
            threshold = self.args.threshold

            if threshold[0] == '+':
                cur = long(fileops.read(target_file))
                threshold = threshold.replace('+', '')
                threshold = cur + self._parse_value(threshold)
            elif threshold[0] == '-':
                cur = long(fileops.read(target_file))
                threshold = threshold.replace('-', '')
                threshold = cur - self._parse_value(threshold)
            else:
                threshold = self._parse_value(threshold)

            if self.args.verbose:
                print("Threshold: %d (%s)" %
                      (threshold, formatter.byte(threshold)))

            arguments.append(threshold)

        elif target_name == 'memory.oom_control':
            if self.args.threshold:
                self.parser.error('Too many arguments: ' + ' '.join(self.args))

        elif target_name == 'memory.pressure_level':
            SUPPORTED_TERMS = ['low', 'medium', 'critical']
            if not self.args.threshold:
                self.parser.error('Less arguments: ' + ' '.join(self.args))
            if self.args.threshold not in SUPPORTED_TERMS:
                self.parser.error('Use one of %p' % SUPPORTED_TERMS)

            arguments.append(self.args.threshold)

        else:
            files = ', '.join(cgroup.EventListener.SUPPORTED_FILES)
            message = "Target file not supported: %s\n" % target_name
            message += "(Supported files: %s)" % files
            self.parser.error(message)

        cg = cgroup.get_cgroup(os.path.dirname(target_file))

        if self.args.verbose:
            self._show_memory_usage('Before', cg)

        pid = os.fork()
        if pid == 0:
            listener = cgroup.EventListener(cg, target_name)
            listener.register(arguments)

            # ret = listener.wait()
            listener.wait()
            os._exit(0)

        timed_out = False
        if self.args.timeout_seconds:
            remained = self.args.timeout_seconds
            while remained > 0:
                ret = os.waitpid(pid, os.WNOHANG)

                if ret != (0, 0):
                    break
                time.sleep(0.1)
                remained -= 0.1
            else:
                timed_out = True
                os.kill(pid, signal.SIGTERM)
        else:
            os.waitpid(pid, 0)

        if not os.path.exists(cg.fullpath):
            print('The cgroup seems to have been removed.')
            sys.exit(1)

        if self.args.verbose:
            self._show_memory_usage('After', cg)

        if timed_out:
            if self.args.verbose:
                print('Timed out')
            sys.exit(2)
        else:
            sys.exit(0)
Beispiel #6
0
    def run(self):
        if not os.path.exists(self.args.target_file):
            print("File not found: %s" % self.args.target_file)
            sys.exit(1)
        target_file = self.args.target_file

        target_name = os.path.basename(target_file)

        arguments = []
        if target_name in ['memory.usage_in_bytes', 'memory.memsw.usage_in_bytes']:
            if not self.args.threshold:
                self.parser.error('Less arguments: ' + ' '.join(self.args))
            threshold = self.args.threshold

            if threshold[0] == '+':
                cur = long(fileops.read(target_file))
                threshold = threshold.replace('+', '')
                threshold = cur + self._parse_value(threshold)
            elif threshold[0] == '-':
                cur = long(fileops.read(target_file))
                threshold = threshold.replace('-', '')
                threshold = cur - self._parse_value(threshold)
            else:
                threshold = self._parse_value(threshold)

            if self.args.verbose:
                print("Threshold: %d (%s)" % (threshold, formatter.byte(threshold)))

            arguments.append(threshold)

        elif target_name == 'memory.oom_control':
            if self.args.threshold:
                self.parser.error('Too many arguments: ' + ' '.join(self.args))

        elif target_name == 'memory.pressure_level':
            SUPPORTED_TERMS = ['low', 'medium', 'critical']
            if not self.args.threshold:
                self.parser.error('Less arguments: ' + ' '.join(self.args))
            if self.args.threshold not in SUPPORTED_TERMS:
                self.parser.error('Use one of %p' % SUPPORTED_TERMS)

            arguments.append(self.args.threshold)

        else:
            files = ', '.join(cgroup.EventListener.SUPPORTED_FILES)
            message = "Target file not supported: %s\n" % target_name
            message += "(Supported files: %s)" % files
            self.parser.error(message)

        cg = cgroup.get_cgroup(os.path.dirname(target_file))

        if self.args.verbose:
            self._show_memory_usage('Before', cg)

        pid = os.fork()
        if pid == 0:
            listener = cgroup.EventListener(cg, target_name)
            listener.register(arguments)

            #ret = listener.wait()
            listener.wait()
            os._exit(0)

        timed_out = False
        if self.args.timeout_seconds:
            remained = self.args.timeout_seconds
            while remained > 0:
                ret = os.waitpid(pid, os.WNOHANG)

                if ret != (0, 0):
                    break
                time.sleep(0.1)
                remained -= 0.1
            else:
                timed_out = True
                os.kill(pid, signal.SIGTERM)
        else:
            os.waitpid(pid, 0)

        if not os.path.exists(cg.fullpath):
            print('The cgroup seems to have been removed.')
            sys.exit(1)

        if self.args.verbose:
            self._show_memory_usage('After', cg)

        if timed_out:
            if self.args.verbose:
                print('Timed out')
            sys.exit(2)
        else:
            sys.exit(0)