Commit | Line | Data |
---|---|---|
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 | ||
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; | |
51480ca2 | 27 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
51480ca2 FG |
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 | */ | |
925f06a2 | 36 | public class CriticalPathModule extends TmfAbstractAnalysisModule implements ICriticalPathProvider { |
51480ca2 FG |
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 | ||
585f9916 | 51 | private volatile @Nullable TmfGraph fCriticalPath; |
51480ca2 FG |
52 | |
53 | /** | |
54 | * Default constructor | |
55 | */ | |
56 | public CriticalPathModule() { | |
57 | super(); | |
925f06a2 GB |
58 | addParameter(PARAM_GRAPH); |
59 | addParameter(PARAM_WORKER); | |
60 | setId(ANALYSIS_ID); | |
51480ca2 FG |
61 | } |
62 | ||
63 | @Override | |
64 | protected boolean executeAnalysis(final IProgressMonitor monitor) throws TmfAnalysisException { | |
65 | /* Get the graph */ | |
66 | TmfGraphBuilderModule graphModule = getGraph(); | |
67 | if (graphModule == null) { | |
68 | Activator.getInstance().logWarning("No graph was found to execute the critical path on"); //$NON-NLS-1$ | |
69 | return true; | |
70 | } | |
71 | graphModule.schedule(); | |
72 | ||
73 | monitor.setTaskName(NLS.bind(Messages.CriticalPathModule_waitingForGraph, graphModule.getName())); | |
74 | if (!graphModule.waitForCompletion(monitor)) { | |
75 | Activator.getInstance().logInfo("Critical path execution: graph building was cancelled. Results may not be accurate."); //$NON-NLS-1$ | |
76 | return false; | |
77 | } | |
78 | TmfGraph graph = graphModule.getGraph(); | |
79 | if (graph == null) { | |
80 | throw new TmfAnalysisException("Critical Path analysis: graph " + graphModule.getName() + " is null"); //$NON-NLS-1$//$NON-NLS-2$ | |
81 | } | |
82 | ||
83 | /* Get the worker id */ | |
84 | Object workerObj = getParameter(PARAM_WORKER); | |
85 | if (workerObj == null) { | |
86 | return false; | |
87 | } | |
88 | if (!(workerObj instanceof IGraphWorker)) { | |
9ee366e2 | 89 | throw new IllegalStateException("Worker parameter must be an IGraphWorker"); //$NON-NLS-1$ |
51480ca2 FG |
90 | } |
91 | IGraphWorker worker = (IGraphWorker) workerObj; | |
92 | ||
93 | TmfVertex head = graph.getHead(worker); | |
94 | if (head == null) { | |
95 | /* Nothing happens with this worker, return an empty graph */ | |
96 | fCriticalPath = new TmfGraph(); | |
97 | return true; | |
98 | } | |
0039d5e8 | 99 | |
51480ca2 | 100 | ICriticalPathAlgorithm cp = getAlgorithm(graph); |
da4232b4 | 101 | try { |
0039d5e8 | 102 | fCriticalPath = cp.compute(head, null); |
da4232b4 MK |
103 | return true; |
104 | } catch (CriticalPathAlgorithmException e) { | |
105 | Activator.getInstance().logError(NonNullUtils.nullToEmptyString(e.getMessage()), e); | |
106 | } | |
107 | return false; | |
51480ca2 FG |
108 | } |
109 | ||
110 | @Override | |
111 | protected void canceling() { | |
112 | ||
113 | } | |
114 | ||
115 | @Override | |
3127c6b8 | 116 | public @Nullable synchronized Object getParameter(String name) { |
51480ca2 FG |
117 | if (name.equals(PARAM_GRAPH)) { |
118 | return getGraph(); | |
119 | } | |
120 | return super.getParameter(name); | |
121 | } | |
122 | ||
123 | @Override | |
124 | public synchronized void setParameter(String name, @Nullable Object value) throws RuntimeException { | |
125 | if (name.equals(PARAM_GRAPH) && (value instanceof String)) { | |
126 | setGraph((String) value); | |
127 | } | |
128 | super.setParameter(name, value); | |
129 | } | |
130 | ||
131 | @Override | |
132 | protected void parameterChanged(String name) { | |
585f9916 | 133 | fCriticalPath = null; |
51480ca2 FG |
134 | cancel(); |
135 | resetAnalysis(); | |
136 | schedule(); | |
137 | } | |
138 | ||
139 | /** | |
140 | * The value of graph should be the id of the analysis module that builds | |
141 | * the required graph | |
142 | * | |
143 | * @param graphName | |
144 | * Id of the graph | |
145 | */ | |
146 | private void setGraph(String graphName) { | |
147 | ITmfTrace trace = getTrace(); | |
148 | if (trace == null) { | |
149 | return; | |
150 | } | |
151 | IAnalysisModule module = trace.getAnalysisModule(graphName); | |
152 | if (module instanceof TmfGraphBuilderModule) { | |
153 | fGraphModule = (TmfGraphBuilderModule) module; | |
154 | } | |
155 | } | |
156 | ||
157 | private @Nullable TmfGraphBuilderModule getGraph() { | |
158 | /* The graph module is null, take the first available graph if any */ | |
159 | TmfGraphBuilderModule module = fGraphModule; | |
160 | if (module == null) { | |
161 | ITmfTrace trace = getTrace(); | |
162 | if (trace == null) { | |
163 | return fGraphModule; | |
164 | } | |
165 | for (TmfGraphBuilderModule mod : TmfTraceUtils.getAnalysisModulesOfClass(trace, TmfGraphBuilderModule.class)) { | |
166 | module = mod; | |
167 | break; | |
168 | } | |
169 | if (module != null) { | |
170 | fGraphModule = module; | |
171 | } | |
172 | } | |
173 | return module; | |
174 | } | |
175 | ||
176 | private static ICriticalPathAlgorithm getAlgorithm(TmfGraph graph) { | |
177 | return new CriticalPathAlgorithmBounded(graph); | |
178 | } | |
179 | ||
180 | @Override | |
181 | public boolean canExecute(@NonNull ITmfTrace trace) { | |
182 | /* | |
183 | * TODO: The critical path executes on a graph, so at least a graph must | |
184 | * be available for this trace | |
185 | */ | |
186 | return true; | |
187 | } | |
188 | ||
189 | /** | |
190 | * Gets the graph for the critical path | |
191 | * | |
192 | * @return The critical path graph | |
193 | */ | |
925f06a2 | 194 | @Override |
51480ca2 FG |
195 | public @Nullable TmfGraph getCriticalPath() { |
196 | return fCriticalPath; | |
197 | } | |
198 | ||
199 | @Override | |
200 | protected @NonNull String getFullHelpText() { | |
201 | return NonNullUtils.nullToEmptyString(Messages.CriticalPathModule_fullHelpText); | |
202 | } | |
203 | ||
204 | @Override | |
205 | protected @NonNull String getShortHelpText(ITmfTrace trace) { | |
206 | return getFullHelpText(); | |
207 | } | |
208 | ||
209 | @Override | |
210 | protected @NonNull String getTraceCannotExecuteHelpText(@NonNull ITmfTrace trace) { | |
211 | return NonNullUtils.nullToEmptyString(Messages.CriticalPathModule_cantExecute); | |
212 | } | |
213 | ||
214 | } |