#!/usr/local/bin/wish
# the next line restarts using wish \
#
# Xmemstat 1.3
# Copyright (c) 1997 Raul Sobon
# This is an Australian made product from the city of Melbourne
#exec wish "$0" "$@"    
#
#
#
proc readsettings { type } {
	global font sfont psid pssize psrss psshrd psname totps totmem

	if {[expr "{$type} == {all}"]} then {
		set psprog [open "|ps am" r]
	} else {
		set psprog [open "|ps m" r]
	}

#	set psprog [open "|ps m" r]
	set num 1
	set totmem 0

    while {[gets $psprog line] > -1} {
	    set proc [lindex $line 0]
		#puts stdout "while... $line"
		if {[expr "{$proc} != {PID}"]} then {
			set psid($num) [lindex $line 0]
			set pssize($num) [lindex $line 6]
			set psrss($num) [lindex $line 8]
			set psshrd($num) [lindex $line 9]
			set psname($num) [lindex $line 12]
			set totmem [expr $totmem + $pssize($num) + $psshrd($num)]
			#puts stdout "setting... $psid($num)"
			incr num
		}
	}
	set totps $num
	close $psprog
}

proc showMemUse {area} {
	global font sfont psid pssize psrss psshrd psname totps totmem

	set x1 10
	set y1 10
	set x2 295
	set y2 170
	set yt 450
	set col(0) #c0a0c0
	set col(1) #c65086
	set col(2) #003080
	set col(3) #8080c0
	set col(4) #800010
	set col(5) #80a090
	set col(6) #409098
	set col(7) #806060


	$area delete all

	for {set i 1} {$i < $totps} {incr i} {
		set mem [expr $psshrd($i) + $pssize($i)]
		set perc [expr $mem*100 / $totmem]
		set ys [expr $perc*$yt/100 ]
		set y2 [expr $y1 + $ys]
		set txty [expr $y1 + 6]
		set colidx [expr $i % 8]
		set color $col($colidx)

		#puts stdout "%=$perc"
		$area create rectangle $x1 $y1 $x2 $y2 -fill $color
		$area create text 15 $txty -text "$psid($i)\t,$mem Kb\t\t: $psname($i)" -anchor w -font $sfont -fill white
		set y1 $y2
	}
}

proc reUpdate {area type} {

	readsettings $type
	showMemUse $area
}





#--------------------- START
#
#
global font sfont psid pssize psrss psshrd psname totps totmem

set font -*-Helvetica-Medium-R-Normal--*-140-*-*-*-*-*-*
set sfont -*-Helvetica-medium-r-normal-*-10-*-*-*-*-*-*-*
#tk_setPalette snow3
wm title . "XMemStat 1.0 (c) Raul Sobon (AU)"
wm iconname . "xmemstat"
set fillsize 90

#readsettings "user"

#---- Start GUI -------
#--- bottom buttons
set area .area

frame .buttons -borderwidth 2
pack .buttons -side bottom -fill x -pady 2m
button .buttons.up		-text "User Procs"			-command "reUpdate $area user"
button .buttons.up2		-text "All Procs"			-command "reUpdate $area all"
button .buttons.cancel	-text "Ok"			-command "exit"
pack .buttons.cancel .buttons.up .buttons.up2 -side left -expand 1

canvas $area -relief sunken -borderwidth 2 -width 300 -height 440
pack $area -side top -fill x

reUpdate $area "user"


