tmf: Add the Tmf- prefix to the state system interfaces
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.kernel.core.tests / src / org / eclipse / linuxtools / lttng2 / kernel / core / tests / headless / BasicStateSystemExample.java
1 /*******************************************************************************
2 * Copyright (c) 2012 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 * Mathieu Denis <mathieu.denis@polymtl.ca> - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.lttng2.kernel.core.tests.headless;
14
15 import java.io.File;
16 import java.util.List;
17
18 import org.eclipse.linuxtools.internal.lttng2.kernel.core.Attributes;
19 import org.eclipse.linuxtools.internal.lttng2.kernel.core.stateprovider.CtfKernelStateInput;
20 import org.eclipse.linuxtools.lttng2.kernel.core.tests.stateprovider.CtfTestFiles;
21 import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException;
22 import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
23 import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
24 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
25 import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
26 import org.eclipse.linuxtools.tmf.core.statesystem.IStateChangeInput;
27 import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
28 import org.eclipse.linuxtools.tmf.core.statesystem.StateSystemManager;
29
30 /**
31 * Simple example of how to use the state system using a CTF kernel trace.
32 *
33 * @author Mathieu Denis
34 */
35 public class BasicStateSystemExample {
36
37 /**
38 * Run the program
39 * @param args Arguments on the command-line
40 */
41 public static void main(String[] args) {
42 /* Read a trace and build the state system */
43 try {
44 File newStateFile = new File("/tmp/helloworldctf.ht"); //$NON-NLS-1$
45 IStateChangeInput input = new CtfKernelStateInput(CtfTestFiles.getTestTrace());
46 ITmfStateSystem ss = StateSystemManager.loadStateHistory(newStateFile, input, true);
47
48 requestExample(ss);
49 } catch (TmfTraceException e) {
50 e.printStackTrace();
51 }
52 }
53
54 /**
55 * From a state system tree previously built with a CTF kernel trace, print
56 * to the console the interval of each state and the ID of the current
57 * thread running on each CPU.
58 *
59 * @param ssb
60 * the State System Builder through which make request
61 */
62 private static void requestExample(final ITmfStateSystem ssb) {
63 try {
64 /* Request the current thread executing on each CPU */
65 List<Integer> currentThreadByCPUS;
66 List<ITmfStateInterval> stateIntervals;
67 StringBuilder output = new StringBuilder();
68
69 currentThreadByCPUS = ssb.getQuarks(Attributes.CPUS, "*", Attributes.CURRENT_THREAD); //$NON-NLS-1$
70
71 for (Integer currentThread : currentThreadByCPUS) {
72 stateIntervals = ssb.queryHistoryRange(currentThread.intValue(), ssb.getStartTime(),
73 ssb.getCurrentEndTime());
74
75 /* Output formatting */
76 output.append("Value of attribute : "); //$NON-NLS-1$
77 output.append(ssb.getFullAttributePath(currentThread.intValue()));
78 output.append("\n------------------------------------------------\n"); //$NON-NLS-1$
79 for (ITmfStateInterval stateInterval : stateIntervals) {
80 /* Print the interval */
81 output.append('[');
82 output.append(String.valueOf(stateInterval.getStartTime()));
83 output.append(", "); //$NON-NLS-1$
84 output.append(String.valueOf(stateInterval.getEndTime()));
85 output.append(']');
86 /* Print the attribute value */
87 output.append(" = "); //$NON-NLS-1$
88 output.append(stateInterval.getStateValue().unboxInt());
89 output.append('\n');
90 }
91 }
92 System.out.println(output.toString());
93 } catch (TimeRangeException e) {
94 e.printStackTrace();
95 } catch (AttributeNotFoundException e) {
96 e.printStackTrace();
97 } catch (StateValueTypeException e) {
98 e.printStackTrace();
99 }
100 }
101 }
This page took 0.03352 seconds and 6 git commands to generate.