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