View Javadoc

1   /*
2    * Copyright 2008 Eric Caspole
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the License at
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under
10   * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11   * KIND, either express or implied. See the License for the specific language governing
12   * permissions and limitations under the License.
13   */
14  
15  package net.sf.madmap;
16  
17  import java.io.*;
18  import java.util.*;
19  import javax.swing.*;
20  
21  
22  public class RetainedHeapTableModel extends MadmapTableModel {
23  
24    ArrayList   _objects;
25    long        _retSizes[];
26    boolean     _leakGuesses[];
27    long        _totalLiveSize;
28    int         _rows;
29    JTextArea   _retLog;
30    
31    private String[] columnNames = {
32      "Object Type",
33      "Object Address",
34      "Leak Guess?",
35      "Retained Size",
36      "Pct of Retained Size"
37    };
38    
39    private Class[] columnType = {
40      (java.lang.Class)((new String("")).getClass()),
41      (java.lang.Class)((new String("")).getClass()),
42      (java.lang.Class)((new String("")).getClass()),
43      (java.lang.Class)((new Long(0)).getClass()),
44      (java.lang.Class)((new Double(0.0)).getClass())
45    };
46      
47    public RetainedHeapTableModel( Madmap m, ArrayList objs, long liveSize, JTextArea log ) {
48  	super(m);
49      _objects  = objs;
50      _rows     = _objects.size();
51      _retSizes = new long[_rows];    
52      _leakGuesses = new boolean[_rows];
53      
54      // record the ret size here in a list because the object field will 
55      // be reused for other calculations
56      for( int i = 0; i < _rows; i++ ) {      
57        _retSizes[i] = ((HprofHeapCollectable)_objects.get(i)).getRetainedSize();              
58        HprofHeapCollectable curr = (HprofHeapCollectable)_objects.get(i);
59        assert (curr != null) : "curr obj is null in retained obj input array" ;
60        Boolean b = (Boolean) getController().getGuesses().get(curr.addr());
61        if (( b != null ) && ( b == true )) {
62          _leakGuesses[i] = true;
63        } else {
64          _leakGuesses[i] = false;
65        }
66      }
67      
68      _totalLiveSize  = liveSize;
69      _retLog         = log;
70    }
71  
72    public Class getColumnClass(int c) {
73      return columnType[c];
74    }
75    
76    public String getColumnName(int col) {
77        return columnNames[col];
78    }
79  
80    public int getColumnCount() { return columnNames.length; }
81    public int getRowCount() { return _rows; }
82  
83    public JTextArea getLog() { return _retLog; }
84  
85    /**
86    * Returns the vector containing the strings of the allocation stack trace recorded in the hprof file.
87    * <p>
88    * Retrieves the object from the list of objects shown in the table, then retrieve the stack trace 
89    * vector from the object.
90    *
91    * @param  row   the row selected by the user in the retained heap table
92    *
93    */
94    public Vector getStack(int row) {
95      //HprofHeapCollectable hk = (HprofHeapCollectable)_objects.get(row);
96      HprofHeapAllocation hk = (HprofHeapAllocation)_objects.get(row);
97      int stackId = hk.getStackTrace();
98      if ( stackId != 0 ) {
99        HprofStackTraceData std = (HprofStackTraceData) getController().getStackTraces().get(stackId);
100       Vector stk = null;
101       if ( std != null ) { 
102         stk = std.getStackVector();
103       }
104       return stk;
105     } else {
106       return null;
107     }
108   }
109 
110   /**
111   * Retrieves the object from the list of objects shown in the table 
112   *
113   * @param  row   the row selected by the user in the retained heap table
114   *
115   */
116   public String childrenSummary(int row) {
117     HprofHeapCollectable hk = (HprofHeapCollectable)_objects.get(row);
118     return hk.toString();    
119   }
120 
121   
122   public Object getValueAt(int row, int col) { 
123     Iterator<HprofHeapAllocation>   ihoc3 = _objects.iterator();
124     int i = 0;
125 
126     while( ihoc3.hasNext() ) {
127       HprofHeapAllocation hk = ihoc3.next();
128       
129       if ( i == row ) {
130         if ( col == 0 ) {
131           HprofThreadData thd = null;
132           // Print the thread name as a convenience to the user, it is in the thread record
133           if ( hk.className().equals("java.lang.Thread") ) {
134             thd = (HprofThreadData) getController().getThreads().get( (int) hk.addr() );
135             if ( thd != null ) {
136               return  new String( hk.className() + " [ " + thd.name() + " ]" );
137             }
138           }
139           return new String( hk.className() );
140         } else if ( col == 1 ) {
141           return new String( Long.toHexString(hk.addr()) );
142         } else if ( col == 2 ) {
143           if ( _leakGuesses[i] == true ) {
144             return "Yes";
145           } else {
146             return null;
147           }
148         } else if ( col == 3 ) {
149           return _retSizes[i];        
150         } else if ( col == 4 ) {
151           long  rs            = _retSizes[i];        
152           double pctLiveSize  = ((double) rs / (double) _totalLiveSize)  *  (double)100.0;
153           return pctLiveSize;
154         }
155       }
156       i += 1;
157     }
158   
159     assert  true == false : "Should not reach here!" ;
160     return null;
161   }    
162 };