From 58f3bc52ca128cb4add59cbcf2c4115123672726 Mon Sep 17 00:00:00 2001 From: Alexandre Montplaisir Date: Thu, 6 Sep 2012 16:49:51 -0400 Subject: [PATCH] tmf: CtfTmfEvent improvements - Remove extraneous Javadoc - Change fTimestamp from a singleton to a proper field, set at the constructor. - Drop unneeded singletons (fReference and fSource) - Remove getTimestampValue(), call getTimestamp.getValue() instead, it is more compatible with ITmfEvent/Timestamp interfaces. Change-Id: I0522a40f3817bcc34bde48dc90358fb745f7bdbc Signed-off-by: Alexandre Montplaisir Reviewed-on: https://git.eclipse.org/r/7648 Tested-by: Hudson CI Reviewed-by: Bernd Hufmann --- .../core/stateprovider/CtfKernelHandler.java | 2 +- .../tests/ctfadaptor/CtfTmfEventTest.java | 4 +- .../CtfTmfLightweightContextTest.java | 2 +- .../tmf/core/ctfadaptor/CtfIterator.java | 10 +- .../tmf/core/ctfadaptor/CtfTmfEvent.java | 124 +++++------------- .../ctfadaptor/CtfTmfLightweightContext.java | 2 +- .../tmf/core/ctfadaptor/CtfTmfTrace.java | 2 +- 7 files changed, 45 insertions(+), 101 deletions(-) diff --git a/org.eclipse.linuxtools.lttng2.kernel.core/src/org/eclipse/linuxtools/internal/lttng2/kernel/core/stateprovider/CtfKernelHandler.java b/org.eclipse.linuxtools.lttng2.kernel.core/src/org/eclipse/linuxtools/internal/lttng2/kernel/core/stateprovider/CtfKernelHandler.java index 607b9b3668..152dc90bc3 100644 --- a/org.eclipse.linuxtools.lttng2.kernel.core/src/org/eclipse/linuxtools/internal/lttng2/kernel/core/stateprovider/CtfKernelHandler.java +++ b/org.eclipse.linuxtools.lttng2.kernel.core/src/org/eclipse/linuxtools/internal/lttng2/kernel/core/stateprovider/CtfKernelHandler.java @@ -71,7 +71,7 @@ class CtfKernelHandler implements Runnable { try { event = inQueue.take(); - while (event.getTimestampValue() != -1) { + while (event.getTimestamp().getValue() != -1) { processEvent(event); event = inQueue.take(); } diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventTest.java index 1ba0e32e4d..591c786c0d 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventTest.java @@ -176,7 +176,7 @@ public class CtfTmfEventTest { assertEquals("No stream", nullEvent.getChannelName()); //$NON-NLS-1$ assertArrayEquals(new ITmfEventField[0], nullEvent.getContent().getFields()); assertEquals(-1L, nullEvent.getID()); - assertEquals(-1L, nullEvent.getTimestampValue()); + assertEquals(-1L, nullEvent.getTimestamp().getValue()); } /** @@ -186,7 +186,7 @@ public class CtfTmfEventTest { @Test public void testGetTimestamp() { CtfTmfEvent nullEvent = CtfTmfEvent.getNullEvent(); - long result = nullEvent.getTimestampValue(); + long result = nullEvent.getTimestamp().getValue(); assertEquals(-1L, result); } diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfLightweightContextTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfLightweightContextTest.java index 874d40395e..c345509736 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfLightweightContextTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfLightweightContextTest.java @@ -98,7 +98,7 @@ public class CtfTmfLightweightContextTest { fixture.getNext(lwc); synchronized(fixture){ if (lwc.getCurrentEvent() != null) { - vals.add(lwc.getCurrentEvent().getTimestampValue()); + vals.add(lwc.getCurrentEvent().getTimestamp().getValue()); } tooManyContexts.add(lwc); } diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfIterator.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfIterator.java index 62add563df..a9fd93f604 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfIterator.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfIterator.java @@ -78,7 +78,7 @@ public class CtfIterator extends CTFTraceReader implements ITmfContext, this.ctfTmfTrace = trace; if (this.hasMoreEvents()) { this.curLocation = new CtfLocation(ctfLocationData); - if (this.getCurrentEvent().getTimestampValue() != ctfLocationData.getTimestamp()) { + if (this.getCurrentEvent().getTimestamp().getValue() != ctfLocationData.getTimestamp()) { this.seek(ctfLocationData); this.curRank = rank; } @@ -135,10 +135,10 @@ public class CtfIterator extends CTFTraceReader implements ITmfContext, */ long index = 0; if (this.getCurrentEvent() != null) { - currTimestamp = this.getCurrentEvent().getTimestampValue(); + currTimestamp = this.getCurrentEvent().getTimestamp().getValue(); for (long i = 0; i < ctfLocationData.getIndex(); i++) { - if (currTimestamp == this.getCurrentEvent().getTimestampValue()) { + if (currTimestamp == this.getCurrentEvent().getTimestamp().getValue()) { index++; } else { index = 0; @@ -150,7 +150,7 @@ public class CtfIterator extends CTFTraceReader implements ITmfContext, } /* Seek the current location accordingly */ if (ret) { - curLocation.setLocation(new CtfLocationData(getCurrentEvent().getTimestampValue(), index)); + curLocation.setLocation(new CtfLocationData(getCurrentEvent().getTimestamp().getValue(), index)); } else { curLocation = NULL_LOCATION; } @@ -253,7 +253,7 @@ public class CtfIterator extends CTFTraceReader implements ITmfContext, boolean ret = super.advance(); if (ret) { - final long timestampValue = getCurrentEvent().getTimestampValue(); + final long timestampValue = getCurrentEvent().getTimestamp().getValue(); if (timestamp == timestampValue) { curLocation.setLocation(timestampValue, index + 1); } else { diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEvent.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEvent.java index 19bd9bf18c..eda49afb0f 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEvent.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEvent.java @@ -6,7 +6,8 @@ * accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * - * Contributors: Alexandre Montplaisir - Initial API and implementation + * Contributors: + * Alexandre Montplaisir - Initial API and implementation *******************************************************************************/ package org.eclipse.linuxtools.tmf.core.ctfadaptor; @@ -50,7 +51,7 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable { // ------------------------------------------------------------------------ private final CtfTmfTrace fTrace; - private final long timestamp; + private final ITmfTimestamp fTimestamp; private final int sourceCPU; private final long typeId; private final String eventName; @@ -78,7 +79,7 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable { this.fTrace = originTrace; if (eventDef == null) { - this.timestamp = -1; + this.fTimestamp = new CtfTmfTimestamp(-1); this.sourceCPU = -1; this.typeId = -1; this.fileName = NO_STREAM; @@ -88,7 +89,8 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable { } /* Read the base event info */ - this.timestamp = this.getTrace().getCTFTrace().timestampCyclesToNanos(eventDef.getTimestamp()); + long ts = this.getTrace().getCTFTrace().timestampCyclesToNanos(eventDef.getTimestamp()); + this.fTimestamp = new CtfTmfTimestamp(ts); this.sourceCPU = eventDef.getCPU(); this.typeId = eventDef.getDeclaration().getId(); this.eventName = eventDef.getDeclaration().getName(); @@ -101,12 +103,8 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable { /** * Extract the field information from the structDefinition haze-inducing * mess, and put them into something ITmfEventField can cope with. - * - * @param eventDef - * CTF EventDefinition to read - * @return CtfTmfEventField[] The array of fields that were read */ - public static CtfTmfEventField[] parseFields(EventDefinition eventDef) { + private static CtfTmfEventField[] parseFields(EventDefinition eventDef) { List fields = new ArrayList(); StructDefinition structFields = eventDef.getFields(); @@ -151,9 +149,17 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable { * CtfTmfEvent to copy */ public CtfTmfEvent(CtfTmfEvent other) { + /* There is only one reference to the trace, so we can shallow-copy it */ this.fTrace = other.getTrace(); + + /* + * Copy the timestamp + * FIXME This can be switched to a shallow-copy once timestamps are + * made immutable. + */ + this.fTimestamp = new CtfTmfTimestamp(other.fTimestamp.getValue()); + /* Primitives, those will be copied by value */ - this.timestamp = other.timestamp; this.sourceCPU = other.sourceCPU; this.typeId = other.typeId; @@ -163,7 +169,6 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable { /* Copy the fields over */ this.fContent = other.fContent.clone(); - this.fTimestamp = other.fTimestamp.clone(); } /** @@ -176,7 +181,7 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable { */ public CtfTmfEvent() { this.fTrace = null; - this.timestamp = -1; + this.fTimestamp = new CtfTmfTimestamp(-1); this.sourceCPU = -1; this.typeId = -1; this.fileName = NO_STREAM; @@ -188,42 +193,34 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable { // Getters/Setters/Predicates // ------------------------------------------------------------------------ - private static CtfTmfEvent nullEvent = null; + private static CtfTmfEvent nullEvent = new CtfTmfEvent(); /** * Get a null event * - * @return an empty event. */ + * @return An empty event. + */ public static CtfTmfEvent getNullEvent() { - if (nullEvent == null) { - nullEvent = new CtfTmfEvent(); - } return nullEvent; } - /** - * Gets the current timestamp of the event - * - * @return the current timestamp (long) */ - public long getTimestampValue() { - return this.timestamp; - } - /** * Gets the cpu core the event was recorded on. * - * @return the cpu id for a given source. In lttng it's from CPUINFO */ + * @return The cpu id for a given source. In lttng it's from CPUINFO + */ public int getCPU() { return this.sourceCPU; } /** - * Return this event's ID, according to the trace's metadata. Watch out, - * this ID is not constant from one trace to another for the same event - * types! Use "getEventName()" for a constant reference. + * Return this event's ID, according to the trace's metadata. * - - * @return the event ID */ + * Watch out, this ID is not constant from one trace to another for the same + * event types! Use "getEventName()" for a constant reference. + * + * @return The event ID + */ public long getID() { return this.typeId; } @@ -231,7 +228,8 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable { /** * Gets the name of a current event. * - * @return the event name */ + * @return The event name + */ public String getEventName() { return eventName; } @@ -239,69 +237,34 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable { /** * Gets the channel name of a field. * - * @return the channel name. */ + * @return The channel name. + */ public String getChannelName() { return this.fileName; } - /** - * Method getTrace. - * @return CtfTmfTrace - * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTrace() - */ @Override public CtfTmfTrace getTrace() { return fTrace; } - /** - * Method getRank. - * @return long - * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getRank() - */ @Override public long getRank() { // TODO Auto-generated method stub return 0; } - private ITmfTimestamp fTimestamp = null; - - // TODO Benchmark if the singleton approach is faster than just - // instantiating a final fTimestramp right away at creation time - /** - * Method getTimestamp. - * @return ITmfTimestamp - * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTimestamp() - */ @Override public ITmfTimestamp getTimestamp() { - if (fTimestamp == null) { - fTimestamp = new CtfTmfTimestamp(timestamp); - } return fTimestamp; } - String fSource = null; - /** - * Method getSource. - * @return String - * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getSource() - */ @Override public String getSource() { // TODO Returns CPU for now - if(fSource == null) { - fSource= Integer.toString(getCPU()); - } - return fSource; + return Integer.toString(getCPU()); } - /** - * Method getType. - * @return ITmfEventType - * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getType() - */ @Override public ITmfEventType getType() { CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(eventName); @@ -311,35 +274,16 @@ public final class CtfTmfEvent implements ITmfEvent, Cloneable { return ctfTmfEventType; } - /** - * Method getContent. - * @return ITmfEventField - * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getContent() - */ @Override public ITmfEventField getContent() { return fContent; } - String fReference = null; - /** - * Method getReference. - * @return String - * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getReference() - */ @Override public String getReference() { - if( fReference == null){ - fReference = getChannelName(); - } - return fReference; + return getChannelName(); } - /** - * Method clone. - * @return CtfTmfEvent - * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#clone() - */ @Override public CtfTmfEvent clone() { return new CtfTmfEvent(this); diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfLightweightContext.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfLightweightContext.java index 1b79f303e7..df6bf13c47 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfLightweightContext.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfLightweightContext.java @@ -125,7 +125,7 @@ public class CtfTmfLightweightContext implements ITmfContext { CtfTmfEvent currentEvent = getIterator().getCurrentEvent(); if (currentEvent != null) { - final long timestampValue = currentEvent.getTimestampValue(); + final long timestampValue = currentEvent.getTimestamp().getValue(); if (curLocationData.getTimestamp() == timestampValue) { curLocation.setLocation(timestampValue, curLocationData.getIndex() + 1); } else { diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfTrace.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfTrace.java index 55ad3b8ed1..8c82e9b3d0 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfTrace.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfTrace.java @@ -217,7 +217,7 @@ public class CtfTmfTrace extends TmfTrace implements ITmfEventParser{ if (location == null) { CtfTmfEvent event = getIterator(this, context).getCurrentEvent(); if (event != null) { - currentLocation.setLocation(event.getTimestampValue(), 0); + currentLocation.setLocation(event.getTimestamp().getValue(), 0); } } if(context.getRank() != 0) { -- 2.34.1