From c89f712923fbd4d09375596d28f5f8025dfcd1d8 Mon Sep 17 00:00:00 2001
From: Brendan Jackman <brendan.jackman@arm.com>
Date: Fri, 25 Nov 2016 11:56:42 +0000
Subject: [PATCH 1/5] cgroups: Raise RuntimeError in freeze if no freezer
 controller

Without this patch, this will result in an error due to trying to call
`.cgroup` on None. Making this a RuntimeError instaed means that a) you
get a more useful error message and b) you can catch the exception
without blanket `except Exception as e`.
---
 devlib/module/cgroups.py | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/devlib/module/cgroups.py b/devlib/module/cgroups.py
index e4b387c..dd514d9 100644
--- a/devlib/module/cgroups.py
+++ b/devlib/module/cgroups.py
@@ -448,6 +448,8 @@ class CgroupsModule(Module):
 
         # Create Freezer CGroup
         freezer = self.controller('freezer')
+        if freezer is None:
+            raise RuntimeError('freezer cgroup controller not present')
         freezer_cg = freezer.cgroup('/DEVLIB_FREEZER')
         thawed_cg = freezer.cgroup('/')
 

From 2f35999f37f667be747306ef29fae2b3dbfd4c46 Mon Sep 17 00:00:00 2001
From: Brendan Jackman <brendan.jackman@arm.com>
Date: Fri, 25 Nov 2016 14:25:28 +0000
Subject: [PATCH 2/5] cgroups: Mount cgroups controllers in devlib working dir

Android seems to have a buggy `mount` command which causes
`mount -o remount` to result in duplicated mounts. We can't unmonunt and
then re-mount /sys/fs/cgroup because there may be pre-existing mounts at
subdirectories. So we create a 'cgroups' directory in the devlib working
directory and mount cgroup controller FS's there.
---
 devlib/module/cgroups.py | 21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)

diff --git a/devlib/module/cgroups.py b/devlib/module/cgroups.py
index dd514d9..dfd22c4 100644
--- a/devlib/module/cgroups.py
+++ b/devlib/module/cgroups.py
@@ -300,7 +300,6 @@ class CgroupsModule(Module):
 
     name = 'cgroups'
     stage = 'setup'
-    cgroup_root = '/sys/fs/cgroup'
 
     @staticmethod
     def probe(target):
@@ -315,22 +314,6 @@ class CgroupsModule(Module):
 
         self.logger = logging.getLogger('CGroups')
 
-        # Initialize controllers mount point
-        mounted = self.target.list_file_systems()
-        if self.cgroup_root not in [e.mount_point for e in mounted]:
-            self.target.execute('mount -t tmpfs {} {}'\
-                    .format('cgroup_root',
-                            self.cgroup_root),
-                            as_root=True)
-        else:
-            self.logger.debug('cgroup_root already mounted at %s',
-                    self.cgroup_root)
-
-        # Ensure CGroups is mounted RW
-        self.target.execute('mount -o remount,rw {}'\
-                            .format(self.cgroup_root),
-                            as_root=True)
-
         # Load list of available controllers
         controllers = []
         subsys = self.list_subsystems()
@@ -346,7 +329,9 @@ class CgroupsModule(Module):
             if not controller.probe(self.target):
                 continue
             try:
-                controller.mount(self.target, self.cgroup_root)
+                cgroup_root = target.path.join(target.working_directory,
+                                               'cgroups')
+                controller.mount(self.target, cgroup_root)
             except TargetError:
                 message = 'cgroups {} controller is not supported by the target'
                 raise TargetError(message.format(controller.kind))

From e45fcca385beb424fb979cf7bc2b1cfb3f05c6e5 Mon Sep 17 00:00:00 2001
From: Brendan Jackman <brendan.jackman@arm.com>
Date: Fri, 25 Nov 2016 16:01:09 +0000
Subject: [PATCH 3/5] shutils/cgroups: Fix typo

---
 devlib/bin/scripts/shutils.in | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/devlib/bin/scripts/shutils.in b/devlib/bin/scripts/shutils.in
index b95ad2e..0bfab5c 100755
--- a/devlib/bin/scripts/shutils.in
+++ b/devlib/bin/scripts/shutils.in
@@ -160,7 +160,7 @@ cgroups_tasks_move() {
 	PIDS=`echo $PIDS`
 	echo "PIDs to save: [$PIDS]"
 	for TID in $PIDS; do
-	  COMM =`$CAT /proc/$TID/comm`
+	  COMM=`$CAT /proc/$TID/comm`
 	  echo "$TID : $COMM"
 	  echo $TID > $SRC_GRP/cgroup.procs
 	done

From 3dd4ea69b43c2ebb354b11c81e3dacfeffe5b65f Mon Sep 17 00:00:00 2001
From: Brendan Jackman <brendan.jackman@arm.com>
Date: Fri, 25 Nov 2016 16:04:34 +0000
Subject: [PATCH 4/5] cgroups: Remove quotes from grep expressions in
 move_all_tasks_to

These quotes end up being passed literally into the shutils function
arguments (i.e $3 is '-e', $4 is '"foo"') which means that the grep
command never finds matches. `exclude` is a list of comms, which don't
have spaces in them, so we can just remove the quotes.
---
 devlib/module/cgroups.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/devlib/module/cgroups.py b/devlib/module/cgroups.py
index dfd22c4..dd9543e 100644
--- a/devlib/module/cgroups.py
+++ b/devlib/module/cgroups.py
@@ -155,7 +155,7 @@ class Controller(object):
         # Build list of tasks to exclude
         grep_filters = ''
         for comm in exclude:
-            grep_filters += '-e "{}" '.format(comm)
+            grep_filters += '-e {} '.format(comm)
         logging.debug('   using grep filter: %s', grep_filters)
         if grep_filters != '':
             logging.debug('   excluding tasks which name matches:')

From 0dc65bddb67bf80153013f83086d9808bf87f851 Mon Sep 17 00:00:00 2001
From: Brendan Jackman <brendan.jackman@arm.com>
Date: Fri, 25 Nov 2016 16:01:20 +0000
Subject: [PATCH 5/5] shutils/cgroups: Use busybox `ps` to fix output format

Different `ps` implementations (e.g. Android vs GNU) have different
default columns and process selections.
---
 devlib/bin/scripts/shutils.in | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/devlib/bin/scripts/shutils.in b/devlib/bin/scripts/shutils.in
index 0bfab5c..2442386 100755
--- a/devlib/bin/scripts/shutils.in
+++ b/devlib/bin/scripts/shutils.in
@@ -9,6 +9,7 @@ GREP=${GREP:-$BUSYBOX grep}
 SED=${SED:-$BUSYBOX sed}
 CAT=${CAT:-$BUSYBOX cat}
 AWK=${AWK:-$BUSYBOX awk}
+PS=${PS:-$BUSYBOX ps}
 
 ################################################################################
 # CPUFrequency Utility Functions
@@ -156,7 +157,7 @@ cgroups_tasks_move() {
 
 	[ "x$FILTERS" = "x" ] && exit 0
 
-	PIDS=`ps | $GREP $FILTERS | $AWK '{print $2}'`
+	PIDS=`$PS -o comm,pid | $GREP $FILTERS | $AWK '{print $2}'`
 	PIDS=`echo $PIDS`
 	echo "PIDs to save: [$PIDS]"
 	for TID in $PIDS; do