mirror of
				https://github.com/ARM-software/devlib.git
				synced 2025-11-04 07:51:21 +00:00 
			
		
		
		
	cgroups: add a couple of functions to move tasks among groups
This patch provides a couple of utility functions which makes it easy to run a command under a specific CGroup or to move all tasks from a group to another. Signed-off-by: Patrick Bellasi <patrick.bellasi@arm.com>
This commit is contained in:
		@@ -4,6 +4,7 @@ CMD=$1
 | 
			
		||||
shift
 | 
			
		||||
 | 
			
		||||
BUSYBOX=${BUSYBOX:-__DEVLIB_BUSYBOX__}
 | 
			
		||||
FIND=${FIND:-$BUSYBOX find}
 | 
			
		||||
GREP=${GREP:-$BUSYBOX grep}
 | 
			
		||||
SED=${SED:-$BUSYBOX sed}
 | 
			
		||||
 | 
			
		||||
@@ -69,6 +70,71 @@ cgroups_get_attributes() {
 | 
			
		||||
		$SED -e "s|$CONTROLLER\.||" -e "s|$CGROUP/||"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
cgroups_run_into() {
 | 
			
		||||
 | 
			
		||||
	# Control groups mount point
 | 
			
		||||
	CGMOUNT=${CGMOUNT:-/sys/fs/cgroup/devlib_*}
 | 
			
		||||
	# The control group we want to run into
 | 
			
		||||
	CGP=${1}
 | 
			
		||||
	# The command to run
 | 
			
		||||
	CMD=${2}
 | 
			
		||||
 | 
			
		||||
	# Execution under root CGgroup
 | 
			
		||||
	if [ "x/" == "x$CGP" ]; then
 | 
			
		||||
 | 
			
		||||
	  $FIND $CGMOUNT -type d -maxdepth 0 | \
 | 
			
		||||
	  while read CGPATH; do
 | 
			
		||||
		# Move this shell into that control group
 | 
			
		||||
		echo $$ > $CGPATH/cgroup.procs
 | 
			
		||||
		echo "Moving task into root CGroup ($CGPATH)"
 | 
			
		||||
	  done
 | 
			
		||||
 | 
			
		||||
	# Execution under specified CGroup
 | 
			
		||||
	else
 | 
			
		||||
 | 
			
		||||
	  # Check if the required CGroup exists
 | 
			
		||||
	  $FIND $CGMOUNT -type d -mindepth 1 | \
 | 
			
		||||
	  $GREP "$CGP" &>/dev/null
 | 
			
		||||
	  if [ $? -ne 0 ]; then
 | 
			
		||||
		echo "ERROR: could not find any $CGP cgroup under $CGMOUNT"
 | 
			
		||||
		exit 1
 | 
			
		||||
	  fi
 | 
			
		||||
 | 
			
		||||
	  $FIND $CGMOUNT -type d -mindepth 1 | \
 | 
			
		||||
	  $GREP "$CGP" | \
 | 
			
		||||
	  while read CGPATH; do
 | 
			
		||||
		  # Move this shell into that control group
 | 
			
		||||
		  echo $$ > $CGPATH/cgroup.procs
 | 
			
		||||
		  echo "Moving task into $CGPATH"
 | 
			
		||||
	  done
 | 
			
		||||
 | 
			
		||||
	fi
 | 
			
		||||
 | 
			
		||||
	# Execute the command
 | 
			
		||||
	$CMD
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
cgroups_tasks_move() {
 | 
			
		||||
	SRC_GRP=${1}
 | 
			
		||||
	DST_GRP=${2}
 | 
			
		||||
	GREP_EXCLUSE=${3:-''}
 | 
			
		||||
 | 
			
		||||
	cat $SRC_GRP/tasks | while read TID; do
 | 
			
		||||
	  echo $TID > $DST_GRP/cgroup.procs
 | 
			
		||||
	done
 | 
			
		||||
 | 
			
		||||
	[ "$GREP_EXCLUSE" = "" ] && exit 0
 | 
			
		||||
 | 
			
		||||
	PIDS=`ps | $GREP "$GREP_EXCLUSE" | awk '{print $2}'`
 | 
			
		||||
	PIDS=`echo $PIDS`
 | 
			
		||||
	echo "PIDs to save: [$PIDS]"
 | 
			
		||||
	for TID in $PIDS; do
 | 
			
		||||
	  CMDLINE=`cat /proc/$TID/cmdline`
 | 
			
		||||
	  echo "$TID : $CMDLINE"
 | 
			
		||||
	  echo $TID > $SRC_GRP/cgroup.procs
 | 
			
		||||
	done
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
################################################################################
 | 
			
		||||
# Main Function Dispatcher
 | 
			
		||||
################################################################################
 | 
			
		||||
@@ -92,6 +158,12 @@ cpufreq_trace_all_frequencies)
 | 
			
		||||
cgroups_get_attributes)
 | 
			
		||||
	cgroups_get_attributes $*
 | 
			
		||||
	;;
 | 
			
		||||
cgroups_run_into)
 | 
			
		||||
    cgroups_run_into $*
 | 
			
		||||
    ;;
 | 
			
		||||
cgroups_tasks_move)
 | 
			
		||||
	cgroups_tasks_move $*
 | 
			
		||||
	;;
 | 
			
		||||
ftrace_get_function_stats)
 | 
			
		||||
    ftrace_get_function_stats
 | 
			
		||||
    ;;
 | 
			
		||||
 
 | 
			
		||||
@@ -275,3 +275,22 @@ class CgroupsModule(Module):
 | 
			
		||||
            return None
 | 
			
		||||
        return self.controllers[kind]
 | 
			
		||||
 | 
			
		||||
    def run_into(self, cgroup, cmdline):
 | 
			
		||||
        """
 | 
			
		||||
        Run the specified command into the specified CGroup
 | 
			
		||||
        """
 | 
			
		||||
        return self.target._execute_util(
 | 
			
		||||
            'cgroups_run_into {} {}'.format(cgroup, cmdline),
 | 
			
		||||
            as_root=True)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def cgroups_tasks_move(self, srcg, dstg, exclude=''):
 | 
			
		||||
        """
 | 
			
		||||
        Move all the tasks from the srcg CGroup to the dstg one.
 | 
			
		||||
        A regexps of tasks names can be used to defined tasks which should not
 | 
			
		||||
        be moved.
 | 
			
		||||
        """
 | 
			
		||||
        return self.target._execute_util(
 | 
			
		||||
            'cgroups_tasks_move {} {} {}'.format(srcg, dstg, exclude),
 | 
			
		||||
            as_root=True)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user