Skip to content
Advertisement

Collect and sum statistics of `strace` commands?

I know that I can runstrace -c ls to collect system call statistics on the ls executable. However, I want to run the command strace -c {some executable here} mulitiple times over different executables, merge the individual results, and then write to a single file.

I want to merge the ‘syscall’ and the ‘calls’ columns. So for example, if ls makes 19 mmap system calls and tr makes 11 mmap system calls, I would like to merge those results so that the final statistics simply show 30 mmap system calls overall in some file. Moreover, if a system call only appears in one executable, it should still be included in the final results.

How can I do that?

Advertisement

Answer

Using grep to find mmap calls, which are then piped to datamash (on Debian variants, install withapt install datamash) to group by column 5, (i.e. mmap), and sum column 4:

for f in /bin/ls /bin/tr ; do strace -c $f 2>&1 ; done |  
    grep mmap | datamash -W -g5 sum 4

Output:

mmap    29

  1. Example showing what similar data looked like before it was summed:

    for f in /bin/false /bin/true ; do strace -c $f 2>&1 ; done |  
         grep mmap | tee /tmp/old | datamash -W -g5 sum 4 ; echo --- ; cat /tmp/old
    
    mmap    12
    ---
      0.00    0.000000           0         6           mmap
      0.00    0.000000           0         6           mmap
    
  2. The -s switch lets datamash group, sort, and separately sum all the syscalls:

    for f in /bin/pwd  /bin/false /bin/true ; do
         strace -c $f 2>&1 > /dev/null |
             grep -v ':' | cut --complement -c 42-50 |
             sed '1s/^% /%_/;2d' | head -n -2 ;  
    done | sed -n '1p;/^%/d;p' | datamash -HW -sg5 sum 4  | 
           xargs printf "%-12st%14sn"
    

    (Note that the strace output needs cleaning, which is done by cut, sed, et al…)

    Output:

    GroupBy(syscall)        sum(calls)
    access                       9
    arch_prctl                   3
    brk                          5
    close                       23
    execve                       3
    fstat                       22
    getcwd                       1
    ioctl                        1
    mmap                        31
    mprotect                    12
    munmap                       3
    open                        34
    read                         5
    write                        1
    
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement