diff --git a/devlib/bin/scripts/shutils.in b/devlib/bin/scripts/shutils.in index f961aed..799461f 100755 --- a/devlib/bin/scripts/shutils.in +++ b/devlib/bin/scripts/shutils.in @@ -8,6 +8,7 @@ FIND=${FIND:-$BUSYBOX find} GREP=${GREP:-$BUSYBOX grep} SED=${SED:-$BUSYBOX sed} CAT=${CAT:-$BUSYBOX cat} +AWK=${AWK:-$BUSYBOX awk} ################################################################################ # CPUFrequency Utility Functions @@ -134,20 +135,21 @@ cgroups_run_into() { cgroups_tasks_move() { SRC_GRP=${1} DST_GRP=${2} - GREP_EXCLUSE=${3:-''} + shift 2 + FILTERS=$* $CAT $SRC_GRP/tasks | while read TID; do echo $TID > $DST_GRP/cgroup.procs done - [ "$GREP_EXCLUSE" = "" ] && exit 0 + [ "x$FILTERS" = "x" ] && exit 0 - PIDS=`ps | $GREP "$GREP_EXCLUSE" | awk '{print $2}'` + PIDS=`ps | $GREP $FILTERS | $AWK '{print $2}'` PIDS=`echo $PIDS` echo "PIDs to save: [$PIDS]" for TID in $PIDS; do - CMDLINE=`$CAT /proc/$TID/cmdline` - echo "$TID : $CMDLINE" + COMM =`$CAT /proc/$TID/comm` + echo "$TID : $COMM" echo $TID > $SRC_GRP/cgroup.procs done } diff --git a/devlib/module/cgroups.py b/devlib/module/cgroups.py index f6871d0..b146abb 100644 --- a/devlib/module/cgroups.py +++ b/devlib/module/cgroups.py @@ -128,10 +128,42 @@ class Controller(object): srcg.directory, dstg.directory, exclude), as_root=True) - def move_all_tasks_to(self, dest): + def move_all_tasks_to(self, dest, exclude=[]): + """ + Move all the tasks to the specified CGroup + + Tasks are moved from all their original CGroup the the specified on. + The tasks which name matches one of the string in exclude are moved + instead in the root CGroup for the controller. + The name of a tasks to exclude must be a substring of the task named as + reported by the "ps" command. Indeed, this list will be translated into + a: "ps | grep -e name1 -e name2..." in order to obtain the PID of these + tasks. + + :param exclude: list of commands to keep in the root CGroup + :type exlude: list(str) + """ + + if isinstance(exclude, str): + exclude = [exclude] + if not isinstance(exclude, list): + raise ValueError('wrong type for "exclude" parameter, ' + 'it must be a str or a list') + + logging.info('Moving all tasks into %s', dest) + + # Build list of tasks to exclude + grep_filters = '' + for comm in exclude: + grep_filters += '-e "{}" '.format(comm) + logging.debug('Using grep filter: %s', grep_filters) + if grep_filters != '': + logging.info('Excluding tasks which name matches:') + logging.info('%s', ','.join(exclude)) + for cgroup in self._cgroups: if cgroup != dest: - self.move_tasks(cgroup, dest) + self.move_tasks(cgroup, dest, grep_filters) def tasks(self, cgroup): try: