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