MemoryUsageView.st
changeset 45 950b84ba89e6
parent 19 4cde336c0794
child 49 6fe62433cfa3
--- a/MemoryUsageView.st	Mon Oct 10 04:15:21 1994 +0100
+++ b/MemoryUsageView.st	Mon Oct 10 04:16:24 1994 +0100
@@ -1,64 +1,61 @@
+"
+ COPYRIGHT (c) 1992 by Claus Gittinger
+	      All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+"
+
+'From Smalltalk/X, Version:2.10.3 on 30-sep-1994 at 11:13:14'!
+
 StandardSystemView subclass:#MemoryUsageView
-         instanceVariableNames:'info list sortBlock'
-         classVariableNames:''
-         poolDictionaries:''
-         category:'Interface-Debugger'
+	 instanceVariableNames:'info list sortBlock'
+	 classVariableNames:''
+	 poolDictionaries:''
+	 category:'Interface-Tools'
 !
 
-!MemoryUsageView methodsFor:'realization'!
-
-realize
-    super realize.
-    self updateInfo.
-    self sortByClass.
-! !
-
-!MemoryUsageView methodsFor:'initialization'!
-
-initialize
-    |l helpView headLine|
+MemoryUsageView comment:'
+ COPYRIGHT (c) 1992 by Claus Gittinger
+	      All Rights Reserved
+'!
 
-    super initialize.
-    self label:'Memory usage'.
-
-    headLine := 'class                           # of insts  avg sz     bytes   %mem'.
+!MemoryUsageView class methodsFor:'documentation'!
 
-    l := Label in:self.
-    l origin:(0.0 @ 0.0) corner:(1.0 @ l height).
-    l borderWidth:0.
-    l label:headLine.
-    l adjust:#left.
-
-    self extent:((font widthOf:headLine) + (device horizontalPixelPerMillimeter * 15) rounded) @ self height.
+copyright
+"
+ COPYRIGHT (c) 1992 by Claus Gittinger
+	      All Rights Reserved
 
-    helpView := ScrollableView for:ListView in:self.
-    helpView origin:(0.0 @ l height)
-             extent:[width @ (height - l height - l margin)].
-
-    l origin:(helpView scrollBar width @ 0.0).
-
-    list := helpView scrolledView.
-    list origin:(0.0 @ 0.0) extent:(1.0 @ 1.0).
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+"
+!
 
-    list middleButtonMenu:(PopUpMenu
-                                labels:#(
-                                         'by class'
-                                         'by inst count'
-                                         'by memory usage'
-                                         '-'
-                                         'update'
-                                        )
+version
+"
+$Header: /cvs/stx/stx/libtool/MemoryUsageView.st,v 1.2 1994-10-10 03:15:56 claus Exp $
+"
+!
 
-                             selectors:#(sortByClass
-                                         sortByInstCount
-                                         sortByMemoryUsage
-                                         nil
-                                         update
-                                        )
-                                receiver:self
-                                     for:list).
+documentation
+"
+    this view shows an overview over the memory usage of the system.
+    usage:
+	MemoryUsageView new open
 
-    "MemoryUsageView start"
+    Since scanning all memory takes some time, this is not done
+    automatically, but upon request. See the middlebuttonmenu-'update'
+    function.
+"
 ! !
 
 !MemoryUsageView methodsFor:'menu actions'!
@@ -81,72 +78,62 @@
     self updateDisplay
 !
 
+inspectInstances
+    |line className class|
+
+    line := list selectionValue.
+    (line notNil and:[line notEmpty]) ifTrue:[
+	className := line asCollectionOfWords first.
+	"
+	 special kludge
+	"
+	(className startsWith:'<') ifFalse:[
+	    (className startsWith:'all') ifFalse:[
+		class := Smalltalk at:className asSymbol.
+		class allInstances inspect
+	    ]
+	]
+    ]
+!
+
 update
     self updateInfo.
     self updateDisplay
 ! !
 
+!MemoryUsageView methodsFor:'realization'!
+
+realize
+    super realize.
+    self updateInfo.
+    self sortByClass.
+! !
+
 !MemoryUsageView methodsFor:'private'!
 
-updateInfo
-    self cursor:Cursor wait.
-    list cursor:Cursor wait.
-
-    info := IdentityDictionary new:600.
-
-    "find all objects, collect stuff in info"
-
-    ObjectMemory allObjectsDo:[:o |
-        |i class|
+updateDisplay
+    "update the displayed list"
 
-        o isBehavior ifTrue:[
-            o isMeta ifTrue:[
-                class := Metaclass
-            ] ifFalse:[
-                class := Class
-            ]
-        ] ifFalse:[
-            class := o class.
-        ].
-        (info includesKey:class) ifFalse:[
-            info at:class put:(Array with:class 
-                                     with:1 
-                                     with:(ObjectMemory sizeOf:o))
-        ] ifTrue:[
-            i := info at:class.
-            i at:2 put:((i at:2) + 1).
-            i at:3 put:((i at:3) + (ObjectMemory sizeOf:o))
-        ]
-    ].
-
-    self cursor:Cursor normal.
-    list cursor:Cursor normal.
-! 
-
-updateDisplay
     |classNames counts sumSizes percents avgSizes rawData l line allMemory overAllCount overAllAvgSize|
 
