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
.Collection
;
20 import java
.util
.HashSet
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.jdt
.annotation
.Nullable
;
25 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegment
;
26 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegmentStore
;
27 import org
.eclipse
.tracecompass
.segmentstore
.core
.treemap
.TreeMapStore
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAbstractAnalysisModule
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.request
.TmfEventRequest
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.segment
.ISegmentAspect
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
37 import com
.google
.common
.collect
.ImmutableList
;
40 * Abstract analysis module to generate a segment store. It is a base class that
41 * can be used as a shortcut by analysis who just need to build a single segment
44 * @author Bernd Hufmann
48 public abstract class AbstractSegmentStoreAnalysisModule
extends TmfAbstractAnalysisModule
{
50 private final Set
<IAnalysisProgressListener
> fListeners
= new HashSet
<>();
52 private @Nullable ISegmentStore
<ISegment
> fSegmentStore
;
54 private @Nullable ITmfEventRequest fOngoingRequest
= null;
57 * Listener for the viewers
60 * listener for each type of viewer
62 public void addListener(IAnalysisProgressListener listener
) {
63 fListeners
.add(listener
);
67 * Returns all the listeners
68 * @return latency listners
70 protected Iterable
<IAnalysisProgressListener
> getListeners() {
75 * Return the pre-defined set of segment aspects exposed by this analysis.
77 * It should not be null, but could be empty.
78 * @return The segment aspects for this analysis
80 public Iterable
<ISegmentAspect
> getSegmentAspects() {
81 Collection
<ISegmentAspect
> coll
= ImmutableList
.of();
82 return checkNotNull(coll
);
86 * Returns the file name for storing segment store
87 * @return segment store fine name
89 protected abstract String
getDataFileName();
92 * Returns the analysis request for creating the segment store
94 * a segment store to fill
95 * @return the segment store analysis request implementation
97 protected abstract AbstractSegmentStoreAnalysisRequest
createAnalysisRequest(ISegmentStore
<ISegment
> segmentStore
);
100 * Read an object from the ObjectInputStream.
103 * the ObjectInputStream to used
104 * @return the read object
105 * @throws ClassNotFoundException
106 * - Class of a serialized object cannot be found.
107 * @throws IOException
108 * - Any of the usual Input/Output related exceptions.
110 protected abstract Object
[] readObject(ObjectInputStream ois
) throws ClassNotFoundException
, IOException
;
113 * Returns the result in a from the analysis in a ISegmentStore
114 * @return Results from the analysis in a ISegmentStore
116 public @Nullable ISegmentStore
<ISegment
> getResults() {
117 return fSegmentStore
;
121 protected void canceling() {
122 ITmfEventRequest req
= fOngoingRequest
;
123 if ((req
!= null) && (!req
.isCompleted())) {
129 public void dispose() {
131 ISegmentStore
<ISegment
> store
= fSegmentStore
;
138 protected boolean executeAnalysis(IProgressMonitor monitor
) throws TmfAnalysisException
{
139 ITmfTrace trace
= checkNotNull(getTrace());
141 /* See if the data file already exists on disk */
142 String dir
= TmfTraceManager
.getSupplementaryFileDir(trace
);
143 final Path file
= Paths
.get(dir
, getDataFileName());
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 TreeMapStore
<>();
150 for (Object element
: segmentArray
) {
151 if (element
instanceof ISegment
) {
152 ISegment segment
= (ISegment
) element
;
156 fSegmentStore
= store
;
157 for (IAnalysisProgressListener listener
: getListeners()) {
158 listener
.onComplete(this, store
);
161 } catch (IOException
| ClassNotFoundException
| ClassCastException e
) {
163 * We did not manage to read the file successfully, we will just
164 * fall-through to rebuild a new one.
168 } catch (IOException e1
) {
173 ISegmentStore
<ISegment
> syscalls
= new TreeMapStore
<>();
175 /* Cancel an ongoing request */
176 ITmfEventRequest req
= fOngoingRequest
;
177 if ((req
!= null) && (!req
.isCompleted())) {
181 /* Create a new request */
182 req
= createAnalysisRequest(syscalls
);
183 fOngoingRequest
= req
;
184 trace
.sendRequest(req
);
187 req
.waitForCompletion();
188 } catch (InterruptedException e
) {
191 /* Do not process the results if the request was cancelled */
192 if (req
.isCancelled() || req
.isFailed()) {
196 /* The request will fill 'syscalls' */
197 fSegmentStore
= syscalls
;
199 /* Serialize the collections to disk for future usage */
200 try (ObjectOutputStream oos
= new ObjectOutputStream(Files
.newOutputStream(file
))) {
201 oos
.writeObject(syscalls
.toArray());
202 } catch (IOException e
) {
203 /* Didn't work, oh well. We will just re-read the trace next time */
206 for (IAnalysisProgressListener listener
: getListeners()) {
207 listener
.onComplete(this, syscalls
);
214 * Abstract event request to fill a a segment store
216 protected static abstract class AbstractSegmentStoreAnalysisRequest
extends TmfEventRequest
{
218 private final ISegmentStore
<ISegment
> fFullLatencyStore
;
223 * @param latencyStore
224 * a latency segment store to fill
226 public AbstractSegmentStoreAnalysisRequest(ISegmentStore
<ISegment
> latencyStore
) {
227 super(ITmfEvent
.class, 0, ITmfEventRequest
.ALL_DATA
, ExecutionType
.BACKGROUND
);
229 * We do NOT make a copy here! We want to modify the list that was
230 * passed in parameter.
232 fFullLatencyStore
= latencyStore
;
236 * Returns the segment store
237 * @return the segment store
239 public ISegmentStore
<ISegment
> getSegmentStore() {
240 return fFullLatencyStore
;