6 echo "EXAMPLE (whole tree) : $0 | neato -T png > ps.png && open ps.png"
7 echo "EXAMPLE (user clusters): $0 | sdp -T png > ps.png && open ps.png"
12 awk -v kernel
="$(uname -v)" -v whoami
="$(whoami)" \
14 function num_scale(src_cur, src_max, dst_min, dst_max) {
15 return dst_min + ((src_cur * (dst_max - dst_min)) / src_max)
18 function vert_print(v, _color, _fontcolor, _shape, _state, _size, _height, _label, _label_base, _label_ext) {
19 _state = child2state[v]
20 _style = "filled,solid"
22 # -----------------------------------------------------------------
24 # -----------------------------------------------------------------
25 # D uninterruptible sleep (usually IO)
28 # I Idle kernel thread
29 } else if (_state == "I") {
31 # S interruptible sleep (waiting for an event to complete)
32 } else if (_state == "S") {
34 # -----------------------------------------------------------------
36 # -----------------------------------------------------------------
37 # R running or runnable (on run queue)
38 } else if (_state == "R") {
40 # -----------------------------------------------------------------
42 # -----------------------------------------------------------------
43 # T stopped by job control signal
44 } else if (_state == "T") {
46 # t stopped by debugger during the tracing
47 } else if (_state == "t") {
49 # -----------------------------------------------------------------
51 # -----------------------------------------------------------------
52 # Z defunct ("zombie") process, terminated but not reaped by its parent
53 } else if (_state == "Z") {
56 # -----------------------------------------------------------------
58 # -----------------------------------------------------------------
60 _shape = "doublecircle"
80 _color == VERT_COLORSCHEME_MAX || _color == VERT_COLORSCHEME_MIN \
81 ? sprintf("/%s/%d", VERT_COLORSCHEME, VERT_COLORSCHEME_MID) \
82 : sprintf("/%s/%d", "greys9", 9)
85 ? sprintf("/%s/%d", "greys9", 9) \
88 sprintf("%s\n%d", child2comm[v], v)
91 ? sprintf("\ncpu: %.1f%%\nmem: %.1f%%", child2cpu[v], child2mem[v]) \
93 _label = _label_base _label_ext
102 , fontname=Helvetica \
105 , fillcolor=\"/%s/%d\" \
120 function edge_print(child, _parent) {
121 _parent = child2parent[child]
125 , fontname=Helvetica \
136 # Hot->Cold gradual colorschemes:
140 # - rdylgn10 # 3 - 11
142 # Light->Dark gradual colorschemes:
150 VERT_COLORSCHEME_MIN = 1
151 VERT_COLORSCHEME_MID = 4
152 VERT_COLORSCHEME_MAX = 8
153 VERT_COLORSCHEME = "rdylgn10"
155 EDGE_COLOR = "/ylorbr9/3"
157 child2comm[0] = "swapper/sched"
161 parent2child_count[$2]++
163 parent2child_count[$2] > max_children\
164 ? parent2child_count[$2]\
166 child2parent[$1] = $2
167 child2user_id[$1] = $3
168 child2user_name[$1] = $4
175 max_cpu = $7 > max_cpu ? $7 : max_cpu
176 max_mem = $8 > max_mem ? $8 : max_mem
180 print "strict digraph G {";
184 print "fontname=Helvetica;";
185 print "label=\"" kernel "\";";
186 print "fontcolor=\"/greys9/9\";"
188 ##### Vertices (clustered by user)
189 for (user_name in user_names) {
190 printf "subgraph \"cluster_%s\" {\n", user_name
191 printf "label=\"%s\"\n", user_name
192 for (c in child2parent)
193 if (child2user_name[c] == user_name)
198 ##### Vertices (without a user)
199 for (c in child2comm)
200 if (!child2user_name[c])
203 ##### Edges (across clusters)
204 for (c in child2parent)
214 if [ "$(uname)" = 'Linux' ]; then
215 ps
-eo 'pid,ppid,euid,euser,nice,s,%cpu,%mem,comm'
217 ps
-eco 'pid,ppid,euid,euser,nice,s,%cpu,%mem,comm'
228 timestamp
="$(date +'%Y-%m-%d_%H:%M:%S%z')"
230 kernel
="$(uname -s | awk '{print tolower($0)}')"
231 filename_base
=$
(mktemp
"ps.$host.$kernel.$timestamp.XXXXX")
232 file_log
="$filename_base.log"
233 file_dot
="$filename_base.dot"
234 mv "$filename_base" "$file_log"
235 procs |
grep "$1" | ps2dot
2> "$file_log" > "$file_dot"
236 time neato
-T png
"$file_dot" 2> "$file_log" > "$filename_base.neato.png"
237 time fdp
-T png
"$file_dot" 2> "$file_log" > "$filename_base.fdp.png"
238 time dot
-T png
"$file_dot" 2> "$file_log" > "$filename_base.dot.png"
239 ls -1 "$filename_base"*
This page took 0.098629 seconds and 4 git commands to generate.