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;
042:
043: import org.netbeans.lib.profiler.ProfilerClient;
044: import org.netbeans.lib.profiler.ProfilerLogger;
045: import org.netbeans.lib.profiler.global.CommonConstants;
046: import org.netbeans.lib.profiler.global.ProfilingSessionStatus;
047: import java.io.*;
048: import java.nio.MappedByteBuffer;
049: import java.nio.channels.FileChannel;
050:
051: /**
052: * Management of the shared-memory "event buffer" file, into which TA instrumentation writes rough profiling
053: * data, and which is processed here at the client side.
054: * So far it's deliberately allstatic. Can be made more object-style, but before doing that, check its current
055: * usage in ProfilerClient and, as a superclass, in CPUCallGraphBuilder etc.
056: *
057: * @author Misha Dmitirev
058: */
059: public class EventBufferProcessor implements CommonConstants {
060: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
061:
062: protected static ProfilingSessionStatus status;
063: protected static ProfilerClient profilerClient;
064: protected static byte[] buf;
065: protected static MappedByteBuffer mapByteBuf;
066: protected static File bufFile;
067: protected static RandomAccessFile raFile;
068: protected static FileChannel bufFileChannel;
069: protected static boolean bufFileExists;
070: protected static long startDataProcessingTime;
071: protected static long dataProcessingTime;
072:
073: //~ Methods ------------------------------------------------------------------------------------------------------------------
074:
075: /**
076: * For statistics only
077: */
078: public static long getDataProcessingTime() {
079: return dataProcessingTime;
080: }
081:
082: public static boolean setEventBufferFile(String fileName) {
083: if ((status != null) && status.remoteProfiling) {
084: return true;
085: }
086:
087: if (bufFile != null) {
088: removeEventBufferFile();
089: }
090:
091: try {
092: bufFile = new File(fileName);
093: raFile = new RandomAccessFile(bufFile, "rw"); // NOI18N
094: bufFileChannel = raFile.getChannel();
095: mapByteBuf = bufFileChannel.map(
096: FileChannel.MapMode.READ_WRITE, 0,
097: EVENT_BUFFER_SIZE_IN_BYTES);
098: mapByteBuf.rewind();
099: mapByteBuf.mark();
100: bufFileExists = true;
101: } catch (FileNotFoundException ex1) {
102: return false;
103: } catch (IOException ex2) {
104: ProfilerLogger
105: .severe("internal error when opening memory-mapped temporary file"); // NOI18N
106: ProfilerLogger.log(ex2);
107: ProfilerLogger.severe(PLEASE_REPORT_PROBLEM);
108:
109: return false;
110: }
111:
112: return true;
113: }
114:
115: public static boolean bufFileExists() {
116: return bufFileExists;
117: }
118:
119: public static void initialize(ProfilerClient inProfilerClient) {
120: profilerClient = inProfilerClient;
121: status = profilerClient.getStatus();
122: }
123:
124: public static synchronized void readDataAndPrepareForProcessing(
125: int bufSizeInBytes) {
126: if ((buf == null) || (buf.length < bufSizeInBytes)) {
127: buf = new byte[bufSizeInBytes];
128: }
129:
130: if (!status.remoteProfiling) {
131: mapByteBuf.reset();
132: mapByteBuf.get(buf, 0, bufSizeInBytes);
133: } else {
134: ObjectInputStream ois = profilerClient
135: .getSocketInputStream();
136:
137: try {
138: ois.readFully(buf, 0, bufSizeInBytes);
139: } catch (IOException ex) { // TODO [misha]: error reporting should be made more user-friendly
140: ProfilerLogger
141: .severe("error reading profiling data from socket:"); // NOI18N
142: ProfilerLogger.log(ex);
143: }
144: }
145:
146: startDataProcessingTime = System.currentTimeMillis();
147: }
148:
149: public static void removeEventBufferFile() {
150: if ((status != null) && status.remoteProfiling) {
151: return; // This may be called "uniformly" even during monitoring, when status isn't initialized
152: }
153:
154: try {
155: if (bufFile != null) {
156: mapByteBuf = null;
157:
158: if (bufFileChannel != null) {
159: bufFileChannel.close(); // bufFileChannel can accidentally be null, if previous connection didn't quite succeed
160: }
161:
162: if (raFile != null) {
163: raFile.close();
164: }
165:
166: System.gc(); // Stupid - but that's the only way to GC mapBuf and thus to enable the buffer file deletion...
167: // Now try to remove the buffer file. If this doesn't happen immediately, try again - it may be that the
168: // target VM has not yet freed this file on its side. Repeat attempts for 2 seconds.
169:
170: for (int i = 0; i < 20; i++) {
171: if (bufFile.delete()) {
172: bufFile = null;
173: bufFileExists = false;
174:
175: return;
176: } else {
177: try {
178: Thread.sleep(100);
179: } catch (InterruptedException e) {
180: }
181: }
182: }
183: }
184: } catch (IOException ex) {
185: ProfilerLogger
186: .severe("internal error when closing memory-mapped temporary file"); // NOI18N
187: ProfilerLogger.severe(PLEASE_REPORT_PROBLEM);
188: } finally {
189: bufFileExists = false;
190: }
191: }
192:
193: public static void reset() {
194: dataProcessingTime = 0;
195:
196: // buf = null; // to cleanup memory allocated for the buffer - we cannot do this here, there may be events in the
197: // buffer that are still unprocessed and the EventBufferProcessor in the cycle of processing it
198: // see http://profiler.netbeans.org/issues/show_bug.cgi?id=69275
199: }
200:
201: protected static synchronized void completeDataProcessing() {
202: dataProcessingTime += (System.currentTimeMillis() - startDataProcessingTime);
203: }
204: }
|