For performance-related questions, aggregated data is often more useful than individual data points. DTrace provides several built-in aggregating functions. When an aggregating function is applied to subsets of a collection of data, then applied again to the results of the analysis of those subsets, the results are identical to the results returned by the aggregating function when it is applied to the collection as a whole.
The DTrace facility stores a running count of data items for aggregations. The aggregating functions store only the current intermediate result and the new element that the function is being applied to. The intermediate results are allocated on a per-CPU basis. Because this allocation scheme does not require locks, the implementation is inherently scalable.
A DTrace aggregation takes the following general form:
@name[ keys ] = aggfunc( args );
In this general form, the variables are defined as follows:
The name of the aggregation, preceded by the @ character.
A comma-separated list of D expressions.
One of the DTrace aggregating functions.
A comma-separated list of arguments appropriate to the aggregating function.
Table 2-1 DTrace Aggregating Functions
|
Example 2-14 Using an Aggregating Function
This example uses the count aggregating function to count the number of write(2) system calls per process. The aggregation does not output any data until the dtrace command is terminated. The output data represents a summary of the data collected during the time that the dtrace command was active.
# cat writes.d #!/usr/sbin/dtrace -s syscall::write:entry] { @numWrites[execname] = count(); } # ./writes.d dtrace: script 'writes.d' matched 1 probe ^C dtrace 1 date 1 bash 3 grep 20 file 197 ls 201