001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.results.memory;
042:
043: import java.io.DataInputStream;
044: import java.io.DataOutputStream;
045: import java.io.IOException;
046: import java.util.ArrayList;
047: import java.util.HashMap;
048: import java.util.Iterator;
049: import java.util.Map;
050:
051: /**
052: * Class representing a difference between two snapshots,
053: * diffing only values available in LivenessMemoryResultsSnapshot
054: *
055: * @author Tomas Hurka
056: * @author Jiri Sedlacek
057: */
058: public class LivenessMemoryResultsDiff extends
059: LivenessMemoryResultsSnapshot {
060: //~ Instance fields ----------------------------------------------------------------------------------------------------------
061:
062: private float[] avgObjectAge;
063: private String[] classNames;
064: private int[] maxSurvGen;
065: private int[] nTotalAllocObjects;
066:
067: // private int[] objectsCounts;
068: // private long[] objectsSizePerClass;
069: private long[] nTrackedAllocObjects;
070: private int[] nTrackedLiveObjects;
071: private long[] trackedLiveObjectsSize;
072: private int nClasses;
073: private long maxTrackedLiveObjectsSizeDiff;
074: private long minTrackedLiveObjectsSizeDiff;
075:
076: //~ Constructors -------------------------------------------------------------------------------------------------------------
077:
078: public LivenessMemoryResultsDiff(
079: LivenessMemoryResultsSnapshot snapshot1,
080: LivenessMemoryResultsSnapshot snapshot2) {
081: computeDiff(snapshot1, snapshot2);
082: }
083:
084: //~ Methods ------------------------------------------------------------------------------------------------------------------
085:
086: public float[] getAvgObjectAge() {
087: return avgObjectAge;
088: }
089:
090: public long getBeginTime() {
091: return -1;
092: }
093:
094: public String getClassName(int classId) {
095: return null;
096: }
097:
098: public String[] getClassNames() {
099: return classNames;
100: }
101:
102: public JMethodIdTable getJMethodIdTable() {
103: return null;
104: }
105:
106: public int[] getMaxSurvGen() {
107: return maxSurvGen;
108: }
109:
110: public long getMaxTrackedLiveObjectsSizeDiff() {
111: return maxTrackedLiveObjectsSizeDiff;
112: }
113:
114: public long getMaxValue() {
115: return -1;
116: }
117:
118: public long getMinTrackedLiveObjectsSizeDiff() {
119: return minTrackedLiveObjectsSizeDiff;
120: }
121:
122: // seems to be unused in LivenessMemoryResultsSnapshot
123: public int getNAlloc() {
124: return getNProfiledClasses();
125: }
126:
127: public int getNInstrClasses() {
128: return getNProfiledClasses();
129: }
130:
131: public int getNProfiledClasses() {
132: return nClasses;
133: }
134:
135: public long getNTotalTracked() {
136: return -1;
137: }
138:
139: public long getNTotalTrackedBytes() {
140: return -1;
141: }
142:
143: //---
144: public long[] getNTrackedAllocObjects() {
145: return nTrackedAllocObjects;
146: }
147:
148: public int getNTrackedItems() {
149: return getNProfiledClasses();
150: }
151:
152: public int[] getNTrackedLiveObjects() {
153: return nTrackedLiveObjects;
154: }
155:
156: //---
157: public long[] getObjectsSizePerClass() {
158: return null;
159: }
160:
161: public long getTimeTaken() {
162: return -1;
163: }
164:
165: public long[] getTrackedLiveObjectsSize() {
166: return trackedLiveObjectsSize;
167: }
168:
169: public boolean containsStacks() {
170: return false;
171: }
172:
173: public PresoObjAllocCCTNode createPresentationCCT(int classId,
174: boolean dontShowZeroLiveObjAllocPaths) {
175: return null;
176: }
177:
178: public int[] getnTotalAllocObjects() {
179: return nTotalAllocObjects;
180: }
181:
182: public void readFromStream(DataInputStream in) throws IOException {
183: throw new UnsupportedOperationException(
184: "Persistence not supported for snapshot comparison"); // NOI18N
185: }
186:
187: //---- Serialization support
188: public void writeToStream(DataOutputStream out) throws IOException {
189: throw new UnsupportedOperationException(
190: "Persistence not supported for snapshot comparison"); // NOI18N
191: }
192:
193: protected PresoObjAllocCCTNode createPresentationCCT(
194: RuntimeMemoryCCTNode rootNode, int classId,
195: boolean dontShowZeroLiveObjAllocPaths) {
196: return null;
197: }
198:
199: private void computeDiff(LivenessMemoryResultsSnapshot snapshot1,
200: LivenessMemoryResultsSnapshot snapshot2) {
201: // number of classes in snapshots
202: int s1nClasses = snapshot1.getNTrackedItems();
203: int s2nClasses = snapshot2.getNTrackedItems();
204:
205: // temporary cache for creating diff
206: HashMap classNamesIdxMap = new HashMap(s1nClasses);
207: ArrayList nTrackedAllocObjectsArr = new ArrayList(s1nClasses);
208: ArrayList nTrackedLiveObjectsArr = new ArrayList(s1nClasses);
209: ArrayList maxSurvGenArr = new ArrayList(s1nClasses);
210: ArrayList avgObjectAgeArr = new ArrayList(s1nClasses);
211: ArrayList trackedLiveObjectsSizeArr = new ArrayList(s1nClasses);
212: ArrayList nTotalAllocObjectsArr = new ArrayList(s1nClasses);
213:
214: // fill the cache with negative values from snapshot1
215: String[] s1ClassNames = snapshot1.getClassNames();
216: long[] s1NTrackedAllocObjects = snapshot1
217: .getNTrackedAllocObjects();
218: int[] s1NTrackedLiveObjects = snapshot1
219: .getNTrackedLiveObjects();
220: int[] s1MaxSurvGen = snapshot1.getMaxSurvGen();
221: float[] s1AvgObjectAge = snapshot1.getAvgObjectAge();
222: long[] s1TrackedLiveObjectsSize = snapshot1
223: .getTrackedLiveObjectsSize();
224: int[] s1NTotalAllocObjects = snapshot1.getnTotalAllocObjects();
225:
226: int idx = 0;
227:
228: for (int i = 0; i < s1nClasses; i++) {
229: if (s1NTotalAllocObjects[i] > 0) {
230: Integer classIdx = (Integer) classNamesIdxMap
231: .get(s1ClassNames[i]);
232:
233: if (classIdx != null) { // duplicate classname
234:
235: int classIndex = classIdx.intValue();
236: long nTrackedAllocObjects = ((Long) nTrackedAllocObjectsArr
237: .get(classIndex)).longValue();
238: int nTrackedLiveObjects = ((Integer) nTrackedLiveObjectsArr
239: .get(classIndex)).intValue();
240: int maxSurvGen = ((Integer) maxSurvGenArr
241: .get(classIndex)).intValue();
242: float avgObjectAge = ((Float) avgObjectAgeArr
243: .get(classIndex)).floatValue();
244: long trackedLiveObjectsSize = ((Long) trackedLiveObjectsSizeArr
245: .get(classIndex)).longValue();
246: int nTotalAllocObjects = ((Integer) nTotalAllocObjectsArr
247: .get(classIndex)).intValue();
248:
249: nTrackedAllocObjectsArr.set(classIndex, new Long(
250: nTrackedAllocObjects
251: - s1NTrackedAllocObjects[i]));
252: nTrackedLiveObjectsArr.set(classIndex, new Integer(
253: nTrackedLiveObjects
254: - s1NTrackedLiveObjects[i]));
255: maxSurvGenArr.set(classIndex, new Integer(
256: maxSurvGen - s1MaxSurvGen[i]));
257: avgObjectAgeArr.set(classIndex, new Float(
258: avgObjectAge - s1AvgObjectAge[i]));
259: trackedLiveObjectsSizeArr.set(classIndex, new Long(
260: trackedLiveObjectsSize
261: - s1TrackedLiveObjectsSize[i]));
262: nTotalAllocObjectsArr.set(classIndex, new Integer(
263: nTotalAllocObjects
264: - s1NTotalAllocObjects[i]));
265: } else {
266: classNamesIdxMap.put(s1ClassNames[i], new Integer(
267: idx));
268: nTrackedAllocObjectsArr.add(new Long(
269: 0 - s1NTrackedAllocObjects[i]));
270: nTrackedLiveObjectsArr.add(new Integer(
271: 0 - s1NTrackedLiveObjects[i]));
272: maxSurvGenArr.add(new Integer(0 - s1MaxSurvGen[i]));
273: avgObjectAgeArr
274: .add(new Float(0 - s1AvgObjectAge[i]));
275: trackedLiveObjectsSizeArr.add(new Long(
276: 0 - s1TrackedLiveObjectsSize[i]));
277: nTotalAllocObjectsArr.add(new Integer(
278: 0 - s1NTotalAllocObjects[i]));
279: idx++;
280: }
281: }
282: }
283:
284: // create diff using values from snapshot2
285: String[] s2ClassNames = snapshot2.getClassNames();
286: long[] s2NTrackedAllocObjects = snapshot2
287: .getNTrackedAllocObjects();
288: int[] s2NTrackedLiveObjects = snapshot2
289: .getNTrackedLiveObjects();
290: int[] s2MaxSurvGen = snapshot2.getMaxSurvGen();
291: float[] s2AvgObjectAge = snapshot2.getAvgObjectAge();
292: long[] s2TrackedLiveObjectsSize = snapshot2
293: .getTrackedLiveObjectsSize();
294: int[] s2NTotalAllocObjects = snapshot2.getnTotalAllocObjects();
295:
296: for (int i = 0; i < s2nClasses; i++) {
297: String className = s2ClassNames[i];
298: long nTrackedAllocObject = s2NTrackedAllocObjects[i];
299: int nTrackedLiveObject = s2NTrackedLiveObjects[i];
300: int maxSGen = s2MaxSurvGen[i];
301: float avtOAge = s2AvgObjectAge[i];
302: long trackedLiveObjectSize = s2TrackedLiveObjectsSize[i];
303: int nTotalAllocObject = s2NTotalAllocObjects[i];
304:
305: if (s2NTotalAllocObjects[i] > 0) {
306: Integer classIdx = (Integer) classNamesIdxMap
307: .get(className);
308: int classIndex;
309:
310: if (classIdx != null) {
311: // class already present in snapshot1
312: classIndex = classIdx.intValue();
313: nTrackedAllocObjectsArr.set(classIndex, new Long(
314: ((Long) nTrackedAllocObjectsArr
315: .get(classIndex)).longValue()
316: + nTrackedAllocObject));
317: nTrackedLiveObjectsArr.set(classIndex, new Integer(
318: ((Integer) nTrackedLiveObjectsArr
319: .get(classIndex)).intValue()
320: + nTrackedLiveObject));
321: maxSurvGenArr.set(classIndex, new Integer(
322: ((Integer) maxSurvGenArr.get(classIndex))
323: .intValue()
324: + maxSGen));
325: avgObjectAgeArr.set(classIndex, new Float(
326: ((Float) avgObjectAgeArr.get(classIndex))
327: .floatValue()
328: + avtOAge));
329: trackedLiveObjectsSizeArr.set(classIndex, new Long(
330: ((Long) trackedLiveObjectsSizeArr
331: .get(classIndex)).longValue()
332: + trackedLiveObjectSize));
333: nTotalAllocObjectsArr.set(classIndex, new Integer(
334: ((Integer) nTotalAllocObjectsArr
335: .get(classIndex)).intValue()
336: + nTotalAllocObject));
337: } else {
338: // class not present in snapshot1
339: classNamesIdxMap.put(className, new Integer(
340: nTrackedAllocObjectsArr.size()));
341: nTrackedAllocObjectsArr.add(new Long(
342: nTrackedAllocObject));
343: nTrackedLiveObjectsArr.add(new Integer(
344: nTrackedLiveObject));
345: maxSurvGenArr.add(new Integer(maxSGen));
346: avgObjectAgeArr.add(new Float(avtOAge));
347: trackedLiveObjectsSizeArr.add(new Long(
348: trackedLiveObjectSize));
349: nTotalAllocObjectsArr.add(new Integer(
350: nTotalAllocObject));
351: }
352: }
353: }
354:
355: // move the diff to instance variables
356: nClasses = classNamesIdxMap.size();
357: classNames = new String[nClasses];
358: nTrackedAllocObjects = new long[nClasses];
359: nTrackedLiveObjects = new int[nClasses];
360: maxSurvGen = new int[nClasses];
361: avgObjectAge = new float[nClasses];
362: trackedLiveObjectsSize = new long[nClasses];
363: nTotalAllocObjects = new int[nClasses];
364: minTrackedLiveObjectsSizeDiff = Integer.MAX_VALUE;
365: maxTrackedLiveObjectsSizeDiff = Integer.MIN_VALUE;
366:
367: Iterator classNamesIter = classNamesIdxMap.entrySet()
368: .iterator();
369: int index = 0;
370:
371: while (classNamesIter.hasNext()) {
372: Map.Entry entry = (Map.Entry) classNamesIter.next();
373: String className = (String) entry.getKey();
374: int classIndex = ((Integer) entry.getValue()).intValue();
375:
376: classNames[index] = className;
377: nTrackedAllocObjects[index] = ((Long) nTrackedAllocObjectsArr
378: .get(classIndex)).longValue();
379: nTrackedLiveObjects[index] = ((Integer) nTrackedLiveObjectsArr
380: .get(classIndex)).intValue();
381: maxSurvGen[index] = ((Integer) maxSurvGenArr
382: .get(classIndex)).intValue();
383: avgObjectAge[index] = ((Float) avgObjectAgeArr
384: .get(classIndex)).floatValue();
385: trackedLiveObjectsSize[index] = ((Long) trackedLiveObjectsSizeArr
386: .get(classIndex)).longValue();
387: nTotalAllocObjects[index] = ((Integer) nTotalAllocObjectsArr
388: .get(classIndex)).intValue();
389:
390: minTrackedLiveObjectsSizeDiff = Math.min(
391: minTrackedLiveObjectsSizeDiff,
392: trackedLiveObjectsSize[index]);
393: maxTrackedLiveObjectsSizeDiff = Math.max(
394: maxTrackedLiveObjectsSizeDiff,
395: trackedLiveObjectsSize[index]);
396:
397: index++;
398: }
399:
400: if ((minTrackedLiveObjectsSizeDiff > 0)
401: && (maxTrackedLiveObjectsSizeDiff > 0)) {
402: minTrackedLiveObjectsSizeDiff = 0;
403: } else if ((minTrackedLiveObjectsSizeDiff < 0)
404: && (maxTrackedLiveObjectsSizeDiff < 0)) {
405: maxTrackedLiveObjectsSizeDiff = 0;
406: }
407: }
408: }
|