CPU accounting
authorRalph Ronnquist <ralph.ronnquist@gmail.com>
Mon, 28 Feb 2022 10:21:29 +0000 (21:21 +1100)
committerRalph Ronnquist <ralph.ronnquist@gmail.com>
Mon, 28 Feb 2022 10:21:29 +0000 (21:21 +1100)
control

diff --git a/control b/control
index c2901f17570ac8e1615335fb96033b04c7c0e0f2..304c93bd1400b6a310f17c8e90288d5594b3cc83 100755 (executable)
--- a/control
+++ b/control
@@ -102,6 +102,26 @@ setup_rootfs() {
     fi
 }
 
+# Set up cgroup CPU accounting (cpuacct)
+accounting() {
+        # 1-- once, system wide
+        grep -q "/sys/fs/cgroup cgroup" /proc/mounts || \
+            mount -t cgroup -ocpuacct none /sys/fs/cgroup
+        if [ -d /sys/fs/cgroup/$NAME ] ; then
+            : # reset?
+        else
+            mkdir /sys/fs/cgroup/$NAME
+           # the following fixes things that sometimes are broken?
+           for f in cpuset.mems cpuset.cpus ; do
+               [ -z "$(cat /sys/fs/cgroups/$NAME/$F)" ] && \
+                   echo 0 > /sys/fs/cgroups/$NAME/$F
+           done
+
+        fi
+       # Register this task for subhost accounting
+        echo $$ > /sys/fs/cgroup/$NAME/tasks
+}
+
 case "$CMD" in
     start)
        [ -e "/run/netns/$NSNAME" ] || setup_network
@@ -109,6 +129,7 @@ case "$CMD" in
        setup_rootfs
        START=/bin/bash
        [ -x $TARGET/startup ] && START=/startup
+       accounting
        exec ip netns exec $NSNAME unshare \
             --fork --pid --mount-proc --kill-child \
             --uts --ipc --mount --cgroup \
@@ -118,6 +139,7 @@ case "$CMD" in
        umount $TARGET
        [ -e $IMAGE ] && umount $MOUNT
        ip netns del $NSNAME
+       rmdir /sys/fs/cgroup/$NAME
        ;;
     *)
        usage