#!/bin/sh
#
# count_threads.sh
#
# Counts the total number of threads on an HP-UX system. Requires
# glance to be installed.
#
# 2004/12/17 Olivier S. Masse, omasse ~at~ mayoxide ~dot~ com
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

tempfile=/tmp/$(basename $0).$$

cat >$tempfile.adv <<EOT
process loop
{
	print PROC_PROC_ID, " ", PROC_PROC_NAME, " ", PROC_THREAD_COUNT
}
EOT

/opt/perf/bin/glance -j 1 -iterations 1 -adviser_only -syntax $tempfile.adv >$tempfile.out 2>/dev/null
if [ $? -ne 0 ]
then	
	echo "Problem running glance, exiting..."
	exit 1
fi

cat $tempfile.out | awk '{
	count=0;
	do 
	{
		if (match($3, /[0-9]/) != 0)
		{
			count=count + $3;
			print;
		}
	}
	while(getline);
	print;
	print "Total number of threads is: " count;
}'

/usr/sbin/kmtune | awk '{
        if (match($1, /^nkthread/) != 0)
                print "The maximum number of total threads (nkthread) is: " $2;
        if (match($1, /^max_thread_proc/) != 0)
                print "The maximum number of threads per process (max_thread_proc) is: " $2;
}'

rm -f $tempfile.adv
rm -f $tempfile.out



