# Copyright (C) 2008 Red Hat, Inc.
# Written by Ulrich Drepper <drepper@redhat.com>
global process
global poll_timeouts
global epoll_timeouts
global select_timeouts
global itimer_timeouts
global nanosleep_timeouts
global futex_timeouts
global signal_timeouts

probe kernel.function("do_sys_poll").return {
      if ($return == 0) {
         p = pid()
	 if (!(p in process))
            process[p] = execname()
      	 poll_timeouts[p]++
      }
}

probe kernel.function("do_select").return {
      if ($return == 0) {
         p = pid()
         if (!(p in process))
            process[p] = execname()
      	 select_timeouts[p]++
      }
}

probe kernel.function("sys_epoll_wait").return {
      if ($return == 0) {
         p = pid()
         if (!(p in process))
            process[p] = execname()
      	 epoll_timeouts[p]++
      }
}

probe kernel.function("do_futex").return {
      if ($return == -110) {
         p = pid()
         if (!(p in process))
            process[p] = execname()
      	 futex_timeouts[p]++
      }
}

probe kernel.function("hrtimer_nanosleep").return,
      kernel.function("sys_clock_nanosleep").return {
      if ($return == 0) {
         p = pid()
         if (!(p in process))
            process[p] = execname()
      	 nanosleep_timeouts[p]++
      }
}

probe kernel.function("it_real_fn") {
        p = pid()
        itimer_timeouts[p]++
        if (!(p in process))
            process[p] = execname()
}

probe kernel.function("sys_rt_sigtimedwait").return {
      if ($return == -11) {
         p = pid()
         if (!(p in process))
            process[p] = execname()
      	 signal_timeouts[p]++
      }
}

probe kernel.function("do_exit") {
        p = pid()
        if (process[p] == execname()) {
                delete process[p]
		poll_timeouts[p] = 0
  		epoll_timeouts[p] = 0
  		select_timeouts[p] = 0
  		itimer_timeouts[p] = 0
  		futex_timeouts[p] = 0
  		nanosleep_timeouts[p] = 0
		signal_timeouts[p] = 0
        }
}

probe timer.ms(1000) {
        printf("\033[2J\033[1;1H") /* clear screen */
        printf ("  uid |   poll  select   epoll  itimer   futex nanosle  signal| process\n");
        foreach (p in process) {
		if (poll_timeouts[p] != 0 || select_timeouts[p] != 0
                    || epoll_timeouts[p] != 0 || itimer_timeouts[p] != 0
 		    || futex_timeouts[p] != 0 || nanosleep_timeouts[p] != 0
		    || signal_timeouts[p] != 0)
                	printf ("%5d |%7d %7d %7d %7d %7d %7d %7d| %-.38s\n", p,
                        	poll_timeouts[p], select_timeouts[p],
                        	epoll_timeouts[p], itimer_timeouts[p],
				futex_timeouts[p], nanosleep_timeouts[p],
                        	signal_timeouts[p], process[p])
        }
}
