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; | |
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; | |
31 | ||
32 | /** | |
33 | * Class to implement the critical path analysis | |
34 | * | |
35 | * @author Francis Giraldeau | |
36 | * @author Geneviève Bastien | |
37 | */ | |
38 | public 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 | ||
53 | private @Nullable TmfGraph fCriticalPath; | |
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)) { | |
88 | throw new IllegalStateException(); | |
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) { | |
141 | cancel(); | |
142 | resetAnalysis(); | |
143 | schedule(); | |
144 | } | |
145 | ||
146 | /** | |
147 | * The value of graph should be the id of the analysis module that builds | |
148 | * the required graph | |
149 | * | |
150 | * @param graphName | |
151 | * Id of the graph | |
152 | */ | |
153 | private void setGraph(String graphName) { | |
154 | ITmfTrace trace = getTrace(); | |
155 | if (trace == null) { | |
156 | return; | |
157 | } | |
158 | IAnalysisModule module = trace.getAnalysisModule(graphName); | |
159 | if (module instanceof TmfGraphBuilderModule) { | |
160 | fGraphModule = (TmfGraphBuilderModule) module; | |
161 | } | |
162 | } | |
163 | ||
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(); | |
169 | if (trace == null) { | |
170 | return fGraphModule; | |
171 | } | |
172 | for (TmfGraphBuilderModule mod : TmfTraceUtils.getAnalysisModulesOfClass(trace, TmfGraphBuilderModule.class)) { | |
173 | module = mod; | |
174 | break; | |
175 | } | |
176 | if (module != null) { | |
177 | fGraphModule = module; | |
178 | } | |
179 | } | |
180 | return module; | |
181 | } | |
182 | ||
183 | private static ICriticalPathAlgorithm getAlgorithm(TmfGraph graph) { | |
184 | return new CriticalPathAlgorithmBounded(graph); | |
185 | } | |
186 | ||
187 | @Override | |
188 | public boolean canExecute(@NonNull ITmfTrace trace) { | |
189 | /* | |
190 | * TODO: The critical path executes on a graph, so at least a graph must | |
191 | * be available for this trace | |
192 | */ | |
193 | return true; | |
194 | } | |
195 | ||
196 | /** | |
197 | * Gets the graph for the critical path | |
198 | * | |
199 | * @return The critical path graph | |
200 | */ | |
201 | public @Nullable TmfGraph getCriticalPath() { | |
202 | return fCriticalPath; | |
203 | } | |
204 | ||
205 | @Override | |
206 | protected @NonNull String getFullHelpText() { | |
207 | return NonNullUtils.nullToEmptyString(Messages.CriticalPathModule_fullHelpText); | |
208 | } | |
209 | ||
210 | @Override | |
211 | protected @NonNull String getShortHelpText(ITmfTrace trace) { | |
212 | return getFullHelpText(); | |
213 | } | |
214 | ||
215 | @Override | |
216 | protected @NonNull String getTraceCannotExecuteHelpText(@NonNull ITmfTrace trace) { | |
217 | return NonNullUtils.nullToEmptyString(Messages.CriticalPathModule_cantExecute); | |
218 | } | |
219 | ||
220 | } |