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