incubator-kato-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ccris...@apache.org
Subject svn commit: r754949 [5/5] - in /incubator/kato/trunk/import/org.apache.kato.tools.katoview: ./ .settings/ src/ src/com/ src/com/ibm/ src/com/ibm/dtfj/ src/com/ibm/dtfj/dtfjview/ src/com/ibm/dtfj/dtfjview/commands/ src/com/ibm/dtfj/dtfjview/commands/hel...
Date Mon, 16 Mar 2009 16:43:31 GMT
Added: incubator/kato/trunk/import/org.apache.kato.tools.katoview/src/com/ibm/dtfj/dtfjview/heapdump/LongListReferenceIterator.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/import/org.apache.kato.tools.katoview/src/com/ibm/dtfj/dtfjview/heapdump/LongListReferenceIterator.java?rev=754949&view=auto
==============================================================================
--- incubator/kato/trunk/import/org.apache.kato.tools.katoview/src/com/ibm/dtfj/dtfjview/heapdump/LongListReferenceIterator.java
(added)
+++ incubator/kato/trunk/import/org.apache.kato.tools.katoview/src/com/ibm/dtfj/dtfjview/heapdump/LongListReferenceIterator.java
Mon Mar 16 16:43:29 2009
@@ -0,0 +1,50 @@
+/*******************************************************************************
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package com.ibm.dtfj.dtfjview.heapdump;
+
+import java.util.Iterator;
+import java.util.List;
+
+
+/**
+ * Reference iterator based on a list of Longs.
+ * 
+ */
+public class LongListReferenceIterator implements ReferenceIterator
+{
+	private final List _data;
+	private Iterator _currentIterator;
+	
+	public LongListReferenceIterator(List data) 
+	{
+		_data = data;
+		reset();
+	}
+	
+	public boolean hasNext()
+	{
+		return _currentIterator.hasNext();
+	}
+
+	public Long next()
+	{
+		return (Long) _currentIterator.next();
+	}
+
+	public void reset()
+	{
+		_currentIterator = _data.iterator();
+	}
+	
+}

Added: incubator/kato/trunk/import/org.apache.kato.tools.katoview/src/com/ibm/dtfj/dtfjview/heapdump/ReferenceIterator.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/import/org.apache.kato.tools.katoview/src/com/ibm/dtfj/dtfjview/heapdump/ReferenceIterator.java?rev=754949&view=auto
==============================================================================
--- incubator/kato/trunk/import/org.apache.kato.tools.katoview/src/com/ibm/dtfj/dtfjview/heapdump/ReferenceIterator.java
(added)
+++ incubator/kato/trunk/import/org.apache.kato.tools.katoview/src/com/ibm/dtfj/dtfjview/heapdump/ReferenceIterator.java
Mon Mar 16 16:43:29 2009
@@ -0,0 +1,42 @@
+/*******************************************************************************
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package com.ibm.dtfj.dtfjview.heapdump;
+
+/**
+ * Type-safe, resettable iterator for references.
+ * 
+ */
+public interface ReferenceIterator
+{
+
+	/**
+	 * 
+	 * @return True if next() will return non-null,
+	 * false otherwise.
+	 */
+	public boolean hasNext();
+	
+	/**
+	 * 
+	 * @return Next reference
+	 */
+	public Long next();
+	
+	/**
+	 * Resets the iterator back to the start of the 
+	 * structure it is iterating. Iterator will behave
+	 * as if it has just been constructed
+	 */
+	public void reset();
+}

