Este conteúdo não está disponível no idioma selecionado.
3.5. Array Operations in SystemTap
3.5.1. Assigning an Associated Value
=
to set an associated value to indexed unique pairs, as in:
array_name[index_expression] = value
index_expression
and value
. For example, you can use arrays to set a timestamp as the associated value to a process name (which you wish to use as your unique key), as in:
Example 3.12. Associating Timestamps to Process Names
foo[tid()] = gettimeofday_s()
tid()
value (i.e. the ID of a thread, which is then used as the unique key). At the same time, SystemTap also uses the function gettimeofday_s()
to set the corresponding timestamp as the associated value to the unique key defined by the function tid()
. This creates an array composed of key pairs containing thread IDs and timestamps.
tid()
returns a value that is already defined in the array foo
, the operator will discard the original associated value to it, and replace it with the current timestamp from gettimeofday_s()
.
3.5.2. Reading Values From Arrays
array_name[index_expression]
statement as an element in a mathematical expression. For example:
Example 3.13. Using Array Values in Simple Computations
delta = gettimeofday_s() - foo[tid()]
foo
was built using the construct in Example 3.12, “Associating Timestamps to Process Names” (from Section 3.5.1, “Assigning an Associated Value”). This sets a timestamp that will serve as a reference point, to be used in computing for delta
.
delta
by subtracting the associated value of the key tid()
from the current gettimeofday_s()
. The construct does this by reading the value of tid()
from the array. This particular construct is useful for determining the time between two events, such as the start and completion of a read operation.
Note
index_expression
cannot find the unique key, it returns a value of 0 (for numerical operations, such as Example 3.13, “Using Array Values in Simple Computations”) or a null/empty string value (for string operations) by default.
3.5.3. Incrementing Associated Values
++
to increment the associated value of a unique key in an array, as in:
array_name[index_expression] ++
index_expression
. For example, if you wanted to tally how many times a specific process performed a read to the virtual file system (using the event vfs.read
), you can use the following probe:
Example 3.14. vfsreads.stp
probe vfs.read { reads[execname()] ++ }
gnome-terminal
(i.e. the first time gnome-terminal
performs a VFS read), that process name is set as the unique key gnome-terminal
with an associated value of 1. The next time that the probe returns the process name gnome-terminal
, SystemTap increments the associated value of gnome-terminal
by 1. SystemTap performs this operation for all process names as the probe returns them.
3.5.4. Processing Multiple Elements in an Array
reads
, but how?
foreach
statement. Consider the following example:
Example 3.15. cumulative-vfsreads.stp
global reads probe vfs.read { reads[execname()] ++ } probe timer.s(3) { foreach (count in reads) printf("%s : %d \n", count, reads[count]) }
foreach
statement uses the variable count
to reference each iteration of a unique key in the array reads
. The reads[count]
array statement in the same probe retrieves the associated value of each unique key.
foreach
statement in Example 3.15, “cumulative-vfsreads.stp” prints all iterations of process names in the array, and in no particular order. You can instruct the script to process the iterations in a particular order by using +
(ascending) or -
(descending). In addition, you can also limit the number of iterations the script needs to process with the limit value
option.
probe timer.s(3) { foreach (count in reads- limit 10) printf("%s : %d \n", count, reads[count]) }
foreach
statement instructs the script to process the elements in the array reads
in descending order (of associated value). The limit 10
option instructs the foreach
to only process the first ten iterations (i.e. print the first 10, starting with the highest value).
3.5.5. Clearing/Deleting Arrays and Array Elements
delete
operator to delete elements in an array, or an entire array. Consider the following example:
Example 3.16. noncumulative-vfsreads.stp
global reads probe vfs.read { reads[execname()] ++ } probe timer.s(3) { foreach (count in reads) printf("%s : %d \n", count, reads[count]) delete reads }
delete reads
statement clears the reads
array within the probe.
Note
global reads, totalreads probe vfs.read { reads[execname()] ++ totalreads[execname()] ++ } probe timer.s(3) { printf("=======\n") foreach (count in reads-) printf("%s : %d \n", count, reads[count]) delete reads } probe end { printf("TOTALS\n") foreach (total in totalreads-) printf("%s : %d \n", total, totalreads[total]) }
reads
and totalreads
track the same information, and are printed out in a similar fashion. The only difference here is that reads
is cleared every 3-second period, whereas totalreads
keeps growing.
3.5.6. Using Arrays in Conditional Statements
if
statements. This is useful if you want to execute a subroutine once a value in the array matches a certain condition. Consider the following example:
Example 3.17. vfsreads-print-if-1kb.stp
global reads probe vfs.read { reads[execname()] ++ } probe timer.s(3) { printf("=======\n") foreach (count in reads-) if (reads[count] >= 1024) printf("%s : %dkB \n", count, reads[count]/1024) else printf("%s : %dB \n", count, reads[count]) }
if
statement in the script converts and prints it out in kB
.
You can also test whether a specific unique key is a member of an array. Further, membership in an array can be used in if
statements, as in:
if([index_expression] in array_name) statement
Example 3.18. vfsreads-stop-on-stapio2.stp
global reads probe vfs.read { reads[execname()] ++ } probe timer.s(3) { printf("=======\n") foreach (count in reads+) printf("%s : %d \n", count, reads[count]) if(["stapio"] in reads) { printf("stapio read detected, exiting\n") exit() } }
if(["stapio"] in reads)
statement instructs the script to print stapio read detected, exiting
once the unique key stapio
is added to the array reads
.
3.5.7. Computing for Statistical Aggregates
<<< value
.
Example 3.19. stat-aggregates.stp
global reads probe vfs.read { reads[execname()] <<< count }
<<< count
stores the amount returned by count
to to the associated value of the corresponding execname()
in the reads
array. Remember, these values are stored; they are not added to the associated values of each unique key, nor are they used to replace the current associated values. In a manner of speaking, think of it as having each unique key (execname()
) having multiple associated values, accumulating with each probe handler run.
Note
count
returns the amount of data written by the returned execname()
to the virtual file system.
@extractor(variable/array index expression)
. extractor
can be any of the following integer extractors:
- count
- Returns the number of all values stored into the variable/array index expression. Given the sample probe in Example 3.19, “stat-aggregates.stp”, the expression
@count(writes[execname()])
will return how many values are stored in each unique key in arraywrites
. - sum
- Returns the sum of all values stored into the variable/array index expression. Again, given sample probe in Example 3.19, “stat-aggregates.stp”, the expression
@sum(writes[execname()])
will return the total of all values stored in each unique key in arraywrites
. - min
- Returns the smallest among all the values stored in the variable/array index expression.
- max
- Returns the largest among all the values stored in the variable/array index expression.
- avg
- Returns the average of all values stored in the variable/array index expression.
Example 3.20. Multiple Array Indexes
global reads probe vfs.read { reads[execname(),pid()] <<< 1 } probe timer.s(3) { foreach([var1,var2] in reads) printf("%s (%d) : %d \n", var1, var2, @count(reads[var1,var2])) }
reads
. Note how the foreach
statement uses the same number of variables (i.e. var1
and var2
) contained in the first instance of the array reads
from the first probe.