critical path: bug 489360 Build the critical path in a separate thread
[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
585f9916 53 private volatile @Nullable TmfGraph fCriticalPath;
51480ca2
FG
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)) {
9ee366e2 88 throw new IllegalStateException("Worker parameter must be an IGraphWorker"); //$NON-NLS-1$
51480ca2
FG
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);
da4232b4
MK
109 try {
110 fCriticalPath = cp.compute(start, null);
111 return true;
112 } catch (CriticalPathAlgorithmException e) {
113 Activator.getInstance().logError(NonNullUtils.nullToEmptyString(e.getMessage()), e);
114 }
115 return false;
51480ca2
FG
116 }
117
118 @Override
119 protected void canceling() {
120
121 }
122
123 @Override
124 public @Nullable Object getParameter(String name) {
125 if (name.equals(PARAM_GRAPH)) {
126 return getGraph();
127 }
128 return super.getParameter(name);
129 }
130
131 @Override
132 public synchronized void setParameter(String name, @Nullable Object value) throws RuntimeException {
133 if (name.equals(PARAM_GRAPH) && (value instanceof String)) {
134 setGraph((String) value);
135 }
136 super.setParameter(name, value);
137 }
138
139 @Override
140 protected void parameterChanged(String name) {
585f9916 141 fCriticalPath = null;
51480ca2
FG
142 cancel();
143 resetAnalysis();
144 schedule();
145 }
146
147 /**
148 * The value of graph should be the id of the analysis module that builds
149 * the required graph
150 *
151 * @param graphName
152 * Id of the graph
153 */
154 private void setGraph(String graphName) {
155 ITmfTrace trace = getTrace();
156 if (trace == null) {
157 return;
158 }
159 IAnalysisModule module = trace.getAnalysisModule(graphName);
160 if (module instanceof TmfGraphBuilderModule) {
161 fGraphModule = (TmfGraphBuilderModule) module;
162 }
163 }
164
165 private @Nullable TmfGraphBuilderModule getGraph() {
166 /* The graph module is null, take the first available graph if any */
167 TmfGraphBuilderModule module = fGraphModule;
168 if (module == null) {
169 ITmfTrace trace = getTrace();
170 if (trace == null) {
171 return fGraphModule;
172 }
173 for (TmfGraphBuilderModule mod : TmfTraceUtils.getAnalysisModulesOfClass(trace, TmfGraphBuilderModule.class)) {
174 module = mod;
175 break;
176 }
177 if (module != null) {
178 fGraphModule = module;
179 }
180 }
181 return module;
182 }
183
184 private static ICriticalPathAlgorithm getAlgorithm(TmfGraph graph) {
185 return new CriticalPathAlgorithmBounded(graph);
186 }
187
188 @Override
189 public boolean canExecute(@NonNull ITmfTrace trace) {
190 /*
191 * TODO: The critical path executes on a graph, so at least a graph must
192 * be available for this trace
193 */
194 return true;
195 }
196
197 /**
198 * Gets the graph for the critical path
199 *
200 * @return The critical path graph
201 */
202 public @Nullable TmfGraph getCriticalPath() {
203 return fCriticalPath;
204 }
205
206 @Override
207 protected @NonNull String getFullHelpText() {
208 return NonNullUtils.nullToEmptyString(Messages.CriticalPathModule_fullHelpText);
209 }
210
211 @Override
212 protected @NonNull String getShortHelpText(ITmfTrace trace) {
213 return getFullHelpText();
214 }
215
216 @Override
217 protected @NonNull String getTraceCannotExecuteHelpText(@NonNull ITmfTrace trace) {
218 return NonNullUtils.nullToEmptyString(Messages.CriticalPathModule_cantExecute);
219 }
220
221}
This page took 0.039809 seconds and 5 git commands to generate.