From 91018c73409bc6d5d01666f5bbf53d62af5e6c18 Mon Sep 17 00:00:00 2001 From: ddomingo Date: Thu, 23 Oct 2008 17:04:20 +1000 Subject: added Arrays and other minor edits --- doc/SystemTap_Beginners_Guide/en-US/Arrays.xml | 79 ++++++++++ .../en-US/ScriptConstructs.xml | 165 ++++++++++++++++++++ doc/SystemTap_Beginners_Guide/en-US/Scripts.xml | 173 +-------------------- .../en-US/Understanding_How_SystemTap_Works.xml | 1 + 4 files changed, 248 insertions(+), 170 deletions(-) create mode 100644 doc/SystemTap_Beginners_Guide/en-US/Arrays.xml create mode 100644 doc/SystemTap_Beginners_Guide/en-US/ScriptConstructs.xml (limited to 'doc/SystemTap_Beginners_Guide/en-US') diff --git a/doc/SystemTap_Beginners_Guide/en-US/Arrays.xml b/doc/SystemTap_Beginners_Guide/en-US/Arrays.xml new file mode 100644 index 00000000..13122189 --- /dev/null +++ b/doc/SystemTap_Beginners_Guide/en-US/Arrays.xml @@ -0,0 +1,79 @@ + + + +
+ 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 arranged in tabular format. 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 the associated value. + +Associative arrays are useful in processing data that would normally be represented best in tabular format. For example, let's say you wanted to see how many times any specific program performs a read to the virtual file system. In this case, your probe would use the event kernel.function("vfs_read"); execname() identifies which program or process performs the read. + + + vfsreads.stp + + +probe kernel.function("vfs_read") +{ + printf("%s\n", execname()) +} + + + + + will display the name of each program that performs a read to the virtual file system as each read is performed. This means that the output of will be a long list of process names, most of which will be repeating. For to be useful, you'd need to feed its output to another program that counts how many times each process name appears and tally the results. + +However, with the help of arrays, you can simplify the task using the following script: + + + vfsreads-using-arrays.stp + + +global reads +probe kernel.function("vfs_read") +{ + reads[execname()] += $count +} +probe timer.s(2) +{ + foreach (key in reads) + printf("%s : %d\n", key, reads[key]) + exit() +} + + + + +The handler in the first probe of does three things: + + + First, the handler defines the variable reads as the associative array. + Next, the handler uses execname() (the names of the processes that execute the VFS reads) as the array's unique keys. For example, once the process gnome-terminal performs a VFS read, the array creates a unique key named gnome-terminal, which has an initial associated value of 1. + Finally, the statement += $count increments the value associated with each unique key every time that unique key "occurs"; for example, each time that the process gnome-terminal performs a VFS read, the value associated with the unique key gnome-terminal gets incremented by 1. + + + + Note + In , $count is not a variable; rather, it is an operation that instructs the array to perform an increment to the array. This book will discuss other similar operations in detail later. + + +TBD WILL ADD MORE INFO ON ARRAY SYNTAX IN A SUBSEQUENT SECTION, FIX PREVIOUS NOTE TO XREF TO SECTION ONCE ITS BUILT + +The handler in the second probe of prints the script's output o the format name : value over the span of two seconds, then exits. name is the process that executed a VFS read, and value is how many times name executed a VFS read). For example: + + + Sample Output for <xref linkend="arraysvfsread"/> + +gnome-terminal : 64 +gnome-screensav : 128 +gnome-power-man : 512 +pcscd : 534 +cupsd : 1023 +mixer_applet2 : 1440 +gnome-vfs-daemo : 2048 +snmpd : 2048 + + + + +
\ No newline at end of file diff --git a/doc/SystemTap_Beginners_Guide/en-US/ScriptConstructs.xml b/doc/SystemTap_Beginners_Guide/en-US/ScriptConstructs.xml new file mode 100644 index 00000000..42054cb9 --- /dev/null +++ b/doc/SystemTap_Beginners_Guide/en-US/ScriptConstructs.xml @@ -0,0 +1,165 @@ + + + +
+Basic SystemTap Handler Constructs + + SystemTap supports the use of several basic constructs in handlers. The syntax for most of these handler constructs are mostly based on C and awk syntax. This section describes several of the most useful SystemTap handler constructs, which should provide you with enough information to write simple yet useful SystemTap scripts. + +
+ Variables + + Variables can be used freely throughout a handler; simply choose a name, assign it to a function, and use it in an expression. SystemTap automatically identifies whether a variable should be identified as a string or integer, based on the function it is assigned to. For instance, if you use set the variable foo to gettimeofday_s() (as in foo = gettimeofday_s()), then foo can be used as an integer argument (%d) in printf(). + + +Note, however, that by default variables are only local to the probe they are used in. This means that variables are initialized, used and disposed at each probe handler invocation. To share a variable between probes, declare the variable name first using global outside of any probe. Consider the following example: + + + timer-jiffies.stp + +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 () +} + + + + attempts to compute the CONFIG_HZ setting of the kernel using timers that count jiffies and milliseconds, then computing accordingly. The global statement allows the script to use the variables count_jiffies and count_ms (set in their own respective probes) to be shared with probe timer.ms(12345). + + + Note + The ++ notation in (i.e. count_jiffies ++ and count_ms ++) is used to increment the value of a variable by 1. In the following probe, count_jiffies is incremented by 1 every 100 jiffies: + +probe timer.jiffies(100) { count_jiffies ++ } + + In this instance, SystemTap understands that count_jiffies is an integer. Because no initial value was assigned to count_jiffies, its initial value is zero by default. + + +
+ + +
+ Conditional Statements + +In some cases, the output of a SystemTap script may be too big. To address this, you need to further refine the script's logic in order to delimit the output into something more relevant or useful to your probe. + + + +You can do this by using conditionals in handlers. SystemTap accepts the following types of conditional statements: + + + + + If/Else Statements + + Format: + +if (condition) + {statement} else {statement} + + + + ifelse.stp + +global countread, countnonread +probe vfs.read,vfs.write +{ + if (probefunc()=="vfs_read") { + countread ++ } + else {countnonread ++} +} +probe timer.s(5) { + exit()} +probe end { + printf("VFS reads total %d\n VFS writes total %d\n", countread, countnonread)} + + + + is a script that counts how many virtual file system reads (vfs.read) and writes (vfs.write) the system performs within a 5-second span. When run, the script increments the value of the variable countread by 1 if the name of the function it probed matches vfs_read (as noted by the condition if (probefunc()=="vfs_read")); otherwise, it increments countnonread (else {countnonread ++}). + + + + + + While Loops + + Format: + +while (condition) {statement} + + + + + + For Loops + + Format: + +for (argument1; argument2; argument3) {statement} + + + + + + + + + +These constructs are better illustrated in the different examples available in . + +will get back to these ones later +
+
+ Command-Line Arguments + You can also allow a SystemTap script to accept simple command-line arguments and declare them in the script without using target(). One way to do this is to use the variable notation $ or @. + + + +commandlineargs.stp + +probe kernel.function(@1) { } +probe kernel.function(@1).return { } + + + + is similar to , except that it allows you to pass the kernel function to be probed as a command-line argument (as in stap commandlineargs.stp kernel function). You can also specify the script to accept multiple command-line arguments, noting them as @1, @2, and so on, in the order they are entered by the user. + +Both variable notations $ and @ also represent a specific variable type. Use $ if you are expecting the user to enter an integer as a command-line argument, and @ if you are expecting a string. +
+ + +
+ diff --git a/doc/SystemTap_Beginners_Guide/en-US/Scripts.xml b/doc/SystemTap_Beginners_Guide/en-US/Scripts.xml index f35bb058..fad12dee 100644 --- a/doc/SystemTap_Beginners_Guide/en-US/Scripts.xml +++ b/doc/SystemTap_Beginners_Guide/en-US/Scripts.xml @@ -51,7 +51,7 @@ probe event, {handler}
- Events + Event SystemTap events can be broadly classified into two types: synchronous and asynchronous. @@ -215,7 +215,7 @@ probe timer.ms(4000)
- Handlers/Probe Body + Handler/Probe Body Consider the following sample script: @@ -304,7 +304,7 @@ hald(2360) open SystemTap Functions - SystemTap supports a wide variety of functions that can be used as printf () arguments. uses the SystemTap functions execname() (current process name) and pid() (current process ID). + SystemTap supports a wide variety of functions that can be used as printf () arguments. uses the SystemTap functions execname() (name of the process that called a kernel function/performed a system call) and pid() (current process ID). is "handler function" an appropriate term? wcohen: use "SystemTap functions" to match up language in man pages @@ -479,172 +479,5 @@ probe syscall.* {
-
- Basic Handler Constructs - -SystemTap supports the use of several basic constructs in handlers. The syntax for most of these handler constructs are mostly based on C and awk syntax. This section describes several of the most useful SystemTap handler constructs. - - - Variables - - Variables can be used freely throughout a handler; simply choose a name, assign it to a function, and use it in an expression. SystemTap automatically identifies whether a variable should be identified as a string or integer, based on the function it is assigned to. For instance, if you use set the variable foo to gettimeofday_s() (as in foo = gettimeofday_s()), then foo can be used as an integer argument (%d) in printf(). - - -Note, however, that by default variables are only local to the probe they are used in. This means that variables are initialized, used and disposed at each probe handler invocation. To share a variable between probes, declare the variable name first using global outside of any probe. Consider the following example: - - - timer-jiffies.stp - -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 () -} - - - - attempts to compute the CONFIG_HZ setting of the kernel using timers that count jiffies and milliseconds, then computing accordingly. The global statement allows the script to use the variables count_jiffies and count_ms (set in their own respective probes) to be shared with probe timer.ms(12345). - - - Note - The ++ notation in (i.e. count_jiffies ++ and count_ms ++) is used to increment the value of a variable by 1. In the following probe, count_jiffies is incremented by 1 every 100 jiffies: - -probe timer.jiffies(100) { count_jiffies ++ } - - In this instance, SystemTap understands that count_jiffies is an integer. Because no initial value was assigned to count_jiffies, its initial value is zero by default. - - - - - - - -
- Conditional Statements - -In some cases, the output of a SystemTap script may be too big. To address this, you need to further refine the script's logic in order to delimit the output into something more relevant or useful to your probe. - - - -You can do this by using conditionals in handlers. SystemTap accepts the following types of conditional statements: - - - - - If/Else Statements - - Format: - -if (condition) - {handler} else {handler} - - - - ifelse.stp - -global countread, countnonread -probe vfs.read,vfs.write -{ - if (probefunc()=="vfs_read") { - countread ++ } - else {countnonread ++} -} -probe timer.s(5) { - exit()} -probe end { - printf("VFS reads total %d\n VFS writes total %d\n", countread, countnonread)} - - - - is a script that counts how many virtual file system reads (vfs.read) and writes (vfs.write) the system performs within a 5-second span. When run, the script increments the value of the variable countread by 1 if the name of the function it probed matches vfs_read (as noted by the condition if (probefunc()=="vfs_read")); otherwise, it increments countnonread (else {countnonread ++}). - - - - - - While Loops - - Format: - -while (condition) {handler} - - - - - - For Loops - - Format: - -for (argument1; argument2; argument3) {handler} - - - - - - - - - -These constructs are better illustrated in the different examples available in . - -will get back to these ones later -
-
- Command-Line Arguments - You can also allow a SystemTap script to accept simple command-line arguments and declare them in the script without using target(). One way to do this is to use the variable notation $ or @. - - - -commandlineargs.stp - -probe kernel.function(@1) { } -probe kernel.function(@1).return { } - - - - is similar to , except that it allows you to pass the kernel function to be probed as a command-line argument (as in stap commandlineargs.stp kernel function). You can also specify the script to accept multiple command-line arguments, noting them as @1, @2, and so on, in the order they are entered by the user. - -Both variable notations $ and @ also represent a specific variable type. Use $ if you are expecting the user to enter an integer as a command-line argument, and @ if you are expecting a string. -
- - -
diff --git a/doc/SystemTap_Beginners_Guide/en-US/Understanding_How_SystemTap_Works.xml b/doc/SystemTap_Beginners_Guide/en-US/Understanding_How_SystemTap_Works.xml index 8dd1b803..81926b34 100644 --- a/doc/SystemTap_Beginners_Guide/en-US/Understanding_How_SystemTap_Works.xml +++ b/doc/SystemTap_Beginners_Guide/en-US/Understanding_How_SystemTap_Works.xml @@ -52,6 +52,7 @@ +