lttng: Add possibility to remove latency listener from latency view
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.timing.core / src / org / eclipse / tracecompass / analysis / timing / core / segmentstore / AbstractSegmentStoreAnalysisModule.java
CommitLineData
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 *******************************************************************************/
9package org.eclipse.tracecompass.analysis.timing.core.segmentstore;
10
11import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
12
13import java.io.IOException;
14import java.io.ObjectInputStream;
15import java.io.ObjectOutputStream;
16import java.nio.file.Files;
17import java.nio.file.Path;
18import java.nio.file.Paths;
76be6c00 19import java.util.ArrayList;
18c18ee0 20import java.util.Collection;
76be6c00 21import java.util.List;
152630e0
BH
22
23import org.eclipse.core.runtime.IProgressMonitor;
76be6c00 24import org.eclipse.core.runtime.ListenerList;
152630e0
BH
25import org.eclipse.jdt.annotation.Nullable;
26import org.eclipse.tracecompass.segmentstore.core.ISegment;
27import org.eclipse.tracecompass.segmentstore.core.ISegmentStore;
28import org.eclipse.tracecompass.segmentstore.core.treemap.TreeMapStore;
29import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
30import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
31import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
32import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
33import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
18c18ee0 34import org.eclipse.tracecompass.tmf.core.segment.ISegmentAspect;
152630e0
BH
35import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
36import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
37
18c18ee0
BH
38import 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 */
49public 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
76be6c00 79 * @return latency listeners
152630e0 80 */
cabc09fe 81 protected Iterable<IAnalysisProgressListener> getListeners() {
76be6c00
BH
82 List<IAnalysisProgressListener> listeners = new ArrayList<>();
83 for (Object listener : fListeners.getListeners()) {
84 listeners.add((IAnalysisProgressListener) listener);
85 }
86 return listeners;
152630e0
BH
87 }
88
18c18ee0
BH
89 /**
90 * Return the pre-defined set of segment aspects exposed by this analysis.
91 *
92 * It should not be null, but could be empty.
93 * @return The segment aspects for this analysis
94 */
95 public Iterable<ISegmentAspect> getSegmentAspects() {
96 Collection<ISegmentAspect> coll = ImmutableList.of();
97 return checkNotNull(coll);
98 }
99
152630e0
BH
100 /**
101 * Returns the file name for storing segment store
102 * @return segment store fine name
103 */
cabc09fe 104 protected abstract String getDataFileName();
152630e0
BH
105
106 /**
107 * Returns the analysis request for creating the segment store
108 * @param segmentStore
109 * a segment store to fill
110 * @return the segment store analysis request implementation
111 */
cabc09fe 112 protected abstract AbstractSegmentStoreAnalysisRequest createAnalysisRequest(ISegmentStore<ISegment> segmentStore);
152630e0
BH
113
114 /**
115 * Read an object from the ObjectInputStream.
116 *
117 * @param ois
118 * the ObjectInputStream to used
119 * @return the read object
120 * @throws ClassNotFoundException
121 * - Class of a serialized object cannot be found.
122 * @throws IOException
123 * - Any of the usual Input/Output related exceptions.
124 */
125 protected abstract Object[] readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException;
126
127 /**
128 * Returns the result in a from the analysis in a ISegmentStore
129 * @return Results from the analysis in a ISegmentStore
130 */
131 public @Nullable ISegmentStore<ISegment> getResults() {
132 return fSegmentStore;
133 }
134
135 @Override
136 protected void canceling() {
137 ITmfEventRequest req = fOngoingRequest;
138 if ((req != null) && (!req.isCompleted())) {
139 req.cancel();
140 }
141 }
142
73f1f3ea
BH
143 @Override
144 public void dispose() {
145 super.dispose();
146 ISegmentStore<ISegment> store = fSegmentStore;
147 if (store != null) {
148 store.dispose();
149 }
150 }
151
152630e0
BH
152 @Override
153 protected boolean executeAnalysis(IProgressMonitor monitor) throws TmfAnalysisException {
154 ITmfTrace trace = checkNotNull(getTrace());
155
156 /* See if the data file already exists on disk */
157 String dir = TmfTraceManager.getSupplementaryFileDir(trace);
158 final Path file = Paths.get(dir, getDataFileName());
159
160 if (Files.exists(file)) {
161 /* Attempt to read the existing file */
162 try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(file))) {
163 Object[] segmentArray = readObject(ois);
164 final ISegmentStore<ISegment> store = new TreeMapStore<>();
165 for (Object element : segmentArray) {
166 if (element instanceof ISegment) {
167 ISegment segment = (ISegment) element;
168 store.add(segment);
169 }
170 }
171 fSegmentStore = store;
172 for (IAnalysisProgressListener listener : getListeners()) {
173 listener.onComplete(this, store);
174 }
175 return true;
176 } catch (IOException | ClassNotFoundException | ClassCastException e) {
177 /*
178 * We did not manage to read the file successfully, we will just
179 * fall-through to rebuild a new one.
180 */
181 try {
182 Files.delete(file);
183 } catch (IOException e1) {
184 }
185 }
186 }
187
188 ISegmentStore<ISegment> syscalls = new TreeMapStore<>();
189
190 /* Cancel an ongoing request */
191 ITmfEventRequest req = fOngoingRequest;
192 if ((req != null) && (!req.isCompleted())) {
193 req.cancel();
194 }
195
196 /* Create a new request */
197 req = createAnalysisRequest(syscalls);
198 fOngoingRequest = req;
199 trace.sendRequest(req);
200
201 try {
202 req.waitForCompletion();
203 } catch (InterruptedException e) {
204 }
205
206 /* Do not process the results if the request was cancelled */
207 if (req.isCancelled() || req.isFailed()) {
208 return false;
209 }
210
211 /* The request will fill 'syscalls' */
212 fSegmentStore = syscalls;
213
214 /* Serialize the collections to disk for future usage */
215 try (ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(file))) {
216 oos.writeObject(syscalls.toArray());
217 } catch (IOException e) {
218 /* Didn't work, oh well. We will just re-read the trace next time */
219 }
220
221 for (IAnalysisProgressListener listener : getListeners()) {
222 listener.onComplete(this, syscalls);
223 }
224
225 return true;
226 }
227
228 /**
229 * Abstract event request to fill a a segment store
230 */
cabc09fe 231 protected static abstract class AbstractSegmentStoreAnalysisRequest extends TmfEventRequest {
152630e0
BH
232
233 private final ISegmentStore<ISegment> fFullLatencyStore;
234
235 /**
236 * Constructor
237 *
238 * @param latencyStore
239 * a latency segment store to fill
240 */
241 public AbstractSegmentStoreAnalysisRequest(ISegmentStore<ISegment> latencyStore) {
242 super(ITmfEvent.class, 0, ITmfEventRequest.ALL_DATA, ExecutionType.BACKGROUND);
243 /*
244 * We do NOT make a copy here! We want to modify the list that was
245 * passed in parameter.
246 */
247 fFullLatencyStore = latencyStore;
248 }
249
250 /**
251 * Returns the segment store
252 * @return the segment store
253 */
254 public ISegmentStore<ISegment> getSegmentStore() {
255 return fFullLatencyStore;
256 }
257 }
258}
This page took 0.035921 seconds and 5 git commands to generate.