| 1 |
#! /usr/bin/env stap |
| 2 |
|
| 3 |
############################################################ |
| 4 |
# Schedtimes.stp |
| 5 |
# |
| 6 |
# Copyright (C) 2009, 2014 Red Hat, Inc. |
| 7 |
# |
| 8 |
# This program is free software you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU General Public License version 2 as |
| 10 |
# published by the Free Software Foundation. |
| 11 |
|
| 12 |
# Authors: Jason Baron <[email protected]> |
| 13 |
# Josh Stone <[email protected]> |
| 14 |
# profiles threads and displays their run times, queued times, |
| 15 |
# wait times, including i/o wait times. |
| 16 |
# Has two modes. When no arguments are given it profiles all |
| 17 |
# threads. Alternatively, you can pass -c "program name" |
| 18 |
############################################################ |
| 19 |
|
| 20 |
//constants |
| 21 |
global DEAD=-1, RUNNING=1, QUEUED=2, SLEEPING=3 |
| 22 |
|
| 23 |
global run_time, queued_time, sleep_time, iowait_time |
| 24 |
global pid_state, pid_names |
| 25 |
|
| 26 |
|
| 27 |
// For new enough kernels, roughly 2.6.32+, the @defined(@task->in_iowait) |
| 28 |
// tests will succeed and reduce these macros to nothing, including these |
| 29 |
// pid-iowait arrays. For older kernels, the rq fallback will remain. |
| 30 |
global pid_in_iowait |
| 31 |
global pid_iowait_count |
| 32 |
|
| 33 |
@define in_iowait(task) %( |
| 34 |
@choose_defined(@task->in_iowait, |
| 35 |
(pid_in_iowait[@task->pid] ? pid_in_iowait[@task->pid]-- : 0)) |
| 36 |
%) |
| 37 |
|
| 38 |
@define clear_iowait(rq, task) %( |
| 39 |
if (!@defined(@task->in_iowait)) |
| 40 |
pid_iowait_count[@task->pid] = @nr_iowait(@rq) |
| 41 |
%) |
| 42 |
|
| 43 |
@define set_iowait(rq, task) %( |
| 44 |
if (!@defined(@task->in_iowait)) |
| 45 |
pid_in_iowait[@task->pid] = (@nr_iowait(@rq) > pid_iowait_count[@task->pid]) |
| 46 |
%) |
| 47 |
|
| 48 |
@define nr_iowait(rq) %( |
| 49 |
atomic_read(&@cast(@rq, "rq", "kernel")->nr_iowait) |
| 50 |
%) |
| 51 |
|
| 52 |
|
| 53 |
global previous_timestamp |
| 54 |
|
| 55 |
function timestamp() |
| 56 |
{ |
| 57 |
return cpu_clock_us(0) |
| 58 |
} |
| 59 |
|
| 60 |
function update_times(pid, now) |
| 61 |
{ |
| 62 |
delta = now - previous_timestamp[pid] |
| 63 |
previous_timestamp[pid] = now |
| 64 |
|
| 65 |
if ((state = pid_state[pid]) > 0) { |
| 66 |
if (state == SLEEPING) |
| 67 |
sleep_time[pid] += delta |
| 68 |
else if (state == QUEUED) |
| 69 |
queued_time[pid] += delta |
| 70 |
else if (state == RUNNING) |
| 71 |
run_time[pid] += delta |
| 72 |
} |
| 73 |
|
| 74 |
return delta |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
function task_targeted(task) |
| 79 |
{ |
| 80 |
pid = task_pid(task) |
| 81 |
if (pid && (!target() || target_set_pid(pid))) { |
| 82 |
pid_names[task_tid(task)] = task_execname(task) |
| 83 |
return 1 |
| 84 |
} |
| 85 |
return 0 |
| 86 |
} |
| 87 |
|
| 88 |
// Update the task name after exec |
| 89 |
probe kernel.trace("sched_process_exec")!, |
| 90 |
kprocess.exec_complete |
| 91 |
{ |
| 92 |
if (tid() in pid_names) |
| 93 |
pid_names[tid()] = execname() |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
probe kernel.trace("sched_switch") |
| 98 |
{ |
| 99 |
// Task $prev is scheduled off this cpu |
| 100 |
if (task_targeted($prev)) { |
| 101 |
pid = $prev->pid |
| 102 |
state = task_state($prev) |
| 103 |
update_times(pid, timestamp()) |
| 104 |
|
| 105 |
if (state > 0) { |
| 106 |
@set_iowait($rq, $prev) |
| 107 |
pid_state[pid] = SLEEPING |
| 108 |
} else if (state == 0) { |
| 109 |
pid_state[pid] = QUEUED |
| 110 |
} else { |
| 111 |
pid_state[pid] = DEAD |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
// Task $next is scheduled onto this cpu |
| 116 |
if (task_targeted($next)) { |
| 117 |
pid = $next->pid |
| 118 |
update_times(pid, timestamp()) |
| 119 |
|
| 120 |
@clear_iowait($rq, $next) |
| 121 |
pid_state[pid] = RUNNING |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
probe kernel.trace("sched_wakeup") |
| 126 |
{ |
| 127 |
// Task $p is awakened |
| 128 |
if (@choose_defined($success, 1) && task_targeted($p)) { |
| 129 |
pid = $p->pid |
| 130 |
delta = update_times(pid, timestamp()) |
| 131 |
if (pid_state[pid] == SLEEPING && @in_iowait($p)) { |
| 132 |
iowait_time[pid] += delta |
| 133 |
} |
| 134 |
pid_state[pid] = QUEUED |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
// Give task $p a final accounting |
| 139 |
probe kernel.trace("sched_process_exit") |
| 140 |
{ |
| 141 |
if (task_targeted($p)) { |
| 142 |
pid = $p->pid |
| 143 |
update_times(pid, timestamp()) |
| 144 |
pid_state[pid] = DEAD |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
probe end |
| 149 |
{ |
| 150 |
t = timestamp() |
| 151 |
printf ("\n%16s: %6s %10s %10s %10s %10s %10s\n\n", |
| 152 |
"execname", "pid", "run(us)", "sleep(us)", "iowait(us)", |
| 153 |
"queued(us)", "total(us)") |
| 154 |
foreach (pid+ in pid_state) { |
| 155 |
update_times(pid, t) |
| 156 |
printf("%16s: %6d %10d %10d %10d %10d %10d\n", pid_names[pid], pid, |
| 157 |
run_time[pid], sleep_time[pid], iowait_time[pid], queued_time[pid], |
| 158 |
(run_time[pid] + sleep_time[pid] + queued_time[pid])) |
| 159 |
} |
| 160 |
} |