2.9 Variables

D provides two basic types of variables for use in your tracing programs: scalar variables and associative arrays. An aggregation is a special kind of array variable. See Chapter 3, Aggregations for more information about aggregations.

To understand the scope of variables, consider the following figure.

Figure 2.2 Scope of Variables

The figure illustrates global, thread-local, and clause-local scopes.


In the figure, system execution is illustrated, showing elapsed time along the horizontal axis and thread number along the vertical axis. D probes fire at different times on different threads, and each time a probe fires, the D script is run. Any D variable would have one of the scopes that are described in the following table.

Scope

Syntax

Initial Value

Thread-safe?

Description

global

myname

0

No

Any probe that fires on any thread accesses the same instance of the variable.

Thread-local

self->myname

0

Yes

Any probe that fires on a thread accesses the thread-specific instance of the variable.

Clause-local

this->myname

Not defined

Yes

Any probe that fires accesses an instance of the variable specific to that particular firing of the probe.

Note

Note the following additional information:

  • Scalar variables and associative arrays have a global scope and are not multi-processor safe (MP-safe). Because the value of such variables can be changed by more than one processor, there is a chance that a variable can become corrupted if more than one probe modifies it.

  • Aggregations are MP-safe even though they have a global scope because independent copies are updated locally before a final aggregation produces the global result.