System information

SystemTap—Filtering and Analyzing System Data 81
For more information about supported SystemTap functions, refer to the stap-
funcs man page.
5.3.3.2 Other Basic Constructs
Apart from functions, you can use several other common constructs in SystemTap
handlers, including variables, conditional statements (like if/else, while loops,
for loops, arrays or command line arguments.
Variables
Variables may be defined anywhere in the script. To define one, simply choose a
name and assign a value from a function or expression to it:
foo = gettimeofday( )
Then you can use the variable in an expression. From the type of values assigned to
the variable, SystemTap automatically infers the type of each identifier (string or
number). Any inconsistencies will be reported as errors. In the example above, foo
would automatically be classified as a number and could be printed via printf()
with the integer format specifier (%d).
However, by default, variables are local to the probe they are used in: They are ini-
tialized, used and disposed of at each handler evocation. To share variables between
probes, declare them global anywhere in the script. To do so, use the global key-
word outside of the probes:
Example5.4: Using Global Variables
global count_jiffies, count_ms
probe timer.jiffies(100) { count_jiffies ++ }
probe timer.ms(100) { count_ms ++ }
probe timer.ms(12345)
{
hz=(1000*count_jiffies) / count_ms
printf ("jiffies:ms ratio %d:%d => CONFIG_HZ=%d\n",
count_jiffies, count_ms, hz)
exit ()
}
This example script computes the CONFIG_HZ setting of the kernel by using timers
that count jiffies and milliseconds, then computing accordingly. (A jiffy is the du-
ration of one tick of the system timer interrupt. It is not an absolute time interval
unit, since its duration depends on the clock interrupt frequency of the particular