f8f33773e73447d9425c0131be2b82dd20e19880
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.kernel.core.tests / src / org / eclipse / tracecompass / lttng2 / kernel / core / tests / analysis / kernel / statesystem / GenerateTestValues.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Alexandre Montplaisir - Initial API and implementation
11 ******************************************************************************/
12
13 package org.eclipse.tracecompass.lttng2.kernel.core.tests.analysis.kernel.statesystem;
14
15 import java.io.File;
16 import java.io.FileWriter;
17 import java.io.PrintWriter;
18 import java.util.List;
19
20 import org.eclipse.tracecompass.internal.lttng2.kernel.core.trace.layout.LttngEventLayout;
21 import org.eclipse.tracecompass.lttng2.kernel.core.analysis.kernel.LttngKernelStateProvider;
22 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
23 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
24 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
25 import org.eclipse.tracecompass.tmf.core.statesystem.ITmfStateProvider;
26 import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
27 import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTrace;
28 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
29
30 /**
31 * Small program to regenerate the values used in "TestValues.java" from the
32 * current LTTng-kernel state provider.
33 *
34 * It will write its output the a file called 'TestValues<something>.java' in your
35 * temporary files directory.
36 *
37 * @author Alexandre Montplaisir
38 */
39 public class GenerateTestValues {
40
41 private static CtfTmfTestTrace testTrace = CtfTmfTestTrace.TRACE2;
42 private static final long targetTimestamp = 18670067372290L + 1331649577946812237L;
43 private static final String INDENT = " ";
44
45 /**
46 * Run the program
47 *
48 * @param args
49 * Command-line arguments, unused.
50 * @throws Exception
51 * I'm messing with Exception. Come at me bro!
52 */
53 public static void main(String[] args) throws Exception {
54 if (!testTrace.exists()) {
55 System.err.println("Trace files not present.");
56 return;
57 }
58
59 /* Prepare the files */
60 File logFile = File.createTempFile("TestValues", ".java");
61 try (final CtfTmfTrace trace = testTrace.getTrace();
62 PrintWriter writer = new PrintWriter(new FileWriter(logFile), true);) {
63
64 /* Build and query the state system */
65 TmfStateSystemAnalysisModule module = new TmfStateSystemAnalysisModule() {
66 @Override
67 protected ITmfStateProvider createStateProvider() {
68 return new LttngKernelStateProvider(trace, LttngEventLayout.getInstance());
69 }
70
71 @Override
72 protected String getSsFileName() {
73 return "test-values";
74 }
75 };
76
77 module.setTrace(trace);
78 module.setId("test-values");
79 module.schedule();
80 module.waitForCompletion();
81 ITmfStateSystem ssq = module.getStateSystem();
82 if (ssq == null) {
83 throw new IllegalStateException();
84 }
85
86 List<ITmfStateInterval> fullState = ssq.queryFullState(targetTimestamp);
87
88 /* Start printing the java file's contents */
89 writer.println("interface TestValues {");
90 writer.println();
91 writer.println(INDENT + "static final int size = " + fullState.size() + ";");
92 writer.println();
93
94 /* Print the array contents */
95 writer.println(INDENT + "static final long[] startTimes = {");
96 for (ITmfStateInterval interval : fullState) {
97 writer.println(INDENT + INDENT + String.valueOf(interval.getStartTime()) + "L,");
98 }
99 writer.println(INDENT + "};");
100 writer.println();
101
102 writer.println(INDENT + "static final long[] endTimes = {");
103 for (ITmfStateInterval interval : fullState) {
104 writer.println(INDENT + INDENT + String.valueOf(interval.getEndTime()) + "L,");
105 }
106 writer.println(INDENT + "};");
107 writer.println();
108
109 writer.println(INDENT + "static final ITmfStateValue[] values = {");
110 for (ITmfStateInterval interval : fullState) {
111 ITmfStateValue val = interval.getStateValue();
112 writer.print(INDENT + INDENT);
113
114 switch (val.getType()) {
115 case NULL:
116 writer.println("TmfStateValue.nullValue(),");
117 break;
118 case INTEGER:
119 writer.println("TmfStateValue.newValueInt(" + val.unboxInt() + "),");
120 break;
121 case LONG:
122 writer.println("TmfStateValue.newValueLong(" + val.unboxLong() + "),");
123 break;
124 case DOUBLE:
125 writer.println("TmfStateValue.newValueDouble(" + val.unboxDouble() + "),");
126 break;
127 case STRING:
128 writer.println("TmfStateValue.newValueString(\"" + val.unboxStr() + "\"),");
129 break;
130 default:
131 writer.println(val.toString());
132 break;
133 }
134 }
135 writer.println(INDENT + "};");
136
137 writer.println("}");
138 writer.println();
139
140 module.dispose();
141 }
142 System.exit(0);
143 }
144
145 }
This page took 0.043885 seconds and 4 git commands to generate.