Remove unneeded checkNotNull() calls
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / markers / LostEventsMarkerEventSource.java
1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 Ericsson
3 *
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 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.internal.tmf.ui.markers;
14
15 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Collections;
20 import java.util.List;
21
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.swt.graphics.RGBA;
25 import org.eclipse.tracecompass.internal.tmf.ui.Messages;
26 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
27 import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
28 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
29 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
30 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
31 import org.eclipse.tracecompass.tmf.core.statistics.TmfStateStatistics.Attributes;
32 import org.eclipse.tracecompass.tmf.core.statistics.TmfStatisticsEventTypesModule;
33 import org.eclipse.tracecompass.tmf.core.statistics.TmfStatisticsModule;
34 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
35 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
36 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.IMarkerEvent;
37 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.IMarkerEventSource;
38 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.MarkerEvent;
39
40 /**
41 * Marker event source for lost events.
42 */
43 public class LostEventsMarkerEventSource implements IMarkerEventSource {
44
45 private static final @NonNull String LOST_EVENTS = checkNotNull(Messages.MarkerEvent_LostEvents);
46
47 private static final RGBA COLOR = new RGBA(255, 0, 0, 50);
48
49 private final @NonNull ITmfTrace fTrace;
50 private long[] fLastRequest;
51 private @NonNull List<@NonNull IMarkerEvent> fLastMarkers = Collections.emptyList();
52
53 /**
54 * Constructor.
55 *
56 * @param trace
57 * the trace
58 */
59 public LostEventsMarkerEventSource(@NonNull ITmfTrace trace) {
60 fTrace = trace;
61 }
62
63 @Override
64 public @NonNull List<@NonNull String> getMarkerCategories() {
65 return Arrays.asList(LOST_EVENTS);
66 }
67
68 @Override
69 public synchronized @NonNull List<@NonNull IMarkerEvent> getMarkerList(@NonNull String category, long startTime, long endTime, long resolution, @NonNull IProgressMonitor monitor) {
70 if (!category.equals(LOST_EVENTS)) {
71 return Collections.emptyList();
72 }
73 ITmfStateSystem ss = getStateSystem();
74 if (ss == null) {
75 return Collections.emptyList();
76 }
77 int lostEventsQuark = getLostEventsQuark(ss);
78 if (lostEventsQuark == -1) {
79 return Collections.emptyList();
80 }
81 long[] request = new long[] { startTime, endTime, resolution, ss.getCurrentEndTime() };
82 if (Arrays.equals(request, fLastRequest)) {
83 return fLastMarkers;
84 }
85 List<@NonNull IMarkerEvent> markers = new ArrayList<>();
86 try {
87 long start = Math.max(startTime, ss.getStartTime());
88 long end = Math.min(endTime, ss.getCurrentEndTime());
89 if (start <= end) {
90 /* Update start to ensure that the previous marker is included. */
91 start = Math.max(start - 1, ss.getStartTime());
92 /* Update end to ensure that the next marker is included. */
93 long nextStartTime = ss.querySingleState(end, lostEventsQuark).getEndTime() + 1;
94 end = Math.min(nextStartTime, ss.getCurrentEndTime());
95 List<ITmfStateInterval> intervals = StateSystemUtils.queryHistoryRange(ss, lostEventsQuark, start, end, resolution, monitor);
96 for (ITmfStateInterval interval : intervals) {
97 if (interval.getStateValue().isNull()) {
98 continue;
99 }
100 long lostEventsStartTime = interval.getStartTime();
101 /*
102 * The end time of the lost events range is the value of the
103 * attribute, not the end time of the interval.
104 */
105 long lostEventsEndTime = interval.getStateValue().unboxLong();
106 long duration = lostEventsEndTime - lostEventsStartTime;
107 IMarkerEvent marker = new MarkerEvent(null, lostEventsStartTime, duration, LOST_EVENTS, COLOR, null, false);
108 markers.add(marker);
109 }
110 }
111 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
112 /* ignored */
113 }
114 fLastRequest = request;
115 fLastMarkers = Collections.unmodifiableList(markers);
116 return fLastMarkers;
117 }
118
119 private ITmfStateSystem getStateSystem() {
120 TmfStatisticsModule module = TmfTraceUtils.getAnalysisModuleOfClass(fTrace, TmfStatisticsModule.class, TmfStatisticsModule.ID);
121 if (module == null) {
122 return null;
123 }
124 return module.getStateSystem(TmfStatisticsEventTypesModule.ID);
125 }
126
127 private static int getLostEventsQuark(ITmfStateSystem ss) {
128 try {
129 return ss.getQuarkAbsolute(Attributes.LOST_EVENTS);
130 } catch (AttributeNotFoundException e) {
131 return -1;
132 }
133 }
134 }
This page took 0.039317 seconds and 5 git commands to generate.