2011-09-14

constructing process tree from the ps -ef result

#! /usr/bin/awk -f

# Usage:
# ps -ef | ./proctree.awk
# ps -f > proclist.txt; ./proctree.awk proclist.txt

BEGIN { pid_col = 2; ppid_col = 3 }

# find the field number for PID and PPID column
/PID +PPID/ {
for (i=1; i < NF; i++) {
if ($i == "PID") {
pid_col = i ; ppid_col = i + 1
print; break
}
}
next
}

{
processes[$pid_col] = $0
children[$ppid_col] = children[$ppid_col] "," $pid_col
if ($ppid_col == 0 || $ppid_col == 1) child_of_swapper_init = child_of_swapper_init "," $pid_col
}

END {
if (0 in processes) {
print processes[0]
split(substr(children[0], 2), child_of_swapper, ",")
for (pid in child_of_swapper) {
if (pid != 1) print processes[pid]
}
}

if (1 in processes) {
print_tree(1, 0)
} else {
## find the top processes.
for (pid in children) {
if (pid in processes) continue
origins = origins "," pid
}

split(substr(origins, 2), top_procs, ",")
for (pid in top_procs) {
print_tree(top_procs[pid], 0)
}
}
}

function print_tree(pid, depth, myfmt, mypid, mychld)
{
if (index(child_of_swapper_init, pid)) depth = 0

if (depth == 0) {
if (pid in processes) {
print processes[pid]
} else {
depth = -1
}
} else {
myfmt = sprintf("%%%is%%s", 3 * depth)
printf(myfmt "\n", " ", processes[pid])
}

split(substr(children[pid], 2), mychld, ",")
for (mypid in mychld) {
print_tree(mychld[mypid], depth + 1)
}
}

#__end__

No comments:

Post a Comment