1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 ******************************************************************************/
9 package org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.pattern
.stateprovider
;
11 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
13 import java
.io
.IOException
;
14 import java
.io
.ObjectInputStream
;
15 import java
.util
.concurrent
.CountDownLatch
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.jdt
.annotation
.NonNull
;
19 import org
.eclipse
.jdt
.annotation
.Nullable
;
20 import org
.eclipse
.tracecompass
.analysis
.timing
.core
.segmentstore
.AbstractSegmentStoreAnalysisModule
;
21 import org
.eclipse
.tracecompass
.analysis
.timing
.core
.segmentstore
.IAnalysisProgressListener
;
22 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegment
;
23 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegmentStore
;
24 import org
.eclipse
.tracecompass
.segmentstore
.core
.treemap
.TreeMapStore
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
29 * Segment store module for pattern analysis defined in XML. This module will
30 * receive all the segments provided by an external source and will build a
33 * @author Jean-Christian Kouame
35 public class XmlPatternSegmentStoreModule
extends AbstractSegmentStoreAnalysisModule
implements ISegmentListener
{
38 * Fake segment indicated that the last segment have been received
40 public static final @NonNull EndSegment END_SEGMENT
= new EndSegment();
41 private final ISegmentStore
<@NonNull ISegment
> fSegments
= new TreeMapStore
<>();
42 private final CountDownLatch fFinished
= new CountDownLatch(1);
43 private final @NonNull XmlPatternAnalysis fParent
;
44 private boolean fSegmentStoreCompleted
;
52 public XmlPatternSegmentStoreModule(@NonNull XmlPatternAnalysis parent
) {
58 protected Object
@NonNull [] readObject(@NonNull ObjectInputStream ois
) throws ClassNotFoundException
, IOException
{
59 return checkNotNull((Object
[]) ois
.readObject());
63 protected boolean buildAnalysisSegments(@NonNull ISegmentStore
<@NonNull ISegment
> segments
, @NonNull IProgressMonitor monitor
) throws TmfAnalysisException
{
64 final @Nullable ITmfTrace trace
= getTrace();
66 /* This analysis was cancelled in the meantime */
67 segmentStoreReady(false);
70 waitForSegmentStoreCompletion();
71 segments
.addAll(getSegments());
76 protected void canceling() {
78 segmentStoreReady(false);
82 protected @Nullable String
getDataFileName() {
83 return getId() + XmlPatternAnalysis
.SEGMENT_STORE_EXTENSION
;
87 * Broadcast the segment store to its listeners. Since this analysis is not
88 * directly register to the trace, the parent analysis is used as the source.
91 * The store to broadcast
94 protected void sendUpdate(final ISegmentStore
<@NonNull ISegment
> store
) {
95 for (IAnalysisProgressListener listener
: getListeners()) {
96 listener
.onComplete(fParent
, store
);
101 public void onNewSegment(@NonNull ISegment segment
) {
102 // We can accept segments until the first END_SEGMENT arrives. Nothing
103 // should be accept after it. This prevents to receive new segments if
104 // the analysis that generates the segments is rescheduled
105 if (!fSegmentStoreCompleted
) {
106 if (segment
== END_SEGMENT
) {
107 segmentStoreReady(true);
110 getSegments().add(segment
);
115 * Get the internal segment store of this module
117 * @return The segment store
119 private synchronized ISegmentStore
<@NonNull ISegment
> getSegments() {
124 * Wait until internal segment store of the module is fully filled. If all
125 * the segments have been received, the completion succeeded, otherwise it
128 * @return True if the completion succeeded, false otherwise
130 public boolean waitForSegmentStoreCompletion() {
133 } catch (InterruptedException e
) {
136 return fSegmentStoreCompleted
;
140 * Make the module available and set whether the segment store completion
141 * succeeded or not. If not, no segment store is available and
142 * {@link #waitForSegmentStoreCompletion()} should return false.
145 * True if the segment store completion succeeded, false
148 private void segmentStoreReady(boolean succeeded
) {
149 fSegmentStoreCompleted
= succeeded
;
150 fFinished
.countDown();
154 * Fake segment indicating the build is over, and the segment store is fully
157 public static class EndSegment
implements ISegment
{
159 * The serial version UID
161 private static final long serialVersionUID
= 7834984029618274707L;
164 public long getStart() {
165 return Long
.MIN_VALUE
;
169 public long getEnd() {
170 return Long
.MIN_VALUE
;