| 1 | #! /bin/sh |
| 2 | |
| 3 | usage() { |
| 4 | echo "EXAMPLE (whole tree) : $0 | neato -T png > ps.png && open ps.png" |
| 5 | echo "EXAMPLE (user clusters): $0 | sdp -T png > ps.png && open ps.png" |
| 6 | } |
| 7 | |
| 8 | |
| 9 | compile() { |
| 10 | awk -v kernel="$(uname -v)" -v whoami="$(whoami)" \ |
| 11 | ' |
| 12 | function num_scale(src_cur, src_max, dst_min, dst_max) { |
| 13 | return dst_min + ((src_cur * (dst_max - dst_min)) / src_max) |
| 14 | } |
| 15 | |
| 16 | function vert_print(v, _color) { |
| 17 | _color =\ |
| 18 | num_scale(\ |
| 19 | parent2child_count[v], |
| 20 | max_children, |
| 21 | COLORSCHEME_MIN, |
| 22 | COLORSCHEME_MAX\ |
| 23 | ) |
| 24 | fontcolor = _color >= 5 ? 1 : 9 |
| 25 | printf(\ |
| 26 | "\"%d\"\ |
| 27 | [ fontsize=8 \ |
| 28 | , style=filled \ |
| 29 | , fontname=Helvetica \ |
| 30 | , shape=ellipse \ |
| 31 | , label=\"%s\n%d\" \ |
| 32 | , colorscheme=%s \ |
| 33 | , color=%d \ |
| 34 | , fontcolor=%d \ |
| 35 | ];", |
| 36 | v, |
| 37 | child2cmd[v], |
| 38 | v, |
| 39 | COLORSCHEME, |
| 40 | _color, |
| 41 | fontcolor\ |
| 42 | ) |
| 43 | } |
| 44 | |
| 45 | function edge_print(child, _parent, _color) { |
| 46 | _parent = child2parent[child] |
| 47 | _color =\ |
| 48 | num_scale(\ |
| 49 | parent2child_count[_parent], |
| 50 | max_children, |
| 51 | COLORSCHEME_MIN, |
| 52 | COLORSCHEME_MAX\ |
| 53 | ) |
| 54 | printf(\ |
| 55 | "\"%s\" -> \"%s\"\ |
| 56 | [ fontsize=8 \ |
| 57 | , fontname=Helvetica \ |
| 58 | , len=2.0 \ |
| 59 | , colorscheme=%s \ |
| 60 | , color=%d \ |
| 61 | ];\n", |
| 62 | _parent, |
| 63 | child, |
| 64 | COLORSCHEME, |
| 65 | _color\ |
| 66 | ) |
| 67 | } |
| 68 | |
| 69 | BEGIN { |
| 70 | COLORSCHEME = "orrd9" |
| 71 | # Good gradient colorschemes: |
| 72 | # - "orrd9" |
| 73 | # - "oranges9" |
| 74 | # - "bupu9" |
| 75 | # - "greys9" |
| 76 | |
| 77 | COLORSCHEME_MIN = 2 |
| 78 | COLORSCHEME_MAX = 9 |
| 79 | child2cmd[0] = "swapper/sched" |
| 80 | } |
| 81 | |
| 82 | NR > 1 { |
| 83 | parent2child_count[$2]++ |
| 84 | max_children = \ |
| 85 | parent2child_count[$2] > max_children\ |
| 86 | ? parent2child_count[$2]\ |
| 87 | : max_children |
| 88 | child2parent[$1] = $2 |
| 89 | child2user_id[$1] = $3 |
| 90 | child2user_name[$1] = $4 |
| 91 | child2cmd[$1] = $5 |
| 92 | user_names[$4] = 1 |
| 93 | } |
| 94 | |
| 95 | END { |
| 96 | print "strict digraph G {"; |
| 97 | |
| 98 | print "start=0;"; |
| 99 | print "fontsize=8;"; |
| 100 | print "fontname=Helvetica;"; |
| 101 | print "label=\"" kernel "\";"; |
| 102 | printf "colorscheme=%s;\n", COLORSCHEME |
| 103 | print "fontcolor=9;" |
| 104 | |
| 105 | ##### Vertices (clustered by user) |
| 106 | for (user_name in user_names) { |
| 107 | printf "subgraph \"cluster_%s\" {\n", user_name |
| 108 | printf "label=\"%s\"\n", user_name |
| 109 | for (c in child2parent) |
| 110 | if (child2user_name[c] == user_name) |
| 111 | vert_print(c) |
| 112 | print "}" |
| 113 | } |
| 114 | |
| 115 | ##### Vertices (without a user) |
| 116 | for (c in child2cmd) |
| 117 | if (!child2user_name[c]) |
| 118 | vert_print(c) |
| 119 | |
| 120 | ##### Edges (across clusters) |
| 121 | for (c in child2parent) |
| 122 | edge_print(c) |
| 123 | |
| 124 | print "}"; |
| 125 | } |
| 126 | ' |
| 127 | } |
| 128 | |
| 129 | |
| 130 | procs() { |
| 131 | if [ "$(uname)" = 'Linux' ]; then |
| 132 | ps -eo pid,ppid,euid,euser,comm |
| 133 | else |
| 134 | ps -eco pid,ppid,euid,euser,comm |
| 135 | fi |
| 136 | } |
| 137 | |
| 138 | |
| 139 | main() { |
| 140 | case "$1" in |
| 141 | '--help') usage |
| 142 | ;; |
| 143 | *) procs | grep "$1" | compile |
| 144 | ;; |
| 145 | esac |
| 146 | } |
| 147 | |
| 148 | |
| 149 | main "$1" |