#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# rspc72V src/rspc/usr/sbin/rtas/getRtasHeap.sh 1.3.1.2 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2005,2020 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)85      1.3.1.2  src/rspc/usr/sbin/rtas/getRtasHeap.sh, rspcpal, rspc72V, v2020_10A3 2/27/20 16:32:26
#
# COMPONENT_NAME: rspcpal
#
# FUNCTION: This command dumps RTAS Heap Memory. This command is called by snap.
#
# ORIGINS: 27
#

if [ "$PASSNO" = 1 ]
then
    echo "Checking space requirement for RTAS heap information...\c" >> $SCRIPTLOG
else
    echo "Gathering RTAS heap memory information...\c" >> $SCRIPTLOG
fi

rtas_heap_line=`echo "ipl -mem" | KDB_AUTOLOAD="" /usr/sbin/kdb -script | /usr/bin/grep RTAS_HEAP`
rtas_size_bytes=`echo $rtas_heap_line | /usr/bin/cut -f 3 -d " "`

if [ "$PASSNO" = 1 ]
then
    # Previously this script manually dumped the RTAS data from command KDB with
    # the 'dpw' subcommand. That design necessitated that the data estimate take
    # into account a fixed KDB header and the fact that the data would be broken
    # up into columns/rows.
    #
    # By using the 'dumpbin' KDB subcommand, the data is stored directly to a
    # file and thus the data size estimate is the size of the RTAS data.
    typeset -i10 total_bytes
    total_bytes=16#$rtas_size_bytes

    # report size for both adapter types
    echo "$total_bytes" > $SCRIPTSIZE
    echo "RTAS Heap first pass reporting size= $total_bytes" >> $SCRIPTLOG
    exit 0
#end of first pass
fi

# Second pass,  return without error on first pass
if [[ -z $SNAPDIR ]]
then
    echo "ERROR: the script directory was not created  " >> $SCRIPTLOG
    exit 1
fi

# The KDB 'dumpbin' command may not properly handle absolute paths, change
# to the specified snap directory and run the command "locally"
cd $SNAPDIR

rtas_address=`echo $rtas_heap_line | /usr/bin/cut -f 2 -d " "`
echo "dumpbin -p $rtas_address $rtas_size_bytes rtas_heap.out" | KDB_AUTOLOAD="" /usr/sbin/kdb -script > /dev/null
if [ -x /usr/bin/gzip/ ]
then
    /usr/bin/rm $SNAPDIR/rtas_heap.out.gz > /dev/null 2>&1
    /usr/bin/gzip $SNAPDIR/rtas_heap.out > /dev/null 2>&1
fi
echo " done." >> $SCRIPTLOG
