Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Linux Tools Project/Systemtap/User Guide/ide/exampleScripts.html
< Linux Tools Project | Systemtap/User Guide | ide
Revision as of 06:18, 9 November 2010 by Unnamed Poltroon (Talk) (New page: <h2> Example SystemTap Scripts </h2> What follows is a few SystemTap Scripts that you may use in SystemTap GUI, taken from and linked to <a href="http://sourceware.org/systemtap/document...)
Contents
- 1 Example SystemTap Scripts
- 1.1 top.stp - Prints the top 20 system calls (<a href="http://sourceware.org/systemtap/examples/top.stp">online source</a>)
- 1.2 prof.stp - Simple profiling (<a href="http://sourceware.org/systemtap/examples/prof.stp">online source</a>)
- 1.3 keyhack.stp - Modifying variables in the kernel. (<a href="http://sourceware.org/systemtap/examples/keyhack.stp">online source</a>)
- 1.4 kmalloc.stp - Statistics example. (<a href="http://sourceware.org/systemtap/examples/kmalloc.stp">online source</a>)
- 1.5 kmalloc2.stp - Example using arrays of statistics. (<a href="http://sourceware.org/systemtap/examples/kmalloc2.stp">online source</a>)
Example SystemTap Scripts
What follows is a few SystemTap Scripts that you may use in SystemTap GUI, taken from and linked to <a href="http://sourceware.org/systemtap/documentation.html">http://sourceware.org/systemtap/documentation.html</a>
There is also a
<a href="http://sourceware.org/systemtap/examples/demo_script.txt">description of the examples</a>.
top.stp - Prints the top 20 system calls (<a href="http://sourceware.org/systemtap/examples/top.stp">online source</a>)
#!/usr/bin/env stap
#
# This script continuously lists the top 20 systemcalls on the system
#
global syscalls
function print_top () {
cnt=0
log ("SYSCALL\t\t\t\tCOUNT")
foreach ([name] in syscalls-) {
printf("%-20s\t\t%5d\n",name, syscalls[name])
if (cnt++ == 20)
break
}
printf("--------------------------------------\n")
delete syscalls
}
probe kernel.function("sys_*") {
syscalls[probefunc()]++
}
# print top syscalls every 5 seconds
probe timer.ms(5000) {
print_top ()
prof.stp - Simple profiling (<a href="http://sourceware.org/systemtap/examples/prof.stp">online source</a>)
#!/usr/bin/env stap
# This is an example of profiling a specific command or pid.
# It works by recording the time when a system call is entered
# exited.
# Usage: prof.stp -c "top -n5"
# Will start up "top" and after 5 iterations, will exit.
#
# Usage: prof.stp -x 3323
# Will profile pid 3323 until it ^c is hit.
#
probe kernel.function("sys_*") {
if (target() == tid())
calltime[tid()] = gettimeofday_us()
}
probe kernel.function("sys_*").return {
if (target() != tid()) next
now = gettimeofday_us()
c = calltime[tid()]
if (!c) next
ttime[probefunc()] <<< now - c
delete calltime[tid()]
}
probe end {
printf("\n")
foreach (x in ttime)
printf("%-20s\tcalls:%6d\tavg time (ms):%5d\ttotal(ms):%7d\n",
x, @count(ttime[x]), @avg(ttime[x]), @sum(ttime[x]))
}
global calltime, ttime
keyhack.stp - Modifying variables in the kernel. (<a href="http://sourceware.org/systemtap/examples/keyhack.stp">online source</a>)
#! /usr/bin/env stap
# This is not useful, but it demonstrates that
# Systemtap can modify variables in a running kernel.
# Usage: ./keyhack.stp -g
probe kernel.function("kbd_event") {
# Changes 'm' to 'b' .
if ($event_code == 50) $event_code = 48
}
probe end {
printf("\nDONE\n")
}
</code>
kmalloc.stp - Statistics example. (<a href="http://sourceware.org/systemtap/examples/kmalloc.stp">online source</a>)
<code>
#! /usr/bin/env stap
# Using statistics to examine kernel memory allocations
global kmalloc
probe kernel.function("__kmalloc") {
kmalloc <<< $size
}
# Exit after 10 seconds
probe timer.ms(10000) { exit () }
probe end {
printf("Count: %d allocations\n", @count(kmalloc))
printf("Sum: %d Kbytes\n", @sum(kmalloc)/1000)
printf("Average: %d bytes\n", @avg(kmalloc))
printf("Min: %d bytes\n", @min(kmalloc))
printf("Max: %d bytes\n", @max(kmalloc))
print("\nAllocations by size in bytes\n")
print(@hist_log(kmalloc))
}
</code>
kmalloc2.stp - Example using arrays of statistics. (<a href="http://sourceware.org/systemtap/examples/kmalloc2.stp">online source</a>)
<code>
#! /usr/bin/env stap
# Using statistics and maps to examine kernel memory allocations
global kmalloc
probe kernel.function("__kmalloc") {
kmalloc[execname()] <<< $size
}
# Exit after 10 seconds
probe timer.ms(10000) { exit () }
probe end {
foreach ([name] in kmalloc) {
printf("Allocations for %s\n", name)
printf("Count: %d allocations\n", @count(kmalloc[name]))
printf("Sum: %d Kbytes\n", @sum(kmalloc[name])/1000)
printf("Average: %d bytes\n", @avg(kmalloc[name]))
printf("Min: %d bytes\n", @min(kmalloc[name]))
printf("Max: %d bytes\n", @max(kmalloc[name]))
print("\nAllocations by size in bytes\n")
print(@hist_log(kmalloc[name]))
printf("-------------------------------------------------------\n\n");
}
}
</code>