#! /usr/bin/env stap

probe begin
{
printf ("%%DataSet:cpu 100 00ff00 bar\n");
printf ("%%DataSet:kbd 75 ff0000 discreet\n");
printf ("%%DataSet:pty 50 0000ff discreet\n");
printf ("cpu %%Title:CPU utilization\n");
printf ("cpu %%XAxisTitle:Time\n");
printf ("cpu %%YAxisTitle:Percent\n");
printf ("%%LineEnd:0\n");
}

# 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") }

global qnames

function qsq_util_reset(q) {
  u=qsq_utilization (q, 100)
  qsq_start (q)
  return u
}

probe timer.ms(100) {  # collect utilization percentages frequently
  foreach (q in qnames)
    printf("cpu %d %d%c", gettimeofday_ms(), qsq_util_reset(q), 0)
}

probe kernel.function("kbd_event") {
 if ($event_type == 1 && $value)
   printf("kbd %d %d\n0x%x%c", gettimeofday_ms(), $event_code, $event_code, 0)
}

probe kernel.function("pty_write") {
    count = @defined($c) ? $c : $count
    if (count > 0) {
        printf("pty %d ", gettimeofday_ms())
        str = $buf
        for (i = 0; i < count; ++i) {
            if (i > 1)
                printf("\n")
            c = kernel_char(str + i)
            printf("%c", c)
        }
        printf("%c", 0)
    }
}