Added: incubator/kato/trunk/import/org.apache.kato.tools.katoview/src/com/ibm/dtfj/dtfjview/heapdump/classic/ClassicHeapDumpFormatter.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/import/org.apache.kato.tools.katoview/src/com/ibm/dtfj/dtfjview/heapdump/classic/ClassicHeapDumpFormatter.java?rev=754949&view=auto
==============================================================================
--- incubator/kato/trunk/import/org.apache.kato.tools.katoview/src/com/ibm/dtfj/dtfjview/heapdump/classic/ClassicHeapDumpFormatter.java
(added)
+++ incubator/kato/trunk/import/org.apache.kato.tools.katoview/src/com/ibm/dtfj/dtfjview/heapdump/classic/ClassicHeapDumpFormatter.java
Mon Mar 16 16:43:29 2009
@@ -0,0 +1,282 @@
+/*******************************************************************************
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package com.ibm.dtfj.dtfjview.heapdump.classic;
+
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.Writer;
+
+import com.ibm.dtfj.dtfjview.heapdump.HeapDumpFormatter;
+import com.ibm.dtfj.dtfjview.heapdump.LongArrayReferenceIterator;
+import com.ibm.dtfj.dtfjview.heapdump.ReferenceIterator;
+
+/**
+ * A formatter for classic heap dumps.
+ * 
+ * Format specification is in the diagnostic guide.
+ * 
+ */
+public class ClassicHeapDumpFormatter extends HeapDumpFormatter
+{
+	private static final String EOF_HEADER = "// EOF: Total 'Objects',Refs(null) :";
+	private static final String BREAKDOWN_HEADER = "// Breakdown - Classes:";
+	private static final String VERSION_HEADER = "// Version: ";
+	
+	// Using a BufferedWriter rather than a PrintWriter because a BufferedWriter preserves IOExceptions
while providing newLine().
+	private final BufferedWriter _out;
+	private int _classCount = 0;
+	//This includes objects + objectArrays + primitiveArrays + classes
+	private int _allObjectCount = 0;
+	private int _objectArraysCount = 0;
+	private int _primitiveArraysCount = 0;
+	private int _referenceCount = 0;
+	private int _nullReferenceCount = 0;
+	private boolean _closed = false;
+	
+	public ClassicHeapDumpFormatter(Writer out,String version,boolean is64bit) throws IOException
+	{
+		super(version,is64bit);
+		_out = new BufferedWriter(out);
+		
+		writeHeader();
+	}
+	
+	private void writeHeader() throws IOException
+	{
+		_out.write(VERSION_HEADER + _version);
+		_out.newLine();
+	}
+
+	private void writeReferences(ReferenceIterator references) throws IOException
+	{
+		if(references == null) {
+			return;
+		}
+		references.reset();
+		if(! references.hasNext()) {
+			return;
+		}
+		
+		StringBuffer referenceString = new StringBuffer();
+		
+		boolean first = true;
+		
+		while(references.hasNext()) {
+			Long thisRef = references.next();
+			
+			if(first) {
+				first = false;
+			} else {
+				referenceString.append(" ");
+			}
+			
+			referenceString.append(toHexString(thisRef.longValue()));
+			
+			_referenceCount++;
+			if(thisRef.longValue() == 0) {
+				_nullReferenceCount++;
+			}
+		}
+		
+		_out.write(referenceString.toString());
+		_out.newLine();
+	}
+	
+	private static String toHexString(long value)
+	{
+		//Fast-path for 0
+		if(value == 0) {
+			return "0x0";
+		}
+		
+		return "0x" + Long.toHexString(value);
+	}
+	
+	public void addClass(long address, String name, long superClassAddress,
+			int size, int instanceSize, int hashCode, ReferenceIterator references)
+			throws IOException
+	{
+		checkClosed();
+		
+		_out.write(toHexString(address) + " [" + size + "] CLS " + name);
+		_out.newLine();
+		
+		writeReferences(references);
+		
+		_classCount++;
+		_allObjectCount++;
+	}
+
+	private void checkClosed()
+	{
+		if(_closed) {
+			throw new UnsupportedOperationException("This dump has been closed");
+		}
+	}
+
+	private void writeEntry(long address,int size,String className,ReferenceIterator references)
throws IOException
+	{
+		_out.write(toHexString(address) + " [" + size + "] OBJ " + className);
+		_out.newLine();
+
+		writeReferences(references);
+		
+		_allObjectCount++;
+	}
+	
+	public void addObject(long address, long classAddress, String className,
+			int size, int hashCode, ReferenceIterator references)
+			throws IOException
+	{
+		checkClosed();
+		
+		//Add an artificial link between the object and its class
+		references = addReference(references,classAddress);
+		
+		writeEntry(address,size,className,references);
+	}
+
+	/**
+	 * Wraps the original reference iterator to add an artificial reference to the list.
+	 */
+	private static ReferenceIterator addReference(final ReferenceIterator original,
+			final long extra)
+	{   
+		return new ReferenceIterator(){
+
+			private boolean _returnedExtra = false;
+			
+			public boolean hasNext()
+			{
+				if(_returnedExtra) {
+					return original.hasNext();
+				} else {
+					return true;
+				}
+			}
+
+			public Long next()
+			{
+				if(_returnedExtra) {
+					return original.next();
+				} else {
+					_returnedExtra = true;
+					return new Long(extra);
+				}
+			}
+
+			public void reset()
+			{
+				original.reset();
+				_returnedExtra = false;
+			}};
+	}
+
+	public void addObjectArray(long address, long arrayClassAddress,
+			String arrayClassName, long elementClassAddress,
+			String elementClassName, int size, int numberOfElements,
+			int hashCode, ReferenceIterator references) throws IOException
+	{
+		checkClosed();
+		
+		writeEntry(address,size,arrayClassName,references);
+		
+		_objectArraysCount++;
+	}
+
+	public void addPrimitiveArray(long address, long arrayClassAddress, int type, int size,
+			int hashCode, int numberOfElements) throws IOException,
+			IllegalArgumentException
+	{
+		checkClosed();
+		
+		//Add an artificial reference to the object class:
+		ReferenceIterator references = new LongArrayReferenceIterator(new long[]{arrayClassAddress});
+		
+		writeEntry(address,size,getPrimitiveArrayName(type),references);
+		
+		_primitiveArraysCount++;
+	}
+
+	private String getPrimitiveArrayName(int type)
+	{
+		switch(type) {
+		case 0:
+			return "[Z";
+		case 1:
+			return "[C";
+		case 2:
+			return "[F";
+		case 3:
+			return "[D";
+		case 4:
+			return "[B";
+		case 5:
+			return "[S";
+		case 6:
+			return "[I";
+		case 7:
+			return "[J";
+		default:
+			throw new IllegalArgumentException("Unknown primitive type code: " + type);
+		}
+	}
+
+	public void close() throws IOException
+	{        
+		printBreakdown();
+		
+		printEOFSummary();
+		
+		_out.close();
+		
+		_closed = true;
+	}
+
+	private void printEOFSummary() throws IOException
+	{
+		StringBuffer buffer = new StringBuffer(EOF_HEADER);
+		
+		buffer.append(_allObjectCount);
+		buffer.append(",");
+		buffer.append(_referenceCount);
+		buffer.append("(");
+		buffer.append(_nullReferenceCount);
+		buffer.append(")");
+		
+		_out.write(buffer.toString());
+		_out.newLine();
+	}
+
+	private void printBreakdown() throws IOException
+	{
+		int objectsCount = ((_allObjectCount - _primitiveArraysCount) - _objectArraysCount) - _classCount;
+		
+		StringBuffer buffer = new StringBuffer(BREAKDOWN_HEADER);
+		buffer.append(_classCount);
+		
+		buffer.append(", Objects:");
+		buffer.append(objectsCount);
+		
+		buffer.append(", ObjectArrays:");
+		buffer.append(_objectArraysCount);
+		
+		buffer.append(", PrimitiveArrays:");
+		buffer.append(_primitiveArraysCount);
+		
+		_out.write(buffer.toString());
+		_out.newLine();
+	}
+
+}



Mime
View raw message