lanĉo: a task launcher powered by cgroups

Vincent Bernat

Up­date (2021-07)

lanĉo does not work with cgroups v2, which are now en­abled by de­fault on mod­ern dis­tri­bu­tions. There is no plan to up­date it. A good al­ter­na­tive is sys­temd-run.

A few months ago, I was look­ing for a piece of soft­ware to spawn long-run­ning tasks on be­half of some dae­mon with the ben­e­fit of the tasks not be­ing in­ter­rupted when this dae­mon is restarted. Here are the spec­i­fi­cations:

  • No spe­cial priv­i­lege is needed to sub­mit a task.
  • Sub­mit­ted tasks are not known be­forehand.
  • Task out­put is redi­rected to a log file.
  • Sub­mit­ted tasks are iden­ti­fied by a pro­vided name.
  • With only the name as a ref­er­ence, a task can be checked for ex­is­tence or killed.
  • Tasks should not be in­ter­rupted by an un­re­lated event, like a con­fig­u­ra­tion change or a soft­ware upgrade.

The last re­quire­ment ex­plains why tasks are not spawned di­rectly by the dae­mon re­quest­ing them: its com­plex­ity or the way it needs to be op­er­ated may need fre­quent restarts. Even if it is pos­si­ble to re-ex­e­cute a dae­mon while keep­ing its chil­dren, like state­ful re-exec sup­port in Up­start, this is quite dif­fi­cult: in­ter­nal state should be se­ri­al­ized and re­stored. Part of this state can be con­tained in a third-party library.

While Up­start or sys­temd seemed to be good can­di­dates for this pur­pose, I didn’t find a straight­for­ward way to run ar­bi­trary tasks with­out any privilege.1

Here comes lanĉo.2 It is a very sim­ple task launcher. It can run any task, stop them and check if they are still run­ning. It lever­ages cgroups in re­cent Linux ker­nels and avoids the use of any daemon.

Quick tour#

Be­fore look­ing at how lanĉo works, let’s have a look at how it can be used. To avoid us­age con­flicts, each task is run in the con­text of a names­pace that needs to be ini­tialized:

$ sudo lanco testns init -u $(id -un) -g $(id -gn)

This is the only com­mand that needs to be run as root. Sub­se­quent ones can be run as a nor­mal user. Let’s run some task:

$ lanco testns run first-task openssl speed aes
$ lanco testns check first-task && echo "Still running"
Still running
$ lanco testns ls
testns
 ├ first-task
 │  → 28456 openssl speed aes
 ╯

The out­put of the task is logged into a file:

$ head -3 /var/log/lanco-testns/task-first-task.log
Doing aes-128 cbc for 3s on 16 size blocks: 8678442 aes-128 cbc's in 2.85s
Doing aes-128 cbc for 3s on 64 size blocks: 2478283 aes-128 cbc's in 2.99s
Doing aes-128 cbc for 3s on 256 size blocks: 628105 aes-128 cbc's in 3.00s

If the task is too long, we can kill it:

$ lanco testns run first-task openssl speed aes
$ lanco testns stop first-task

You can­not run a task that al­ready ex­ists or kill a task that does not exist:

$ lanco testns run first-task openssl speed aes
2013-06-09T22:50:34 [WARN/run] task first-task is already running
$ lanco testns stop second-task
2013-06-09T22:50:45 [WARN/stop] task second-task is not running

Thanks to the use of cgroups, lanĉo is able to track mul­ti­ple pro­cesses even when they are fork­ing away:3

$ lanco testns run second-task sh -c \
>  "while true; do (sleep 30 &)& sleep 1; done"
$ lanco testns ls
testns
 ├ first-task
 │  → 28456 openssl speed aes
 ├ second-task
 │  → 29572 sh -c while true; do (sleep 30 &)& sleep 1; done
 │  → 29575 sleep 30
 │  → 29593 sleep 30
 │  → 29596 sleep 30
 │  → 29599 sleep 30
 │  → 29622 sleep 30
 │  → 29644 sleep 1
 │  → 29645 sleep 30
 ╯
$ lanco testns stop second-task
$ lanco testns check second-task || echo "Killed!"
Killed!

Also, there is a top-like com­mand (lanco testns top):

lanco top
top-like output of running tasks

Using cgroups#

Con­trol groups (cgroups) is a mech­a­nism to par­ti­tion a set of tasks and their fu­ture chil­dren into hi­er­ar­chi­cal groups with a set of pa­rameters.

Hierarchy#

