Skip to content

Commit 3b76859

Browse files
PatrickTassebhufmann
authored andcommitted
Fix ts decimal format precision
The timestamp field ts, which should be in us with 3 decimals, was losing the precision of its decimals due to double rounding error. Use String.format() with units and decimals as separate long values. Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
1 parent ecd78da commit 3b76859

2 files changed

Lines changed: 84 additions & 86 deletions

File tree

src/main/java/org/eclipse/tracecompass/traceeventlogger/LogUtils.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
*******************************************************************************/
2424
package org.eclipse.tracecompass.traceeventlogger;
2525

26-
import java.text.DecimalFormat;
27-
import java.text.Format;
2826
import java.util.Arrays;
2927
import java.util.HashMap;
3028
import java.util.HashSet;
@@ -142,7 +140,7 @@ public interface IFlowScopeLog {
142140
int getId();
143141
}
144142

145-
private static final Format FORMAT = new DecimalFormat("#.###"); //$NON-NLS-1$
143+
private static final String FORMAT = "%d.%03d"; //$NON-NLS-1$
146144

147145
/**
148146
* Offset between System.currentTimeMillis() and System.nanoTime() in nanoseconds.
@@ -153,7 +151,7 @@ public interface IFlowScopeLog {
153151
// Get current time in both bases
154152
long currentMillis = System.currentTimeMillis();
155153
long nanoTime = System.nanoTime();
156-
154+
157155
// Convert millis to nanos and calculate offset, handling potential overflow
158156
long millisToNanos = currentMillis * 1_000_000L;
159157
TIME_OFFSET = millisToNanos - nanoTime;
@@ -163,7 +161,7 @@ public interface IFlowScopeLog {
163161
* Gets the current time in nanoseconds since the Unix epoch.
164162
* This maintains nanosecond precision while being comparable to timestamps from
165163
* System.currentTimeMillis().
166-
*
164+
*
167165
* @return Current time in nanoseconds since Unix epoch
168166
*/
169167
private static long currentTimeNanos() {
@@ -920,7 +918,7 @@ public static void traceMarker(Logger logger, Level level, String name, long dur
920918
* USE ME FIRST
921919
*/
922920
private static StringBuilder appendCommon(StringBuilder appendTo, char phase, long time, long threadId) {
923-
writeObject(appendTo, TIMESTAMP, FORMAT.format((double) time / 1000)).append(','); // $NON-NLS-1$
921+
writeObject(appendTo, TIMESTAMP, String.format(FORMAT, time / 1000, time % 1000)).append(','); // $NON-NLS-1$
924922
writeObject(appendTo, PHASE, phase).append(',');
925923
writeObject(appendTo, TID, threadId).append(',');
926924
return writeObject(appendTo, PID, threadId); // $NON-NLS-1$

0 commit comments

Comments
 (0)