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
.trace
.ITmfTrace
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
31 * Class to implement the critical path analysis
33 * @author Francis Giraldeau
34 * @author Geneviève Bastien
36 public class CriticalPathModule
extends TmfAbstractAnalysisModule
{
39 * Analysis ID for this module
41 public static final String ANALYSIS_ID
= "org.eclipse.tracecompass.analysis.graph.core.criticalpath"; //$NON-NLS-1$
43 /** Graph parameter name */
44 public static final String PARAM_GRAPH
= "graph"; //$NON-NLS-1$
46 /** Worker_id parameter name */
47 public static final String PARAM_WORKER
= "workerid"; //$NON-NLS-1$
49 private @Nullable TmfGraphBuilderModule fGraphModule
;
51 private volatile @Nullable TmfGraph fCriticalPath
;
56 public CriticalPathModule() {
61 protected boolean executeAnalysis(final IProgressMonitor monitor
) throws TmfAnalysisException
{
63 TmfGraphBuilderModule graphModule
= getGraph();
64 if (graphModule
== null) {
65 Activator
.getInstance().logWarning("No graph was found to execute the critical path on"); //$NON-NLS-1$
68 graphModule
.schedule();
70 monitor
.setTaskName(NLS
.bind(Messages
.CriticalPathModule_waitingForGraph
, graphModule
.getName()));
71 if (!graphModule
.waitForCompletion(monitor
)) {
72 Activator
.getInstance().logInfo("Critical path execution: graph building was cancelled. Results may not be accurate."); //$NON-NLS-1$
75 TmfGraph graph
= graphModule
.getGraph();
77 throw new TmfAnalysisException("Critical Path analysis: graph " + graphModule
.getName() + " is null"); //$NON-NLS-1$//$NON-NLS-2$
80 /* Get the worker id */
81 Object workerObj
= getParameter(PARAM_WORKER
);
82 if (workerObj
== null) {
85 if (!(workerObj
instanceof IGraphWorker
)) {
86 throw new IllegalStateException("Worker parameter must be an IGraphWorker"); //$NON-NLS-1$
88 IGraphWorker worker
= (IGraphWorker
) workerObj
;
90 TmfVertex head
= graph
.getHead(worker
);
92 /* Nothing happens with this worker, return an empty graph */
93 fCriticalPath
= new TmfGraph();
97 ICriticalPathAlgorithm cp
= getAlgorithm(graph
);
99 fCriticalPath
= cp
.compute(head
, null);
101 } catch (CriticalPathAlgorithmException e
) {
102 Activator
.getInstance().logError(NonNullUtils
.nullToEmptyString(e
.getMessage()), e
);
108 protected void canceling() {
113 public @Nullable Object
getParameter(String name
) {
114 if (name
.equals(PARAM_GRAPH
)) {
117 return super.getParameter(name
);
121 public synchronized void setParameter(String name
, @Nullable Object value
) throws RuntimeException
{
122 if (name
.equals(PARAM_GRAPH
) && (value
instanceof String
)) {
123 setGraph((String
) value
);
125 super.setParameter(name
, value
);
129 protected void parameterChanged(String name
) {
130 fCriticalPath
= null;
137 * The value of graph should be the id of the analysis module that builds
143 private void setGraph(String graphName
) {
144 ITmfTrace trace
= getTrace();
148 IAnalysisModule module
= trace
.getAnalysisModule(graphName
);
149 if (module
instanceof TmfGraphBuilderModule
) {
150 fGraphModule
= (TmfGraphBuilderModule
) module
;
154 private @Nullable TmfGraphBuilderModule
getGraph() {
155 /* The graph module is null, take the first available graph if any */
156 TmfGraphBuilderModule module
= fGraphModule
;
157 if (module
== null) {
158 ITmfTrace trace
= getTrace();
162 for (TmfGraphBuilderModule mod
: TmfTraceUtils
.getAnalysisModulesOfClass(trace
, TmfGraphBuilderModule
.class)) {
166 if (module
!= null) {
167 fGraphModule
= module
;
173 private static ICriticalPathAlgorithm
getAlgorithm(TmfGraph graph
) {
174 return new CriticalPathAlgorithmBounded(graph
);
178 public boolean canExecute(@NonNull ITmfTrace trace
) {
180 * TODO: The critical path executes on a graph, so at least a graph must
181 * be available for this trace
187 * Gets the graph for the critical path
189 * @return The critical path graph
191 public @Nullable TmfGraph
getCriticalPath() {
192 return fCriticalPath
;
196 protected @NonNull String
getFullHelpText() {
197 return NonNullUtils
.nullToEmptyString(Messages
.CriticalPathModule_fullHelpText
);
201 protected @NonNull String
getShortHelpText(ITmfTrace trace
) {
202 return getFullHelpText();
206 protected @NonNull String
getTraceCannotExecuteHelpText(@NonNull ITmfTrace trace
) {
207 return NonNullUtils
.nullToEmptyString(Messages
.CriticalPathModule_cantExecute
);