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
.Collections
;
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
;
39 * Abstract analysis module to generate a segment store. It is a base class that
40 * can be used as a shortcut by analysis who just need to build a single segment
43 * @author Bernd Hufmann
47 public abstract class AbstractSegmentStoreAnalysisModule
extends TmfAbstractAnalysisModule
implements ISegmentStoreProvider
{
49 private final ListenerList fListeners
= new ListenerList(ListenerList
.IDENTITY
);
51 private @Nullable ISegmentStore
<ISegment
> fSegmentStore
;
53 private @Nullable ITmfEventRequest fOngoingRequest
= null;
56 public void addListener(IAnalysisProgressListener listener
) {
57 fListeners
.add(listener
);
61 public void removeListener(IAnalysisProgressListener listener
) {
62 fListeners
.remove(listener
);
66 * Returns all the listeners
68 * @return latency listeners
70 protected Iterable
<IAnalysisProgressListener
> getListeners() {
71 List
<IAnalysisProgressListener
> listeners
= new ArrayList
<>();
72 for (Object listener
: fListeners
.getListeners()) {
73 if (listener
!= null) {
74 listeners
.add((IAnalysisProgressListener
) listener
);
81 public Iterable
<ISegmentAspect
> getSegmentAspects() {
82 return Collections
.emptyList();
86 * Returns the file name for storing segment store
88 * @return segment store fine name, or null if you don't want a file
90 protected @Nullable String
getDataFileName() {
95 * Returns the analysis request for creating the segment store
98 * a segment store to fill
99 * @return the segment store analysis request implementation
101 protected abstract AbstractSegmentStoreAnalysisRequest
createAnalysisRequest(ISegmentStore
<ISegment
> segmentStore
);
104 * Read an object from the ObjectInputStream.
107 * the ObjectInputStream to used
108 * @return the read object
109 * @throws ClassNotFoundException
110 * - Class of a serialized object cannot be found.
111 * @throws IOException
112 * - Any of the usual Input/Output related exceptions.
114 protected abstract Object
[] readObject(ObjectInputStream ois
) throws ClassNotFoundException
, IOException
;
117 public @Nullable ISegmentStore
<ISegment
> getSegmentStore() {
118 return fSegmentStore
;
122 protected void canceling() {
123 ITmfEventRequest req
= fOngoingRequest
;
124 if ((req
!= null) && (!req
.isCompleted())) {
130 public void dispose() {
132 ISegmentStore
<ISegment
> store
= fSegmentStore
;
139 protected boolean executeAnalysis(IProgressMonitor monitor
) throws TmfAnalysisException
{
140 ITmfTrace trace
= checkNotNull(getTrace());
142 final @Nullable String dataFileName
= getDataFileName();
143 if (dataFileName
!= null) {
144 /* See if the data file already exists on disk */
145 String dir
= TmfTraceManager
.getSupplementaryFileDir(trace
);
146 final Path file
= Paths
.get(dir
, dataFileName
);
148 if (Files
.exists(file
)) {
149 /* Attempt to read the existing file */
150 try (ObjectInputStream ois
= new ObjectInputStream(Files
.newInputStream(file
))) {
151 Object
[] segmentArray
= readObject(ois
);
152 final ISegmentStore
<ISegment
> store
= new TreeMapStore
<>();
153 for (Object element
: segmentArray
) {
154 if (element
instanceof ISegment
) {
155 ISegment segment
= (ISegment
) element
;
159 fSegmentStore
= store
;
160 for (IAnalysisProgressListener listener
: getListeners()) {
161 listener
.onComplete(this, store
);
164 } catch (IOException
| ClassNotFoundException
| ClassCastException e
) {
166 * We did not manage to read the file successfully, we will
167 * just fall-through to rebuild a new one.
171 } catch (IOException e1
) {
176 ISegmentStore
<ISegment
> segmentStore
= new TreeMapStore
<>();
178 /* Cancel an ongoing request */
179 ITmfEventRequest req
= fOngoingRequest
;
180 if ((req
!= null) && (!req
.isCompleted())) {
184 /* Create a new request */
185 req
= createAnalysisRequest(segmentStore
);
186 fOngoingRequest
= req
;
187 trace
.sendRequest(req
);
190 req
.waitForCompletion();
191 } catch (InterruptedException e
) {
194 /* Do not process the results if the request was cancelled */
195 if (req
.isCancelled() || req
.isFailed()) {
199 /* The request will fill 'syscalls' */
200 fSegmentStore
= segmentStore
;
202 if (dataFileName
!= null) {
203 String dir
= TmfTraceManager
.getSupplementaryFileDir(trace
);
204 final Path file
= Paths
.get(dir
, dataFileName
);
206 /* Serialize the collections to disk for future usage */
207 try (ObjectOutputStream oos
= new ObjectOutputStream(Files
.newOutputStream(file
))) {
208 oos
.writeObject(segmentStore
.toArray());
209 } catch (IOException e
) {
211 * Didn't work, oh well. We will just re-read the trace next
217 for (IAnalysisProgressListener listener
: getListeners()) {
218 listener
.onComplete(this, segmentStore
);
225 * Abstract event request to fill a a segment store
227 protected static abstract class AbstractSegmentStoreAnalysisRequest
extends TmfEventRequest
{
229 private final ISegmentStore
<ISegment
> fFullLatencyStore
;
234 * @param latencyStore
235 * a latency segment store to fill
237 public AbstractSegmentStoreAnalysisRequest(ISegmentStore
<ISegment
> latencyStore
) {
238 super(ITmfEvent
.class, 0, ITmfEventRequest
.ALL_DATA
, ExecutionType
.BACKGROUND
);
240 * We do NOT make a copy here! We want to modify the list that was
241 * passed in parameter.
243 fFullLatencyStore
= latencyStore
;
247 * Returns the segment store
249 * @return the segment store
251 public ISegmentStore
<ISegment
> getSegmentStore() {
252 return fFullLatencyStore
;