Sunday, January 12, 2014

Dtrace : Time spent in system call or function

Dtrace is a awk and C++ like programming language also called D Language.
It can be used very efficiently to provide amazing results that other tools can't provide.
But Dtrace is not the replacement of other tools but you can say Dtrace is a complement to other tools.
Although it is not the first tool to be used while diagnosing a problem but it can be used at a later point of time to dig much deeper into the particular area of problem.

Let us take an example, if you are suspecting an application to be taking more time than expected then you may want to use first other tools to verify things like physical memory, CPU usage, swap etc. And then later we may want to start looking at the application.

Let us see if we just want to see which system calls or functions in an application are taking most time, then we can use the below simple script :

#!/usr/sbin/dtrace -s
pid$1:::entry
{
    t[probefunc] = timestamp;
}
pid$1:::return
/t[probefunc]/
{
    @funct[probefunc] = sum(timestamp - t[probefunc]);
    t[probefunc] = 0;
}


How to run this script ?
Save this script in a file eg. myprobe.d and give executable permissions. But note that ONLY root user or user with equivalent role can execute the script and provide process id "PID" of the application as an argument.
Now run the script as below and see the results :
# myprobe.d <PID>

In future posts I will explain what more about the Dtrace in details. This was just an introduction to see what Dtrace can do.