tmf : extract duplicate code into sortNodes() in GraphNode
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.graph.core / src / org / eclipse / tracecompass / analysis / graph / core / criticalpath / CriticalPathModule.java
CommitLineData
51480ca2
FG
1/*******************************************************************************
2 * Copyright (c) 2015 École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9
10package org.eclipse.tracecompass.analysis.graph.core.criticalpath;
11
12import org.eclipse.core.runtime.IProgressMonitor;
13import org.eclipse.jdt.annotation.NonNull;
14import org.eclipse.jdt.annotation.Nullable;
15import org.eclipse.osgi.util.NLS;
16import org.eclipse.tracecompass.analysis.graph.core.base.IGraphWorker;
17import org.eclipse.tracecompass.analysis.graph.core.base.TmfGraph;
18import org.eclipse.tracecompass.analysis.graph.core.base.TmfVertex;
19import org.eclipse.tracecompass.analysis.graph.core.building.TmfGraphBuilderModule;
20import org.eclipse.tracecompass.common.core.NonNullUtils;
21import org.eclipse.tracecompass.internal.analysis.graph.core.Activator;
22import org.eclipse.tracecompass.internal.analysis.graph.core.criticalpath.CriticalPathAlgorithmBounded;
23import org.eclipse.tracecompass.internal.analysis.graph.core.criticalpath.Messages;
24import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
25import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
26import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
27import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
28import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
29import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
30import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
31
32/**
33 * Class to implement the critical path analysis
34 *
35 * @author Francis Giraldeau
36 * @author Geneviève Bastien
37 */
38public class CriticalPathModule extends TmfAbstractAnalysisModule {
39
40 /**
41 * Analysis ID for this module
42 */
43 public static final String ANALYSIS_ID = "org.eclipse.tracecompass.analysis.graph.core.criticalpath"; //$NON-NLS-1$
44
45 /** Graph parameter name */
46 public static final String PARAM_GRAPH = "graph"; //$NON-NLS-1$
47
48 /** Worker_id parameter name */
49 public static final String PARAM_WORKER = "workerid"; //$NON-NLS-1$
50
51 private @Nullable TmfGraphBuilderModule fGraphModule;
52
53 private @Nullable TmfGraph fCriticalPath;
54
55 /**
56 * Default constructor
57 */
58 public CriticalPathModule() {
59 super();
60 }
61
62 @Override
63 protected boolean executeAnalysis(final IProgressMonitor monitor) throws TmfAnalysisException {
64 /* Get the graph */
65 TmfGraphBuilderModule graphModule = getGraph();
66 if (graphModule == null) {
67 Activator.getInstance().logWarning("No graph was found to execute the critical path on"); //$NON-NLS-1$
68 return true;
69 }
70 graphModule.schedule();
71
72 monitor.setTaskName(NLS.bind(Messages.CriticalPathModule_waitingForGraph, graphModule.getName()));
73 if (!graphModule.waitForCompletion(monitor)) {
74 Activator.getInstance().logInfo("Critical path execution: graph building was cancelled. Results may not be accurate."); //$NON-NLS-1$
75 return false;
76 }
77 TmfGraph graph = graphModule.getGraph();
78 if (graph == null) {
79 throw new TmfAnalysisException("Critical Path analysis: graph " + graphModule.getName() + " is null"); //$NON-NLS-1$//$NON-NLS-2$
80 }
81
82 /* Get the worker id */
83 Object workerObj = getParameter(PARAM_WORKER);
84 if (workerObj == null) {
85 return false;
86 }
87 if (!(workerObj instanceof IGraphWorker)) {
88 throw new IllegalStateException();
89 }
90 IGraphWorker worker = (IGraphWorker) workerObj;
91
92 TmfVertex head = graph.getHead(worker);
93 if (head == null) {
94 /* Nothing happens with this worker, return an empty graph */
95 fCriticalPath = new TmfGraph();
96 return true;
97 }
98 TmfTimeRange tr = TmfTraceManager.getInstance().getCurrentTraceContext().getWindowRange();
99 TmfVertex start = graph.getVertexAt(tr.getStartTime(), worker);
100 if (start == null) {
101 /*
102 * Nothing happens with this worker after start, return an empty
103 * graph
104 */
105 fCriticalPath = new TmfGraph();
106 return true;
107 }
108 ICriticalPathAlgorithm cp = getAlgorithm(graph);
109 fCriticalPath = cp.compute(start, null);
110
111 return true;
112 }
113
114 @Override
115 protected void canceling() {
116
117 }
118
119 @Override
120 public @Nullable Object getParameter(String name) {
121 if (name.equals(PARAM_GRAPH)) {
122 return getGraph();
123 }
124 return super.getParameter(name);
125 }
126
127 @Override
128 public synchronized void setParameter(String name, @Nullable Object value) throws RuntimeException {
129 if (name.equals(PARAM_GRAPH) && (value instanceof String)) {
130 setGraph((String) value);
131 }
132 super.setParameter(name, value);
133 }
134
135 @Override
136 protected void parameterChanged(String name) {
137 cancel();
138 resetAnalysis();
139 schedule();
140 }
141
142 /**
143 * The value of graph should be the id of the analysis module that builds
144 * the required graph
145 *
146 * @param graphName
147 * Id of the graph
148 */
149 private void setGraph(String graphName) {
150 ITmfTrace trace = getTrace();
151 if (trace == null) {
152 return;
153 }
154 IAnalysisModule module = trace.getAnalysisModule(graphName);
155 if (module instanceof TmfGraphBuilderModule) {
156 fGraphModule = (TmfGraphBuilderModule) module;
157 }
158 }
159
160 private @Nullable TmfGraphBuilderModule getGraph() {
161 /* The graph module is null, take the first available graph if any */
162 TmfGraphBuilderModule module = fGraphModule;
163 if (module == null) {
164 ITmfTrace trace = getTrace();
165 if (trace == null) {
166 return fGraphModule;
167 }
168 for (TmfGraphBuilderModule mod : TmfTraceUtils.getAnalysisModulesOfClass(trace, TmfGraphBuilderModule.class)) {
169 module = mod;
170 break;
171 }
172 if (module != null) {
173 fGraphModule = module;
174 }
175 }
176 return module;
177 }
178
179 private static ICriticalPathAlgorithm getAlgorithm(TmfGraph graph) {
180 return new CriticalPathAlgorithmBounded(graph);
181 }
182
183 @Override
184 public boolean canExecute(@NonNull ITmfTrace trace) {
185 /*
186 * TODO: The critical path executes on a graph, so at least a graph must
187 * be available for this trace
188 */
189 return true;
190 }
191
192 /**
193 * Gets the graph for the critical path
194 *
195 * @return The critical path graph
196 */
197 public @Nullable TmfGraph getCriticalPath() {
198 return fCriticalPath;
199 }
200
201 @Override
202 protected @NonNull String getFullHelpText() {
203 return NonNullUtils.nullToEmptyString(Messages.CriticalPathModule_fullHelpText);
204 }
205
206 @Override
207 protected @NonNull String getShortHelpText(ITmfTrace trace) {
208 return getFullHelpText();
209 }
210
211 @Override
212 protected @NonNull String getTraceCannotExecuteHelpText(@NonNull ITmfTrace trace) {
213 return NonNullUtils.nullToEmptyString(Messages.CriticalPathModule_cantExecute);
214 }
215
216}
This page took 0.044878 seconds and 5 git commands to generate.