Associative Arrays
SystemTap also supports the use of associative arrays. While an ordinary variable represents a single value, associative arrays can represent a list of values. Simply put, an associative array is a collection of unique keys; each key in the array has a value associated with it. Illustrating this visually would be similar to creating a two-column table: the first column would have the unique key, while the second column would have each key's associated value. Each unique key and its associated value is referred to as a key pair.
Since associative arrays are normally processed in multiple probes (as we will demonstrate later), they are declared as global variables in the SystemTap script. The syntax for manipulating arrays (i.e. accessing elements in an associative array) is similar to that of awk, and is as follows:
array_name[index_expression] operation
Here, the array_name is any arbitrary name the array uses. The index_expression is used to refer to a specific unique key (or set of unique keys) in the array, and the operation defines what to do with the index_expression. To illustrate, let us try to build an array named foo that specifies the ages of three people (i.e. the unique keys): tom, dick, and harry. To assign them the ages (i.e. associated values) of 23, 24, and 25 respectively, we'd use the following array statements:
Basic Array Statements
foo["tom"] = 23
foo["dick"] = 24
foo["harry"] = 25
Important
All associate arrays must be declared as global, regardless of whether the associate array is used in one or multiple probes.
Tuples
Another important point to remember in arrays is that each element therein (i.e. the indexed expression) exists in a slot. A key pair's slot is defined by the order in which each pair's unique key is defined. In our sample array foo in , the key pair that uses the unique key tom is in the first slot, since tom was the first unique key to be defined. dick is in the second slot, and so on.
The sequence in which each key pair appears in an array (as defined by each pair's slot) is referred to as a tuple. Tuples allow us to refer to key pairs in an array by the order in which they appear in the sequence.
For example, the array statements in set 23 as the associated value of the unique key tom. Given the same array foo, we can increment the associated value of tom by 1 using the operator ++, like so:
foo["tom"] ++
The above statement will increase the associated value of unique key tom to 24. Now, looking back at , we know that dick was the first uniqe key to be defined. As such, we can perform the same operation (i.e. incrementing associated value by 1) to dick using the following statement:
foo[2] ++
Note
You can specify up to 5 index expressons in an array statement, each one delimited by a comma (,). This is useful if you wish to perform the same operation to a set of key pairs. For example, to increase the associated value of all the key pairs defined by , you can use the following statement:
foo["tom",2,"harry"] ++