45
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
92
|
13 |
'From Smalltalk/X, Version:2.10.5 on 11-apr-1995 at 2:41:00 am'!
|
45
|
14 |
|
19
|
15 |
StandardSystemView subclass:#MemoryUsageView
|
57
|
16 |
instanceVariableNames:'rawInfo info list sortBlock'
|
45
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
|
19 |
category:'Interface-Tools'
|
19
|
20 |
!
|
|
21 |
|
45
|
22 |
!MemoryUsageView class methodsFor:'documentation'!
|
19
|
23 |
|
45
|
24 |
copyright
|
|
25 |
"
|
|
26 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
|
27 |
All Rights Reserved
|
19
|
28 |
|
45
|
29 |
This software is furnished under a license and may be used
|
|
30 |
only in accordance with the terms of that license and with the
|
|
31 |
inclusion of the above copyright notice. This software may not
|
|
32 |
be provided or otherwise made available to, or used by, any
|
|
33 |
other person. No title to or ownership of the software is
|
|
34 |
hereby transferred.
|
|
35 |
"
|
|
36 |
!
|
19
|
37 |
|
45
|
38 |
version
|
|
39 |
"
|
128
|
40 |
$Header: /cvs/stx/stx/libtool/MemoryUsageView.st,v 1.12 1995-08-30 18:36:07 claus Exp $
|
45
|
41 |
"
|
|
42 |
!
|
19
|
43 |
|
45
|
44 |
documentation
|
|
45 |
"
|
|
46 |
this view shows an overview over the memory usage of the system.
|
|
47 |
usage:
|
|
48 |
MemoryUsageView new open
|
19
|
49 |
|
45
|
50 |
Since scanning all memory takes some time, this is not done
|
|
51 |
automatically, but upon request. See the middlebuttonmenu-'update'
|
|
52 |
function.
|
|
53 |
"
|
19
|
54 |
! !
|
|
55 |
|
|
56 |
!MemoryUsageView methodsFor:'menu actions'!
|
|
57 |
|
|
58 |
sortByInstCount
|
|
59 |
self label:'Memory usage; by instance count'.
|
|
60 |
sortBlock := [:a :b | (a at:2) > (b at:2) ].
|
|
61 |
self updateDisplay
|
|
62 |
!
|
|
63 |
|
|
64 |
sortByMemoryUsage
|
|
65 |
self label:'Memory usage; by memory usage'.
|
|
66 |
sortBlock := [:a :b | (a at:3) > (b at:3)].
|
|
67 |
self updateDisplay
|
92
|
68 |
|
|
69 |
|
|
70 |
!
|
|
71 |
|
|
72 |
sortByClass
|
|
73 |
self label:'Memory usage; by class'.
|
|
74 |
sortBlock := [:a :b | (a at:1) name < (b at:1) name].
|
|
75 |
self updateDisplay
|
19
|
76 |
!
|
|
77 |
|
45
|
78 |
inspectInstances
|
57
|
79 |
|class|
|
45
|
80 |
|
57
|
81 |
list selection notNil ifTrue:[
|
|
82 |
class := (info at:(list selection)) at:1.
|
45
|
83 |
"
|
|
84 |
special kludge
|
|
85 |
"
|
57
|
86 |
class == Class ifTrue:[^self].
|
|
87 |
class == Metaclass ifTrue:[^self].
|
|
88 |
self withCursor:(Cursor questionMark) do:[
|
|
89 |
|insts|
|
52
|
90 |
|
57
|
91 |
insts := class allInstances.
|
|
92 |
insts size > 500 ifTrue:[
|
|
93 |
(self confirm:'there are ' , insts size printString , ' instances.\\Do you really want to see them all ?' withCRs)
|
|
94 |
ifFalse:[^ self]
|
|
95 |
].
|
|
96 |
insts inspect
|
52
|
97 |
]
|
|
98 |
]
|
|
99 |
!
|
|
100 |
|
92
|
101 |
usageMenu
|
|
102 |
^ PopUpMenu
|
98
|
103 |
labels:(resources array:#(
|
|
104 |
'sort by class'
|
|
105 |
'sort by inst count'
|
|
106 |
'sort by memory usage'
|
|
107 |
'sort by average size'
|
|
108 |
'-'
|
|
109 |
'inspect instances'
|
|
110 |
'owners'
|
|
111 |
'-'
|
|
112 |
'update'
|
|
113 |
))
|
92
|
114 |
|
98
|
115 |
selectors:#(sortByClass
|
|
116 |
sortByInstCount
|
|
117 |
sortByMemoryUsage
|
|
118 |
sortByAverageSize
|
|
119 |
nil
|
|
120 |
inspectInstances
|
|
121 |
inspectOwners
|
|
122 |
nil
|
|
123 |
update
|
|
124 |
).
|
92
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
!
|
|
129 |
|
52
|
130 |
inspectOwners
|
69
|
131 |
|class inspector|
|
52
|
132 |
|
57
|
133 |
list selection notNil ifTrue:[
|
|
134 |
class := (info at:(list selection)) at:1.
|
52
|
135 |
"
|
|
136 |
special kludge
|
|
137 |
"
|
57
|
138 |
class == Class ifTrue:[^self].
|
|
139 |
class == Metaclass ifTrue:[^self].
|
|
140 |
self withCursor:(Cursor questionMark) do:[
|
|
141 |
|owners dict|
|
52
|
142 |
|
57
|
143 |
owners := (ObjectMemory whoReferencesInstancesOf:class).
|
|
144 |
owners isNil ifTrue:[
|
|
145 |
self information:'no owners found - next GC should remove it'.
|
|
146 |
^ self
|
|
147 |
].
|
|
148 |
owners := owners asOrderedCollection.
|
|
149 |
owners size > 500 ifTrue:[
|
|
150 |
(self confirm:'there are ' , owners size printString , ' owners.\\Do you really want to see them all ?' withCRs)
|
|
151 |
ifFalse:[^ self]
|
|
152 |
].
|
|
153 |
dict := IdentityDictionary new.
|
|
154 |
owners do:[:owner |
|
|
155 |
|set names oClass s|
|
52
|
156 |
|
57
|
157 |
"
|
|
158 |
skip weakArrays ... (they dont count)
|
|
159 |
"
|
|
160 |
(owner isMemberOf:WeakArray) ifFalse:[
|
|
161 |
set := Set new.
|
69
|
162 |
owner == Smalltalk ifTrue:[
|
|
163 |
owner keysAndValuesDo:[:key :val |
|
|
164 |
(val isMemberOf:class) ifTrue:[
|
|
165 |
set add:key
|
|
166 |
]
|
|
167 |
]
|
|
168 |
] ifFalse:[
|
|
169 |
names := owner class allInstVarNames.
|
|
170 |
oClass := owner class.
|
|
171 |
1 to:oClass instSize do:[:i |
|
|
172 |
((owner instVarAt:i) isMemberOf:class) ifTrue:[
|
|
173 |
set add:(names at:i).
|
|
174 |
].
|
57
|
175 |
].
|
69
|
176 |
oClass isVariable ifTrue:[
|
|
177 |
oClass isPointers ifTrue:[
|
|
178 |
1 to:owner basicSize do:[:i |
|
|
179 |
((owner basicAt:i) isMemberOf:class) ifTrue:[
|
|
180 |
set add:i
|
|
181 |
]
|
52
|
182 |
]
|
57
|
183 |
]
|
69
|
184 |
].
|
52
|
185 |
].
|
57
|
186 |
"
|
|
187 |
put a describing string into the dictionary
|
|
188 |
"
|
|
189 |
s := 'references in: '.
|
|
190 |
set do:[:name |
|
|
191 |
name isString ifTrue:[
|
|
192 |
s := s , name , ' '
|
|
193 |
] ifFalse:[
|
|
194 |
s := s , '[' , name printString , '] '
|
|
195 |
]
|
|
196 |
].
|
|
197 |
dict at:owner put:s.
|
|
198 |
"/ dict at:owner put:set
|
52
|
199 |
]
|
57
|
200 |
].
|
69
|
201 |
inspector := DictionaryInspectorView openOn:dict.
|
|
202 |
inspector listView doubleClickAction:[:lineNr | inspector doInspectKey].
|
45
|
203 |
]
|
|
204 |
]
|
|
205 |
!
|
|
206 |
|
19
|
207 |
update
|
|
208 |
self updateInfo.
|
|
209 |
self updateDisplay
|
92
|
210 |
!
|
45
|
211 |
|
92
|
212 |
sortByAverageSize
|
|
213 |
self label:'Memory usage; by average size'.
|
|
214 |
sortBlock := [:a :b | ((a at:3)/(a at:2)) > ((b at:3)/(b at:2))].
|
|
215 |
self updateDisplay
|
|
216 |
|
|
217 |
|
45
|
218 |
! !
|
|
219 |
|
19
|
220 |
!MemoryUsageView methodsFor:'private'!
|
|
221 |
|
45
|
222 |
updateDisplay
|
|
223 |
"update the displayed list"
|
19
|
224 |
|
49
|
225 |
windowGroup withCursor:Cursor wait do:[
|
108
|
226 |
|classNames counts sumSizes maxSizes percents avgSizes
|
|
227 |
l line allMemory
|
|
228 |
overAllCount overAllAvgSize overAllMaxSize|
|
49
|
229 |
|
98
|
230 |
info := rawInfo asSortedCollection:sortBlock.
|
19
|
231 |
|
98
|
232 |
classNames := info collect:[:i |
|
|
233 |
|cls|
|
19
|
234 |
|
98
|
235 |
cls := i at:1.
|
|
236 |
cls == Class ifTrue:[
|
|
237 |
'<all classes>'
|
|
238 |
] ifFalse:[
|
|
239 |
cls == Metaclass ifTrue:[
|
|
240 |
'<all metaclasses>'
|
|
241 |
] ifFalse:[
|
|
242 |
cls displayString "/name
|
|
243 |
]
|
|
244 |
]
|
|
245 |
].
|
19
|
246 |
|
98
|
247 |
counts := info collect:[:i | (i at:2) ].
|
|
248 |
sumSizes := info collect:[:i | (i at:3) ].
|
108
|
249 |
maxSizes := info collect:[:i | (i at:4) ].
|
98
|
250 |
allMemory := sumSizes inject:0 into:[:sum :this | sum + this].
|
|
251 |
"/ allMemory := ObjectMemory bytesUsed.
|
|
252 |
percents := sumSizes collect:[:sz | (sz asFloat / allMemory * 1000) rounded / 10.0].
|
|
253 |
avgSizes := (1 to:sumSizes size) collect:[:i | (((sumSizes at:i) / (counts at:i)) * 10) rounded / 10.0].
|
52
|
254 |
|
98
|
255 |
l := OrderedCollection new.
|
|
256 |
1 to:classNames size do:[:i |
|
108
|
257 |
|line avgSz maxSz|
|
19
|
258 |
|
108
|
259 |
avgSz := avgSizes at:i.
|
|
260 |
maxSz := maxSizes at:i.
|
|
261 |
avgSz = maxSz ifTrue:[
|
|
262 |
avgSz := avgSz asInteger printString , ' '.
|
|
263 |
].
|
98
|
264 |
line := (classNames at:i) printStringPaddedTo:33 with:Character space.
|
|
265 |
line := line , ((counts at:i) printStringLeftPaddedTo:7).
|
108
|
266 |
line := line , (avgSz printStringLeftPaddedTo:10).
|
|
267 |
line := line , (maxSz printStringLeftPaddedTo:8).
|
98
|
268 |
line := line , ((sumSizes at:i) printStringLeftPaddedTo:10).
|
|
269 |
line := line , ((percents at:i) printStringLeftPaddedTo:7).
|
|
270 |
l add:line
|
|
271 |
].
|
19
|
272 |
|
98
|
273 |
"add summary line"
|
|
274 |
overAllCount := counts inject:0 into:[:sum :this | sum + this].
|
|
275 |
overAllAvgSize := ((allMemory / overAllCount) * 10) rounded / 10.0.
|
108
|
276 |
overAllMaxSize := maxSizes max.
|
19
|
277 |
|
98
|
278 |
l add:''.
|
|
279 |
line := 'all objects' printStringPaddedTo:33 with:Character space.
|
|
280 |
line := line , (overAllCount printStringLeftPaddedTo:7).
|
|
281 |
line := line , (overAllAvgSize printStringLeftPaddedTo:10).
|
108
|
282 |
line := line , (overAllMaxSize printStringLeftPaddedTo:8).
|
98
|
283 |
line := line , (allMemory printStringLeftPaddedTo:10).
|
|
284 |
line := line , (100.0 printStringLeftPaddedTo:7).
|
|
285 |
l add:line.
|
92
|
286 |
|
98
|
287 |
list list:l.
|
49
|
288 |
]
|
45
|
289 |
!
|
19
|
290 |
|
45
|
291 |
updateInfo
|
|
292 |
"scan all memory and collect the information"
|
|
293 |
|
49
|
294 |
windowGroup withCursor:Cursor questionMark do:[
|
52
|
295 |
|myProcess myPriority|
|
45
|
296 |
|
|
297 |
"find all objects, collect stuff in info"
|
|
298 |
|
|
299 |
"
|
|
300 |
this is a time consuming operation; therefore lower my priority ...
|
|
301 |
"
|
|
302 |
myProcess := Processor activeProcess.
|
|
303 |
myPriority := myProcess priority.
|
|
304 |
myProcess priority:(Processor userBackgroundPriority).
|
|
305 |
|
57
|
306 |
rawInfo := IdentityDictionary new:600.
|
52
|
307 |
|
45
|
308 |
[
|
|
309 |
ObjectMemory allObjectsDo:[:o |
|
108
|
310 |
|i class bytes|
|
45
|
311 |
|
|
312 |
o isBehavior ifTrue:[
|
|
313 |
o isMeta ifTrue:[
|
|
314 |
class := Metaclass
|
|
315 |
] ifFalse:[
|
|
316 |
class := Class
|
|
317 |
]
|
|
318 |
] ifFalse:[
|
|
319 |
class := o class.
|
|
320 |
].
|
108
|
321 |
bytes := ObjectMemory sizeOf:o.
|
57
|
322 |
i := rawInfo at:class ifAbsent:[].
|
52
|
323 |
i isNil ifTrue:[
|
108
|
324 |
i := Array
|
|
325 |
with:class
|
|
326 |
with:1
|
|
327 |
with:bytes
|
|
328 |
with:bytes.
|
57
|
329 |
rawInfo at:class put:i.
|
52
|
330 |
] ifFalse:[
|
45
|
331 |
i at:2 put:((i at:2) + 1).
|
108
|
332 |
i at:3 put:((i at:3) + bytes).
|
|
333 |
i at:4 put:((i at:4) max: bytes).
|
45
|
334 |
]
|
|
335 |
].
|
|
336 |
] valueNowOrOnUnwindDo:[
|
|
337 |
myProcess priority:myPriority.
|
|
338 |
].
|
|
339 |
]
|
19
|
340 |
! !
|
45
|
341 |
|
92
|
342 |
!MemoryUsageView methodsFor:'realization'!
|
|
343 |
|
|
344 |
realize
|
|
345 |
super realize.
|
|
346 |
self updateInfo.
|
|
347 |
self sortByClass.
|
|
348 |
! !
|
|
349 |
|
45
|
350 |
!MemoryUsageView methodsFor:'initialization'!
|
|
351 |
|
|
352 |
initialize
|
|
353 |
|l helpView headLine|
|
|
354 |
|
|
355 |
super initialize.
|
|
356 |
self label:'Memory usage'.
|
|
357 |
|
108
|
358 |
headLine := ' class # of insts avg sz max sz bytes %mem '.
|
45
|
359 |
|
|
360 |
l := Label in:self.
|
|
361 |
l origin:(0.0 @ 0.0) corner:(1.0 @ l height).
|
|
362 |
l borderWidth:0.
|
|
363 |
l label:headLine.
|
|
364 |
l adjust:#left.
|
|
365 |
|
|
366 |
self extent:((font widthOf:headLine) + (device horizontalPixelPerMillimeter * 15) rounded) @ self height.
|
|
367 |
|
|
368 |
helpView := ScrollableView for:SelectionInListView in:self.
|
|
369 |
helpView origin:(0.0 @ l height) corner:1.0 @ 1.0.
|
|
370 |
|
52
|
371 |
list := helpView scrolledView.
|
|
372 |
|
|
373 |
l origin:(list originRelativeTo:self) x @ 0.0.
|
45
|
374 |
|
|
375 |
list font:(self font).
|
52
|
376 |
l font:(self font).
|
98
|
377 |
list menuHolder:self; menuPerformer:self; menuMessage:#usageMenu.
|
45
|
378 |
|
52
|
379 |
"
|
|
380 |
MemoryUsageView open
|
|
381 |
"
|
45
|
382 |
! !
|