Support relative brightness increases/decreases
[khome.git] / home / bin / ps2dot
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, _shape, _state, _size, _height, _label, _label_base, _label_ext) {
17 _state = child2state[v]
18 _style = "filled,solid"
19
20 # -----------------------------------------------------------------
21 # Sleeping/idling
22 # -----------------------------------------------------------------
23 # D uninterruptible sleep (usually IO)
24 if (_state == "D") {
25 _shape = "circle"
26 # I Idle kernel thread
27 } else if (_state == "I") {
28 _shape = "circle"
29 # S interruptible sleep (waiting for an event to complete)
30 } else if (_state == "S") {
31 _shape = "circle"
32 # -----------------------------------------------------------------
33 # Running
34 # -----------------------------------------------------------------
35 # R running or runnable (on run queue)
36 } else if (_state == "R") {
37 _shape = "rarrow"
38 # -----------------------------------------------------------------
39 # Stopped
40 # -----------------------------------------------------------------
41 # T stopped by job control signal
42 } else if (_state == "T") {
43 _shape = "square"
44 # t stopped by debugger during the tracing
45 } else if (_state == "t") {
46 _shape = "square"
47 # -----------------------------------------------------------------
48 # Dead
49 # -----------------------------------------------------------------
50 # Z defunct ("zombie") process, terminated but not reaped by its parent
51 } else if (_state == "Z") {
52 _shape = "Msquare"
53 _style = "solid"
54 # -----------------------------------------------------------------
55 # UNKNOWN STATE
56 # -----------------------------------------------------------------
57 } else {
58 _shape = "doublecircle"
59 }
60
61 _color =\
62 num_scale(\
63 child2cpu[v],
64 max_cpu,
65 VERT_COLORSCHEME_MAX,
66 VERT_COLORSCHEME_MIN\
67 )
68 _size =\
69 num_scale(\
70 child2mem[v],
71 max_mem,
72 1,
73 4\
74 ) / 4
75 _height = _size
76 _width = _size
77 _fontcolor = \
78 _color == VERT_COLORSCHEME_MAX || _color == VERT_COLORSCHEME_MIN \
79 ? sprintf("/%s/%d", VERT_COLORSCHEME, VERT_COLORSCHEME_MID) \
80 : sprintf("/%s/%d", "greys9", 9)
81 _fontcolor = \
82 _size < 0.5 \
83 ? sprintf("/%s/%d", "greys9", 9) \
84 : _fontcolor
85 _label_base = \
86 sprintf("%s\n%d", child2comm[v], v)
87 _label_ext = \
88 _size >= 0.5 \
89 ? sprintf("\ncpu: %.1f%%\nmem: %.1f%%", child2cpu[v], child2mem[v]) \
90 : ""
91 _label = _label_base _label_ext
92 printf(\
93 "\"%d\"\
94 [ fontsize=8 \
95 , fixedsize=true \
96 , height=%f \
97 , width=%f \
98 , border=1 \
99 , style=\"%s\" \
100 , fontname=Helvetica \
101 , label=\"%s\" \
102 , shape=\"%s\" \
103 , fillcolor=\"/%s/%d\" \
104 , fontcolor=\"%s\" \
105 ];",
106 v,
107 _height,
108 _width,
109 _style,
110 _label,
111 _shape,
112 VERT_COLORSCHEME,
113 _color,
114 _fontcolor\
115 )
116 }
117
118 function edge_print(child, _parent) {
119 _parent = child2parent[child]
120 printf(\
121 "\"%s\" -> \"%s\"\
122 [ fontsize=8 \
123 , fontname=Helvetica \
124 , len=2.0 \
125 , color=\"%s\" \
126 ];\n",
127 _parent,
128 child,
129 EDGE_COLOR\
130 )
131 }
132
133 BEGIN {
134 # Hot->Cold gradual colorschemes:
135 # - rdbu11
136 # - rdbu9
137 # - rdbu8
138 # - rdylgn10 # 3 - 11
139
140 # Light->Dark gradual colorschemes:
141 # - reds9
142 # - blues9
143 # - orrd9
144 # - oranges9
145 # - bupu9
146 # - greys9
147
148 VERT_COLORSCHEME_MIN = 1
149 VERT_COLORSCHEME_MID = 4
150 VERT_COLORSCHEME_MAX = 8
151 VERT_COLORSCHEME = "rdylgn10"
152
153 EDGE_COLOR = "/ylorbr9/3"
154
155 child2comm[0] = "swapper/sched"
156 }
157
158 NR > 1 {
159 parent2child_count[$2]++
160 max_children = \
161 parent2child_count[$2] > max_children\
162 ? parent2child_count[$2]\
163 : max_children
164 child2parent[$1] = $2
165 child2user_id[$1] = $3
166 child2user_name[$1] = $4
167 child2nice[$1] = $5
168 child2state[$1] = $6
169 child2cpu[$1] = $7
170 child2mem[$1] = $8
171 child2comm[$1] = $9
172 user_names[$4] = 1
173 max_cpu = $7 > max_cpu ? $7 : max_cpu
174 max_mem = $8 > max_mem ? $8 : max_mem
175 }
176
177 END {
178 print "strict digraph G {";
179
180 print "start=0;";
181 print "fontsize=8;";
182 print "fontname=Helvetica;";
183 print "label=\"" kernel "\";";
184 print "fontcolor=\"/greys9/9\";"
185
186 ##### Vertices (clustered by user)
187 for (user_name in user_names) {
188 printf "subgraph \"cluster_%s\" {\n", user_name
189 printf "label=\"%s\"\n", user_name
190 for (c in child2parent)
191 if (child2user_name[c] == user_name)
192 vert_print(c)
193 print "}"
194 }
195
196 ##### Vertices (without a user)
197 for (c in child2comm)
198 if (!child2user_name[c])
199 vert_print(c)
200
201 ##### Edges (across clusters)
202 for (c in child2parent)
203 edge_print(c)
204
205 print "}";
206 }
207 '
208 }
209
210
211 procs() {
212 if [ "$(uname)" = 'Linux' ]; then
213 ps -eo 'pid,ppid,euid,euser,nice,s,%cpu,%mem,comm'
214 else
215 ps -eco 'pid,ppid,euid,euser,nice,s,%cpu,%mem,comm'
216 fi
217 }
218
219
220 main() {
221 case "$1" in
222 '--help') usage
223 ;;
224 *) procs | grep "$1" | compile
225 ;;
226 esac
227 }
228
229
230 main "$1"
This page took 0.0767949999999999 seconds and 4 git commands to generate.