MemoryMonitor.st
changeset 52 7b48409ae088
parent 49 6fe62433cfa3
child 56 d0cb937cbcaa
equal deleted inserted replaced
51:57c1ccc3d7e0 52:7b48409ae088
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
    12 
    12 
    13 StandardSystemView subclass:#MemoryMonitor
    13 StandardSystemView subclass:#MemoryMonitor
    14 	 instanceVariableNames:'updateInterval updateBlock myProcess oldData newData
    14 	 instanceVariableNames:'updateInterval updateBlock myProcess oldData newData
    15 		freeData updateIndex org maxTotal minTotal newColor dX
    15 		freeData updateIndex org maxTotal minTotal dX
    16 		freeColor oldColor prevTotal prevFree prevFree2 prevOld'
    16 		newColor freeColor oldColor 
       
    17 		prevTotal prevFree prevFree2 prevOld scale'
    17 	 classVariableNames:''
    18 	 classVariableNames:''
    18 	 poolDictionaries:''
    19 	 poolDictionaries:''
    19 	 category:'Interface-Tools'
    20 	 category:'Interface-Tools'
    20 !
    21 !
    21 
    22 
    22 MemoryMonitor comment:'
    23 MemoryMonitor comment:'
    23  COPYRIGHT (c) 1991 by Claus Gittinger
    24  COPYRIGHT (c) 1991 by Claus Gittinger
    24 	      All Rights Reserved
    25 	      All Rights Reserved
    25 
    26 
    26 $Header: /cvs/stx/stx/libtool/MemoryMonitor.st,v 1.3 1994-10-28 03:30:33 claus Exp $
    27 $Header: /cvs/stx/stx/libtool/MemoryMonitor.st,v 1.4 1994-11-17 14:46:56 claus Exp $
    27 '!
    28 '!
    28 
    29 
    29 !MemoryMonitor class methodsFor:'documentation'!
    30 !MemoryMonitor class methodsFor:'documentation'!
    30 
    31 
    31 copyright
    32 copyright
    42 "
    43 "
    43 !
    44 !
    44 
    45 
    45 version
    46 version
    46 "
    47 "
    47 $Header: /cvs/stx/stx/libtool/MemoryMonitor.st,v 1.3 1994-10-28 03:30:33 claus Exp $
    48 $Header: /cvs/stx/stx/libtool/MemoryMonitor.st,v 1.4 1994-11-17 14:46:56 claus Exp $
    48 "
    49 "
    49 !
    50 !
    50 
    51 
    51 documentation
    52 documentation
    52 "
    53 "
    59 
    60 
    60 	all     current oldSpace in use + newSpace in use
    61 	all     current oldSpace in use + newSpace in use
    61 
    62 
    62 	new     current newSpace in use
    63 	new     current newSpace in use
    63 
    64 
    64 	free    the first number shows free space in (fragmented) free lists
    65 	frl     free space in (fragmented) free lists
    65 		the second shows the compact free area above the used oldSpace
    66 	fre     compact free area above the used oldSpace
    66 
    67 
    67 	old     current oldSpace in use
    68 	old     current oldSpace in use
    68 
    69 
    69 	max,    extreme values of 'all' since the monitor started
    70 	max,    extreme values of 'tot' since the monitor started
    70 	min
    71 	min     (can be reset by typing 'r' in the view)
    71 
    72 
    72 	t       tenure threshold
    73 	t       tenure threshold
    73 
    74 
    74 	I       incremental GC state
    75 	I       incremental GC state
       
    76 
       
    77 	%       percentage of live objects in newSpace after last scavenge
       
    78 		(i.e. this is the garbage vs. living objects ratio of
       
    79 		 newSpace objects after the last scavenge)
    75 
    80 
    76     the graphic shows:
    81     the graphic shows:
    77 	yellow  newSpace used
    82 	yellow  newSpace used
    78 	green   free memory in freeLists
    83 	green   free memory in freeLists
    79 	white   oldSpace used
    84 	white   oldSpace used
       
    85 
       
    86 
       
    87     the popupMenu offers GC functions; keyboard options are:
       
    88 	'f' -> faster; 's' -> slower; 'r' -> reset min/max
    80 "
    89 "
    81 ! !
    90 ! !
    82 
    91 
    83 !MemoryMonitor class methodsFor:'defaults'!
    92 !MemoryMonitor class methodsFor:'defaults'!
    84 
    93 
    85 defaultExtent
    94 defaultExtent
    86     ^ (200 @ 200)
    95     ^ (200 @ 200)
    87 ! !
    96 !
    88 
    97 
    89 !MemoryMonitor class methodsFor:'startup'!
    98 defaultLabel
    90 
    99     ^ 'Memory Monitor'
    91 open 
   100 !
    92     |m|
   101 
    93 
   102 defaultIcon
    94     m := self new.
   103     |i|
    95 
   104 
    96     m label:'Memory Monitor'.
   105     i := Image fromFile:'bitmaps/monitor.icon'.
    97     m icon:(Form fromFile:'Monitor.icon' resolution:100).
   106     i notNil ifTrue:[^ i].
    98     m minExtent:(100 @ 100).
   107     ^ super defaultIcon
    99 
       
   100     m open.
       
   101     ^ m
       
   102 
       
   103     "
       
   104      MemoryMonitor open
       
   105     "
       
   106 ! !
   108 ! !
   107 
   109 
   108 !MemoryMonitor methodsFor:'drawing'!
   110 !MemoryMonitor methodsFor:'drawing'!
   109 
   111 
   110 redraw
   112 redraw
   115 !
   117 !
   116 
   118 
   117 redrawX:x y:y width:w height:h
   119 redrawX:x y:y width:w height:h
   118     "redraw data"
   120     "redraw data"
   119 
   121 
   120     |total oldSpaceUsed newSpaceUsed freeMem lx scale s startIdx endIdx|
   122     |total oldSpaceUsed newSpaceUsed freeMem lx s startIdx endIdx
       
   123      right|
   121 
   124 
   122     shown ifFalse:[^ self].
   125     shown ifFalse:[^ self].
   123 
   126 
   124     (x + w - 1) > org ifTrue:[
   127     right := x + w - 1.
       
   128     right >= org ifTrue:[
   125 	lx := x.
   129 	lx := x.
   126 	lx < org ifTrue:[
   130 	lx < org ifTrue:[
   127 	    lx := org
   131 	    lx := org
   128 	].
   132 	].
   129 
   133 
   130 	scale := height asFloat / (maxTotal + 100000).
       
   131 
       
   132 	total := ObjectMemory oldSpaceSize + ObjectMemory newSpaceSize.
   134 	total := ObjectMemory oldSpaceSize + ObjectMemory newSpaceSize.
   133 
   135 
   134 	startIdx := (x-org+1).
   136 	startIdx := (lx-org+1).
   135 	startIdx < 1 ifTrue:[
   137 	startIdx < 1 ifTrue:[
   136 	    startIdx := 1
   138 	    startIdx := 1
   137 	].
   139 	].
   138 
   140 
   139 	endIdx := (x + w - 1)-org+1.
   141 	endIdx := right-org+1.
   140 	endIdx >= updateIndex ifTrue:[
   142 	endIdx >= updateIndex ifTrue:[
   141 	    endIdx := updateIndex-1.
   143 	    endIdx := updateIndex-1.
   142 	].
   144 	].
   143 	dX := 0.
   145 	dX := 0.
   144 
   146 
   150 
   152 
   151 		self updateLineX:lx - dX
   153 		self updateLineX:lx - dX
   152 		       total:total 
   154 		       total:total 
   153 		       old:oldSpaceUsed 
   155 		       old:oldSpaceUsed 
   154 		       new:newSpaceUsed 
   156 		       new:newSpaceUsed 
   155 		       free:freeMem 
   157 		       free:freeMem.
   156 		       scale:scale.
       
   157 	    ].
   158 	    ].
   158 	    lx := lx + 1
   159 	    lx := lx + 1
   159 	]
   160 	]
   160     ].
   161     ].
   161 
   162 
   162     x < org ifTrue:[
   163     x < org ifTrue:[
   163 	prevFree := prevFree2 := prevOld := prevTotal := nil.
   164 	prevFree := prevFree2 := prevOld := prevTotal := nil.
   164 
   165 
   165 	self updateNumbers.
   166 	self updateNumbers.
   166 
       
   167 	self paint:White on:Black.
       
   168 	s := 'max ' , ((maxTotal // 1024) printStringRightAdjustLen:5) , 'k '.
       
   169 	self displayOpaqueString:s x:0 y:font ascent.
       
   170 
       
   171 	s := 'min ' , ((minTotal // 1024) printStringRightAdjustLen:5) , 'k '.
       
   172 	self displayOpaqueString:s x:0 y:(height - font descent).
       
   173 
       
   174     ]
   167     ]
   175 !
   168 !
   176 
   169 
   177 updateLineX:x total:total old:oldSpaceUsed new:newSpaceUsed free:freeMem scale:scale
   170 updateLineX:x total:total old:oldSpaceUsed new:newSpaceUsed free:freeMem
   178     |hNew hOld hFree y1 y2|
   171     |hNew hOld hFree y1 y2|
   179 
   172 
   180     hNew := (newSpaceUsed * scale) asInteger.
   173     hNew := (newSpaceUsed * scale) asInteger.
   181     hOld := (oldSpaceUsed * scale) asInteger // 2.
   174     hOld := (oldSpaceUsed * scale) // 2.
   182     hFree := (freeMem * scale) asInteger.
   175     hFree := (freeMem * scale) asInteger.
   183 
       
   184 "/ self paint:Black.
       
   185 "/ self displayLineFromX:x y:0 toX:x y:height-1.
       
   186 
       
   187 
   176 
   188     y1 := height - 1.
   177     y1 := height - 1.
   189     y2 := y1 - hOld.
   178     y2 := y1 - hOld.
   190     self paint:oldColor.
   179     self paint:oldColor.
   191     self displayLineFromX:x y:y1 toX:x y:y2.
   180     self displayLineFromX:x y:y1 toX:x y:y2.
   212 	free:   current size of freelist in oldSpace
   201 	free:   current size of freelist in oldSpace
   213 	old:    current oldSpace in use
   202 	old:    current oldSpace in use
   214     "
   203     "
   215 
   204 
   216     |oldSpaceSize newSpaceSize memUsed oldMemUsed newMemUsed freeMem free2 
   205     |oldSpaceSize newSpaceSize memUsed oldMemUsed newMemUsed freeMem free2 
   217      x y 
   206      x y half s thisStringLen fontHeight total n|
   218      half scaleChange s thisStringLen scale fontHeight total|
       
   219 
   207 
   220     oldMemUsed := ObjectMemory oldSpaceUsed.
   208     oldMemUsed := ObjectMemory oldSpaceUsed.
   221     newMemUsed := ObjectMemory newSpaceUsed.
   209     newMemUsed := ObjectMemory newSpaceUsed.
   222     freeMem := ObjectMemory freeListSpace.
   210     freeMem := ObjectMemory freeListSpace.
   223     oldSpaceSize := ObjectMemory oldSpaceSize.
   211     oldSpaceSize := ObjectMemory oldSpaceSize.
   225 
   213 
   226     memUsed := oldMemUsed + newMemUsed "- freeMem".
   214     memUsed := oldMemUsed + newMemUsed "- freeMem".
   227     total := oldSpaceSize + newSpaceSize.
   215     total := oldSpaceSize + newSpaceSize.
   228     free2 := oldSpaceSize - oldMemUsed.
   216     free2 := oldSpaceSize - oldMemUsed.
   229 
   217 
   230     ((total - freeMem) < minTotal) ifTrue:[
       
   231 	minTotal := total - freeMem.
       
   232     ].
       
   233     (total > maxTotal) ifTrue:[
       
   234 	maxTotal := total.
       
   235     ].
       
   236 
       
   237     self paint:White on:Black.
   218     self paint:White on:Black.
   238 
   219 
   239     fontHeight := font height + font descent.
   220     fontHeight := font height + font descent.
   240     half := height // 2 + font descent.
   221     half := height // 2 + font descent.
   241 
   222 
   242     y := half - (fontHeight * 3).
   223     y := half - (fontHeight * 3).
   243 
   224 
   244     total ~~ prevTotal ifTrue:[
   225     total ~~ prevTotal ifTrue:[
       
   226 	((total - freeMem) < minTotal) ifTrue:[
       
   227 	    minTotal := total - freeMem.
       
   228 	].
       
   229 	(total > maxTotal) ifTrue:[
       
   230 	    maxTotal := total.
       
   231 	].
       
   232 
       
   233 	s := 'max ' , ((maxTotal // 1024) printStringRightAdjustLen:5) , 'k '.
       
   234 	self displayOpaqueString:s x:0 y:font ascent.
       
   235 
       
   236 	s := 'min ' , ((minTotal // 1024) printStringRightAdjustLen:5) , 'k '.
       
   237 	self displayOpaqueString:s x:0 y:(height - font descent).
       
   238 
   245 	s := 'tot ' , ((total  // 1024) printStringRightAdjustLen:5) , 'k '.
   239 	s := 'tot ' , ((total  // 1024) printStringRightAdjustLen:5) , 'k '.
   246 	self displayOpaqueString:s x:0 y:y.
   240 	self displayOpaqueString:s x:0 y:y.
       
   241 
   247 	prevTotal := total.
   242 	prevTotal := total.
   248     ].
   243     ].
   249 
   244 
   250     y := y + fontHeight.
   245     y := y + fontHeight.
   251     s := 'all ' , ((memUsed // 1024) printStringRightAdjustLen:5) , 'k '.
   246     s := 'all ' , ((memUsed // 1024) printStringRightAdjustLen:5) , 'k '.
   257     self displayOpaqueString:s x:0 y:y.
   252     self displayOpaqueString:s x:0 y:y.
   258 
   253 
   259     y := y + fontHeight.
   254     y := y + fontHeight.
   260     freeMem ~~ prevFree ifTrue:[
   255     freeMem ~~ prevFree ifTrue:[
   261 	self paint:freeColor.
   256 	self paint:freeColor.
   262 	s := 'free' , ((freeMem // 1024) printStringRightAdjustLen:5) , 'k '.
   257 	s := 'frl ' , ((freeMem // 1024) printStringRightAdjustLen:5) , 'k '.
   263 	self displayOpaqueString:s x:0 y:y.
   258 	self displayOpaqueString:s x:0 y:y.
   264 	prevFree := freeMem.
   259 	prevFree := freeMem.
   265     ].
   260     ].
   266 
   261 
   267     y := y + fontHeight.
   262     y := y + fontHeight.
   268     free2 ~~ prevFree2 ifTrue:[
   263     free2 ~~ prevFree2 ifTrue:[
   269 	self paint:freeColor.
   264 	self paint:freeColor.
   270 	s := '    ' , ((free2 // 1024) printStringRightAdjustLen:5) , 'k '.
   265 	s := 'fre ' , ((free2 // 1024) printStringRightAdjustLen:5) , 'k '.
   271 	self displayOpaqueString:s x:0 y:y.
   266 	self displayOpaqueString:s x:0 y:y.
   272 	prevFree2 := free2.
   267 	prevFree2 := free2.
   273     ].
   268     ].
   274 
   269 
   275     y := y + fontHeight.
   270     y := y + fontHeight.
   293 "/    y := y + fontHeight.
   288 "/    y := y + fontHeight.
   294 "/    ObjectMemory runsSingleOldSpace ifTrue:[
   289 "/    ObjectMemory runsSingleOldSpace ifTrue:[
   295 "/        self displayOpaqueString:'single' x:0 y:(half + (fontHeight*4)).
   290 "/        self displayOpaqueString:'single' x:0 y:(half + (fontHeight*4)).
   296 "/    ].
   291 "/    ].
   297 
   292 
       
   293     y := y + fontHeight.
       
   294     n := ObjectMemory lastScavangeReclamation / ObjectMemory newSpaceSize * 100.0.
       
   295     n := 100 - n asInteger.
       
   296     s := (n printStringRightAdjustLen:2) , '%'.
       
   297     self displayOpaqueString:s x:0 y:y.
   298 !
   298 !
   299 
   299 
   300 updateDisplay
   300 updateDisplay
   301     "update picture; trigger next update"
   301     "update picture; trigger next update"
   302 
   302 
   303     |total oldSpaceUsed newSpaceUsed freeMem 
   303     |total oldSpaceUsed newSpaceUsed freeMem 
   304      gWidth shift scaleChange scale margin|
   304      gWidth shift scaleChange margin mustWait|
   305 
   305 
   306     shown ifTrue:[
   306     shown ifTrue:[
   307 	oldSpaceUsed := ObjectMemory oldSpaceUsed.
   307 	oldSpaceUsed := ObjectMemory oldSpaceUsed.
   308 	newSpaceUsed := ObjectMemory newSpaceUsed.
   308 	newSpaceUsed := ObjectMemory newSpaceUsed.
   309 	freeMem := ObjectMemory freeListSpace.
   309 	freeMem := ObjectMemory freeListSpace.
   310 	total := oldSpaceUsed + newSpaceUsed.
   310 	total := oldSpaceUsed + newSpaceUsed.
   311 
   311 
   312 	scaleChange := false.
   312 	scaleChange := false.
   313         
   313 
   314 	((total - freeMem) < minTotal) ifTrue:[
   314 	((total - freeMem) < minTotal) ifTrue:[
   315 	    minTotal := total - freeMem.
   315 	    minTotal := total - freeMem.
   316 	    scaleChange := true
   316 	    scaleChange := true
   317 	].
   317 	].
   318 	(total > maxTotal) ifTrue:[
   318 	(total > maxTotal) ifTrue:[
   324 	newData at:updateIndex put:newSpaceUsed.
   324 	newData at:updateIndex put:newSpaceUsed.
   325 	freeData at:updateIndex put:freeMem.
   325 	freeData at:updateIndex put:freeMem.
   326 	updateIndex := updateIndex + 1.
   326 	updateIndex := updateIndex + 1.
   327 
   327 
   328 	scaleChange ifTrue:[
   328 	scaleChange ifTrue:[
       
   329 	    scale := height asFloat / (maxTotal + 100000).
   329 	    self redraw
   330 	    self redraw
   330 	].
   331 	].
   331 
   332 
   332 	gWidth := width - org.
   333 	gWidth := width - org.
   333 	margin := 1.
   334 	margin := 1.
   334 
   335 
       
   336 	mustWait := false.
   335 	((updateIndex-1) >= (gWidth - margin)) ifTrue:[
   337 	((updateIndex-1) >= (gWidth - margin)) ifTrue:[
   336 "on slow displays, use:"
   338 "on slow displays, use:"
   337 "/            shift := gWidth // 4.
   339 "/            shift := gWidth // 4.
   338 
   340 
   339 "for smooth display, use:"
   341 "for smooth display, use:"
   352 		      toX:org y:0
   354 		      toX:org y:0
   353 		    width:(gWidth - shift - margin)
   355 		    width:(gWidth - shift - margin)
   354 		   height:height.
   356 		   height:height.
   355 	    self clearRectangleX:(width - margin - shift) y:0 
   357 	    self clearRectangleX:(width - margin - shift) y:0 
   356 			   width:shift height:height.
   358 			   width:shift height:height.
   357 	    self waitForExpose.
   359 	    mustWait := true.
   358 	].
   360 	].
   359 
   361 
   360 	scale := height asFloat / (maxTotal + 100000).
   362 	self updateLineX:(updateIndex - 1 + org - 1)
   361 	self updateLineX:(updateIndex - 1 + org)
       
   362 		   total:total 
   363 		   total:total 
   363 		   old:oldSpaceUsed 
   364 		   old:oldSpaceUsed 
   364 		   new:newSpaceUsed 
   365 		   new:newSpaceUsed 
   365 		   free:freeMem 
   366 		   free:freeMem.
   366 		   scale:scale.
       
   367 
   367 
   368 	self updateNumbers.
   368 	self updateNumbers.
       
   369 	mustWait ifTrue:[
       
   370 	    self waitForExpose.
       
   371 	]
   369 
   372 
   370     ].
   373     ].
   371 
   374 
   372     updateBlock notNil ifTrue:[
   375     updateBlock notNil ifTrue:[
   373 	Processor addTimedBlock:updateBlock afterSeconds:updateInterval
   376 	Processor addTimedBlock:updateBlock afterSeconds:updateInterval
   389 
   392 
   390 !MemoryMonitor methodsFor:'events'!
   393 !MemoryMonitor methodsFor:'events'!
   391 
   394 
   392 keyPress:key x:x y:y
   395 keyPress:key x:x y:y
   393     key == $f ifTrue:[
   396     key == $f ifTrue:[
       
   397 	"faster"
   394 	updateInterval := updateInterval / 2
   398 	updateInterval := updateInterval / 2
   395     ].
   399     ].
   396     key == $s ifTrue:[
   400     key == $s ifTrue:[
       
   401 	"slower"
   397 	updateInterval := updateInterval * 2
   402 	updateInterval := updateInterval * 2
       
   403     ].
       
   404     key == $r ifTrue:[
       
   405 	"reset max"
       
   406 	maxTotal := prevTotal.
       
   407 	scale := height asFloat / (maxTotal + 100000).
       
   408 	self redraw.
   398     ]
   409     ]
   399 !
   410 !
   400 
   411 
   401 sizeChanged:how
   412 sizeChanged:how
   402     |nn no nf delta oldSize newSize|
   413     |nn no nf delta oldSize newSize|
   427     ].
   438     ].
   428     newData := nn.
   439     newData := nn.
   429     oldData := no.
   440     oldData := no.
   430     freeData := nf.
   441     freeData := nf.
   431 
   442 
       
   443     scale := height asFloat / (maxTotal + 100000).
       
   444     self clear.
   432     self redraw
   445     self redraw
   433 ! !
   446 ! !
   434 
   447 
   435 !MemoryMonitor methodsFor:'initialization'!
   448 !MemoryMonitor methodsFor:'initialization'!
   436 
   449 
   467     oldData := Array new:1000.
   480     oldData := Array new:1000.
   468     newData := Array new:1000.
   481     newData := Array new:1000.
   469     freeData := Array new:1000.
   482     freeData := Array new:1000.
   470 
   483 
   471     updateIndex := 1.
   484     updateIndex := 1.
   472     org := font widthOf:'used:9999k '.
   485     org := font widthOf:'max 99999k'.
       
   486     level := 0.
   473 
   487 
   474     maxTotal := minTotal := ObjectMemory oldSpaceSize + ObjectMemory newSpaceSize.
   488     maxTotal := minTotal := ObjectMemory oldSpaceSize + ObjectMemory newSpaceSize.
   475 
   489 
   476     viewBackground := Black.
   490     viewBackground := Black.
   477 
   491 
   488     self font:(Font family:'courier' face:'medium' style:'roman' size:10).
   502     self font:(Font family:'courier' face:'medium' style:'roman' size:10).
   489 
   503 
   490     "
   504     "
   491      MemoryMonitor open
   505      MemoryMonitor open
   492     "
   506     "
   493 ! !
   507 !
   494 
   508 
       
   509 initializeMiddleButtonMenu
       
   510     self middleButtonMenu:
       
   511 	     (PopUpMenu labels:(resources array:#(
       
   512 						  'collect Garbage'
       
   513 						  'collect Garbage & compress'
       
   514 						  '-'
       
   515 						  'background collect'
       
   516 						 ))
       
   517 			selectors:#(
       
   518 			    garbageCollect
       
   519 			    compressingGarbageCollect
       
   520 			    nil
       
   521 			    backgroundCollect
       
   522 			   )
       
   523 		receiver:self
       
   524 		     for:self)
       
   525 ! !
       
   526 
       
   527 !MemoryMonitor methodsFor:'menu functions'!
       
   528 
       
   529 garbageCollect
       
   530     ObjectMemory markAndSweep
       
   531 !
       
   532 
       
   533 compressingGarbageCollect
       
   534     ObjectMemory verboseGarbageCollect
       
   535 !
       
   536 
       
   537 backgroundCollect
       
   538     [ObjectMemory incrementalGC] forkAt:4
       
   539 ! !
       
   540