If you are a bit into using the command line, chances are high you know
Depending on your environment, it’s either a command of your shell or a real program in
In both cases, it provides a way to run another program and easily get its resource usage.
Say it takes 5 seconds for a command to do something,
time will tell you.
While pretty handy for simple commands, I recently needed to understand a whole tree of processes. Think of a build system which takes a long time to build some documentation. You know it takes that many minutes in total, but which part is the one you should optimize? In particular if the build system calls many processes which in turn fork more commands and so on…
My solution was the following:
Compiling this code gives a shared library:
The result can be injected into processes via the environment variable
By specifying a shared library, the dynamic loader will “preload” our code into each process.
Eventually this calls the destructor
trace_end and adds the spent user time to the file specified via
So in total, a traced execution of a command, say
make, looks like this:
Note that it sets an absolute path for
This is probably a good idea so the code does not need to rely on the current working directory.
After the command finished, you get a list of all processes and how long they took. This needs some further processing to understand the data. To get an idea, here’s some hacky Python script to sum up the time spent by executable. At the end, it prints the executables called most often and spending the most time:
No, the output is not nicely formatted, but it was enough to get me the information I needed. In case you also need something like this, go use the code and adapt it if required — you are welcome!
You do not need to agree with my opinions expressed in this blog post, and I'm fine with different views on certain topics. However, if there is a technical fault please send me a message so that I can correct it!