Let’s start with the hi­er­ar­chi­cal stuff first. To cre­ate a new hi­er­ar­chy, you have to mount the cgroup filesys­tem on some empty di­rec­tory. Usu­ally, cgroups hi­er­ar­chies are set up in /sys/​fs/​cgroups which is a tmpfs filesystem:

# mount -t tmpfs tmpfs /sys/fs/cgroup -o nosuid,nodev,noexec,relatime,mode=755

Now, we can cre­ate our first hi­erarchy:

# cd /sys/fs/cgroup
# mkdir my-first-hierarchy
# mount -t cgroup cgroup my-first-hierarchy -o name=my-first-hierarchy,none
# ls -1 my-first-hierarchy
cgroup.clone_children
cgroup.event_control
cgroup.procs
notify_on_release
release_agent
tasks

We will see later the pur­pose of none. The most in­ter­est­ing file is tasks. It con­tains the PIDs of all pro­cesses in our group. Since there are cur­rently no sub-groups, all pro­cesses are part of it. Let’s cre­ate a sub-group and at­tach a process to it:

# mkdir first-child
# cd first-child
# ls -1
cgroup.clone_children
cgroup.event_control
cgroup.procs
notify_on_release
tasks
# echo $$ > tasks
# cat tasks
23184
23311
# cat /proc/$$/cgroup
9:name=my-first-hierarchy:/first-child
8:perf_event:/
7:blkio:/
6:net_cls:/
5:freezer:/
4:devices:/
3:cpuacct,cpu:/
2:cpuset:/
1:name=systemd:/user/bernat/1

We have added our shell to the new cgroup. More­over, all its chil­dren will also be part of this group. This ex­plains why we have two tasks: the shell and cat. The last com­mand is quite in­ter­est­ing: for each hi­er­ar­chy, it shows which cgroup the task be­longs to. For a given hi­er­ar­chy, each task is ex­actly in one group.

The most use­ful fea­tures of lanĉo are done us­ing just this: a names­pace is a named hi­er­ar­chy and each task is en­closed into a cgroup so it can be tracked properly.

Subsystems#

A sub­sys­tem makes use of the task group­ing pro­vided by cgroups to as­sign re­sources to a set of tasks. Each avail­able sub­sys­tem can only be in one hi­er­ar­chy, so the usual way to set up things is to as­sign a hi­er­ar­chy for each sub­system.4

Let’s have a look at the cpuset sub­sys­tem: it al­lows one to as­sign tasks to spe­cific CPUs and mem­ory nodes (for NUMA sys­tems). Sup­pose we have 4 cores and we want to as­sign the first core to com­mon sys­tem tasks and the three re­main­ing ones to nginx:

# cd /sys/fs/cgroup
# mkdir cpuset
# mount -t cgroup cgroup cpuset -o cpuset
# echo 0-3 > cpuset/cpuset.cpus
# echo 0 > cpuset/cpuset.mems

# mkdir cpuset/system
# echo 0 > cpuset/system/cpuset.cpus
# echo 0 > cpuset/system/cpuset.mems
# for task in $(cat cpuset/tasks); do
>    echo $task > cpuset/system/tasks
> done

# mkdir cpuset/nginx
# echo 1-3 > cpuset/nginx/cpuset.cpus
# echo 0 > cpuset/nginx/cpuset.mems
# for task in $(pidof nginx); do
>    echo $task > cpuset/nginx/tasks
> done

The first mount is not needed if some­thing else (like sys­temd) has set up the ap­pro­pri­ate sub­sys­tem. Now, even if some sys­tem process is go­ing crazy, it won’t af­fect the per­for­mance of your webserver.

The ker­nel comes with more com­plete doc­u­men­ta­tion if you need ad­di­tional de­tails on cgroups. There are high-level tools to ma­nip­u­late them, like the tools pro­vided by libcg, but they are cur­rently quite buggy and invasive.

Use in lanĉo#

Here are the fea­tures used by lanĉo:

  • For each names­pace, a spe­cific named hi­er­ar­chy is created.
  • By set­ting the ap­pro­pri­ate per­mis­sions on the hi­er­ar­chy, an un­priv­i­leged user can cre­ate subgroups.
  • Each sub­mit­ted task has its own sub-group for track­ing purposes.
  • Ac­tions can be ex­e­cuted when a task ter­mi­nates by us­ing the re­lease agent mechanism.5
  • The cpuacct sub­sys­tem is used to track CPU us­age: a group is cre­ated in this sub­sys­tem for this purpose.