Distinguish process states by shape
[khome.git] / home / bin / ps2dot
index 9a31c80..528d100 100755 (executable)
@@ -9,100 +9,143 @@ usage() {
 compile() {
     awk -v kernel="$(uname -v)" -v whoami="$(whoami)" \
     '
+        function num_scale(src_cur, src_max, dst_min, dst_max) {
+            return dst_min + ((src_cur * (dst_max - dst_min)) / src_max)
+        }
+
+        function vert_print(v,    _color, _fontcolor, _shape, _state) {
+            _shape = "rectangle"
+            _state = child2state[v]
+            if (_state == "D") {
+                _shape = "circle"
+            } else if (_state == "I") {
+                _shape = "octagon"
+            } else if (_state == "R") {
+                _shape = "star"
+            } else if (_state == "S") {
+                _shape = "oval"
+            } else if (_state == "T") {
+                _shape = "square"
+            } else if (_state == "t") {
+                _shape = "Msquare"
+            } else if (_state == "Z") {
+                _shape = "diamond"
+            }
+            _color =\
+                num_scale(\
+                    child2nice[v] + 20,
+                    20 + 20,
+                    COLORSCHEME_MIN,
+                    COLORSCHEME_MAX\
+                )
+            _fontcolor = \
+                _color == COLORSCHEME_MAX || _color == COLORSCHEME_MIN \
+                ? sprintf("/%s/%d", COLORSCHEME, COLORSCHEME_MID) \
+                : sprintf("/%s/%d", "greys9", 9)
+            printf(\
+                "\"%d\"\
+                [ fontsize=8 \
+                , border=1 \
+                , style=\"filled,solid\" \
+                , fontname=Helvetica \
+                , label=\"%s\n%d\" \
+                , shape=\"%s\" \
+                , fillcolor=\"/%s/%d\" \
+                , fontcolor=\"%s\" \
+                ];",
+                v,
+                child2comm[v],
+                v,
+                _shape,
+                COLORSCHEME,
+                _color,
+                _fontcolor\
+            )
+        }
+
+        function edge_print(child,    _parent, _color, _colorscheme) {
+            _parent = child2parent[child]
+            _colorscheme = COLORSCHEME
+            _color = COLORSCHEME_MID
+            printf(\
+                "\"%s\" -> \"%s\"\
+                [ fontsize=8 \
+                , fontname=Helvetica \
+                , len=2.0 \
+                , colorscheme=%s \
+                , color=%d \
+                ];\n",
+                _parent,
+                child,
+                _colorscheme,
+                _color\
+            )
+        }
+
         BEGIN {
-            p2cmd[0] = "swapper/sched"
-            color_base = 20
-            color_incr = 30
-            color_hi  = sprintf("grey%d", (color_base + (color_incr * 0)))
-            color_mid = sprintf("grey%d", (color_base + (color_incr * 1)))
-            color_low = sprintf("grey%d", (color_base + (color_incr * 2)))
+            # Hot->Cold gradual colorschemes:
+            # - rdbu11
+            # - rdbu9
+            # - rdbu8
+
+            # Light->Dark gradual colorschemes:
+            # - reds9
+            # - blues9
+            # - orrd9
+            # - oranges9
+            # - bupu9
+            # - greys9
+
+            COLORSCHEME_MIN = 1
+            COLORSCHEME_MID = 5
+            COLORSCHEME_MAX = 10
+            COLORSCHEME = sprintf("rdylgn%d", COLORSCHEME_MAX)
+
+            child2comm[0] = "swapper/sched"
         }
 
         NR > 1 {
-            p2pp[$1]=$2;
-            p2user_id[$1]=$3
-            p2user_name[$1]=$4
-            p2cmd[$1]=$5
-            user_names[$4]=1
+            parent2child_count[$2]++
+            max_children = \
+                parent2child_count[$2] > max_children\
+                ? parent2child_count[$2]\
+                : max_children
+            child2parent[$1]    = $2
+            child2user_id[$1]   = $3
+            child2user_name[$1] = $4
+            child2nice[$1]      = $5
+            child2state[$1]     = $6
+            child2comm[$1]      = $7
+            user_names[$4]      = 1
         }
 
         END {
             print "strict digraph G {";
 
             print "start=0;";
-            print "colorscheme=brewer;";
             print "fontsize=8;";
             print "fontname=Helvetica;";
             print "label=\"" kernel "\";";
-
-            printf(\
-                "edge [ fontsize=8 \
-                      , fontname=Helvetica \
-                      , len=2.0 \
-                      , color=%s \
-                      ];",
-                color_low\
-            )
-
-            print "node [ fontsize=8 \
-                        , fontname=Helvetica \
-                        , shape=ellipse \
-                        ]; \
-                    ";
+            print "fontcolor=\"/greys9/9\";"
 
             ##### Vertices (clustered by user)
-            for (user in user_names) {
-                printf "subgraph \"cluster_%s\" {\n", user
-                printf "label=\"%s\"\n", user
-                for (p in p2pp) {
-                    if (p2user_name[p] == user) {
-                        color     = p2user_name[p] == whoami ? color_hi : color_low
-                        fontcolor = p2user_name[p] == whoami ? color_hi : color_mid
-                        printf(\
-                            "\"%d\"\
-                            [ fontsize=8 \
-                            , fontname=Helvetica \
-                            , shape=ellipse \
-                            , label=\"%s\n%d\" \
-                            , color=\"%s\" \
-                            , fontcolor=\"%s\" \
-                            ];",
-                            p,
-                            p2cmd[p],
-                            p,
-                            color,
-                            fontcolor\
-                        )
-                    }
-                }
+            for (user_name in user_names) {
+                printf "subgraph \"cluster_%s\" {\n", user_name
+                printf "label=\"%s\"\n", user_name
+                for (c in child2parent)
+                    if (child2user_name[c] == user_name)
+                        vert_print(c)
                 print "}"
             }
 
             ##### Vertices (without a user)
-            for (p in p2cmd) {
-                if (!p2user_name[p]) {
-                    printf(\
-                        "\"%d\"\
-                        [ fontsize=8 \
-                        , fontname=Helvetica \
-                        , shape=ellipse \
-                        , label=\"%s\n%d\" \
-                        , color=\"%s\" \
-                        , fontcolor=\"%s\" \
-                        ];",
-                        p,
-                        p2cmd[p],
-                        p,
-                        color_low,
-                        color_mid\
-                    )
-                }
-            }
+            for (c in child2comm)
+                if (!child2user_name[c])
+                    vert_print(c)
 
             ##### Edges (across clusters)
-            for (p in p2pp) {
-                printf "\"%d\" -> \"%d\";\n",  p2pp[p], p, p
-            }
+            for (c in child2parent)
+                edge_print(c)
 
             print "}";
         }
@@ -112,9 +155,9 @@ compile() {
 
 procs() {
     if [ "$(uname)" = 'Linux' ]; then
-        ps -eo pid,ppid,euid,euser,comm
+        ps -eo 'pid,ppid,euid,euser,nice,s,comm'
     else
-        ps -eco pid,ppid,euid,euser,comm
+        ps -eco 'pid,ppid,euid,euser,nice,s,comm'
     fi
 }
 
This page took 0.036763 seconds and 4 git commands to generate.