1 /*******************************************************************************
2 * Copyright (c) 2015 École Polytechnique de Montréal
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.analysis
.graph
.core
.criticalpath
;
12 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
13 import org
.eclipse
.jdt
.annotation
.NonNull
;
14 import org
.eclipse
.jdt
.annotation
.Nullable
;
15 import org
.eclipse
.osgi
.util
.NLS
;
16 import org
.eclipse
.tracecompass
.analysis
.graph
.core
.base
.IGraphWorker
;
17 import org
.eclipse
.tracecompass
.analysis
.graph
.core
.base
.TmfGraph
;
18 import org
.eclipse
.tracecompass
.analysis
.graph
.core
.base
.TmfVertex
;
19 import org
.eclipse
.tracecompass
.analysis
.graph
.core
.building
.TmfGraphBuilderModule
;
20 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
21 import org
.eclipse
.tracecompass
.internal
.analysis
.graph
.core
.Activator
;
22 import org
.eclipse
.tracecompass
.internal
.analysis
.graph
.core
.criticalpath
.CriticalPathAlgorithmBounded
;
23 import org
.eclipse
.tracecompass
.internal
.analysis
.graph
.core
.criticalpath
.Messages
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAbstractAnalysisModule
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
33 * Class to implement the critical path analysis
35 * @author Francis Giraldeau
36 * @author Geneviève Bastien
38 public class CriticalPathModule
extends TmfAbstractAnalysisModule
{
41 * Analysis ID for this module
43 public static final String ANALYSIS_ID
= "org.eclipse.tracecompass.analysis.graph.core.criticalpath"; //$NON-NLS-1$
45 /** Graph parameter name */
46 public static final String PARAM_GRAPH
= "graph"; //$NON-NLS-1$
48 /** Worker_id parameter name */
49 public static final String PARAM_WORKER
= "workerid"; //$NON-NLS-1$
51 private @Nullable TmfGraphBuilderModule fGraphModule
;
53 private @Nullable TmfGraph fCriticalPath
;
58 public CriticalPathModule() {
63 protected boolean executeAnalysis(final IProgressMonitor monitor
) throws TmfAnalysisException
{
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$
70 graphModule
.schedule();
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$
77 TmfGraph graph
= graphModule
.getGraph();
79 throw new TmfAnalysisException("Critical Path analysis: graph " + graphModule
.getName() + " is null"); //$NON-NLS-1$//$NON-NLS-2$
82 /* Get the worker id */
83 Object workerObj
= getParameter(PARAM_WORKER
);
84 if (workerObj
== null) {
87 if (!(workerObj
instanceof IGraphWorker
)) {
88 throw new IllegalStateException();
90 IGraphWorker worker
= (IGraphWorker
) workerObj
;
92 TmfVertex head
= graph
.getHead(worker
);
94 /* Nothing happens with this worker, return an empty graph */
95 fCriticalPath
= new TmfGraph();
98 TmfTimeRange tr
= TmfTraceManager
.getInstance().getCurrentTraceContext().getWindowRange();
99 TmfVertex start
= graph
.getVertexAt(tr
.getStartTime(), worker
);
102 * Nothing happens with this worker after start, return an empty
105 fCriticalPath
= new TmfGraph();
108 ICriticalPathAlgorithm cp
= getAlgorithm(graph
);
110 fCriticalPath
= cp
.compute(start
, null);
112 } catch (CriticalPathAlgorithmException e
) {
113 Activator
.getInstance().logError(NonNullUtils
.nullToEmptyString(e
.getMessage()), e
);
119 protected void canceling() {
124 public @Nullable Object
getParameter(String name
) {
125 if (name
.equals(PARAM_GRAPH
)) {
128 return super.getParameter(name
);
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
);
136 super.setParameter(name
, value
);
140 protected void parameterChanged(String name
) {
147 * The value of graph should be the id of the analysis module that builds
153 private void setGraph(String graphName
) {
154 ITmfTrace trace
= getTrace();
158 IAnalysisModule module
= trace
.getAnalysisModule(graphName
);
159 if (module
instanceof TmfGraphBuilderModule
) {
160 fGraphModule
= (TmfGraphBuilderModule
) module
;
164 private @Nullable TmfGraphBuilderModule
getGraph() {
165 /* The graph module is null, take the first available graph if any */
166 TmfGraphBuilderModule module
= fGraphModule
;
167 if (module
== null) {
168 ITmfTrace trace
= getTrace();
172 for (TmfGraphBuilderModule mod
: TmfTraceUtils
.getAnalysisModulesOfClass(trace
, TmfGraphBuilderModule
.class)) {
176 if (module
!= null) {
177 fGraphModule
= module
;
183 private static ICriticalPathAlgorithm
getAlgorithm(TmfGraph graph
) {
184 return new CriticalPathAlgorithmBounded(graph
);
188 public boolean canExecute(@NonNull ITmfTrace trace
) {
190 * TODO: The critical path executes on a graph, so at least a graph must
191 * be available for this trace
197 * Gets the graph for the critical path
199 * @return The critical path graph
201 public @Nullable TmfGraph
getCriticalPath() {
202 return fCriticalPath
;
206 protected @NonNull String
getFullHelpText() {
207 return NonNullUtils
.nullToEmptyString(Messages
.CriticalPathModule_fullHelpText
);
211 protected @NonNull String
getShortHelpText(ITmfTrace trace
) {
212 return getFullHelpText();
216 protected @NonNull String
getTraceCannotExecuteHelpText(@NonNull ITmfTrace trace
) {
217 return NonNullUtils
.nullToEmptyString(Messages
.CriticalPathModule_cantExecute
);