Set default font color to near-black
[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) {
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 print "fontcolor=\"/greys9/9\";"
110
111 ##### Vertices (clustered by user)
112 for (user_name in user_names) {
113 printf "subgraph \"cluster_%s\" {\n", user_name
114 printf "label=\"%s\"\n", user_name
115 for (c in child2parent)
116 if (child2user_name[c] == user_name)
117 vert_print(c)
118 print "}"
119 }
120
121 ##### Vertices (without a user)
122 for (c in child2cmd)
123 if (!child2user_name[c])
124 vert_print(c)
125
126 ##### Edges (across clusters)
127 for (c in child2parent)
128 edge_print(c)
129
130 print "}";
131 }
132 '
133 }
134
135
136 procs() {
137 if [ "$(uname)" = 'Linux' ]; then
138 ps -eo 'pid,ppid,euid,euser,nice,comm'
139 else
140 ps -eco 'pid,ppid,euid,euser,nice,comm'
141 fi
142 }
143
144
145 main() {
146 case "$1" in
147 '--help') usage
148 ;;
149 *) procs | grep "$1" | compile
150 ;;
151 esac
152 }
153
154
155 main "$1"
This page took 0.059485 seconds and 4 git commands to generate.