From b6d74e0b404092cf2e9b4df78585f70fc8600124 Mon Sep 17 00:00:00 2001 From: Patrick Tasse Date: Tue, 19 May 2015 17:12:05 -0400 Subject: [PATCH] tmf: Fix IllegalStateException in HistogramDataModel It should be allowed to have a last bucket set to 0 in the histogram data model. This can happen if all trace events have the same timestamp. The histogram time range is fixed to display even if the start and end times are equal, as long as there is at least one event in the model. The scaling is updated to make sure that these single-timestamp events are spread over the whole width of the histogram. The scaled model will have a bucket duration of 0 when it represents a single timestamp. Change-Id: I5341aa6a158a3b4c1b3d4edee982ed67558e8a51 Signed-off-by: Patrick Tasse Reviewed-on: https://git.eclipse.org/r/48224 Reviewed-by: Hudson CI Reviewed-by: Matthew Khouzam Tested-by: Matthew Khouzam --- .../ui/tests/histogram/HistogramDataModelTest.java | 8 +++++--- .../tmf/ui/views/histogram/Histogram.java | 6 +++--- .../tmf/ui/views/histogram/HistogramDataModel.java | 12 +++++++++--- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/org.eclipse.tracecompass.tmf.ui.tests/src/org/eclipse/tracecompass/tmf/ui/tests/histogram/HistogramDataModelTest.java b/org.eclipse.tracecompass.tmf.ui.tests/src/org/eclipse/tracecompass/tmf/ui/tests/histogram/HistogramDataModelTest.java index 007b5c0b57..51f17088c1 100644 --- a/org.eclipse.tracecompass.tmf.ui.tests/src/org/eclipse/tracecompass/tmf/ui/tests/histogram/HistogramDataModelTest.java +++ b/org.eclipse.tracecompass.tmf.ui.tests/src/org/eclipse/tracecompass/tmf/ui/tests/histogram/HistogramDataModelTest.java @@ -130,7 +130,7 @@ public class HistogramDataModelTest { * {@link HistogramDataModel#countEvent(long,long, ITmfTrace)} and * {@link HistogramDataModel#scaleTo(int,int,int)}. */ - @Test(expected = IllegalStateException.class) + @Test public void testCountEvent_2() { final int nbBuckets = 100; final int maxHeight = 10; @@ -139,9 +139,11 @@ public class HistogramDataModelTest { model.countEvent(0, 1, null); HistogramScaledData result = model.scaleTo(nbBuckets, maxHeight, 1); - assertEquals(_1, result.fData[0]); - assertArrayEqualsInt(1, result.fData, 1); + for (int i = 0; i < result.fData.length - 1; i++) { + assertEquals(_1, result.fData[i]); + } + assertEquals(_0, result.fData[result.fData.length - 1]); testModelConsistency(model, nbBuckets, 1, 1, 1, 1, 1, nbBuckets + 1); } diff --git a/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/histogram/Histogram.java b/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/histogram/Histogram.java index aa5adf40d2..bbe0b401cf 100644 --- a/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/histogram/Histogram.java +++ b/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/histogram/Histogram.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2014 Ericsson + * Copyright (c) 2011, 2015 Ericsson * * All rights reserved. This program and the accompanying materials are * made available under the terms of the Eclipse Public License v1.0 which @@ -724,7 +724,7 @@ public abstract class Histogram implements ControlListener, PaintListener, KeyLi * Update the range text controls */ private void updateRangeTextControls() { - if (fDataModel.getStartTime() < fDataModel.getEndTime()) { + if (fDataModel.getNbEvents() != 0) { fTimeRangeStartLabel.setText(TmfTimestampFormat.getDefaulTimeFormat().format(fDataModel.getStartTime())); fTimeRangeEndLabel.setText(TmfTimestampFormat.getDefaulTimeFormat().format(fDataModel.getEndTime())); } else { @@ -1020,7 +1020,7 @@ public abstract class Histogram implements ControlListener, PaintListener, KeyLi @Override public void mouseHover(final MouseEvent event) { - if (fDataModel.getStartTime() < fDataModel.getEndTime() && fScaledData != null) { + if (fDataModel.getNbEvents() != 0 && fScaledData != null) { final String tooltip = formatToolTipLabel(event.x - fOffset); fCanvas.setToolTipText(tooltip); return; diff --git a/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/histogram/HistogramDataModel.java b/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/histogram/HistogramDataModel.java index b617c4cf6f..db187897a6 100644 --- a/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/histogram/HistogramDataModel.java +++ b/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/histogram/HistogramDataModel.java @@ -623,7 +623,7 @@ public class HistogramDataModel implements IHistogramDataModel { if ((width <= 0) || (height <= 0) || (barWidth <= 0)) { throw new AssertionError("Invalid histogram dimensions (" + width + "x" + height + ", barWidth=" + barWidth + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ } - if (fLastBucket == 0 || fBucketDuration == 0) { + if (fBucketDuration == 0) { throw new IllegalStateException("Bucket width is 0, that should be impossible"); //$NON-NLS-1$ } @@ -637,10 +637,16 @@ public class HistogramDataModel implements IHistogramDataModel { double bucketsPerBar = ((double) fLastBucket / nbBars); final long modelBucketStartTime = fFirstBucketTime; final long modelBucketEndTime = fEndTime; - result.fBucketDuration = (modelBucketEndTime - modelBucketStartTime) / (double) nbBars; + /* + * If there is only one model bucket, use a duration of 1 to spread the + * value over the scaled width, but store a scaled bucket duration of 0 + * to prevent the half-bucket offset in the bucket time calculations. + */ + double bucketDuration = Math.max(modelBucketEndTime - modelBucketStartTime, 1) / (double) nbBars; + result.fBucketDuration = fLastBucket == 0 ? 0 : bucketDuration; int scaledCount = 0; int scaledCountLostEvent = 0; - int offset = (int) (0.5 / result.fBucketDuration); + int offset = (int) (0.5 / bucketDuration); for (int i = 0; i < result.fData.length; i++) { result.fData[i] = new HistogramBucket(getNbTraces()); } -- 2.34.1