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: /*
042: * MemoryTestCase.java
043: *
044: * Created on July 19, 2005, 5:21 PM
045: *
046: * To change this template, choose Tools | Options and locate the template under
047: * the Source Creation and Management node. Right-click the template and choose
048: * Open. You can then make changes to the template in the Source Editor.
049: */
050: package org.netbeans.lib.profiler.tests.jfluid.memory;
051:
052: import org.netbeans.lib.profiler.ProfilerEngineSettings;
053: import org.netbeans.lib.profiler.TargetAppRunner;
054: import org.netbeans.lib.profiler.results.EventBufferResultsProvider;
055: import org.netbeans.lib.profiler.results.ProfilingResultsDispatcher;
056: import org.netbeans.lib.profiler.results.RuntimeCCTNode;
057: import org.netbeans.lib.profiler.results.memory.MemoryCCTProvider;
058: import org.netbeans.lib.profiler.results.memory.MemoryCallGraphBuilder;
059: import org.netbeans.lib.profiler.tests.jfluid.*;
060: import org.netbeans.lib.profiler.tests.jfluid.utils.*;
061: import org.netbeans.lib.profiler.utils.StringUtils;
062: import java.util.ArrayList;
063: import java.util.Collections;
064:
065: /**
066: *
067: * @author ehucka
068: */
069: public abstract class MemoryTestCase extends CommonProfilerTestCase {
070: //~ Inner Classes ------------------------------------------------------------------------------------------------------------
071:
072: private static class MemoryResultListener implements
073: MemoryCCTProvider.Listener {
074: //~ Instance fields ------------------------------------------------------------------------------------------------------
075:
076: private final Object resultsLock = new Object();
077: private boolean hasResults = false;
078:
079: //~ Methods --------------------------------------------------------------------------------------------------------------
080:
081: public void cctEstablished(RuntimeCCTNode appRootNode) {
082: System.out.println("Memory CCT Established");
083:
084: synchronized (resultsLock) {
085: hasResults = true;
086: resultsLock.notify();
087: }
088: }
089:
090: public void cctReset() {
091: synchronized (resultsLock) {
092: hasResults = false;
093: resultsLock.notify();
094: }
095: }
096:
097: public boolean wait4results(long timeout) {
098: synchronized (resultsLock) {
099: if (!hasResults) {
100: try {
101: resultsLock.wait(timeout);
102: } catch (InterruptedException e) {
103: }
104: }
105:
106: return hasResults;
107: }
108: }
109: }
110:
111: //~ Instance fields ----------------------------------------------------------------------------------------------------------
112:
113: MemoryCallGraphBuilder builder = new MemoryCallGraphBuilder();
114: MemoryResultListener resultListener = null;
115:
116: //~ Constructors -------------------------------------------------------------------------------------------------------------
117:
118: /**
119: * Creates a new instance of MemoryTestCase
120: */
121: public MemoryTestCase(String name) {
122: super (name);
123: }
124:
125: //~ Methods ------------------------------------------------------------------------------------------------------------------
126:
127: protected void checkMemoryResults(TargetAppRunner targetAppRunner,
128: String[] matchingPrefixes,
129: MemoryResultListener resultListener) throws Exception {
130: boolean gotResults = false;
131: int retryCounter = 4;
132:
133: do {
134: targetAppRunner.getProfilerClient()
135: .forceObtainedResultsDump();
136: gotResults = resultListener.wait4results(2500);
137: } while (!gotResults && (--retryCounter > 0));
138:
139: assertTrue("CallGraphBuilder: Results do not exist.",
140: gotResults);
141: log("results obtained: " + System.currentTimeMillis());
142:
143: int[] totalAllocObjects = targetAppRunner.getProfilerClient()
144: .getAllocatedObjectsCountResults();
145: String[] classnames = targetAppRunner.getProfilerClient()
146: .getStatus().getClassNames();
147: long[] totalAllocObjectsSize = null;
148: ArrayList list = new ArrayList(128);
149: totalAllocObjectsSize = builder.getAllocObjectNumbers();
150:
151: for (int i = 0; i < totalAllocObjectsSize.length; i++) {
152: boolean match = false;
153:
154: for (int j = 0; j < matchingPrefixes.length; j++) {
155: if (classnames[i].startsWith(matchingPrefixes[j])) {
156: match = true;
157:
158: break;
159: }
160: }
161:
162: if (match) {
163: StringBuffer out = new StringBuffer();
164: out.append(complete(StringUtils
165: .userFormClassName(classnames[i]), 32));
166: out.append(complete(String
167: .valueOf(totalAllocObjects[i]), 8));
168: //out.append(complete(StringUtils.nBytesToString(totalAllocObjectsSize[i]), 10));
169: list.add(out.toString());
170: }
171: }
172:
173: ref(complete("Name", 32) + complete("DCount", 8)); //+complete("DSize", 10));
174:
175: try {
176: MemoryCCTProvider.ObjectNumbersContainer liveness = builder
177: .getLivenessObjectNumbers();
178:
179: totalAllocObjectsSize = liveness.trackedLiveObjectsSize;
180:
181: float[] avgage = liveness.avgObjectAge;
182: int[] maxSurvGen = liveness.maxSurvGen;
183: long[] ntrackedallocobjects = liveness.nTrackedAllocObjects;
184: int[] ntrackedliveobjects = liveness.nTrackedLiveObjects;
185:
186: for (int i = 0; i < totalAllocObjectsSize.length; i++) {
187: boolean match = false;
188:
189: for (int j = 0; j < matchingPrefixes.length; j++) {
190: if (classnames[i].startsWith(matchingPrefixes[j])) {
191: match = true;
192:
193: break;
194: }
195: }
196:
197: if (match) {
198: StringBuffer out = new StringBuffer();
199: out.append(complete(StringUtils
200: .userFormClassName(classnames[i]), 32));
201: //out.append(complete(StringUtils.nBytesToString(totalAllocObjectsSize[i]), 10));
202: out.append(complete(String
203: .valueOf(ntrackedliveobjects[i]), 10));
204: out.append(complete(String
205: .valueOf(ntrackedallocobjects[i]), 8));
206: //out.append(complete(String.valueOf((int)avgage[i]), 8));
207: //out.append(complete(String.valueOf(maxSurvGen[i]), 8));
208: list.add(out.toString());
209: }
210: }
211:
212: ref(complete("Name", 32) /*complete("LiveBytes", 10)+*/
213: + complete("LiveObjs", 10) + complete("Allocs", 8)); //+complete("AvgAge", 8)+complete("MaxSurv", 8));
214: } catch (IllegalStateException e) {
215: }
216:
217: //log results
218: Collections.sort(list);
219:
220: for (int i = 0; i < list.size(); i++) {
221: ref(list.get(i));
222: }
223:
224: ref("");
225: }
226:
227: protected ProfilerEngineSettings initMemoryTest(String projectName,
228: String className) {
229: ProfilerEngineSettings settings = initTest(projectName,
230: className, null);
231: //defaults
232: settings.setThreadCPUTimerOn(false);
233: settings.setAllocTrackEvery(1); //default is not strict - cannot be measured in test
234: settings.setRunGCOnGetResultsInMemoryProfiling(true);
235:
236: return settings;
237: }
238:
239: protected void startMemoryTest(ProfilerEngineSettings settings,
240: int instrMode, String[] classPrefixes) {
241: //create runner //instrMode CommonConstants.INSTR_OBJECT_ALLOCATIONS
242: assertTrue(builder != null);
243:
244: TestProfilerAppHandler handler = new TestProfilerAppHandler(
245: this );
246: TargetAppRunner runner = new TargetAppRunner(settings, handler,
247: new TestProfilingPointsProcessor());
248: runner.addProfilingEventListener(Utils
249: .createProfilingListener(this ));
250:
251: builder.removeAllListeners();
252: ProfilingResultsDispatcher.getDefault().removeAllListeners();
253:
254: resultListener = new MemoryResultListener();
255: builder.addListener(resultListener);
256:
257: ProfilingResultsDispatcher.getDefault().addListener(builder);
258: builder.startup(runner.getProfilerClient());
259: EventBufferResultsProvider.getDefault().startup(
260: runner.getProfilerClient());
261:
262: try {
263: assertTrue("not read calibration data", runner
264: .readSavedCalibrationData());
265:
266: Process p = startTargetVM(runner);
267: assertNotNull("Target JVM is not started", p);
268: bindStreams(p);
269: runner.connectToStartedVMAndStartTA();
270:
271: runner.getProfilerClient()
272: .initiateMemoryProfInstrumentation(instrMode);
273:
274: waitForStatus(STATUS_RUNNING);
275: assertTrue("runner is not running", runner
276: .targetAppIsRunning());
277:
278: waitForStatus(STATUS_RESULTS_AVAILABLE
279: | STATUS_APP_FINISHED);
280:
281: if (!isStatus(STATUS_APP_FINISHED)) {
282: waitForStatus(STATUS_APP_FINISHED);
283: }
284:
285: checkMemoryResults(runner, classPrefixes, resultListener);
286: setStatus(STATUS_MEASURED);
287: } catch (Exception ex) {
288: log(ex);
289: assertTrue("Exception thrown: " + ex.getMessage(), false);
290: } finally {
291: ProfilingResultsDispatcher.getDefault().pause(true);
292: builder.shutdown();
293:
294: builder.removeListener(resultListener);
295: ProfilingResultsDispatcher.getDefault().removeListener(
296: builder);
297:
298: finalizeTest(runner);
299: }
300: }
301: }
|