-    self cursor:Cursor wait.
-    list cursor:Cursor wait.
-
     rawData := info asSortedCollection:sortBlock.
 
     "this avoids getting a sorted collection in the collect: below"
     rawData := rawData asArray.
 
     classNames := rawData collect:[:i | 
-        |cls|
+	|cls|
 
-        cls := i at:1.
-        cls == Class ifTrue:[
-            '<all classes>'
-        ] ifFalse:[
-            cls == Metaclass ifTrue:[
-                '<all metaclasses>'
-            ] ifFalse:[
-                cls name
-            ] 
-        ] 
+	cls := i at:1.
+	cls == Class ifTrue:[
+	    '<all classes>'
+	] ifFalse:[
+	    cls == Metaclass ifTrue:[
+		'<all metaclasses>'
+	    ] ifFalse:[
+		cls name
+	    ] 
+	] 
     ].
 
     counts := rawData collect:[:i | (i at:2) ].
@@ -157,12 +144,12 @@
 
     l := OrderedCollection new.
     1 to:classNames size do:[:i |
-        line := (classNames at:i) printStringPaddedTo:30 with:Character space.
-        line := line , ((counts at:i) printStringLeftPaddedTo:10).
-        line := line , ((avgSizes at:i) printStringLeftPaddedTo:10).
-        line := line , ((sumSizes at:i) printStringLeftPaddedTo:10).
-        line := line , ((percents at:i) printStringLeftPaddedTo:7).
-        l add:line
+	line := (classNames at:i) printStringPaddedTo:30 with:Character space.
+	line := line , ((counts at:i) printStringLeftPaddedTo:10).
+	line := line , ((avgSizes at:i) printStringLeftPaddedTo:10).
+	line := line , ((sumSizes at:i) printStringLeftPaddedTo:10).
+	line := line , ((percents at:i) printStringLeftPaddedTo:7).
+	l add:line
     ].
 
     "add summary line"
@@ -178,7 +165,103 @@
     l add:line.
 
     list list:l.
+!
 
-    self cursor:Cursor normal.
-    list cursor:Cursor normal.
+updateInfo
+    "scan all memory and collect the information"
+
+    |myProcess myPriority|
+
+    windowGroup withCursor:Cursor wait do:[
+
+	info := IdentityDictionary new:600.
+
+	"find all objects, collect stuff in info"
+
+	"
+	 this is a time consuming operation; therefore lower my priority ...
+	"
+	myProcess := Processor activeProcess.
+	myPriority := myProcess priority.
+	myProcess priority:(Processor userBackgroundPriority).
+
+	[
+	    ObjectMemory allObjectsDo:[:o |
+		|i class|
+
+		o isBehavior ifTrue:[
+		    o isMeta ifTrue:[
+			class := Metaclass
+		    ] ifFalse:[
+			class := Class
+		    ]
+		] ifFalse:[
+		    class := o class.
+		].
+		(info includesKey:class) ifFalse:[
+		    info at:class put:(Array with:class 
+					     with:1 
+					     with:(ObjectMemory sizeOf:o))
+		] ifTrue:[
+		    i := info at:class.
+		    i at:2 put:((i at:2) + 1).
+		    i at:3 put:((i at:3) + (ObjectMemory sizeOf:o))
+		]
+	    ].
+	] valueNowOrOnUnwindDo:[
+	    myProcess priority:myPriority.
+	].
+    ]
 ! !
+
+!MemoryUsageView methodsFor:'initialization'!
+
+initialize
+    |l helpView headLine|
+
+    super initialize.
+    self label:'Memory usage'.
+
+    headLine := ' class                           # of insts  avg sz     bytes   %mem '.
+
+    l := Label in:self.
+    l origin:(0.0 @ 0.0) corner:(1.0 @ l height).
+    l borderWidth:0.
+    l label:headLine.
+    l adjust:#left.
+
+    self extent:((font widthOf:headLine) + (device horizontalPixelPerMillimeter * 15) rounded) @ self height.
+
+    helpView := ScrollableView for:SelectionInListView in:self.
+    helpView origin:(0.0 @ l height) corner:1.0 @ 1.0.
+
+    l origin:(helpView scrollBar width @ 0.0).
+
+    list := helpView scrolledView.
+    list font:(self font).
+    list middleButtonMenu:(PopUpMenu
+				labels:(
+					resources array:#(
+					    'sort by class'
+					    'sort by inst count'
+					    'sort by memory usage'
+					    '-'
+					    'inspect instances'
+					    '-'
+					    'update'
+					))
+
+			     selectors:#(sortByClass
+					 sortByInstCount
+					 sortByMemoryUsage
+					 nil
+					 inspectInstances
+					 nil
+					 update
+					)
+				receiver:self
+				     for:list).
+
+    "MemoryUsageView start"
+! !
+