use of org.eclipse.tracecompass.tmf.core.markers.ITimeReference in project tracecompass by tracecompass.
the class PeriodicAnnotationProvider method adjustReference.
/*
* Adjust to a reference that is closer to the start time, to avoid rounding
* errors in floating point calculations with large numbers.
*/
private ITimeReference adjustReference(ITimeReference baseReference, long time) {
long offsetIndex = (long) ((time - baseReference.getTime()) / fPeriod);
long offsetTime = 0;
Fraction fraction = fPeriodFraction;
if (fraction != null) {
/*
* If period = int num/den, find an offset index that is an exact
* multiple of den and calculate index * period = (index * int) +
* (index / den * num), all exact calculations.
*/
offsetIndex = offsetIndex - offsetIndex % fraction.getDenominator();
offsetTime = offsetIndex * fPeriodInteger + offsetIndex / fraction.getDenominator() * fraction.getNumerator();
} else {
/*
* Couldn't compute fractional part as fraction, use simple
* multiplication but with possible rounding error.
*/
offsetTime = Math.round(offsetIndex * fPeriod);
}
return new TimeReference(baseReference.getTime() + offsetTime, baseReference.getIndex() + offsetIndex);
}
use of org.eclipse.tracecompass.tmf.core.markers.ITimeReference in project tracecompass by tracecompass.
the class PeriodicAnnotationProvider method fetchAnnotations.
@Override
@NonNull
public TmfModelResponse<@NonNull AnnotationModel> fetchAnnotations(@NonNull Map<@NonNull String, @NonNull Object> fetchParameters, @Nullable IProgressMonitor monitor) {
List<Long> times = DataProviderParameterUtils.extractTimeRequested(fetchParameters);
if (times == null) {
return EMPTY_MODEL_RESPONSE;
}
int size = times.size() - 1;
long startTime = times.get(0);
long endTime = times.get(size);
long resolution = (endTime - startTime) / (size);
if (startTime > endTime) {
return EMPTY_MODEL_RESPONSE;
}
long step = Math.max(Math.round(fPeriod), resolution);
OutputElementStyle[] styles = new OutputElementStyle[2];
styles[0] = generateOutputElementStyle(fColor1);
RGBAColor color2 = fColor2;
styles[1] = color2 == null ? styles[0] : generateOutputElementStyle(color2);
Collection<Annotation> annotations = new ArrayList<>();
/* Subtract 1.5 periods to ensure previous marker is included */
long time = startTime - Math.max(Math.round(1.5 * fPeriod), resolution);
ITimeReference reference = adjustReference(fReference, time);
Annotation annotation = null;
while (true) {
long index = Math.round((time - reference.getTime()) / fPeriod) + reference.getIndex();
long markerTime = Math.round((index - reference.getIndex()) * fPeriod) + reference.getTime();
long duration = (fColor2 == null) ? 0 : Math.round((index + 1 - reference.getIndex()) * fPeriod) + reference.getTime() - markerTime;
long labelIndex = index;
if (fRollover != 0) {
labelIndex %= fRollover;
if (labelIndex < 0) {
labelIndex += fRollover;
}
}
/* Add previous marker if current is visible */
if ((markerTime >= startTime || markerTime + duration > startTime) && annotation != null) {
annotations.add(annotation);
}
if (isApplicable(labelIndex)) {
OutputElementStyle style = Objects.requireNonNull((index % 2) == 0 ? styles[0] : styles[1]);
annotation = new Annotation(markerTime, duration, -1, getAnnotationLabel(labelIndex), style);
} else {
annotation = null;
}
if (markerTime > endTime || (monitor != null && monitor.isCanceled())) {
if (annotation != null && isApplicable(labelIndex)) {
/* The next marker out of range is included */
annotations.add(annotation);
}
break;
}
time += step;
}
Map<String, Collection<Annotation>> model = new HashMap<>();
model.put(fCategory, annotations);
// $NON-NLS-1$
return new TmfModelResponse<>(new AnnotationModel(model), Status.COMPLETED, "");
}
use of org.eclipse.tracecompass.tmf.core.markers.ITimeReference in project tracecompass by tracecompass.
the class PeriodicAnnotationProviderTest method testReference.
/**
* Test a periodic annotation provider with non-zero reference.
*/
@Test
public void testReference() {
ITimeReference reference = new TimeReference(250L, 10);
IOutputAnnotationProvider provider = new PeriodicAnnotationProvider(CATEGORY, reference, 100L, 0, COLOR, null);
Map<String, List<Annotation>> expected = ImmutableMap.of(CATEGORY, Arrays.asList(new Annotation(-50L, 0L, -1, "7", COLOR_STYLE), new Annotation(50L, 0L, -1, "8", COLOR_STYLE), new Annotation(150L, 0L, -1, "9", COLOR_STYLE), new Annotation(250L, 0L, -1, "10", COLOR_STYLE), new Annotation(350L, 0L, -1, "11", COLOR_STYLE)));
Map<String, Object> fetchParameters = ImmutableMap.of(DataProviderParameterUtils.REQUESTED_TIME_KEY, StateSystemUtils.getTimes(0L, 300L, 1));
assertAnnotationModelResponse(expected, provider.fetchAnnotations(fetchParameters, new NullProgressMonitor()));
}
use of org.eclipse.tracecompass.tmf.core.markers.ITimeReference in project tracecompass by tracecompass.
the class CustomAnnotationProvider method configure.
private CustomPeriodicAnnotationProvider configure(Marker marker) {
if (marker instanceof PeriodicMarker) {
PeriodicMarker periodicMarker = (PeriodicMarker) marker;
long rollover = periodicMarker.getRange().hasUpperBound() ? (periodicMarker.getRange().upperEndpoint() - periodicMarker.getRange().lowerEndpoint() + 1) : 0;
RGBAColor evenColor = getColor(marker);
RGBAColor oddColor = getOddColor(evenColor);
ITmfTrace trace = fTrace;
double period = IMarkerConstants.convertToNanos(periodicMarker.getPeriod(), periodicMarker.getUnit(), trace);
String referenceId = periodicMarker.getReferenceId();
ITimeReference baseReference = null;
if (trace instanceof IAdaptable && !referenceId.isEmpty()) {
ITimeReferenceProvider adapter = ((IAdaptable) trace).getAdapter(ITimeReferenceProvider.class);
if (adapter != null) {
baseReference = adapter.apply(referenceId);
}
}
if (baseReference == null) {
baseReference = ITimeReference.ZERO;
}
ITimeReference reference = new TimeReference(baseReference.getTime() + Math.round(IMarkerConstants.convertToNanos(periodicMarker.getOffset(), periodicMarker.getUnit(), trace)), baseReference.getIndex());
return new CustomPeriodicAnnotationProvider(periodicMarker, reference, period, rollover, evenColor, oddColor);
}
// $NON-NLS-1$
throw new IllegalArgumentException("Marker must be of type PeriodicMarker or SubMarker");
}
Aggregations