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