#! /usr/bin/env stap
#
# filemap kernel tracepoints:
# mm_filemap_add_to_page_cache,
#   mm_filemap_delete_from_page_cache

global traced_pid, command
global allocation, free

global old_set_hits
global file_fault, file_cow, file_uunmap, file_kunmap
global file_page_cache_add, file_page_cache_delete

function log_event:long ()
{
  return (!traced_pid || traced_pid == pid())
}

probe kernel.{trace("mm_page_alloc")!, trace("mm_page_allocation")} {
  if (!log_event()) next
  allocation[pid()] <<< 1
  command[pid()] = execname()
}

probe kernel.{trace("mm_page_free")!, trace("mm_page_free_direct")} {
  if (!log_event()) next
  free[pid()] <<< 1
}

probe kernel.trace("mm_filemap_add_to_page_cache")? {
  if (!log_event()) next
  file_page_cache_add[pid()] <<< 1
}

probe kernel.trace("mm_filemap_delete_from_page_cache")? {
  if (!log_event()) next
  file_page_cache_delete[pid()] <<< 1
}

probe never {
  # Do a few initializations to let stap know what the global
  # variable types are. Note that since we're in a "never"
  # probe, these initializations will never actually happen.
  traced_pid = 0
  old_set_hits <<< 1
  file_fault[0] <<< 1
  file_cow[0] <<< 1
  file_uunmap[0] <<< 1
  file_kunmap[0] <<< 1
  file_page_cache_add[0] <<< 1
  file_page_cache_delete[0] <<< 1
}

probe begin {
  printf("Starting data collection\n")
  if (target())
    printf("mode Specific Pid, traced pid: %d\n\n", target())
  else
    printf("mode - All Pids\n\n")
}

function print_old_format:long ()
{
  printf("Command                 Pid     Alloc      Free  F_fault    F_cow F_kunmap F_uunmap\n")
  printf("-------                 ---     -----      ----  -------    ----- -------- --------\n")

  foreach (pid+ in allocation) {
      printf("%-16s %10d %9d %9d %8d %8d %8d %8d\n",
	     command[pid], pid,
	     @count(allocation[pid]), @count(free[pid]),
	     @count(file_fault[pid]), @count(file_cow[pid]),
	     @count(file_kunmap[pid]), @count(file_uunmap[pid]))
  }
}

function print_new_format:long ()
{
  printf("Command                 Pid     Alloc      Free CacheAdd CacheDel\n")
  printf("-------                 ---     -----      ---- -------- --------\n")

  foreach (pid+ in allocation) {
      printf("%-16s %10d %9d %9d %8d %8d\n",
	     command[pid], pid,
	     @count(allocation[pid]), @count(free[pid]),
	     @count(file_page_cache_add[pid]),
	     @count(file_page_cache_delete[pid]))
  }
}

probe end {
  printf("Terminating data collection\n")

  # We'll have hits from the filemap tracepoints
  # (mm_filemap_add_to_page_cache,
  # mm_filemap_delete_from_page_cache). Print out the set that got
  # hits.
  if (@count(old_set_hits))
    print_old_format()
  else
    print_new_format()
}
