1 /*******************************************************************************
2 * Copyright (c) 2014 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
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.trace
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import java
.io
.BufferedInputStream
;
19 import java
.io
.FileInputStream
;
20 import java
.io
.IOException
;
21 import java
.util
.ArrayList
;
22 import java
.util
.List
;
23 import java
.util
.function
.Predicate
;
25 import org
.eclipse
.jdt
.annotation
.NonNull
;
26 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
27 import org
.eclipse
.jdt
.annotation
.Nullable
;
28 import org
.eclipse
.tracecompass
.common
.core
.StreamUtils
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.component
.ITmfEventProvider
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.request
.TmfEventRequest
;
35 import com
.google
.common
.collect
.Iterables
;
38 * Utility methods for ITmfTrace's.
40 * @author Alexandre Montplaisir
43 public final class TmfTraceUtils
{
45 private static final int MAX_NB_BINARY_BYTES
= 2048;
47 private TmfTraceUtils() {
51 * Return the first result of the first analysis module belonging to this trace or its children,
52 * with the specified ID and class.
55 * The trace for which you want the modules
57 * Returned modules must extend this class
59 * The ID of the analysis module
60 * @return The analysis module with specified class and ID, or null if no
63 public static @Nullable <T
extends IAnalysisModule
> T
getAnalysisModuleOfClass(ITmfTrace trace
,
64 Class
<T
> moduleClass
, String id
) {
65 Iterable
<T
> modules
= getAnalysisModulesOfClass(trace
, moduleClass
);
66 for (T module
: modules
) {
67 if (id
.equals(module
.getId())) {
75 * Return the analysis modules that are of a given class. The modules will be
76 * cast to the requested class. If the trace has children, the childrens modules
80 * The trace for which you want the modules, the children trace modules
83 * Returned modules must extend this class
84 * @return List of modules of class moduleClass
86 public static <T
> Iterable
<@NonNull T
> getAnalysisModulesOfClass(ITmfTrace trace
, Class
<T
> moduleClass
) {
87 Iterable
<IAnalysisModule
> analysisModules
= trace
.getAnalysisModules();
88 List
<@NonNull T
> modules
= new ArrayList
<>();
89 for (IAnalysisModule module
: analysisModules
) {
90 if (moduleClass
.isAssignableFrom(module
.getClass())) {
91 modules
.add(checkNotNull(moduleClass
.cast(module
)));
94 for (ITmfEventProvider child
: trace
.getChildren()) {
95 if (child
instanceof ITmfTrace
) {
96 ITmfTrace childTrace
= (ITmfTrace
) child
;
97 Iterables
.addAll(modules
, getAnalysisModulesOfClass(childTrace
, moduleClass
));
104 * Return the first result of the first aspect that resolves as non null for
105 * the event received in parameter. If the returned value is not null, it
106 * can be safely cast to the aspect's class proper return type.
109 * The trace for which you want the event aspects
111 * The class of the aspect(s) to resolve
113 * The event for which to get the aspect
114 * @return The first result of the
115 * {@link ITmfEventAspect#resolve(ITmfEvent)} that returns non null
116 * for the event or {@code null} otherwise
118 public static <T
extends ITmfEventAspect
<?
>> @Nullable Object
resolveEventAspectOfClassForEvent(
119 ITmfTrace trace
, Class
<T
> aspectClass
, ITmfEvent event
) {
120 return StreamUtils
.getStream(trace
.getEventAspects())
121 .filter(aspect
-> aspectClass
.isAssignableFrom(aspect
.getClass()))
122 .map(aspect
-> aspect
.resolve(event
))
123 .filter(obj
-> obj
!= null)
124 .findFirst().orElse(null);
128 * Return the first result of the first aspect that resolves as a non-null
129 * Integer for the event received in parameter. If no matching aspects are
130 * found then null is returned.
133 * The trace for which you want the event aspects
135 * The class of the aspect(s) to resolve
137 * The event for which to get the aspect
138 * @return Integer of the first result of the
139 * {@link ITmfEventAspect#resolve(ITmfEvent)} that returns non null
140 * for the event or {@code null} otherwise
143 public static <T
extends ITmfEventAspect
<Integer
>> @Nullable Integer
resolveIntEventAspectOfClassForEvent(
144 ITmfTrace trace
, Class
<T
> aspectClass
, ITmfEvent event
) {
145 return StreamUtils
.getStream(trace
.getEventAspects())
146 .filter(aspect
-> aspectClass
.isAssignableFrom(aspect
.getClass()))
147 /* Enforced by the T parameter bounding */
148 .map(aspect
-> (Integer
) aspect
.resolve(event
))
149 .filter(obj
-> obj
!= null)
150 .findFirst().orElse(null);
154 * Checks for text file.
156 * Note that it checks for binary value 0 in the first MAX_NB_BINARY_BYTES
157 * bytes to determine if the file is text.
160 * the file to check. Caller has to make sure that file exists.
161 * @return true if it is binary else false
162 * @throws IOException
163 * if IOException occurs
166 public static boolean isText(File file
) throws IOException
{
167 try (BufferedInputStream bufferedInputStream
= new BufferedInputStream(new FileInputStream(file
))) {
169 int val
= bufferedInputStream
.read();
170 while ((count
< MAX_NB_BINARY_BYTES
) && (val
>= 0)) {
175 val
= bufferedInputStream
.read();
181 // ------------------------------------------------------------------------
182 // Event matching methods
183 // ------------------------------------------------------------------------
186 * Retrieve from a trace the next event, from a starting rank, matching the
192 * The rank of the event at which to start searching. Use
193 * <code>0</code> to search from the start of the trace.
195 * The predicate to test events against
196 * @return The first event matching the predicate, or null if the end of the
197 * trace was reached and no event was found
199 public static @Nullable ITmfEvent
getNextEventMatching(ITmfTrace trace
, long startRank
, Predicate
<ITmfEvent
> predicate
) {
200 /* rank + 1 because we do not want to include the start event itself in the search */
201 EventMatchingRequest req
= new EventMatchingRequest(startRank
+ 1, predicate
, false);
202 trace
.sendRequest(req
);
204 req
.waitForCompletion();
205 } catch (InterruptedException e
) {
209 return req
.getFoundEvent();
213 * Retrieve from a trace the previous event, from a given rank, matching the
219 * The rank of the event at which to start searching backwards.
221 * The predicate to test events against
222 * @return The first event found matching the predicate, or null if the
223 * beginning of the trace was reached and no event was found
225 public static @Nullable ITmfEvent
getPreviousEventMatching(ITmfTrace trace
, long startRank
, Predicate
<ITmfEvent
> predicate
) {
227 * Slightly less straightforward since we unfortunately cannot iterate
228 * backwards on events. Do a series of forward-queries.
230 final int targetStep
= 100;
233 * If we are close to the beginning of the trace, make sure we only look
234 * for the events before the startRank.
236 int step
= (startRank
< targetStep ?
(int) startRank
: targetStep
);
238 long currentRank
= startRank
;
240 while (currentRank
> 0) {
241 currentRank
= Math
.max(currentRank
- step
, 0);
243 EventMatchingRequest req
= new EventMatchingRequest(currentRank
, step
, predicate
, true);
244 trace
.sendRequest(req
);
245 req
.waitForCompletion();
247 ITmfEvent event
= req
.getFoundEvent();
249 /* We found an actual event, return it! */
252 /* Keep searching, next loop */
255 } catch (InterruptedException e
) {
260 * We searched up to the beginning of the trace and didn't find
268 * Event request looking for an event matching a Predicate.
270 private static class EventMatchingRequest
extends TmfEventRequest
{
272 private final Predicate
<ITmfEvent
> fPredicate
;
273 private final boolean fReturnLast
;
275 private @Nullable ITmfEvent fFoundEvent
= null;
278 * Basic constructor, will query the trace until the end.
281 * The rank at which to start, use 0 for the beginning
283 * The predicate to test against each event
285 * Should we return the last or first event found. If false,
286 * the request ends as soon as a matching event is found. If
287 * false, we will go through all events to find a possible
290 public EventMatchingRequest(long startRank
, Predicate
<ITmfEvent
> predicate
, boolean returnLast
) {
291 super(ITmfEvent
.class, startRank
, ALL_DATA
, ExecutionType
.FOREGROUND
);
292 fPredicate
= predicate
;
293 fReturnLast
= returnLast
;
297 * Basic constructor, will query the trace the limit is reached.
300 * The rank at which to start, use 0 for the beginning
302 * The limit on the number of events
304 * The predicate to test against each event
306 * Should we return the last or first event found. If false,
307 * the request ends as soon as a matching event is found. If
308 * false, we will go through all events to find a possible
311 public EventMatchingRequest(long startRank
, int limit
, Predicate
<ITmfEvent
> predicate
, boolean returnLast
) {
312 super(ITmfEvent
.class, startRank
, limit
, ExecutionType
.FOREGROUND
);
313 fPredicate
= predicate
;
314 fReturnLast
= returnLast
;
317 public @Nullable ITmfEvent
getFoundEvent() {
322 public void handleData(ITmfEvent event
) {
323 super.handleData(event
);
325 if (fPredicate
.test(event
)) {