#! /usr/bin/env stap
# Copyright (C) 2011-2016 Red Hat, Inc.
# Written by William Cohen <wcohen@redhat.com>
#
# This script give an idea what periodic things are running on
# the system. Usage:
#
# stap --all-modules periodic.stp

global last_expire, period, funct, data, proc_info, delayed_work_info

probe kernel.trace("timer_expire_entry")
{
  old_expire = last_expire[$timer]
  new_expire = gettimeofday_us()
  if (old_expire) {
    elapsed = new_expire - old_expire
    period[$timer] <<< elapsed
    funct[$timer] = $timer->function
    data[$timer] = @defined($timer->data) ? $timer->data : 0
    proc_info[$timer] = @defined($timer->data) ? 0 : @container_of($timer, "struct process_timer", timer)->task
    delayed_work_info[$timer] = @defined($timer->data) ? 0 : & @container_of($timer, "struct delayed_work", timer)
  }
  last_expire[$timer] = new_expire
}

function output()
{
  printf("%-7s %-50s %15s %25s %9s\n", "#type", "function", "avg period(us)", "period variance(us^2)", "count")
  # print out the various timers firing
  foreach([timer] in period-) {
    fname = symname(funct[timer])
    if (fname == "process_timeout") {
      ptr = (data[timer] ? data[timer] : proc_info[timer])
      fname = sprintf("%s(%d)",
                      kernel_string_n(@cast(ptr, "struct task_struct", "kernel<linux/sched.h>")->comm, 16),
                      @cast(ptr, "struct task_struct", "kernel<linux/sched.h>")->pid)
      type="process"
    } else if (fname == "delayed_work_timer_fn") {
      ptr = (data[timer] ? data[timer] : delayed_work_info[timer])
      faddr = @defined(@cast(ptr, "struct delayed_work", "kernel<linux/workqueue.h>")->work->func)
      ? @cast(ptr, "struct delayed_work", "kernel<linux/workqueue.h>")->work->func
      : @cast(ptr, "struct work_struct", "kernel<linux/workqueue.h>")->func
      fname = sprintf("%s", symname(faddr))
      type="work_q"
    } else {
      fileline = symfileline(funct[timer])
      if (strtol(fileline, 16)) // fileline is just the address
        fname = sprintf("%s", symdata(funct[timer]))
      else
        fname = sprintf("%s@%s", symname(funct[timer]), fileline)
      type="kernel"
    }
    printf("%-7s %-50.50s %15d %25d %9d\n", type, fname,
           @avg(period[timer]), @variance(period[timer], 2), @count(period[timer]))
  }
}

probe begin
{
  printf("#monitoring timer periods (press control-c for output)\n")
}

probe end { output() }

# allow optional period output from script
%( $# > 0 %?
probe timer.s($1)
{
  output();
  delete last_expire
  delete period
  delete funct
  delete data
  delete proc_info
  delete delayed_work_info
}
%)
