2.3 Performing an Action at Specified Intervals

The profile provider provides the tick probes that you can use to sample some aspect of a system's state at regular intervals. You must load the profile kernel module to use these probes.

The following example program, tick.d, declares and initializes the variable i when the D program starts, displays its initial value, increments the variable and prints its value once every second, and displays the final value of i when the program exits.

Example 2.4 tick.d: Perform an action at regular intervals

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

BEGIN
{
  i = 0;
}

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

END
{
  trace(i);
}

When you run this program, it produces output such as the following until you type Ctrl-C:

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

  1    618                       :tick-1sec i = 2

  1    618                       :tick-1sec i = 3

  1    618                       :tick-1sec i = 4

  1    618                       :tick-1sec i = 5

^C
  0      2                       :END         5

You can suppress all output except that from printf() and trace() by specifying the -q option:

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