#!/bin/sh

USAGE="
Usage:

mlt [-h] 
    [-d <Entry Index to be displayed in detail>] 
    [-s <Number of entries to be displayed. < 100 expected>]
    [-t <Number of entries to be displayed. < 100 expected>]

    -s      Displays entries sorting stack traces by total allocation size
    -t      Displays entries sorting stack traces by total allocation count
    -d      displays memory leak information of one entry in detail. 
            Entry Index should be provided from \"Display Index\" column of mlt output
    -h      Displays this text
 "

if [ ! -d "/proc/mlt" ]
then
    echo "MLT not enabled"
    exit 0
fi

basic=
size_sort=1
cnt_sort=0
num_entries=20
detail=0
num_opts=0

# Parse command line options.
while getopts hbs:d:t: OPT; do
    case "$OPT" in
        h)
            echo "$USAGE"
            exit 0
            ;;
        d)
	    detail="$OPTARG"
            if [[ $detail = *[![:digit:]]* ]]; then
                echo "Error: Entry Index input to -d arg is not a number"
                exit 1
	    fi
	    if [ "$detail" -le 0 ]
	    then
                echo "Error: Entry Index input to -d arg should be > 0"
                exit 1
	    fi

	    num_opts=`expr $num_opts + 1`
            ;;
        s)
	    num_entries="$OPTARG"
            if [[ $num_entries = *[![:digit:]]* ]]; then
                exec >&2; echo "Error: Number of entries should be < 100"; exit 1
                exit 1
            fi
	    if [ "$num_entries" -gt 100 ]
	    then
                echo "Error: Number of entries should be < 100"
                exit 1
	    fi
	    size_sort=1
	    cnt_sort=0
	    num_opts=`expr $num_opts + 1`
            ;;
        t)
	    num_entries="$OPTARG"
            if [[ $num_entries = *[![:digit:]]* ]]; then
                exec >&2; echo "Error: Number of entries should be < 100"; exit 1
                exit 1
            fi
	    if [ "$num_entries" -gt 100 ]
	    then
                echo "Error: Number of entries should be < 100"
                exit 1
	    fi
	    cnt_sort=1
	    size_sort=0
	    num_opts=`expr $num_opts + 1`
            ;;
        b)
	    # This option is supported for backward compatibility purpose. 
	    # Helpful incase some is using mlt commands in their scripts
	    cnt_sort=0
	    size_sort=1
	    num_entries=20
	    num_opts=`expr $num_opts + 1`
            ;;
        \?)
            # getopts issues an error message
            echo "$USAGE"
            exit 1
    esac
done

if [ "$num_opts" -gt 1 ]
then
    echo "-d, -s and -t are mutually exclusive options"
    exit 0
fi

filename=/proc/mlt/config
#buffer1=`cat $filename |  sed -n '2p'`

if [ "$detail" -gt 0 ]
then
    echo $detail > /proc/mlt/det_mem_leaks
    cat /proc/mlt/det_mem_leaks
else
    buffer=`cat $filename |  sed -n '2p' | sed -e 's/^\([0-9]*\) \(.*\)/'"$cnt_sort"' \2/'`  
    buffer2=`echo $buffer | sed '$s/ *\([^ ]* *\)$//'`
    buffer3=`echo $buffer2 | sed '$s/ *\([^ ]* *\)$//'`
    buffer4=`echo $buffer3 $size_sort $num_entries`
    echo $buffer4 > $filename

    cat /proc/slabinfo | awk '{ SUM += $2 * $4} END {print " "; print "Outstanding kmem_cache_alloc Memory: " SUM / 1048576 "MB"}' ; cat /proc/slabinfo | awk '{ SUM += $3 * $4} END {print "Mem used in SLUB (including unused objects): " SUM / 1048576 "MB "}'

    cat /proc/mlt/mem_leaks

    cat /proc/slabinfo | grep -v "<active_objs>" | grep MLT_hash_nodes_Pool | awk '{ SUM += $2 * $4} END {print "Memory used by MLT module itself: " SUM / 1048576 "MB \n"}'
fi


#echo $buffer1 > $filename


