481d37cdb8ca7b05a065faf4cc5508fe79947eab
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.graph.core / src / org / eclipse / tracecompass / analysis / graph / core / criticalpath / CriticalPathModule.java
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
10 package org.eclipse.tracecompass.analysis.graph.core.criticalpath;
11
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;
29
30 /**
31 * Class to implement the critical path analysis
32 *
33 * @author Francis Giraldeau
34 * @author Geneviève Bastien
35 */
36 public class CriticalPathModule extends TmfAbstractAnalysisModule {
37
38 /**
39 * Analysis ID for this module
40 */
41 public static final String ANALYSIS_ID = "org.eclipse.tracecompass.analysis.graph.core.criticalpath"; //$NON-NLS-1$
42
43 /** Graph parameter name */
44 public static final String PARAM_GRAPH = "graph"; //$NON-NLS-1$
45
46 /** Worker_id parameter name */
47 public static final String PARAM_WORKER = "workerid"; //$NON-NLS-1$
48
49 private @Nullable TmfGraphBuilderModule fGraphModule;
50
51 private volatile @Nullable TmfGraph fCriticalPath;
52
53 /**
54 * Default constructor
55 */
56 public CriticalPathModule() {
57 super();
58 }
59
60 @Override
61 protected boolean executeAnalysis(final IProgressMonitor monitor) throws TmfAnalysisException {
62 /* Get the graph */
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$
66 return true;
67 }
68 graphModule.schedule();
69
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$
73 return false;
74 }
75 TmfGraph graph = graphModule.getGraph();
76 if (graph == null) {
77 throw new TmfAnalysisException("Critical Path analysis: graph " + graphModule.getName() + " is null"); //$NON-NLS-1$//$NON-NLS-2$
78 }
79
80 /* Get the worker id */
81 Object workerObj = getParameter(PARAM_WORKER);
82 if (workerObj == null) {
83 return false;
84 }
85 if (!(workerObj instanceof IGraphWorker)) {
86 throw new IllegalStateException("Worker parameter must be an IGraphWorker"); //$NON-NLS-1$
87 }
88 IGraphWorker worker = (IGraphWorker) workerObj;
89
90 TmfVertex head = graph.getHead(worker);
91 if (head == null) {
92 /* Nothing happens with this worker, return an empty graph */
93 fCriticalPath = new TmfGraph();
94 return true;
95 }
96
97 ICriticalPathAlgorithm cp = getAlgorithm(graph);
98 try {
99 fCriticalPath = cp.compute(head, null);
100 return true;
101 } catch (CriticalPathAlgorithmException e) {
102 Activator.getInstance().logError(NonNullUtils.nullToEmptyString(e.getMessage()), e);
103 }
104 return false;
105 }
106
107 @Override
108 protected void canceling() {
109
110 }
111
112 @Override
113 public @Nullable Object getParameter(String name) {
114 if (name.equals(PARAM_GRAPH)) {
115 return getGraph();
116 }
117 return super.getParameter(name);
118 }
119
120 @Override
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);
124 }
125 super.setParameter(name, value);
126 }
127
128 @Override
129 protected void parameterChanged(String name) {
130 fCriticalPath = null;
131 cancel();
132 resetAnalysis();
133 schedule();
134 }
135
136 /**
137 * The value of graph should be the id of the analysis module that builds
138 * the required graph
139 *
140 * @param graphName
141 * Id of the graph
142 */
143 private void setGraph(String graphName) {
144 ITmfTrace trace = getTrace();
145 if (trace == null) {
146 return;
147 }
148 IAnalysisModule module = trace.getAnalysisModule(graphName);
149 if (module instanceof TmfGraphBuilderModule) {
150 fGraphModule = (TmfGraphBuilderModule) module;
151 }
152 }
153
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();
159 if (trace == null) {
160 return fGraphModule;
161 }
162 for (TmfGraphBuilderModule mod : TmfTraceUtils.getAnalysisModulesOfClass(trace, TmfGraphBuilderModule.class)) {
163 module = mod;
164 break;
165 }
166 if (module != null) {
167 fGraphModule = module;
168 }
169 }
170 return module;
171 }
172
173 private static ICriticalPathAlgorithm getAlgorithm(TmfGraph graph) {
174 return new CriticalPathAlgorithmBounded(graph);
175 }
176
177 @Override
178 public boolean canExecute(@NonNull ITmfTrace trace) {
179 /*
180 * TODO: The critical path executes on a graph, so at least a graph must
181 * be available for this trace
182 */
183 return true;
184 }
185
186 /**
187 * Gets the graph for the critical path
188 *
189 * @return The critical path graph
190 */
191 public @Nullable TmfGraph getCriticalPath() {
192 return fCriticalPath;
193 }
194
195 @Override
196 protected @NonNull String getFullHelpText() {
197 return NonNullUtils.nullToEmptyString(Messages.CriticalPathModule_fullHelpText);
198 }
199
200 @Override
201 protected @NonNull String getShortHelpText(ITmfTrace trace) {
202 return getFullHelpText();
203 }
204
205 @Override
206 protected @NonNull String getTraceCannotExecuteHelpText(@NonNull ITmfTrace trace) {
207 return NonNullUtils.nullToEmptyString(Messages.CriticalPathModule_cantExecute);
208 }
209
210 }
This page took 0.034167 seconds and 4 git commands to generate.