#!/bin/sh
#
# ------------------------------------------------------------------
#  sh_proc.sh
# 
#  November 2011, Ritesh Sharma
# 
#  Copyright (c) 2010-2013 by Cisco Systems, Inc.
#  All rights reserved.
# ------------------------------------------------------------------

# TBD : TimeInState and HR:MM:SS:MSEC

# SH_PROC_FORMAT_STRING
#
# The format string used when printing information.
SH_PROC_FORMAT_STRING="%5.5s %5.5s %5.5s %8.8s %5.5s  %-7.7s  %-17s  %-17s  %-s\n"


# Load the show_proc / pidin library.
source `dirname $0`/sh_proc_lib


function main
{
    local node_list=""
    local process_list=""
    local thread_list=""
    
    printf "$SH_PROC_FORMAT_STRING" \
         "JID" "TID" "CPU" "Stack" "pri" "state"     "NAME"

    # Generate a list of nodes.
    sh_proc_node_iterator
    node_list=("${sh_proc_node_iterator_out[@]}")

    # Iterate over each node.
    for node in ${node_list[@]}; do
        # If there are multiple nodes we print a header for each node.
        if [ ${#node_list[@]} -gt 1 ]; then
            sh_proc_print_node_header $node
        fi
        # Set the path for the /proc filesystem of the current node.
        sh_proc_set_proc_path $node

        # Genereate a list of processes.
        sh_proc_process_iterator $node
        process_list=($sh_proc_process_iterator_out)

        # Generate a list of JIDs from the process list.
        sh_proc_pid_list_to_jid_list_in=("${process_list[@]}")
        sh_proc_pid_list_to_jid_list
        jid_list=($sh_proc_pid_list_to_jid_list_out)

        # Iterate over each process.
        local index=0
        while [ $index -lt ${#process_list[@]} ]; do
            local process=${process_list[$index]}

            # Generate a list of threads.
            sh_proc_thread_iterator $process
            thread_list=$sh_proc_thread_iterator_out

            # Iterate over each thread.              
            for thread in $thread_list; do

                # Retrieve the required information.
                sh_proc_get_item $thread "priority"
                priority_out=$sh_proc_get_item_out

                sh_proc_get_item $thread "thread_state"
                thread_state_out=$sh_proc_get_item_out

                sh_proc_get_item $thread "basename15"
                name_out=$sh_proc_get_item_out

                sh_proc_get_item $thread "last_cpu"
                cpu_out=$sh_proc_get_item_out

                # If the information was succesfully retrieved print it. If the
                # PID could not be converted to a JID print an apporpriate
                # error message.                
                if [ $sh_proc_get_item_error -eq 0 ]; then
                    if [ ${jid_list[$index]} == "-1" ]; then
                        printf "Failed to get jobid from pid\n"
                    else
                        printf "$SH_PROC_FORMAT_STRING" \
                               ${jid_list[$index]} $thread $cpu_out "OK" \
                               $priority_out $thread_state_out \
                               $name_out
                    fi
                else
                    sh_proc_get_item_error=0
                fi
            done
            let index+=1       
        done
        printf "\n"
    done
}


sh_proc_command_parser "$@"
main
