Output to files from ps2dot
[khome.git] / home / bin / ps2dot
index 39e8f4f..c6bf85f 100755 (executable)
@@ -1,12 +1,14 @@
 #! /bin/sh
 
+set -e
+
 usage() {
     echo "EXAMPLE (whole tree)   : $0 | neato -T png > ps.png && open ps.png"
     echo "EXAMPLE (user clusters): $0 | sdp   -T png > ps.png && open ps.png"
 }
 
 
-compile() {
+ps2dot() {
     awk -v kernel="$(uname -v)" -v whoami="$(whoami)" \
     '
         function num_scale(src_cur, src_max, dst_min, dst_max) {
@@ -14,23 +16,50 @@ compile() {
         }
 
         function vert_print(v,    _color, _fontcolor, _shape, _state, _size, _height, _label, _label_base, _label_ext) {
-            _shape = "rectangle"
             _state = child2state[v]
+            _style = "filled,solid"
+
+            # -----------------------------------------------------------------
+            # Sleeping/idling
+            # -----------------------------------------------------------------
+            # D uninterruptible sleep (usually IO)
             if (_state == "D") {
                 _shape = "circle"
+            # I Idle kernel thread
             } else if (_state == "I") {
-                _shape = "octagon"
-            } else if (_state == "R") {
-                _shape = "star"
+                _shape = "circle"
+            # S interruptible sleep (waiting for an event to complete)
             } else if (_state == "S") {
-                _shape = "oval"
+                _shape = "circle"
+            # -----------------------------------------------------------------
+            # Running
+            # -----------------------------------------------------------------
+            # R running or runnable (on run queue)
+            } else if (_state == "R") {
+                _shape = "rarrow"
+            # -----------------------------------------------------------------
+            # Stopped
+            # -----------------------------------------------------------------
+            # T stopped by job control signal
             } else if (_state == "T") {
                 _shape = "square"
+            # t stopped by debugger during the tracing
             } else if (_state == "t") {
-                _shape = "Msquare"
+                _shape = "square"
+            # -----------------------------------------------------------------
+            # Dead
+            # -----------------------------------------------------------------
+            # Z defunct ("zombie") process, terminated but not reaped by its parent
             } else if (_state == "Z") {
-                _shape = "diamond"
+                _shape = "Msquare"
+                _style = "solid"
+            # -----------------------------------------------------------------
+            # UNKNOWN STATE
+            # -----------------------------------------------------------------
+            } else {
+                _shape = "doublecircle"
             }
+
             _color =\
                 num_scale(\
                     child2cpu[v],
@@ -43,9 +72,10 @@ compile() {
                     child2mem[v],
                     max_mem,
                     1,
-                    5\
-                ) / 5
+                    4\
+                ) / 4
             _height = _size
+            _width  = _size
             _fontcolor = \
                 _color == VERT_COLORSCHEME_MAX || _color == VERT_COLORSCHEME_MIN \
                 ? sprintf("/%s/%d", VERT_COLORSCHEME, VERT_COLORSCHEME_MID) \
@@ -66,8 +96,9 @@ compile() {
                 [ fontsize=8 \
                 , fixedsize=true \
                 , height=%f \
+                , width=%f \
                 , border=1 \
-                , style=\"filled,solid\" \
+                , style=\"%s\" \
                 , fontname=Helvetica \
                 , label=\"%s\" \
                 , shape=\"%s\" \
@@ -76,6 +107,8 @@ compile() {
                 ];",
                 v,
                 _height,
+                _width,
+                _style,
                 _label,
                 _shape,
                 VERT_COLORSCHEME,
@@ -188,9 +221,22 @@ procs() {
 
 main() {
     case "$1" in
-        '--help') usage
+        '--help')
+            usage
         ;;
-        *) procs | grep "$1" | compile
+        *)
+            timestamp="$(date +'%Y-%m-%d_%H:%M:%S%z')"
+            host="$(hostname)"
+            kernel="$(uname -s | awk '{print tolower($0)}')"
+            filename_base=$(mktemp "ps.$host.$kernel.$timestamp.XXXXX")
+            file_log="$filename_base.log"
+            file_dot="$filename_base.dot"
+            mv "$filename_base" "$file_log"
+            procs | grep "$1" | ps2dot 2> "$file_log" > "$file_dot"
+            time neato -T png "$file_dot" 2> "$file_log" > "$filename_base.neato.png"
+            time fdp   -T png "$file_dot" 2> "$file_log" > "$filename_base.fdp.png"
+            time dot   -T png "$file_dot" 2> "$file_log" > "$filename_base.dot.png"
+            ls -1 "$filename_base"*
         ;;
     esac
 }
This page took 0.031314 seconds and 4 git commands to generate.