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
.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
.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 * @param segmentStore
109 * The segment store to fill
112 * @return Whether the segments was resolved successfully or not
113 * @throws TmfAnalysisException
114 * Method may throw an analysis exception
116 protected abstract boolean buildAnalysisSegments(ISegmentStore
<ISegment
> segmentStore
, IProgressMonitor monitor
) throws TmfAnalysisException
;
119 public @Nullable ISegmentStore
<ISegment
> getSegmentStore() {
120 return fSegmentStore
;
124 public void dispose() {
126 ISegmentStore
<ISegment
> store
= fSegmentStore
;
133 protected boolean executeAnalysis(IProgressMonitor monitor
) throws TmfAnalysisException
{
134 ITmfTrace trace
= checkNotNull(getTrace());
136 final @Nullable String dataFileName
= getDataFileName();
137 if (dataFileName
!= null) {
138 /* See if the data file already exists on disk */
139 String dir
= TmfTraceManager
.getSupplementaryFileDir(trace
);
140 final Path file
= Paths
.get(dir
, dataFileName
);
142 if (Files
.exists(file
)) {
143 /* Attempt to read the existing file */
144 try (ObjectInputStream ois
= new ObjectInputStream(Files
.newInputStream(file
))) {
145 Object
[] segmentArray
= readObject(ois
);
146 final ISegmentStore
<ISegment
> store
= new TreeMapStore
<>();
147 for (Object element
: segmentArray
) {
148 if (element
instanceof ISegment
) {
149 ISegment segment
= (ISegment
) element
;
153 fSegmentStore
= store
;
154 for (IAnalysisProgressListener listener
: getListeners()) {
155 listener
.onComplete(this, store
);
158 } catch (IOException
| ClassNotFoundException
| ClassCastException e
) {
160 * We did not manage to read the file successfully, we will
161 * just fall-through to rebuild a new one.
165 } catch (IOException e1
) {
171 ISegmentStore
<ISegment
> segmentStore
= new TreeMapStore
<>();
172 boolean completed
= buildAnalysisSegments(segmentStore
, monitor
);
176 fSegmentStore
= segmentStore
;
178 if (dataFileName
!= null) {
179 String dir
= TmfTraceManager
.getSupplementaryFileDir(trace
);
180 final Path file
= Paths
.get(dir
, dataFileName
);
182 /* Serialize the collections to disk for future usage */
183 try (ObjectOutputStream oos
= new ObjectOutputStream(Files
.newOutputStream(file
))) {
184 oos
.writeObject(segmentStore
.toArray());
185 } catch (IOException e
) {
187 * Didn't work, oh well. We will just re-read the trace next
193 for (IAnalysisProgressListener listener
: getListeners()) {
194 listener
.onComplete(this, segmentStore
);