# Real-Time Graphing of Disk and CPU Utilization

http://sourceware.org/systemtap/examples/subsystem-index.html Graphing Disk and CPU Utilization - http://sourceware.org/systemtap/examples/general/graphs.stp

This section describes how you can graph disk and CPU utilization in real-time, that is, in samples of 1 second each.

disk-usage-graph.stp

    #! stap
    # disk I/O stats
    probe begin { qnames["ioblock"] ++; qsq_start ("ioblock") }
    probe ioblock.request { qs_wait ("ioblock") qs_run("ioblock") }
    probe ioblock.end { qs_done ("ioblock") }
    # CPU utilization
    probe begin { qnames["cpu"] ++; qsq_start ("cpu") }
    probe scheduler.cpu_on { if (!idle) {qs_wait ("cpu") qs_run ("cpu") }}
    probe scheduler.cpu_off { if (!idle) qs_done ("cpu") }
    # ------------------------------------------------------------------------
    # utilization history tracking
    global N
    probe begin { N = 50 }
    global qnames, util, histidx
    function qsq_util_reset(q) {
      u=qsq_utilization (q, 100)
      qsq_start (q)
      return u
    }
    probe timer.ms(100) {  # collect utilization percentages frequently
      histidx = (histidx + 1) % N  # into circular buffer
      foreach (q in qnames)
        util[histidx,q] = qsq_util_reset(q)
    }
    # ------------------------------------------------------------------------
    # general gnuplot graphical report generation
    probe timer.ms(1000) {
      # emit gnuplot command to display recent history
      printf ("set yrange [0:100]\n")
      printf ("plot ")
      foreach (q in qnames+)
        {
          if (++nq >= 2) printf (", ")
          printf ("'-' title \"%s\" with lines", q)
        }
      printf ("\n")
      foreach (q in qnames+) {
        for (i = (histidx + 1) % N; i != histidx; i = (i + 1) % N)
          printf("%d\n", util[i,q])
        printf ("e\n")
      }
      printf ("pause 1\n")
    }

outputs raw statistics on both CPU and disk usage per second. I/O usage is tracked through the events `ioblock.request` and `ioblock.request.end`, which track each request (and request completion) for a generic block I/O. CPU usage is tracked through `scheduler.cpu_on` and `scheduler.cpu_off`, which are activated whenever a process begins (and ends) a command execution on a CPU.

## gnuplot

Running by itself hardly presents any data that is useful, as in .

Raw disk-usage-graph.stp Output

    [...]
    62
    5
    3
    4
    6
    4
    4
    5
    5
    3
    6
    5
    e
    pause 1

However, refining the output of through `gnuplot` presents us with a more useful result. `gnuplot` is a lightweight, command-line driven plotting program that helps you display data in a graphical format.

By piping output to `gnuplot` (as in `stap disk-usage-gr http://sourceware.org/systemtap/examples/subsystem-index.html Graphing Disk and CPU Utilization - http://sourceware.org/systemtap/examples/general/graphs.stp aph.stp | gnuplot`), we get a graphical output similar to the following:

Graphical Output Sample

Sample output

Sample output of when piped through `gnuplot`

presents a cleaner, more useful graphical output. This graph can show you the level of utilization for both I/O and CPU, in real time.

question: does this script also capture stap process? that is, does the graph also include CPU utilization by systemtap while the script is running?