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: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package dwarfvsmodel;
043:
044: import java.io.File;
045: import java.io.FileNotFoundException;
046: import java.text.NumberFormat;
047: import java.util.HashSet;
048: import java.util.Set;
049: import org.netbeans.modules.cnd.dwarfdump.CompilationUnit;
050: import org.netbeans.modules.cnd.dwarfdump.dwarf.DwarfMacinfoTable;
051: import java.util.List;
052: import org.netbeans.modules.cnd.dwarfdump.Dwarf;
053: import java.io.IOException;
054: import java.util.ArrayList;
055: import java.util.Iterator;
056: import modeldump.ModelDump;
057: import java.io.PrintStream;
058: import java.util.Collection;
059:
060: import org.netbeans.modules.cnd.api.model.*;
061: import org.netbeans.modules.cnd.modelimpl.csm.core.FileImpl;
062: import org.netbeans.modules.cnd.modelimpl.csm.core.ProjectBase;
063: import org.netbeans.modules.cnd.modelimpl.debug.Diagnostic;
064:
065: /**
066: * Main class for "Dwarf vs Model"
067: * @author ak119685, vk155633
068: */
069: public class DwarfVsModelMain {
070:
071: private ModelDump modelDump;
072: private File tempDir = null;
073: boolean printToScreen = false;
074: private File errorStatisticsFile;
075: private int verbosity = 1;
076:
077: public static void main(String[] args) {
078: try {
079: DMFlags.parse(args);
080: new DwarfVsModelMain().main();
081: } catch (Config.WrongArgumentException ex) {
082: //ex.printStackTrace();
083: System.err.println(ex.getMessage());
084: }
085: }
086:
087: public DwarfVsModelMain() {
088: printToScreen = DMFlags.printToScreen.getValue();
089: }
090:
091: public void main() {
092:
093: try {
094:
095: if (!printToScreen) {
096: tempDir = new File(DMFlags.tempDir.getValue());
097: if (tempDir.exists()) {
098: if (!tempDir.isDirectory()) {
099: System.err.println("File "
100: + tempDir.getAbsolutePath()
101: + " exists and it isn't directory"); // NOI18N
102: return;
103: } else if (!tempDir.canWrite()) {
104: System.err.println("Directory "
105: + tempDir.getAbsolutePath()
106: + " isn't writeble"); // NOI18N
107: return;
108: }
109: } else {
110: if (!tempDir.mkdirs()) {
111: System.err.println("Can't create directory "
112: + tempDir.getAbsolutePath()); // NOI18N
113: return;
114: }
115: }
116: }
117:
118: PrintStream globalTraceLog = printToScreen ? System.out
119: : DMUtils.createStream(tempDir, "_all", "trace"); // NOI18N
120:
121: initErrorStatistics();
122:
123: String logFile = DMFlags.logFile.getValue();
124: PrintStream resultLog = (logFile == null) ? System.out
125: : new PrintStream(logFile);
126: resultLog.println(""); // NOI18N
127:
128: String configFileName = DMFlags.configFile.getValue();
129:
130: if (configFileName == null) {
131: return;
132: }
133:
134: ConfigFile configFile = new ConfigFile(configFileName,
135: globalTraceLog);
136:
137: modelDump = new ModelDump(System.out);
138:
139: Collection<FileInfo> filesToProcess = configFile
140: .getFilesToProcess();
141: ComparationResult result = new ComparationResult("Total",
142: 0, 0, 0); // NOI18N
143:
144: if (DMFlags.COMPILE_ALL_FIRST) {
145: long memo = usedMemory();
146: long time = System.currentTimeMillis();
147: for (Iterator<FileInfo> i = filesToProcess.iterator(); i
148: .hasNext();) {
149: FileInfo file = i.next();
150: PrintStream traceLog = getTraceStream(file, false);
151: if (traceLog != System.out && verbosity > 0) {
152: System.out.println("Compiling file: "
153: + file.getSrcFileName()); // NOI18N
154: }
155: compileFile(traceLog, file);
156: if (traceLog != System.out) {
157: traceLog.close();
158: }
159: }
160: printTime("\nTotal parsing time:", time, resultLog); // NOI18N
161: printMemory("Total memory used by code model:", memo,
162: resultLog); // NOI18N
163: resultLog.println("");
164: }
165:
166: long memo = usedMemory();
167: long time = System.currentTimeMillis();
168:
169: for (Iterator<FileInfo> i = filesToProcess.iterator(); i
170: .hasNext();) {
171: FileInfo fileInfo = i.next();
172:
173: Dwarf dwarfDump = null;
174: CompilationUnit dwarfData = null;
175: try {
176: dwarfDump = new Dwarf(fileInfo.getObjFileName());
177: dwarfData = dwarfDump.getCompilationUnit(fileInfo
178: .getSrcFileName());
179: } catch (IOException e) {
180: resultLog.println(e.toString());
181: continue;
182: }
183:
184: if (dwarfData == null) {
185: resultLog.println("Cannot get DWARF data from "
186: + fileInfo.getObjFileName() + " for "
187: + fileInfo.getSrcFileName()); // NOI18N
188: continue;
189: }
190:
191: PrintStream traceLog = getTraceStream(fileInfo,
192: DMFlags.COMPILE_ALL_FIRST);
193: if (!DMFlags.COMPILE_ALL_FIRST) {
194: compileFile(traceLog, fileInfo);
195: }
196: CsmFile codeModel = fileInfo.getCsmFile();
197: traceLog.println("Comparing file: "
198: + fileInfo.getSrcFileName()); // NOI18N
199:
200: ModelComparator comparator = new ModelComparator(
201: codeModel, dwarfData, dwarfDump, fileInfo,
202: resultLog, traceLog);
203: comparator.setBidirectional(DMFlags.bidirectional
204: .getValue());
205: comparator.setPrintToScreen(printToScreen);
206: comparator.setCompareBodies(!DMFlags.flat.getValue());
207: comparator.setTemp(tempDir);
208: //comparator.setDumpDwarf(true); // config.flagSet("-u")); // // NOI18N
209: try {
210: result.add(comparator.compare());
211: } catch (Exception e) {
212: System.err.println("Error when processing files "
213: + fileInfo.getObjFileName() + " and "
214: + fileInfo.getSrcFileName());
215: e.printStackTrace(System.err);
216: //return;
217: }
218: }
219:
220: //resultLog.println("Final statistics:"); // NOI18N
221: result.dump(resultLog);
222: resultLog.printf("Total parser error count: %5d",
223: calculateTotalErrorCount()); // NOI18N
224:
225: if (DMFlags.COMPILE_ALL_FIRST) {
226: printTime("\nTotal comparison time:", time, resultLog); // NOI18N
227: printMemory("Total memory used by comparison:", memo,
228: resultLog); // NOI18N
229: } else {
230: printTime("\nTotal processing time:", time, resultLog); // NOI18N
231: printMemory("Total memory used", memo, resultLog); // NOI18N
232: }
233: printMemory("\nFinal memory footprint:", -1, resultLog); // NOI18N
234: resultLog.println("");
235:
236: } catch (Exception ex) {
237: System.err.println("Fatal error: " + ex.getMessage());
238: ex.printStackTrace(System.err);
239: } finally {
240: if (modelDump != null) {
241: modelDump.stopModel();
242: }
243: }
244: printErrorStatistics();
245: }
246:
247: private void compileFile(final PrintStream traceLog,
248: final FileInfo file) {
249: traceLog.println("Compiling file: " + file.getSrcFileName()); // NOI18N
250: // Setup includes ...
251: ArrayList<String> includes = file.getQuoteIncludes();
252: List<String> cl_includes = DMFlags.userIncludes.getValue();
253: if (cl_includes != null) {
254: includes.addAll(cl_includes);
255: }
256:
257: // ArrayList<String> dwarfIncludes = file.convertPaths(dwarfData.getStatementList().getIncludeDirectories());
258: // includes.addAll(dwarfIncludes);
259:
260: // Setup defines ...
261: ArrayList<String> defines = file.getDefines();
262: List<String> cl_defines = DMFlags.userDefines.getValue();
263: if (cl_defines != null) {
264: defines.addAll(cl_defines);
265: }
266:
267: // DwarfMacinfoTable dwarfMacrosTable = dwarfData.getMacrosTable();
268: //
269: // if (dwarfMacrosTable != null) {
270: // ArrayList<String> dwarfDefines = dwarfMacrosTable.getCommandLineDefines();
271: // defines.addAll(dwarfDefines);
272: // }
273:
274: modelDump.setLog(traceLog);
275:
276: // Get Model to compare ...
277:
278: CsmFile tmpCsmFile = modelDump.process(file.getSrcFileName(),
279: includes, defines);
280: file.setCsmFile(tmpCsmFile);
281: }
282:
283: private PrintStream getTraceStream(FileInfo file, boolean append)
284: throws FileNotFoundException {
285: return printToScreen ? System.out : DMUtils.createStream(
286: tempDir, file.getSrcFileName(), "trace", append); // NOI18N
287: }
288:
289: private long usedMemory() {
290: System.gc();
291: return Runtime.getRuntime().totalMemory()
292: - Runtime.getRuntime().freeMemory();
293: }
294:
295: private void printMemory(String text, long memUsed, PrintStream ps) {
296: long delta = usedMemory();
297: if (memUsed >= 0) {
298: delta -= memUsed;
299: }
300: NumberFormat nf = NumberFormat.getIntegerInstance();
301: nf.setGroupingUsed(true);
302: nf.setMinimumIntegerDigits(6);
303: ps.printf("%s %s Kb\n", text, nf.format((delta) / 1024)); // NOI18N
304: }
305:
306: private void printTime(String text, long time, PrintStream ps) {
307: long delta = System.currentTimeMillis() - time;
308: NumberFormat nf = NumberFormat.getIntegerInstance();
309: nf.setGroupingUsed(true);
310: nf.setMinimumIntegerDigits(6);
311: //ps.println(text + nf.format((delta)/1000) + " Kb"); // NOI18N
312: ps.printf("%s %s seconds\n", text, nf.format((delta) / 1000)); // NOI18N
313: }
314:
315: private void initErrorStatistics() {
316: errorStatisticsFile = new File(tempDir, "_errorStat"); // NOI18N
317: // if( errorStatisticsFile.exists() ) {
318: // errorStatisticsFile.delete();
319: // }
320: Diagnostic.setStatisticsLevel(Integer.getInteger(
321: "cnd.modelimpl.stat.level", 1).intValue()); // NOI18N
322: Diagnostic.initFileStatistics(errorStatisticsFile
323: .getAbsolutePath());
324: }
325:
326: private void printErrorStatistics() {
327: try {
328: Diagnostic.dumpUnresolvedStatistics(errorStatisticsFile
329: .getAbsolutePath(), false);
330: Diagnostic.dumpFileStatistics(errorStatisticsFile
331: .getAbsolutePath(), true);
332: } catch (Exception e) {
333: e.printStackTrace(System.err);
334: }
335: }
336:
337: private int calculateTotalErrorCount() {
338: int cnt = 0;
339: Set<CsmProject> processedProjects = new HashSet<CsmProject>();
340: Set<CsmFile> processedFiles = new HashSet<CsmFile>();
341: for (CsmProject prj : (Collection<CsmProject>) CsmModelAccessor
342: .getModel().projects()) {
343: cnt += calculateTotalErrorCount(prj, processedProjects,
344: processedFiles);
345: }
346: return cnt;
347: }
348:
349: private int calculateTotalErrorCount(CsmProject prj,
350: Set<CsmProject> processedProjects,
351: Set<CsmFile> processedFiles) {
352: int cnt = 0;
353: if (!processedProjects.contains(prj)) {
354: for (CsmProject lib : (Collection<CsmProject>) prj
355: .getLibraries()) {
356: cnt += calculateTotalErrorCount(lib, processedProjects,
357: processedFiles);
358: }
359: for (FileImpl file : ((ProjectBase) prj).getFileList()) {
360: cnt += file.getErrorCount();
361: }
362: }
363: return cnt;
364: }
365: }
|