1 /*******************************************************************************
2 * Copyright (c) 2012 Ericsson
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
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.tests
.headless
;
15 import java
.text
.DateFormat
;
16 import java
.text
.SimpleDateFormat
;
17 import java
.util
.Date
;
18 import java
.util
.Vector
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTraceReader
;
25 public class ReadTrace
{
30 @SuppressWarnings("nls")
31 public static void main(String
[] args
) {
32 final String TRACE_PATH
= "/home/ematkho/lttng-traces/lt-hello-4175-20120405-092230";
34 // Change this to enable text output
35 final boolean USE_TEXT
= true;
37 final int LOOP_COUNT
= 1;
41 Vector
<Double
> benchs
= new Vector
<Double
>();
42 CTFTrace trace
= null;
44 for (int loops
= 0; loops
< LOOP_COUNT
; loops
++) {
47 trace
= new CTFTrace(TRACE_PATH
);
48 } catch (CTFReaderException e
) {
52 start
= System
.nanoTime();
54 System
.out
.println("Event, " + " Time, " + " type, " + " CPU ");
57 CTFTraceReader traceReader
= new CTFTraceReader(trace
);
59 start
= System
.nanoTime();
61 while (traceReader
.hasMoreEvents()) {
62 EventDefinition ed
= traceReader
.getCurrentEventDef();
64 if (prev
== traceReader
.getIndex()) {
65 System
.out
.println("Error on events " + prev
);
67 prev
= traceReader
.getIndex();
69 String output
= formatDate(ed
.timestamp
71 System
.out
.println(traceReader
.getIndex() + ", "
72 + output
+ ", " + ed
.getDeclaration().getName()
73 + ", " + ed
.getCPU() + ed
.getFields().toString()) ;
76 traceReader
.advance();
79 stop
= System
.nanoTime();
80 System
.out
.print('.');
81 double time
= (stop
- start
) / (double) nbEvent
;
84 System
.out
.println("");
86 for (Double val
: benchs
) {
90 System
.out
.println("Time to read " + nbEvent
+ " events = " + avg
92 for (Double val
: benchs
) {
93 System
.out
.print(val
);
94 System
.out
.print(", ");
103 private static long getTimestamp(CTFTraceReader fixture
) {
104 if (fixture
.getCurrentEventDef() != null) {
105 return fixture
.getCurrentEventDef().timestamp
;
107 return Long
.MIN_VALUE
;
110 public static void testSeekIndex(CTFTrace trace
) {
111 CTFTraceReader fixture
= new CTFTraceReader(trace
);
113 long timeRank
= 4281275394331L;
114 long nearEnd
= 4287422858132L;
116 long seekIndex_0
= 0;
117 long seekNext_300000
= 0;
118 long seekIndex_300000
= 0;
119 long seekTime_300000
= 0;
120 String cr
= "\n"; //$NON-NLS-1$
122 for (int i
= 0; i
< 100; i
++) {
126 fixture
.seek(nearEnd
);
128 * we need to read the trace before seeking
131 seekTime_0
= getTimestamp(fixture
);
132 for (int i
= 0; i
< rank
; i
++) {
135 seekNext_300000
= getTimestamp(fixture
);
136 fixture
.seek(timeRank
);
137 seekTime_300000
= getTimestamp(fixture
);
138 fixture
.seekIndex(0);
139 seekIndex_0
= getTimestamp(fixture
);
141 fixture
.seekIndex(rank
);
142 seekIndex_300000
= getTimestamp(fixture
);
143 System
.out
.print(cr
);
144 System
.out
.println("seek(0) " + seekTime_0
+ cr
+ //$NON-NLS-1$
145 "seekIndex(0) " + seekIndex_0
+ cr
+ //$NON-NLS-1$
146 "Next(300000) " + seekNext_300000
+ cr
+ //$NON-NLS-1$
147 "seek(time(300000)) " + seekTime_300000
+ cr
+ //$NON-NLS-1$
148 "seekIndex(300000) " + seekIndex_300000
//$NON-NLS-1$
154 * the timestamp in UTC to convert to nanoseconds.
155 * @return formatted string.
157 private static String
formatDate(long timestamp
) {
158 Date d
= new Date(timestamp
/ 1000000);
159 DateFormat df
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss."); //$NON-NLS-1$
160 String output
= df
.format(d
) + (timestamp
% 1000000000);