1
0
mirror of https://github.com/ARM-software/devlib.git synced 2024-10-06 02:40:50 +01:00

Merge pull request #33 from derkling/fix-cgroups-noprefix

Fix cgroups noprefix
This commit is contained in:
setrofim 2016-04-27 08:55:18 +01:00
commit 232204633f
2 changed files with 145 additions and 13 deletions

View File

@ -4,6 +4,7 @@ CMD=$1
shift
BUSYBOX=${BUSYBOX:-__DEVLIB_BUSYBOX__}
FIND=${FIND:-$BUSYBOX find}
GREP=${GREP:-$BUSYBOX grep}
SED=${SED:-$BUSYBOX sed}
@ -61,12 +62,93 @@ ftrace_get_function_stats() {
################################################################################
cgroups_get_attributes() {
[[ $# -eq 2 ]] || exit -1
test $# -eq 2 || exit -1
CGROUP="$1"
CONTROLLER="$2"
$GREP '' $CGROUP/* | \
$GREP "$CONTROLLER\." | \
$SED -e "s|$CONTROLLER\.||" -e "s|$CGROUP/||"
# Check if controller is mounted with "noprefix" option, which is quite
# common on Android for backward compatibility
ls $CGROUP/$CONTROLLER\.* 2>&1 >/dev/null
if [ $? -eq 0 ]; then
# no "noprefix" option, attributes format is:
# mnt_point/controller.attribute_name
$GREP '' $CGROUP/* | \
$GREP "$CONTROLLER\." | \
$SED -e "s|$CONTROLLER\.||" -e "s|$CGROUP/||"
else
# "noprefix" option, attribute format is:
# mnt_point/attribute_name
$GREP '' $(\
$FIND $CGROUP -type f -maxdepth 1 |
$GREP -v -e ".*tasks" -e ".*cgroup\..*") | \
$SED "s|$CGROUP/||"
fi
}
cgroups_run_into() {
# Control groups mount point
CGMOUNT=${CGMOUNT:-/sys/fs/cgroup/devlib_*}
# The control group we want to run into
CGP=${1}
shift 1
# The command to run
CMD="${@}"
# 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
exec $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
}
################################################################################
@ -92,6 +174,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
;;

View File

@ -34,6 +34,7 @@ class Controller(object):
self.mount_name = 'devlib_'+kind
self.kind = kind
self.target = None
self._noprefix = False
self.logger = logging.getLogger('cgroups.'+self.kind)
self.mount_point = None
@ -68,8 +69,15 @@ class Controller(object):
self.mount_point),
as_root=True)
self.logger.debug('Controller %s mounted under: %s',
self.kind, self.mount_point)
# Check if this controller uses "noprefix" option
output = target.execute('mount | grep "{} "'.format(self.mount_name))
if 'noprefix' in output:
self._noprefix = True
# self.logger.debug('Controller %s using "noprefix" option',
# self.kind)
self.logger.debug('Controller %s mounted under: %s (noprefix=%s)',
self.kind, self.mount_point, self._noprefix)
# Mark this contoller as available
self.target = target
@ -96,7 +104,8 @@ class Controller(object):
def list_all(self):
self.logger.debug('Listing groups for %s controller', self.kind)
output = self.target.execute('{} find {} -type d'\
.format(self.target.busybox, self.mount_point))
.format(self.target.busybox, self.mount_point),
as_root=True)
cgroups = []
for cg in output.splitlines():
cg = cg.replace(self.mount_point + '/', '/')
@ -154,7 +163,7 @@ class CGroup(object):
def exists(self):
try:
self.target.execute('[ -d {0} ]'\
.format(self.directory))
.format(self.directory), as_root=True)
return True
except TargetError:
return False
@ -168,7 +177,8 @@ class CGroup(object):
self.directory)
output = self.target._execute_util(
'cgroups_get_attributes {} {}'.format(
self.directory, self.controller.kind))
self.directory, self.controller.kind),
as_root=True)
for res in output.splitlines():
attr = res.split(':')[0]
value = res.split(':')[1]
@ -181,14 +191,25 @@ class CGroup(object):
if isiterable(attrs[idx]):
attrs[idx] = list_to_ranges(attrs[idx])
# Build attribute path
path = '{}.{}'.format(self.controller.kind, idx)
path = self.target.path.join(self.directory, path)
if self.controller._noprefix:
attr_name = '{}'.format(idx)
else:
attr_name = '{}.{}'.format(self.controller.kind, idx)
path = self.target.path.join(self.directory, attr_name)
self.logger.debug('Set attribute [%s] to: %s"',
path, attrs[idx])
# Set the attribute value
self.target.write_value(path, attrs[idx])
try:
self.target.write_value(path, attrs[idx])
except TargetError:
# Check if the error is due to a non-existing attribute
attrs = self.get()
if idx not in attrs:
raise ValueError('Controller [{}] does not provide attribute [{}]'\
.format(self.controller.kind, attr_name))
raise
def get_tasks(self):
task_ids = self.target.read_value(self.tasks_file).split()
@ -214,7 +235,11 @@ class CgroupsModule(Module):
@staticmethod
def probe(target):
return target.config.has('cgroups') and target.is_rooted
if not target.is_rooted:
return False
if target.file_exists('/proc/cgroups'):
return True
return target.config.has('cgroups')
def __init__(self, target):
super(CgroupsModule, self).__init__(target)
@ -275,3 +300,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)