|
Added
Link Here
|
| 0 |
- |
1 |
/******************************************************************************* |
|
|
2 |
* Copyright (c) 2012 Red Hat, Inc. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Camilo Bernal <cabernal@redhat.com> - Initial Implementation. |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.linuxtools.internal.perf.model; |
| 12 |
|
| 13 |
import java.util.ArrayList; |
| 14 |
|
| 15 |
import org.eclipse.core.resources.IFile; |
| 16 |
import org.eclipse.debug.core.ILaunchConfiguration; |
| 17 |
import org.eclipse.linuxtools.internal.perf.PerfCore; |
| 18 |
import org.eclipse.linuxtools.internal.perf.PerfPlugin; |
| 19 |
import org.eclipse.linuxtools.internal.perf.launch.PerfDefaultLaunchConfig; |
| 20 |
|
| 21 |
public class PMSymbolMatcher { |
| 22 |
|
| 23 |
/** |
| 24 |
* Get PMSymbol's from report of file |
| 25 |
* |
| 26 |
* @param file |
| 27 |
* the file to be reported |
| 28 |
* @return array of PMSymbol's |
| 29 |
*/ |
| 30 |
private static ArrayList<PMSymbol> getSymbols(IFile file) { |
| 31 |
String projectName = file.getProject().getName(); |
| 32 |
|
| 33 |
PerfDefaultLaunchConfig perfConfig = new PerfDefaultLaunchConfig(); |
| 34 |
ILaunchConfiguration config = perfConfig |
| 35 |
.createDefaultConfiguration(projectName); |
| 36 |
|
| 37 |
PerfCore.Report(config, null, null, null, file.getLocation() |
| 38 |
.toOSString(), null); |
| 39 |
|
| 40 |
// get model from reported perf data file |
| 41 |
TreeParent invisibleRoot = PerfPlugin.getDefault().getModelRoot(); |
| 42 |
|
| 43 |
if (invisibleRoot.getChildren().length == 0) { |
| 44 |
return null; |
| 45 |
} |
| 46 |
|
| 47 |
ArrayList<PMSymbol> symbols = new ArrayList<PMSymbol>(); |
| 48 |
|
| 49 |
// get all PMSymbols |
| 50 |
for (TreeParent event : invisibleRoot.getChildren()) { |
| 51 |
for (TreeParent cmd : event.getChildren()) { |
| 52 |
for (TreeParent dso : cmd.getChildren()) { |
| 53 |
for (TreeParent dsoFile : dso.getChildren()) { |
| 54 |
for (TreeParent sym : dsoFile.getChildren()) { |
| 55 |
symbols.add((PMSymbol) sym); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
return symbols; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get PMSymbol's matches from the two given PMSymbol arrays |
| 66 |
* |
| 67 |
* @param stale |
| 68 |
* PMSymbol array of older PMSymbol's |
| 69 |
* @param fresh |
| 70 |
* PMSymbol array of newer PMSymbol's |
| 71 |
* @return array of PMSymbolMatch's, each containing a PMSymbol match pair |
| 72 |
*/ |
| 73 |
private static ArrayList<PMSymbolMatch> buildMatches( |
| 74 |
ArrayList<PMSymbol> stale, ArrayList<PMSymbol> fresh) { |
| 75 |
ArrayList<PMSymbolMatch> result = new ArrayList<PMSymbolMatch>(); |
| 76 |
|
| 77 |
ArrayList<PMSymbol> staleNoMatch = (ArrayList<PMSymbol>) stale.clone(); |
| 78 |
|
| 79 |
// Gather all matches/non-matches of the fresh dsos against the stale. |
| 80 |
boolean added = false; |
| 81 |
for (PMSymbol freshElement : fresh) { |
| 82 |
added = true; |
| 83 |
for (PMSymbol staleElement : stale) { |
| 84 |
if (equals(freshElement, staleElement)) { |
| 85 |
result.add(new PMSymbolMatch(freshElement, staleElement)); |
| 86 |
staleNoMatch.remove(staleElement); |
| 87 |
added = false; |
| 88 |
break; |
| 89 |
} |
| 90 |
} |
| 91 |
// New dso with no match in the stale list |
| 92 |
if (added) { |
| 93 |
result.add(new PMSymbolMatch((PMSymbol) freshElement, null)); |
| 94 |
} |
| 95 |
} |
| 96 |
// Gather stale dsos with no match in the fresh list |
| 97 |
for (PMSymbol staleElement : staleNoMatch) { |
| 98 |
result.add(new PMSymbolMatch(null, (PMSymbol) staleElement)); |
| 99 |
} |
| 100 |
return result; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Build matches given two files |
| 105 |
* |
| 106 |
* @param staleData |
| 107 |
* older data file |
| 108 |
* @param freshData |
| 109 |
* newer data file |
| 110 |
* @return array of PMSymbolMatch's, each containing a PMSymbol match pair |
| 111 |
*/ |
| 112 |
public static ArrayList<PMSymbolMatch> buildResults(IFile staleData, |
| 113 |
IFile freshData) { |
| 114 |
ArrayList<PMSymbol> staleDsos = getSymbols(staleData); |
| 115 |
ArrayList<PMSymbol> freshDsos = getSymbols(freshData); |
| 116 |
if (staleDsos == null || freshDsos == null) { |
| 117 |
return null; |
| 118 |
} |
| 119 |
ArrayList<PMSymbolMatch> result = buildMatches(staleDsos, freshDsos); |
| 120 |
return result; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Check TreeParent's are equal (i.e. have the same ancestors) |
| 125 |
* |
| 126 |
* @param t1 |
| 127 |
* TreeParent element |
| 128 |
* @param t2 |
| 129 |
* TreeParent element |
| 130 |
* @return TreeParent's equality |
| 131 |
*/ |
| 132 |
public static boolean equals(TreeParent t1, TreeParent t2) { |
| 133 |
// base case (invisible root) |
| 134 |
if ("".equals(t1.getName()) && "".equals(t2.getName())) { |
| 135 |
return true; |
| 136 |
} else if (t1.equals(t2.getName())) { |
| 137 |
return equals(t1.getParent(), t2.getParent()); |
| 138 |
} else { |
| 139 |
return false; |
| 140 |
} |
| 141 |
} |
| 142 |
} |