12.6.3 Global Variables

D provides fundamental data types for integers and floating-point constants. You can perform arithmetic only on integers in D programs. D does not support floating-point operations. D provides floating-point types for compatibility with ANSI-C declarations and types. You can trace floating-point data objects and use the printf() function to format them for output. In the current implementation, DTrace supports only the 64-bit data model for writing D programs.

You can use declarations to introduce D variables and external C symbols, or to define new types for use in D. 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 when the program exits.

BEGIN
{
  i = 0;
  trace(i);
}

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

END
{
  trace(i);
}

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

# dtrace -s tick.d 
dtrace: script 'tick.d' matched 3 probes
CPU     ID               FUNCTION:NAME
  1      1                       :BEGIN       0
  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

Whenever a probe is triggered, dtrace displays the number of the CPU core on which the process indicated by its ID is running, and the name of the function and the probe. BEGIN and END are DTrace probes that trigger when the dtrace program starts and finishes.

To suppress all output except that from printa(), printf(), and trace(), specify #pragma D option quiet in the program or the -q option to dtrace.

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