Commit | Line | Data |
---|---|---|
dee39dbd MD |
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.IStateSystemQuerier; | |
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 | public static void main(String[] args) { | |
38 | /* Read a trace and build the state system */ | |
39 | try { | |
40 | File newStateFile = new File("/tmp/helloworldctf.ht"); //$NON-NLS-1$ | |
41 | IStateChangeInput input = new CtfKernelStateInput(CtfTestFiles.getTestTrace()); | |
42 | IStateSystemQuerier ss = StateSystemManager.loadStateHistory(newStateFile, input, true); | |
43 | ||
44 | requestExample(ss); | |
45 | } catch (TmfTraceException e) { | |
46 | e.printStackTrace(); | |
47 | } | |
48 | } | |
49 | ||
50 | /** | |
51 | * From a state system tree previously built with a CTF kernel trace, print | |
52 | * to the console the interval of each state and the ID of the current | |
53 | * thread running on each CPU. | |
54 | * | |
55 | * @param ssb | |
56 | * the State System Builder through which make request | |
57 | */ | |
58 | private static void requestExample(final IStateSystemQuerier ssb) { | |
59 | try { | |
60 | /* Request the current thread executing on each CPU */ | |
61 | List<Integer> currentThreadByCPUS; | |
62 | List<ITmfStateInterval> stateIntervals; | |
63 | StringBuilder output = new StringBuilder(); | |
64 | ||
65 | currentThreadByCPUS = ssb.getQuarks(Attributes.CPUS, "*", Attributes.CURRENT_THREAD); //$NON-NLS-1$ | |
66 | ||
67 | for (Integer currentThread : currentThreadByCPUS) { | |
68 | stateIntervals = ssb.queryHistoryRange(currentThread.intValue(), ssb.getStartTime(), | |
69 | ssb.getCurrentEndTime()); | |
70 | ||
71 | /* Output formatting */ | |
72 | output.append("Value of attribute : "); //$NON-NLS-1$ | |
73 | output.append(ssb.getFullAttributePath(currentThread.intValue())); | |
74 | output.append("\n------------------------------------------------\n"); //$NON-NLS-1$ | |
75 | for (ITmfStateInterval stateInterval : stateIntervals) { | |
76 | /* Print the interval */ | |
77 | output.append('['); | |
78 | output.append(String.valueOf(stateInterval.getStartTime())); | |
79 | output.append(", "); //$NON-NLS-1$ | |
80 | output.append(String.valueOf(stateInterval.getEndTime())); | |
81 | output.append(']'); | |
82 | /* Print the attribute value */ | |
83 | output.append(" = "); //$NON-NLS-1$ | |
84 | output.append(stateInterval.getStateValue().unboxInt()); | |
85 | output.append('\n'); | |
86 | } | |
87 | } | |
88 | System.out.println(output.toString()); | |
89 | } catch (TimeRangeException e) { | |
90 | e.printStackTrace(); | |
91 | } catch (AttributeNotFoundException e) { | |
92 | e.printStackTrace(); | |
93 | } catch (StateValueTypeException e) { | |
94 | e.printStackTrace(); | |
95 | } | |
96 | } | |
97 | } |