4

I'm pretty sure the Linux kernel has a feature which allows to track all the reads and writes (IO) of an application and all its children however I haven't seen any utilities which can calculate it and show it.

For instance for CPU time you could simply use time and get neat CPU use information:

$ time cat --version > /dev/null

real    0m0.001s
user    0m0.001s
sys 0m0.000s

I'm looking for something similar in regard to IO, e.g.

$ calc_io task
Bytes read: 123456
Bytes written: 0

Of course, we have /proc/$PID/io which contains runtime information but tracking it for applications which spawn and destroy children dynamically, e.g. web-browsers seems like a daunting task. I guess if you run strace -fF firefox then monitor all children being spawned and try to track in real time /proc/$PID/io - nah, seems like too difficult to implement and then how often will you poll this file for information? Children may exist for a split second.

Another idea is to use cgroups but then what if I don't want to use them? Also I've checked /sys/fs/cgroup and I don't see any relevant statistics.

5

1 Answer 1

1
+100

I came across this post and found it very interesting. I thought this problem was not that difficult since the question you are asking is quite natural after all.

I could only find an imperfect and incomplete solution. I decided to post it anyway, as the question was not answered yet. This requires a system with systemd and cgroups2 (I read what you said about it but it might be interesting to see this solution). I learned about both, I don't master them.
I tested only on an arch-based linux distribution.

~]$ cat /etc/systemd/system/user\@1000.service.d/override.conf 
[Service]
Delegate=pids memory io

It seems that you need to "delegate" io controller to your "user systemd sub tree" to use this as an unprivileged user (I can't point one specific place. man systemd.resource-control. https://systemd.io/CGROUP_DELEGATION . https://wiki.archlinux.org/title/cgroups#As_unprivileged_user )

~]$ cat ~/.config/systemd/user/my.slice 
[Slice]
IOAccounting=true

Then create a slice with IOAccounting enabled to run you processes in.

reboot

~]$ cat foo.sh 
#!/bin/sh 

dd if=/dev/random of=/home/yarl/bar bs=1M count=7
dd if=/dev/random of=/home/yarl/bar bs=1M count=3

~]$ systemd-run --user --slice=my.slice /home/yarl/foo.sh

~]$ systemctl --user status my.slice
● my.slice - Slice /my
     Loaded: loaded (/home/yarl/.config/systemd/user/my.slice; static)
     Active: active since Sun 2021-11-07 20:25:20 CET; 12s ago
         IO: 100.0K read, 10.0M written
      Tasks: 0
     Memory: 3.2M
        CPU: 162ms
     CGroup: /user.slice/user-1000.slice/[email protected]/my.slice

nov. 07 20:25:20 pbpro systemd[1229]: Created slice Slice /my.
1
  • Wow, amazing, kinda convoluted but it works. Great many thanks. Would be nice to get a utility which automates all of that or a command which does that automatally. We already have e.g. systemd-run --user -t -p MemoryMax=300M command which operates with cgroups which doesn't need to edit anything and works under a normal user account. Commented Nov 7, 2021 at 20:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .