2.3.1 Example: Using tick.d

The following is an example of the tick.d program.

/* tick.d -- Perform an action at regular intervals */

BEGIN
{
  i = 0;
}

profile:::tick-1sec
{
  printf("i = %d\n",++i);
}

END
{
  trace(i);
}

In this example, the program declares and initializes the variable i when the D program starts, increments the variable and prints its value once every second, and displays the final value of i when the program exits.

When you run this program, it produces output that is similar to the following, until you type Ctrl-C:

# dtrace -s tick.d 
dtrace: script 'tick.d' matched 3 probes
CPU     ID                    FUNCTION:NAME
  1   5315                       :tick-1sec i = 1

  1   5315                       :tick-1sec i = 2

  1   5315                       :tick-1sec i = 3

  1   5315                       :tick-1sec i = 4

  1   5315                       :tick-1sec i = 5

  1   5315                       :tick-1sec i = 6

^C
  1   5315                       :tick-1sec i = 7

  0      2                             :END         7 

To suppress all of the output except the output from printf() and trace(), specify the -q option:

# dtrace -q -s tick.d 
i = 1
i = 2
i = 3
i = 4
^C
i = 5
5