Commit | Line | Data |
---|---|---|
77bd540c SK |
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 | ' | |
4a01bf49 SK |
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 | ||
5259a527 | 16 | function vert_print(v, _color, _fontcolor) { |
4a01bf49 SK |
17 | _color =\ |
18 | num_scale(\ | |
5259a527 SK |
19 | child2nice[v] + 20, |
20 | 20 + 20, | |
4a01bf49 SK |
21 | COLORSCHEME_MIN, |
22 | COLORSCHEME_MAX\ | |
23 | ) | |
5259a527 SK |
24 | _fontcolor = \ |
25 | _color == COLORSCHEME_MAX || _color == COLORSCHEME_MIN \ | |
26 | ? sprintf("/%s/%d", COLORSCHEME, COLORSCHEME_MID) \ | |
27 | : sprintf("/%s/%d", "greys9", 9) | |
4a01bf49 SK |
28 | printf(\ |
29 | "\"%d\"\ | |
30 | [ fontsize=8 \ | |
31 | , style=filled \ | |
32 | , fontname=Helvetica \ | |
33 | , shape=ellipse \ | |
34 | , label=\"%s\n%d\" \ | |
5259a527 SK |
35 | , color=\"/%s/%d\" \ |
36 | , fontcolor=\"%s\" \ | |
4a01bf49 SK |
37 | ];", |
38 | v, | |
39 | child2cmd[v], | |
40 | v, | |
41 | COLORSCHEME, | |
42 | _color, | |
5259a527 | 43 | _fontcolor\ |
4a01bf49 SK |
44 | ) |
45 | } | |
46 | ||
5259a527 | 47 | function edge_print(child, _parent, _color, _colorscheme) { |
4a01bf49 | 48 | _parent = child2parent[child] |
5259a527 SK |
49 | _colorscheme = "greys9" |
50 | _color = 3 | |
4a01bf49 SK |
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, | |
5259a527 | 61 | _colorscheme, |
4a01bf49 SK |
62 | _color\ |
63 | ) | |
64 | } | |
65 | ||
77bd540c | 66 | BEGIN { |
5259a527 SK |
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 | |
4a01bf49 | 82 | COLORSCHEME_MAX = 9 |
5259a527 SK |
83 | COLORSCHEME = sprintf("rdbu%d", COLORSCHEME_MAX) |
84 | ||
4a01bf49 | 85 | child2cmd[0] = "swapper/sched" |
77bd540c SK |
86 | } |
87 | ||
88 | NR > 1 { | |
4a01bf49 SK |
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 | |
5259a527 SK |
97 | child2nice[$1] = $5 |
98 | child2cmd[$1] = $6 | |
4a01bf49 | 99 | user_names[$4] = 1 |
77bd540c SK |
100 | } |
101 | ||
102 | END { | |
103 | print "strict digraph G {"; | |
104 | ||
105 | print "start=0;"; | |
77bd540c SK |
106 | print "fontsize=8;"; |
107 | print "fontname=Helvetica;"; | |
108 | print "label=\"" kernel "\";"; | |
4a01bf49 SK |
109 | printf "colorscheme=%s;\n", COLORSCHEME |
110 | print "fontcolor=9;" | |
77bd540c SK |
111 | |
112 | ##### Vertices (clustered by user) | |
4a01bf49 SK |
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) | |
77bd540c SK |
119 | print "}" |
120 | } | |
121 | ||
122 | ##### Vertices (without a user) | |
4a01bf49 SK |
123 | for (c in child2cmd) |
124 | if (!child2user_name[c]) | |
125 | vert_print(c) | |
77bd540c SK |
126 | |
127 | ##### Edges (across clusters) | |
4a01bf49 SK |
128 | for (c in child2parent) |
129 | edge_print(c) | |
77bd540c SK |
130 | |
131 | print "}"; | |
132 | } | |
133 | ' | |
134 | } | |
135 | ||
136 | ||
137 | procs() { | |
138 | if [ "$(uname)" = 'Linux' ]; then | |
5259a527 | 139 | ps -eo 'pid,ppid,euid,euser,nice,comm' |
77bd540c | 140 | else |
5259a527 | 141 | ps -eco 'pid,ppid,euid,euser,nice,comm' |
77bd540c SK |
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" |