1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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
;
11 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
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
;
19 import java
.util
.ArrayList
;
20 import java
.util
.Collection
;
21 import java
.util
.List
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.ListenerList
;
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
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.segment
.ISegmentAspect
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
38 import com
.google
.common
.collect
.ImmutableList
;
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
45 * @author Bernd Hufmann
49 public abstract class AbstractSegmentStoreAnalysisModule
extends TmfAbstractAnalysisModule
{
51 private final ListenerList fListeners
= new ListenerList(ListenerList
.IDENTITY
);
53 private @Nullable ISegmentStore
<ISegment
> fSegmentStore
;
55 private @Nullable ITmfEventRequest fOngoingRequest
= null;
58 * Listener for the viewers
61 * listener for each type of viewer
63 public void addListener(IAnalysisProgressListener listener
) {
64 fListeners
.add(listener
);
68 * Removes a listener for the viewers
71 * listener for each type of viewer to remove
73 public void removeListener(IAnalysisProgressListener listener
) {
74 fListeners
.remove(listener
);
78 * Returns all the listeners
79 * @return latency listeners
81 protected Iterable
<IAnalysisProgressListener
> getListeners() {
82 List
<IAnalysisProgressListener
> listeners
= new ArrayList
<>();
83 for (Object listener
: fListeners
.getListeners()) {
84 if (listener
!= null) {
85 listeners
.add((IAnalysisProgressListener
) listener
);
92 * Return the pre-defined set of segment aspects exposed by this analysis.
94 * It should not be null, but could be empty.
95 * @return The segment aspects for this analysis
97 public Iterable
<ISegmentAspect
> getSegmentAspects() {
98 Collection
<ISegmentAspect
> coll
= ImmutableList
.of();
99 return checkNotNull(coll
);
103 * Returns the file name for storing segment store
104 * @return segment store fine name
106 protected abstract String
getDataFileName();
109 * Returns the analysis request for creating the segment store
110 * @param segmentStore
111 * a segment store to fill
112 * @return the segment store analysis request implementation
114 protected abstract AbstractSegmentStoreAnalysisRequest
createAnalysisRequest(ISegmentStore
<ISegment
> segmentStore
);
117 * Read an object from the ObjectInputStream.
120 * the ObjectInputStream to used
121 * @return the read object
122 * @throws ClassNotFoundException
123 * - Class of a serialized object cannot be found.
124 * @throws IOException
125 * - Any of the usual Input/Output related exceptions.
127 protected abstract Object
[] readObject(ObjectInputStream ois
) throws ClassNotFoundException
, IOException
;
130 * Returns the result in a from the analysis in a ISegmentStore
131 * @return Results from the analysis in a ISegmentStore
133 public @Nullable ISegmentStore
<ISegment
> getResults() {
134 return fSegmentStore
;
138 protected void canceling() {
139 ITmfEventRequest req
= fOngoingRequest
;
140 if ((req
!= null) && (!req
.isCompleted())) {
146 public void dispose() {
148 ISegmentStore
<ISegment
> store
= fSegmentStore
;
155 protected boolean executeAnalysis(IProgressMonitor monitor
) throws TmfAnalysisException
{
156 ITmfTrace trace
= checkNotNull(getTrace());
158 /* See if the data file already exists on disk */
159 String dir
= TmfTraceManager
.getSupplementaryFileDir(trace
);
160 final Path file
= Paths
.get(dir
, getDataFileName());
162 if (Files
.exists(file
)) {
163 /* Attempt to read the existing file */
164 try (ObjectInputStream ois
= new ObjectInputStream(Files
.newInputStream(file
))) {
165 Object
[] segmentArray
= readObject(ois
);
166 final ISegmentStore
<ISegment
> store
= new TreeMapStore
<>();
167 for (Object element
: segmentArray
) {
168 if (element
instanceof ISegment
) {
169 ISegment segment
= (ISegment
) element
;
173 fSegmentStore
= store
;
174 for (IAnalysisProgressListener listener
: getListeners()) {
175 listener
.onComplete(this, store
);
178 } catch (IOException
| ClassNotFoundException
| ClassCastException e
) {
180 * We did not manage to read the file successfully, we will just
181 * fall-through to rebuild a new one.
185 } catch (IOException e1
) {
190 ISegmentStore
<ISegment
> syscalls
= new TreeMapStore
<>();
192 /* Cancel an ongoing request */
193 ITmfEventRequest req
= fOngoingRequest
;
194 if ((req
!= null) && (!req
.isCompleted())) {
198 /* Create a new request */
199 req
= createAnalysisRequest(syscalls
);
200 fOngoingRequest
= req
;
201 trace
.sendRequest(req
);
204 req
.waitForCompletion();
205 } catch (InterruptedException e
) {
208 /* Do not process the results if the request was cancelled */
209 if (req
.isCancelled() || req
.isFailed()) {
213 /* The request will fill 'syscalls' */
214 fSegmentStore
= syscalls
;
216 /* Serialize the collections to disk for future usage */
217 try (ObjectOutputStream oos
= new ObjectOutputStream(Files
.newOutputStream(file
))) {
218 oos
.writeObject(syscalls
.toArray());
219 } catch (IOException e
) {
220 /* Didn't work, oh well. We will just re-read the trace next time */
223 for (IAnalysisProgressListener listener
: getListeners()) {
224 listener
.onComplete(this, syscalls
);
231 * Abstract event request to fill a a segment store
233 protected static abstract class AbstractSegmentStoreAnalysisRequest
extends TmfEventRequest
{
235 private final ISegmentStore
<ISegment
> fFullLatencyStore
;
240 * @param latencyStore
241 * a latency segment store to fill
243 public AbstractSegmentStoreAnalysisRequest(ISegmentStore
<ISegment
> latencyStore
) {
244 super(ITmfEvent
.class, 0, ITmfEventRequest
.ALL_DATA
, ExecutionType
.BACKGROUND
);
246 * We do NOT make a copy here! We want to modify the list that was
247 * passed in parameter.
249 fFullLatencyStore
= latencyStore
;
253 * Returns the segment store
254 * @return the segment store
256 public ISegmentStore
<ISegment
> getSegmentStore() {
257 return fFullLatencyStore
;