Next: 8 Statistics (aggregates)
Up: SystemTap Language Reference
Previous: 6 Statement types
Contents
Index
Subsections
7 Associative arrays
Associative arrays are implemented as hash tables with a maximum size set
at startup. Associative arrays are too large to be created dynamically for
individual probe handler runs, so they must be declared as global. The basic
operations for arrays are setting and looking up elements. These operations
are expressed in awk syntax: the array name followed by an opening bracket
([), a comma-separated list of up to five index index expressions, and
a closing bracket (]). Each index expression may be a string or a number,
as long as it is consistently typed throughout the script.
-
# Increment the named array slot:
foo [4,"hello"] ++
# Update a statistic:
processusage [uid(),execname()] ++
# Set a timestamp reference point:
times [tid()] = get_cycles()
# Compute a timestamp delta:
delta = get_cycles() - times [tid()]
Array elements may be set to a number or a string. The type must be consistent
throughout the use of the array. The first assignment to the array defines
the type of the elements. Unset array elements may be fetched and return
a null value (zero or empty string) as appropriate, but they are not seen
by a membership test.
Array sizes can be specified explicitly or allowed to default to the maximum
size as defined by MAXMAPENTRIES. See Section 1.6
for details on changing MAXMAPENTRIES.
You can explicitly specify the size of an array as follows:
-
global ARRAY[<size>]
If you do not specify the size parameter, then the array is created to hold
MAXMAPENTRIES number of elements
7.4 Iteration, foreach
Like awk, SystemTap's foreach creates a loop that iterates over key tuples
of an array, not only values. The iteration may be sorted by any single key
or a value by adding an extra plus symbol (+) or minus symbol (-) to the
code. The following are examples.
-
# Simple loop in arbitrary sequence:
foreach ([a,b] in foo)
fuss_with(foo[a,b])
# Loop in increasing sequence of value:
foreach ([a,b] in foo+) { ... }
# Loop in decreasing sequence of first key:
foreach ([a-,b] in foo) { ... }
The break and continue statements also work inside foreach
loops. Since arrays can be large but probe handlers must execute quickly,
you should write scripts that exit iteration early, if possible. For simplicity,
SystemTap forbids any modification of an array during iteration with a foreach.
Next: 8 Statistics (aggregates)
Up: SystemTap Language Reference
Previous: 6 Statement types
Contents
Index