1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 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
.internal
.analysis
.timing
.core
.store
.ArrayListStore
;
27 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegment
;
28 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegmentStore
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAbstractAnalysisModule
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.segment
.ISegmentAspect
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
36 * Abstract analysis module to generate a segment store. It is a base class that
37 * can be used as a shortcut by analysis who just need to build a single segment
40 * @author Bernd Hufmann
44 public abstract class AbstractSegmentStoreAnalysisModule
extends TmfAbstractAnalysisModule
implements ISegmentStoreProvider
{
46 private final ListenerList fListeners
= new ListenerList(ListenerList
.IDENTITY
);
48 private @Nullable ISegmentStore
<ISegment
> fSegmentStore
;
51 public void addListener(IAnalysisProgressListener listener
) {
52 fListeners
.add(listener
);
56 public void removeListener(IAnalysisProgressListener listener
) {
57 fListeners
.remove(listener
);
61 * Returns all the listeners
63 * @return latency listeners
65 protected Iterable
<IAnalysisProgressListener
> getListeners() {
66 List
<IAnalysisProgressListener
> listeners
= new ArrayList
<>();
67 for (Object listener
: fListeners
.getListeners()) {
68 if (listener
!= null) {
69 listeners
.add((IAnalysisProgressListener
) listener
);
76 public Iterable
<ISegmentAspect
> getSegmentAspects() {
77 return Collections
.emptyList();
81 * Returns the file name for storing segment store
83 * @return segment store fine name, or null if you don't want a file
85 protected @Nullable String
getDataFileName() {
90 * Read an object from the ObjectInputStream.
93 * the ObjectInputStream to used
94 * @return the read object
95 * @throws ClassNotFoundException
96 * - Class of a serialized object cannot be found.
98 * - Any of the usual Input/Output related exceptions.
100 protected abstract Object
[] readObject(ObjectInputStream ois
) throws ClassNotFoundException
, IOException
;
103 * Fills the segment store. This is the main method that children classes
104 * need to implement to build the segment store. For example, if the
105 * segments are found by parsing the events of a trace, the event request
106 * would be done in this method.
108 * Note: After this method, the segment store should be completed, so it
109 * should also close the segment store at the end of the analysis
111 * @param segmentStore
112 * The segment store to fill
115 * @return Whether the segments was resolved successfully or not
116 * @throws TmfAnalysisException
117 * Method may throw an analysis exception
119 protected abstract boolean buildAnalysisSegments(ISegmentStore
<ISegment
> segmentStore
, IProgressMonitor monitor
) throws TmfAnalysisException
;
122 public @Nullable ISegmentStore
<ISegment
> getSegmentStore() {
123 return fSegmentStore
;
127 public void dispose() {
129 ISegmentStore
<ISegment
> store
= fSegmentStore
;
136 protected boolean executeAnalysis(IProgressMonitor monitor
) throws TmfAnalysisException
{
137 ITmfTrace trace
= checkNotNull(getTrace());
139 final @Nullable String dataFileName
= getDataFileName();
140 if (dataFileName
!= null) {
141 /* See if the data file already exists on disk */
142 String dir
= TmfTraceManager
.getSupplementaryFileDir(trace
);
143 final Path file
= Paths
.get(dir
, dataFileName
);
145 if (Files
.exists(file
)) {
146 /* Attempt to read the existing file */
147 try (ObjectInputStream ois
= new ObjectInputStream(Files
.newInputStream(file
))) {
148 Object
[] segmentArray
= readObject(ois
);
149 final ISegmentStore
<ISegment
> store
= new ArrayListStore
<>();
150 for (Object element
: segmentArray
) {
151 if (element
instanceof ISegment
) {
152 ISegment segment
= (ISegment
) element
;
156 fSegmentStore
= store
;
159 } catch (IOException
| ClassNotFoundException
| ClassCastException e
) {
161 * We did not manage to read the file successfully, we will
162 * just fall-through to rebuild a new one.
166 } catch (IOException e1
) {
172 ISegmentStore
<ISegment
> segmentStore
= new ArrayListStore
<>();
173 boolean completed
= buildAnalysisSegments(segmentStore
, monitor
);
177 fSegmentStore
= segmentStore
;
179 if (dataFileName
!= null) {
180 String dir
= TmfTraceManager
.getSupplementaryFileDir(trace
);
181 final Path file
= Paths
.get(dir
, dataFileName
);
183 /* Serialize the collections to disk for future usage */
184 try (ObjectOutputStream oos
= new ObjectOutputStream(Files
.newOutputStream(file
))) {
185 oos
.writeObject(segmentStore
.toArray());
186 } catch (IOException e
) {
188 * Didn't work, oh well. We will just re-read the trace next
194 sendUpdate(segmentStore
);
200 * Send the segment store to all its listener
203 * The segment store to broadcast
205 protected void sendUpdate(final ISegmentStore
<ISegment
> store
) {
206 for (IAnalysisProgressListener listener
: getListeners()) {
207 listener
.onComplete(this, store
);