| 1 |
#! /usr/bin/env stap |
| 2 |
|
| 3 |
global processes, stime, virtmem, rssmem, shrmem, utime, proc_state |
| 4 |
global tot_intervals, sec_counter |
| 5 |
|
| 6 |
// configuration options: |
| 7 |
// -G sort=[pid|process|state|virtmem|rssmem|shrdmem|stime|utime] |
| 8 |
// -G sortdir=[increase|decrease] |
| 9 |
// -G show=# |
| 10 |
// -G interval=# |
| 11 |
// -G clear_screen=[y|n] |
| 12 |
global sort="stime", sortdir="decrease", show=25, interval=5, clear_screen="n" |
| 13 |
|
| 14 |
probe scheduler.ctxswitch { |
| 15 |
//don't let the process name get overwritten |
| 16 |
if (processes[prev_pid] == "" || prev_task_name != "") |
| 17 |
processes[prev_pid] = prev_task_name |
| 18 |
|
| 19 |
if (task_stime() > stime[prev_pid]){ |
| 20 |
stime[prev_pid] = task_stime() |
| 21 |
utime[prev_pid] = task_utime() |
| 22 |
virtmem[prev_pid] = proc_mem_size(prev_pid) |
| 23 |
rssmem[prev_pid] = proc_mem_rss(prev_pid) |
| 24 |
shrmem[prev_pid] = proc_mem_shr(prev_pid) |
| 25 |
} |
| 26 |
|
| 27 |
// update the state if we've encountered the process before |
| 28 |
if (processes[prev_pid] != "") |
| 29 |
proc_state[prev_pid] = prevtsk_state |
| 30 |
} |
| 31 |
|
| 32 |
probe scheduler.process_exit { |
| 33 |
proc_state[pid] = 65 |
| 34 |
} |
| 35 |
|
| 36 |
function print_pid_info (proc_pid){ |
| 37 |
// calc the state of the process |
| 38 |
if (proc_state[proc_pid] == 0) state = "R" |
| 39 |
else if (proc_state[proc_pid] == 1) state = "S" |
| 40 |
else if (proc_state[proc_pid] == 2) state = "D" |
| 41 |
else if (proc_state[proc_pid] == 4 || proc_state[proc_pid] == 8) state = "T" |
| 42 |
else if (proc_state[proc_pid] == 16) state = "Z" |
| 43 |
else if (proc_state[proc_pid] == 32) state = "EXIT_DEAD" |
| 44 |
else if (proc_state[proc_pid] == 65) state = "DEAD" |
| 45 |
else state = "DEAD" // let's assume the worst |
| 46 |
|
| 47 |
printf("%8d %20s %10s %12d %12d %12d %16d %16d\n", |
| 48 |
proc_pid, |
| 49 |
processes[proc_pid], |
| 50 |
state, |
| 51 |
virtmem[proc_pid] * mem_page_size(), |
| 52 |
rssmem[proc_pid] * mem_page_size(), |
| 53 |
shrmem[proc_pid] * mem_page_size(), |
| 54 |
stime[proc_pid], |
| 55 |
utime[proc_pid]) |
| 56 |
} |
| 57 |
|
| 58 |
function print_info (){ |
| 59 |
tot_processes = 0; |
| 60 |
|
| 61 |
// count the number of processes |
| 62 |
foreach ([a] in processes) |
| 63 |
tot_processes ++; |
| 64 |
|
| 65 |
if (clear_screen == "y") |
| 66 |
ansi_clear_screen() |
| 67 |
else |
| 68 |
println(); |
| 69 |
|
| 70 |
printf("%d seconds: showing %d out of %d tasks\n", |
| 71 |
tot_intervals*interval, ((show>tot_processes) ? tot_processes : show), |
| 72 |
tot_processes) |
| 73 |
|
| 74 |
printf("%8s %20s %10s %12s %12s %12s %16s %16s\n", |
| 75 |
"PID", "Process", "State", "VirtMem", "RSSMem", "ShrdMem", "CPU Time", "User Time") |
| 76 |
|
| 77 |
if (sortdir == "increase") { |
| 78 |
if (sort == "pid") foreach ([proc_pid+] in processes limit show) print_pid_info(proc_pid) |
| 79 |
if (sort == "process") foreach ([proc_pid] in processes+ limit show) print_pid_info(proc_pid) |
| 80 |
if (sort == "state") foreach ([proc_pid] in proc_state+ limit show) print_pid_info(proc_pid) |
| 81 |
if (sort == "virtmem") foreach ([proc_pid] in virtmem+ limit show) print_pid_info(proc_pid) |
| 82 |
if (sort == "rssmem") foreach ([proc_pid] in rssmem+ limit show) print_pid_info(proc_pid) |
| 83 |
if (sort == "shrdmem") foreach ([proc_pid] in shrmem+ limit show) print_pid_info(proc_pid) |
| 84 |
if (sort == "stime") foreach ([proc_pid] in stime+ limit show) print_pid_info(proc_pid) |
| 85 |
if (sort == "utime") foreach ([proc_pid] in utime+ limit show) print_pid_info(proc_pid) |
| 86 |
} else if (sortdir == "decrease") { |
| 87 |
if (sort == "pid") foreach ([proc_pid-] in processes limit show) print_pid_info(proc_pid) |
| 88 |
if (sort == "process") foreach ([proc_pid] in processes- limit show) print_pid_info(proc_pid) |
| 89 |
if (sort == "state") foreach ([proc_pid] in proc_state- limit show) print_pid_info(proc_pid) |
| 90 |
if (sort == "virtmem") foreach ([proc_pid] in virtmem- limit show) print_pid_info(proc_pid) |
| 91 |
if (sort == "rssmem") foreach ([proc_pid] in rssmem- limit show) print_pid_info(proc_pid) |
| 92 |
if (sort == "shrdmem") foreach ([proc_pid] in shrmem- limit show) print_pid_info(proc_pid) |
| 93 |
if (sort == "stime") foreach ([proc_pid] in stime- limit show) print_pid_info(proc_pid) |
| 94 |
if (sort == "utime") foreach ([proc_pid] in utime- limit show) print_pid_info(proc_pid) |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
if (clear_screen != "y") |
| 99 |
println("-----------------------------------------------------------------------------------------------------------------") |
| 100 |
} |
| 101 |
|
| 102 |
probe timer.s(1) { |
| 103 |
if (++sec_counter >= interval){ |
| 104 |
tot_intervals++ |
| 105 |
print_info() |
| 106 |
sec_counter = 0; |
| 107 |
} |
| 108 |
} |