Table of Contents > Reference

## Example SystemTap Scripts

What follows is a few SystemTap Scripts that you may use in SystemTap GUI, taken from and linked to http://sourceware.org/systemtap/documentation.html
There is also a description of the examples.

### top.stp - Prints the top 20 system calls (online source)

`

    #!/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 (online source)

`

    #!/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. (online source)

`

    #! /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")
    }

`

### kmalloc.stp - Statistics example. (online source)

`

    #! /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))
    }

`

### kmalloc2.stp - Example using arrays of statistics. (online source)

`

    #! /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");
    	}
    }

`