#!/bin/ksh ## ## SCRIPT: ps_memsort_anyhost_bygui ## ## Where: in $FEDIR/scripts where FEDIR=/apps/nns_com/fea ## ############################################################################ ## PURPOSE: ## Lists the top processes on ANY host in terms of the SZ (Total memory ## size) column of the 'ps -el' SZ:RSS column. ## ## 'SZ' is Total size (in pages) of the process, including code, ## data, shared memory, mapped files, shared libraries and stack. ## [Pages associated with mapped devices are not counted.] ## ## 'RSS' is Total resident size (in pages) of process. ## ## Uses the commands 'ps', 'sed', 'awk', and 'sort'. ## ## A Tcl-Tk script presents a GUI that prompts for hostid. ## The entry field is initialized with the hostname of the ## SGI host on which this script is running. ############################################################################ ## CALLED BY: 'perftools' in $FEDIR/scripts --- ## actually 'perftools.chestdef' in $FEDIR/scripts. ## ## The SGI toolchest-drawers path is ## ## SGI Toolchest -> ## Handy Tools -> ## Any Host - Perf Tools (Processes) ## ############################################################################ ## METHOD: ## Uses 'ps -el' piped to 'sed', 'awk', and 'sort'. ## ## But the '-l' option shows UID as a number rather than a name. ## And mem-sizes print as SZ:RSS. ## ## Some investigation of 'man ps' AND strings in '/sbin/ps' indicate that ## it would be better (more flexible) to use a '-o' set of options like ## ## ps -e -o 'user ruser pid ppid otime time sz rss cpu pcpu state pri nice tty etime stime args' ## ## Some other options: ## pgid sid class pri opri nice addr wname sz:rss vsz tty comm ## group rgroup uid flag util label ############################################################################ ## MAINTENANCE HISTORY: ## Written by: B. Montandon 27Apr2000 Based on 'topps_mem_anyhost' and ## 'ps_cumcpu_1host_bygui' in $FEDIR/scripts ## ## Updated by: Blaise Montandon 08apr2004 To avoid a bug in new SGI 'sort' ## in IRIX 6.5.22, changed several cases ## of 'sort -k5nr' to 'sort +4 -5nr'. ## and 'sort -k4nr' to 'sort +3 -4nr'. ## Updated by: Blaise Montandon 13apr2004 Update the trailer of the report. ## E.g. add toolchest-drawer path. ############################################################################ if test "$FEDIR" = "" then FEDIR=/apps/nns_com/fea export FEDIR fi ########################################################################## ## PROMPT FOR HOSTNAME. ########################################################################## WINTITLE="Processes (sorted by Memory usage) @AnyHost" export WINTITLE WIN_INFO="\ INPUT: Enter/select the name of an SGI-IRIX host on the NNS network --- and click OK. (You can enter an IP address, if necessary.) ------ OUTPUT: This utility shows all the processes on the specified host --- sorted by 'total' (potential, RAM or RAM + swap) memory usage of each process. This utility also shows 'resident' (actual, RAM-chip) memory usage of each process. ----- USAGE: This utility is helpful to check for processes on the host that MAY be putting a HEAVY PAGING LOAD on the host --- because of processes exceeding the amount of 'real' memory at the host. 'Real' memory is memory in chips --- in contrast to much slower overflow memory on disk (known as 'swap' space). Generally, you want to avoid 'paging' of programs & data to the disk swap space --- because it SLOWS DOWN YOU AND YOUR JOB(S). ----- Note that you can do queries on MULTIPLE HOSTS and bring the report windows side-by-side for comparison --- to look for abnormal memory usage conditions. OR you may want to start other displays for the SAME HOST -- like 'top' or 'gr_osview' or network-I/O or memory-usage displays. " export WIN_INFO HOST_ID="`hostname`" export HOST_ID # HOST_ID=`$FEDIR/tkGUIs/enter_hostid.tk` # HOST_ID=`$FEDIR/tkGUIs/enter_hostid_optmenu.tk` HOST_ID=`$FEDIR/tkGUIs/enter_hostid_optmenu_toghelp-scroll.tk` if test "$HOST_ID" = "" then exit fi ########################################################################## ## SET OUTPUT FILENAME, local if possible. ########################################################################## . $FEDIR/scripts/set_localoutlist ########################################################################## ## SET REPORT HEADING. ########################################################################## ## HOSTNAME3=`/usr/etc/arp $HOST_ID` ## HOSTNAME2=`echo "$HOSTNAME3" |sed "s|-- no entry||"` echo "\ ..................... `date '+%Y %b %d %a %T%p'` ....................... ALL PROCESSES ON HOST: $HOST_ID ( SORTED BY Memory Usage --- SZ, then RSS, then PID ) SZ is the Total memory needed by the process. RSS is the portion of SZ that is actually resident in 'real' (chip) memory, at the moment. More meanings of column headings are given at the bottom of this report. Cum CPU TIME UID# PID PPID SZ RSS (min:sec) CMD -------- -------- -------- -------- -------- --------- ---------------------------------------- " > $OUTLIST ########################################################################## ## SET INDICATOR OF Local or Remote host. ########################################################################## THISHOST=`hostname` LOCALHOST="N" if test "$HOST_ID" = "$THISHOST" then LOCALHOST="Y" fi ########################################################################## ## PUT 'ps -el' output into an in-memory variable, PS_OUTPUT. ########################################################################## if test "$LOCALHOST" = "Y" then PS_OUTPUT=`ps -el` else ############################################## ## MAKE .rhosts FILE FOR THE USER, if needed. ############################################## # echo "+ $USER" > $HOME/.rhosts . $FEDIR/scripts/mak_rhosts PS_OUTPUT=`rsh $HOST_ID ps -el` # . $FEDIR/scripts/mv_rhosts fi ########################################################################## ## GENERATE REPORT CONTENTS from the PS_OUTPUT in-memory variable. ########################################################################## ## echo "$PS_OUTPUT" | sed '1d' | cut -c5-10,11-16,17-22,36-47,64-70,71- | \ ## sed 's|:| |' | sort -r +4 >> $OUTLIST ##################################### ## Try 'awk' instead of 'cut': ##################################### echo "$PS_OUTPUT" | sed 's|:| |' | awk 'BEGIN { } ## Skip the "ps" header line. NR == 1 {next} { printf ("%8s %8s %8s %8s %8s %8s %s\n", $3, $4, $5, $10, $11, $14, $15) } ## 3 4 5 10 11 14 15 ## UID PID PPID SZ:RSS TIME CMD ## ---- -------- -------- -------- -------- ---------- ---------------------------------------- ## ## ## Handle case when two colons are NOT in the line. (STIME is a "month day" string.) ## $5 !~ /..:..:../ { ## # printf ("%8s %-8s %8s %8s %1s %3s %-4s %-8s %s \n", $8, $1, $2, $3, $4, $5, $6, $7, $9) ## # printf ("%8s %-8s %8s %8s %1s %3s %4s %-8s %s \n", $8, $1, $2, $3, $4, $5, $6, $7, $9) ## printf ("%8s %-8s %8s %8s %1s %5s %2s %-8s %s \n", $8, $1, $2, $3, $4, $5, $6, $7, $9) ## next ## } ' | sort +3nr -4 +4nr -5 +0n -1 >> $OUTLIST ## Sort by SZ, RSS, PID. # ' | sort +3 -4nr +4 -5nr +0 -1n >> $OUTLIST # ' | sort -k4nr -k5nr -k1n >> $OUTLIST ########################################################################## ## ADD REPORT 'TRAILER'. ########################################################################## echo " ..................... `date '+%Y %b %d %a %T%p'` ....................... The output above is from script $0 which was run from host $THISHOST . Commands used: 'ps -el' piped to 'sed', 'awk', and 'sort' ............................................................................ Column heading meanings --- in this formatted 'ps -l' listing: UID The user ID number of the process owner (0 = 'root'). PID The process ID of the process. PPID The process ID of the parent process. SZ Total size (in pages) of the process, including code, data, shared memory, mapped files, shared libraries and stack. Pages associated with mapped devices (such as graphics) are not counted. (A page is 4096 bytes --- or 16K on IRIX64. Refer to sysconf(1) or sysconf(3C) for information on determining the page size. Use command 'sysconf | grep PAGE' on a machine.) RSS Total resident size (in pages) of process. This includes only those pages of the process that are physically resident in memory. Mapped devices (such as graphics) are not included. Shared memory (shmget(2)) and the shared parts of a forked child (code, shared objects, and files mapped MAP_SHARED) have the number of pages prorated by the number of processes sharing the page. Two independent processes that use the same shared objects and/or the same code each count all valid resident pages as part of their own resident size. The page size can either be 4096 or 16384 bytes as determined by the return value of the getpagesize(2) system call. In general the larger page size is used on systems where uname(1) returns 'IRIX64'. This is not displayed in X/OPEN XPG4 conformance mode. TIME The cumulative execution time for the process (mins:secs) --- CPU-time, not wall-clock time. CMD The command/executable name, without its arguments. ............................................................................. IMPLEMENTATION METHODS: This 'Processes (Mem-usage sort)' script can be accessed via a drawer in a command toolchest-utility, like 'Perf Tools' --- which is available as a drawer in the command/toolchest utility 'Handy Tools'. A site SGI toolchest-drawer path is SGI Toolchest -> Handy Tools -> Any Host - Perf Tools (Processes) -> Processes (Mem sort) @AnyHost Or, you can implement the script (or the 'perftools' script or the 'handytools' script) as - a command alias, via your .profile file; alias psmem='$0' alias ptools='$FEDIR/scripts/perftools' alias htools='$FEDIR/scripts/handytools' - a desktop icon, via the 'Find, File QuickFind' tool drawers; - a drawer in the SGI toolchest, via your .auxchestrc file. ..................... `date '+%Y %b %d %a %T%p'` ....................... " >> $OUTLIST ########################################################################## ## SHOW REPORT. ########################################################################## $FEDIR/scripts/shofil $OUTLIST ############################################################################# ## END of script. Notes on 'ps -el' and 'ps -ef' follow. ############################################################################# ## Output of 'ps -el': ############################################################################# ## ## Cols: ##1-2 5-10 11-16 17-22 26-29 33-35 36-47 48-55 56-63 64-70 71- ## 3-4 23-25 30-32 ## F S UID PID PPID C PRI NI P SZ:RSS WCHAN TTY TIME COMD ## ## Examples: ##12345678901234567890123456789012345678901234567890123456789012345678901234567 ## ##30 S 0 1 0 0 39 20 * 74:46 881b65b0 ? 0:04 init ##30 S 0 507 1 0 28 20 * 294:103 881b78d0 tablet 0:00 getty ##30 S 2250 3320 3297 0 28 20 * 348:116 881b6a30 pts/9 0:00 ksh ## ## Columns wanted in this list: (marked by ** or *) ## F S UID PID PPID C PRI NI P SZ:RSS WCHAN TTY TIME COMD ## ** ** * * *** ** ## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ## ## I.e. we want: UID PID PPID SZ:RSS TIME COMD ## 3 4 5 10 13 14 ## ## ps -el | cut -c5-10,11-16,17-22,36-47,64-70,71- | sort -r +4 | \ ## head -$HEADRECS >> $1 ############################################################################# ############################################################################# ## NOTES ON 'ps -ef' output attempts: ############################################################################# ## ## First attempts: (Does not sort well on TIME, for some non-obvious reason?) ## ps -ef | sort +6 -r | head >> $1 ## ps -ef | sort +6 -r >> $1 ## ############################################################################# ## Output of 'ps -ef': ## ## Cols: ## 1-8 9-14 15-20 21-23 24-32 33-39 40-46 47- ## UID PID PPID C STIME TTY TIME COMD ## ## Examples: ## root 1 0 0 Sep 03 ? 0:04 /etc/init ## root 507 1 0 Sep 03 tablet 0:00 /sbin/getty ttyd1 co_9600 ## bmo01 3320 3297 0 08:16:38 pts/9 0:00 -ksh ## ## Importance to this list: ## UID PID PPID C STIME TTY TIME COMD ## ** * * * *** ** ## ## UID PID PPID STIME TIME COMD ## ## ps -ef | cut -c1-8,9-14,15-20,24-29,31-32,40-46,47- | sort -r +4 | \ ## head -$HEADRECS >> $1 ## #############################################################################