Commit | Line | Data |
---|---|---|
152630e0 BH |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | *******************************************************************************/ | |
9 | package org.eclipse.tracecompass.analysis.timing.core.segmentstore; | |
10 | ||
11 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; | |
12 | ||
13 | import java.io.IOException; | |
14 | import java.io.ObjectInputStream; | |
15 | import java.io.ObjectOutputStream; | |
16 | import java.nio.file.Files; | |
17 | import java.nio.file.Path; | |
18 | import java.nio.file.Paths; | |
76be6c00 | 19 | import java.util.ArrayList; |
18c18ee0 | 20 | import java.util.Collection; |
76be6c00 | 21 | import java.util.List; |
152630e0 BH |
22 | |
23 | import org.eclipse.core.runtime.IProgressMonitor; | |
76be6c00 | 24 | import org.eclipse.core.runtime.ListenerList; |
152630e0 BH |
25 | import org.eclipse.jdt.annotation.Nullable; |
26 | import org.eclipse.tracecompass.segmentstore.core.ISegment; | |
27 | import org.eclipse.tracecompass.segmentstore.core.ISegmentStore; | |
28 | import org.eclipse.tracecompass.segmentstore.core.treemap.TreeMapStore; | |
29 | import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule; | |
30 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; | |
31 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException; | |
32 | import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest; | |
33 | import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest; | |
18c18ee0 | 34 | import org.eclipse.tracecompass.tmf.core.segment.ISegmentAspect; |
152630e0 BH |
35 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
36 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; | |
37 | ||
18c18ee0 BH |
38 | import com.google.common.collect.ImmutableList; |
39 | ||
152630e0 BH |
40 | /** |
41 | * Abstract analysis module to generate a segment store. It is a base class that | |
42 | * can be used as a shortcut by analysis who just need to build a single segment | |
43 | * store. | |
44 | * | |
45 | * @author Bernd Hufmann | |
46 | * @since 2.0 | |
47 | * | |
48 | */ | |
49 | public abstract class AbstractSegmentStoreAnalysisModule extends TmfAbstractAnalysisModule { | |
50 | ||
76be6c00 | 51 | private final ListenerList fListeners = new ListenerList(ListenerList.IDENTITY); |
152630e0 BH |
52 | |
53 | private @Nullable ISegmentStore<ISegment> fSegmentStore; | |
54 | ||
55 | private @Nullable ITmfEventRequest fOngoingRequest = null; | |
56 | ||
57 | /** | |
58 | * Listener for the viewers | |
59 | * | |
60 | * @param listener | |
61 | * listener for each type of viewer | |
62 | */ | |
63 | public void addListener(IAnalysisProgressListener listener) { | |
64 | fListeners.add(listener); | |
65 | } | |
66 | ||
76be6c00 BH |
67 | /** |
68 | * Removes a listener for the viewers | |
69 | * | |
70 | * @param listener | |
71 | * listener for each type of viewer to remove | |
72 | */ | |
73 | public void removeListener(IAnalysisProgressListener listener) { | |
74 | fListeners.remove(listener); | |
75 | } | |
76 | ||
152630e0 BH |
77 | /** |
78 | * Returns all the listeners | |
07b705e8 | 79 | * |
76be6c00 | 80 | * @return latency listeners |
152630e0 | 81 | */ |
cabc09fe | 82 | protected Iterable<IAnalysisProgressListener> getListeners() { |
76be6c00 BH |
83 | List<IAnalysisProgressListener> listeners = new ArrayList<>(); |
84 | for (Object listener : fListeners.getListeners()) { | |
aa353506 AM |
85 | if (listener != null) { |
86 | listeners.add((IAnalysisProgressListener) listener); | |
87 | } | |
76be6c00 BH |
88 | } |
89 | return listeners; | |
152630e0 BH |
90 | } |
91 | ||
18c18ee0 BH |
92 | /** |
93 | * Return the pre-defined set of segment aspects exposed by this analysis. | |
94 | * | |
95 | * It should not be null, but could be empty. | |
07b705e8 | 96 | * |
18c18ee0 BH |
97 | * @return The segment aspects for this analysis |
98 | */ | |
99 | public Iterable<ISegmentAspect> getSegmentAspects() { | |
100 | Collection<ISegmentAspect> coll = ImmutableList.of(); | |
101 | return checkNotNull(coll); | |
102 | } | |
103 | ||
152630e0 BH |
104 | /** |
105 | * Returns the file name for storing segment store | |
07b705e8 MK |
106 | * |
107 | * @return segment store fine name, or null if you don't want a file | |
152630e0 | 108 | */ |
07b705e8 MK |
109 | protected @Nullable String getDataFileName() { |
110 | return null; | |
111 | } | |
152630e0 BH |
112 | |
113 | /** | |
114 | * Returns the analysis request for creating the segment store | |
07b705e8 | 115 | * |
152630e0 | 116 | * @param segmentStore |
07b705e8 | 117 | * a segment store to fill |
152630e0 BH |
118 | * @return the segment store analysis request implementation |
119 | */ | |
cabc09fe | 120 | protected abstract AbstractSegmentStoreAnalysisRequest createAnalysisRequest(ISegmentStore<ISegment> segmentStore); |
152630e0 BH |
121 | |
122 | /** | |
123 | * Read an object from the ObjectInputStream. | |
124 | * | |
125 | * @param ois | |
126 | * the ObjectInputStream to used | |
127 | * @return the read object | |
128 | * @throws ClassNotFoundException | |
129 | * - Class of a serialized object cannot be found. | |
130 | * @throws IOException | |
131 | * - Any of the usual Input/Output related exceptions. | |
132 | */ | |
133 | protected abstract Object[] readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException; | |
134 | ||
135 | /** | |
136 | * Returns the result in a from the analysis in a ISegmentStore | |
07b705e8 | 137 | * |
152630e0 BH |
138 | * @return Results from the analysis in a ISegmentStore |
139 | */ | |
140 | public @Nullable ISegmentStore<ISegment> getResults() { | |
141 | return fSegmentStore; | |
142 | } | |
143 | ||
144 | @Override | |
145 | protected void canceling() { | |
146 | ITmfEventRequest req = fOngoingRequest; | |
147 | if ((req != null) && (!req.isCompleted())) { | |
148 | req.cancel(); | |
149 | } | |
150 | } | |
151 | ||
73f1f3ea BH |
152 | @Override |
153 | public void dispose() { | |
154 | super.dispose(); | |
155 | ISegmentStore<ISegment> store = fSegmentStore; | |
156 | if (store != null) { | |
157 | store.dispose(); | |
158 | } | |
159 | } | |
160 | ||
152630e0 BH |
161 | @Override |
162 | protected boolean executeAnalysis(IProgressMonitor monitor) throws TmfAnalysisException { | |
163 | ITmfTrace trace = checkNotNull(getTrace()); | |
164 | ||
07b705e8 MK |
165 | final @Nullable String dataFileName = getDataFileName(); |
166 | if (dataFileName != null) { | |
167 | /* See if the data file already exists on disk */ | |
168 | String dir = TmfTraceManager.getSupplementaryFileDir(trace); | |
169 | final Path file = Paths.get(dir, dataFileName); | |
170 | ||
171 | if (Files.exists(file)) { | |
172 | /* Attempt to read the existing file */ | |
173 | try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(file))) { | |
174 | Object[] segmentArray = readObject(ois); | |
175 | final ISegmentStore<ISegment> store = new TreeMapStore<>(); | |
176 | for (Object element : segmentArray) { | |
177 | if (element instanceof ISegment) { | |
178 | ISegment segment = (ISegment) element; | |
179 | store.add(segment); | |
180 | } | |
181 | } | |
182 | fSegmentStore = store; | |
183 | for (IAnalysisProgressListener listener : getListeners()) { | |
184 | listener.onComplete(this, store); | |
185 | } | |
186 | return true; | |
187 | } catch (IOException | ClassNotFoundException | ClassCastException e) { | |
188 | /* | |
189 | * We did not manage to read the file successfully, we will | |
190 | * just fall-through to rebuild a new one. | |
191 | */ | |
192 | try { | |
193 | Files.delete(file); | |
194 | } catch (IOException e1) { | |
152630e0 | 195 | } |
152630e0 BH |
196 | } |
197 | } | |
198 | } | |
07b705e8 | 199 | ISegmentStore<ISegment> segmentStore = new TreeMapStore<>(); |
152630e0 BH |
200 | |
201 | /* Cancel an ongoing request */ | |
202 | ITmfEventRequest req = fOngoingRequest; | |
203 | if ((req != null) && (!req.isCompleted())) { | |
204 | req.cancel(); | |
205 | } | |
206 | ||
207 | /* Create a new request */ | |
07b705e8 | 208 | req = createAnalysisRequest(segmentStore); |
152630e0 BH |
209 | fOngoingRequest = req; |
210 | trace.sendRequest(req); | |
211 | ||
212 | try { | |
213 | req.waitForCompletion(); | |
214 | } catch (InterruptedException e) { | |
215 | } | |
216 | ||
217 | /* Do not process the results if the request was cancelled */ | |
218 | if (req.isCancelled() || req.isFailed()) { | |
219 | return false; | |
220 | } | |
221 | ||
222 | /* The request will fill 'syscalls' */ | |
07b705e8 MK |
223 | fSegmentStore = segmentStore; |
224 | ||
225 | if (dataFileName != null) { | |
226 | String dir = TmfTraceManager.getSupplementaryFileDir(trace); | |
227 | final Path file = Paths.get(dir, dataFileName); | |
152630e0 | 228 | |
07b705e8 MK |
229 | /* Serialize the collections to disk for future usage */ |
230 | try (ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(file))) { | |
231 | oos.writeObject(segmentStore.toArray()); | |
232 | } catch (IOException e) { | |
233 | /* | |
234 | * Didn't work, oh well. We will just re-read the trace next | |
235 | * time | |
236 | */ | |
237 | } | |
152630e0 BH |
238 | } |
239 | ||
240 | for (IAnalysisProgressListener listener : getListeners()) { | |
07b705e8 | 241 | listener.onComplete(this, segmentStore); |
152630e0 BH |
242 | } |
243 | ||
244 | return true; | |
245 | } | |
246 | ||
247 | /** | |
248 | * Abstract event request to fill a a segment store | |
249 | */ | |
cabc09fe | 250 | protected static abstract class AbstractSegmentStoreAnalysisRequest extends TmfEventRequest { |
152630e0 BH |
251 | |
252 | private final ISegmentStore<ISegment> fFullLatencyStore; | |
253 | ||
254 | /** | |
255 | * Constructor | |
256 | * | |
257 | * @param latencyStore | |
258 | * a latency segment store to fill | |
259 | */ | |
260 | public AbstractSegmentStoreAnalysisRequest(ISegmentStore<ISegment> latencyStore) { | |
261 | super(ITmfEvent.class, 0, ITmfEventRequest.ALL_DATA, ExecutionType.BACKGROUND); | |
262 | /* | |
263 | * We do NOT make a copy here! We want to modify the list that was | |
264 | * passed in parameter. | |
265 | */ | |
266 | fFullLatencyStore = latencyStore; | |
267 | } | |
268 | ||
269 | /** | |
270 | * Returns the segment store | |
07b705e8 | 271 | * |
152630e0 BH |
272 | * @return the segment store |
273 | */ | |
274 | public ISegmentStore<ISegment> getSegmentStore() { | |
275 | return fFullLatencyStore; | |
276 | } | |
277 | } | |
278 | } |