Debian Codegraph @

testsuite/systemtap.examples/process/cycle_thief.stp

raw
1 #! /usr/bin/env stap
2 # Copyright (C) 2010-2016 Red Hat, Inc.
3 # Write by William Cohen <[email protected]>
4 #
5 # cycle_thief provide the following information for the specified pid:
6 # Number of times the task is migrated to another cpu
7 # How long the task is scheduled on and off a processor
8 # Tasks that run when the task is scheduled off the processor
9 # IRQs that run when the task is scheduled on the processor
10 #
11 # Run the script with:
12 # stap cycle_thief.stp -x pid
13 #
14 # control-c to exit data collection
15
16 global last_cpu = -1
17 global pid_on, pid_off, migrated
18 global time_on, time_off, irq_entry
19 global cycle_thief, irq_thief, cycle_thief_name
20
21
22 probe kernel.trace("sched_switch") {
23 t = gettimeofday_us(); c = cpu();
24 if ($prev->pid == target() && $next->pid != target()) {
25 /* being switched out */
26 pid_off = t; last_cpu = c;
27 if (pid_on) time_on <<< pid_off - pid_on;
28 } else if ($prev->pid != target() && $next->pid == target()) {
29 /* being switched in */
30 pid_on = t;
31 if (pid_off) time_off <<< pid_on - pid_off;
32 if (c != last_cpu) ++migrated;
33 }
34 /* watch what other processes scheduled while pid off */
35 if (pid_off > pid_on && c == last_cpu) {
36 cycle_thief[$next->pid] <<< 1;
37 cycle_thief_name[$next->pid] = kernel_string($next->comm)
38 }
39 }
40
41 /* what interrupts occur while process is running */
42 probe kernel.{trace("irq_handler_entry")!, trace("irq_entry")}
43 {
44 t = gettimeofday_us(); c = cpu();
45 i = @defined($irq) ? $irq : $id;
46 /* watch what irq run while pid is running */
47 if (pid_on > pid_off && c == last_cpu) {
48 irq_entry[i] = t;
49 }
50 }
51
52
53 probe kernel.{trace("irq_handler_exit")!, trace("irq_exit")}
54 {
55 i = @defined($irq) ? $irq : $id;
56 t = gettimeofday_us(); c = cpu(); irqt=irq_entry[i]
57 /* watch what irq run while pid is running */
58 if (pid_on > pid_off && c == last_cpu && irqt) {
59 irq_thief[i] <<< t - irqt;
60 delete irq_entry[i];
61 }
62 }
63
64
65 probe end {
66 printf("\n")
67 printf("task %d migrated: %d\n", target(), migrated)
68 if (@count(time_on)) {
69 printf("\n")
70 printf("task %d on processor (us):\n", target())
71 print(@hist_log(time_on));
72 }
73 if (@count(time_off)) {
74 printf("\n")
75 printf("task %d off processor (us)\n", target())
76 print(@hist_log(time_off));
77 }
78
79 printf("\n")
80 printf("other pids taking processor from task %d\n", target())
81 printf("%6s %10s %s\n", "PID", "count", "command");
82 foreach (p in cycle_thief-)
83 printf("%6d %10d %s\n", p, @count(cycle_thief[p]), cycle_thief_name[p])
84
85 printf("\n")
86 printf("irq taking processor from task %d\n", target())
87
88 printf("%6s %10s %10s %10s %10s %10s\n", "irq", "count",
89 "min(us)", "avg(us)", "max(us)", "variance(us^2)")
90 foreach (p in irq_thief-)
91 printf("%6d %10d %10d %10d %10d %10d\n", p, @count(irq_thief[p]),
92 @min(irq_thief[p]), @avg(irq_thief[p]), @max(irq_thief[p]),
93 @variance(irq_thief[p]))
94
95 }