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