next hack: timeouttop

i had a ‘future work’ item in my last post. i couldn’t resist doing it so now it’s done.

timeouttop is a program that when coupled with the kernel patch from my last entry will show a top-like display of which processes are being most frequently woken by timeouts.

notes: it works by keeping track of the state of the counters from up to 60 seconds ago and comparing it with the current state. if a process isn’t in both of the lists then it is not shown. this means that if a new process (just started) wasn’t running 60 seconds ago then it won’t show in the list until it has run for the full 60. this could be considered a bug.

the progrram also assumes a terminal width of 80 and a length of however long it needs to show all of the processes with non-zero timeouts in the sample period. the terminal is assumed to support vt100-like codes for clearing the screen.

good enough for now :)

today’s hack: pstimeouts

today i wrote a kernel patch to find out just how often processes are waking up as a result of setting timers for themselves. the intention is to provide a tool to make it extremely easy to spot poorly behaved applications.

the kernel patch recognises 5 types of “timers” that might cause a process to wake up:

  • poll returning due to timeout after sleeping
  • select, same as above
  • epoll, same as above
  • the ‘real time’ interval timer (SIGALRM)
  • anything else inside the kernel that uses schedule_timeout

the kernel patch makes this information available in /proc/pid/timeouts as 5 numbers (corresponding to the list above).

i wrote ‘pstimeouts’ as a small utility to read this data and present it to the user. it’s written in straight-up C with no library dependencies (not even glib) as to be usable by the biggest number of people.

here is a ‘screenshot’:

desrt@acquiesce:~$ pstimeouts
  pid timeouts process
 4568       44 x-session-manager
 4607        7 /usr/bin/dbus-daemon
 4609      704 /usr/lib/libgconf2-4/gconfd-2
 4612        0 /usr/bin/gnome-keyring-daemon
 4615     6615 /usr/lib/control-center/gnome-settings-daemon
 4629     5428 /usr/bin/metacity
 4634    39574 gnome-panel
 4636    17635 nautilus
 4639     1391 gnome-volume-manager
 4643        0 /usr/lib/bonobo-activation/bonobo-activation-server
 4648    10346 update-notifier
 4655    37128 nm-applet
 4658      128 /usr/lib/gnome-vfs-2.0/gnome-vfs-daemon
 4661     4058 gnome-cups-icon
 4680    28102 gnome-power-manager
 4718     3388 /usr/lib/nautilus-cd-burner/mapping-daemon
 4742       59 /usr/lib/gnome-applets/gweather-applet-2
 4758   290997 gnome-terminal
 4766    21571 gnome-screensaver
 4768        0 -bash
10005    12195 /usr/lib/notification-daemon/notification-daemon
11068        0 -bash
11099        0 -bash
14420        0 -bash
14503        0 -bash
14529        0 -bash
14595        0 -bash
  539        0 pstimeouts

pstimeouts has -a and -u options that do the same as their ‘ps’ counterparts. -u will show each timeout type separately.

the kernel patch (which applies against ubuntu linux-source-2.6.17-7.20) and the source for pstimeouts are located here: enjoy :)

future work: make a ‘top’ sort of utility.

vim syntax highlighting for glib, gobject, gdk and gtk types

i’ve created a file to enable vim syntax highlighting of glib, gobject, gdk and gtk types.

i handled the base gtypes (gboolean, gpointer, etc) by hand but the rest of them were generated semi-automatically using the following commands:

there are probably some inaccuracies in both directions which could be fixed with better handling but it looks to be pretty good. i considered grepping for ‘get_type’ functions in the symbols list but then you don’t get typedefs for things like callback function types.

# from inside glib source tree
find | grep .h$ | xargs cat | sed -e 'sx[/*].*$xx' |
       tr ' \t' '\n\n' | grep ^G[A-Z][A-Za-z]*$ |
       grep -v '^[A-Z]*$' | sort | uniq > ~/GTypes

# from inside gdk/ directory of gtk source tree
find | grep .h$ | xargs cat | sed -e 'sx[/*].*$xx' |
       tr ' \t' '\n\n' | grep ^Gdk[A-Z][A-Za-z]*$ |
       grep -v '^[A-Z]*$' | sort | uniq > ~/GdkTypes

# from inside gtk/ directory of gtk source tree
find | grep .h$ | xargs cat | sed -e 'sx[/*].*$xx' |
       tr ' \t' '\n\n' | grep ^Gtk[A-Z][A-Za-z]*$ |
       grep -v '^[A-Z]*$' | sort | uniq > ~/GtkTypes

# from ~
cat ~/G*Types | tr '\n' ' ' | fmt -w 60 |
  sed -e 's/^/syn keyword cType /' > gtk-highlight.vim

thanks to tpope on #vim for telling me about ~/.vim and to jdub for ‘fmt’