Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Linux Tools Project/Systemtap/User Guide/ide/exampleScripts.html"

(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...)
 
 
Line 4: Line 4:
  
 
What follows is a few SystemTap Scripts that you may use in SystemTap GUI, taken from and linked to  
 
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>
+
http://sourceware.org/systemtap/documentation.html
  
<br><br>There is also a
+
<br><br>There is also a description of the examples - http://sourceware.org/systemtap/examples/demo_script.txt
<a href="http://sourceware.org/systemtap/examples/demo_script.txt">description of the examples</a>.
+
  
 
<h3>
 
<h3>
top.stp - Prints the top 20 system calls (<a href="http://sourceware.org/systemtap/examples/top.stp">online source</a>)
+
top.stp - Prints the top 20 system calls (http://sourceware.org/systemtap/examples/top.stp)
 
</h3>
 
</h3>
  
<code><pre>
+
<pre>
 
#!/usr/bin/env stap  
 
#!/usr/bin/env stap  
 
#
 
#
Line 40: Line 39:
 
probe timer.ms(5000) {
 
probe timer.ms(5000) {
 
print_top ()
 
print_top ()
</pre></code>
+
</pre>
 
+
  
 
<h3>
 
<h3>
prof.stp - Simple profiling (<a href="http://sourceware.org/systemtap/examples/prof.stp">online source</a>)
+
prof.stp - Simple profiling (http://sourceware.org/systemtap/examples/prof.stp)
 
</h3>
 
</h3>
  
<code><pre>
+
<pre>
 
#!/usr/bin/env stap
 
#!/usr/bin/env stap
  
Line 83: Line 81:
  
 
global calltime, ttime
 
global calltime, ttime
</pre></code>
+
</pre>
  
  
 
<h3>
 
<h3>
keyhack.stp - Modifying variables in the kernel. (<a href="http://sourceware.org/systemtap/examples/keyhack.stp">online source</a>)
+
keyhack.stp - Modifying variables in the kernel. (http://sourceware.org/systemtap/examples/keyhack.stp)
 
</h3>
 
</h3>
  
<code><pre>
+
<pre>
 
#! /usr/bin/env stap
 
#! /usr/bin/env stap
  
Line 106: Line 104:
 
printf("\nDONE\n")
 
printf("\nDONE\n")
 
}
 
}
</code></pre>
+
</pre>
  
  
 
<h3>
 
<h3>
kmalloc.stp - Statistics example. (<a href="http://sourceware.org/systemtap/examples/kmalloc.stp">online source</a>)
+
kmalloc.stp - Statistics example. (http://sourceware.org/systemtap/examples/kmalloc.stp)
 
</h3>
 
</h3>
  
<code><pre>
+
<pre>
 
#! /usr/bin/env stap
 
#! /usr/bin/env stap
  
Line 136: Line 134:
 
print(@hist_log(kmalloc))
 
print(@hist_log(kmalloc))
 
}
 
}
</code></pre>
+
</pre>
  
  
 
<h3>
 
<h3>
kmalloc2.stp - Example using arrays of statistics. (<a href="http://sourceware.org/systemtap/examples/kmalloc2.stp">online source</a>)
+
kmalloc2.stp - Example using arrays of statistics. (http://sourceware.org/systemtap/examples/kmalloc2.stp)
 
</h3>
 
</h3>
  
<code><pre>
+
<pre>
 
#! /usr/bin/env stap
 
#! /usr/bin/env stap
  
Line 170: Line 168:
 
}
 
}
 
}
 
}
</code></pre>
+
</pre>

Latest revision as of 11:45, 9 November 2010

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 - http://sourceware.org/systemtap/examples/demo_script.txt

top.stp - Prints the top 20 system calls (http://sourceware.org/systemtap/examples/top.stp)

#!/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 (http://sourceware.org/systemtap/examples/prof.stp)

#!/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. (http://sourceware.org/systemtap/examples/keyhack.stp)

#! /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. (http://sourceware.org/systemtap/examples/kmalloc.stp)

#! /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. (http://sourceware.org/systemtap/examples/kmalloc2.stp)

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

Back to the top