2

I'd like to obtain CPU utilization (%us, %sy, %id, %wa) using SystemTap. I wrote a script that uses timer.profile probe and counts processor ticks:

#! /usr/bin/env stap

probe timer.profile {
# probe perf.sw.cpu_clock {
  if (!user_mode()) {
    if (pid() == 0) {
      iticks <<< 1
    } else {
      kticks <<< 1
    }
  } else {
    uticks <<< 1
  }
  ticks <<< 1  
}

global uticks, kticks, ticks, iticks

probe timer.s(1), end {
  allticks = @count(ticks)

  # printf("us: %d, sy: %d, id: %d, tot: %d\n",
  #        @count(uticks), @count(kticks), @count(iticks), allticks);
  printf("us: %d, sy: %d, id: %d\n",
         @count(uticks)*100/allticks, 
         @count(kticks)*100/allticks,
         @count(iticks)*100/allticks);

  delete uticks
  delete kticks
  delete iticks
  delete ticks
}

The above script yields different values than other system tools, like vmstat. When I apply a known load (calculating checksum of a big file), vmstat tells CPU is 25% us, why my script gives 35% us. Sample applies when performs a task that runs mostly in kernel - my stap script gives approximately 7 to 10% bigger value.

  1. What is the reason for this difference - is there a flaw in this script?
  2. How can I calculate number of ticks spent waiting for I/O (wa%)?
adgud
  • 21
  • 4

0 Answers0