Commit | Line | Data |
---|---|---|
dffc234f | 1 | /******************************************************************************* |
7b72df0d | 2 | * Copyright (c) 2014, 2016 École Polytechnique de Montréal and others |
dffc234f GB |
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 | * Geneviève Bastien - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
8b131c41 | 13 | package org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.cpuusage; |
dffc234f | 14 | |
178d3c0e JCK |
15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
16 | ||
17 | import java.util.Set; | |
18 | import java.util.TreeSet; | |
19 | ||
20 | import org.eclipse.jdt.annotation.NonNull; | |
7b72df0d | 21 | import org.eclipse.jdt.annotation.Nullable; |
e9a0d1cb GB |
22 | import org.eclipse.jface.viewers.ISelection; |
23 | import org.eclipse.jface.viewers.ISelectionChangedListener; | |
24 | import org.eclipse.jface.viewers.IStructuredSelection; | |
25 | import org.eclipse.jface.viewers.SelectionChangedEvent; | |
54404589 MAL |
26 | import org.eclipse.swt.events.ControlAdapter; |
27 | import org.eclipse.swt.events.ControlEvent; | |
dffc234f | 28 | import org.eclipse.swt.widgets.Composite; |
3fda290f | 29 | import org.eclipse.tracecompass.analysis.os.linux.core.signals.TmfCpuSelectedSignal; |
178d3c0e | 30 | import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Activator; |
3fda290f | 31 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler; |
2bdf0193 AM |
32 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal; |
33 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
178d3c0e | 34 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceContext; |
21852dfa | 35 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
7b72df0d BH |
36 | import org.eclipse.tracecompass.tmf.ui.viewers.TmfViewer; |
37 | import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.TmfXYChartViewer; | |
38 | import org.eclipse.tracecompass.tmf.ui.views.TmfChartView; | |
dffc234f | 39 | |
178d3c0e JCK |
40 | import com.google.common.base.Predicates; |
41 | import com.google.common.collect.Iterables; | |
42 | ||
dffc234f GB |
43 | /** |
44 | * CPU usage view. It contains 2 viewers: one tree viewer showing all the | |
45 | * threads who were on the CPU in the time range, and one XY chart viewer | |
46 | * plotting the total time spent on CPU and the time of the threads selected in | |
47 | * the tree viewer. | |
48 | * | |
49 | * @author Geneviève Bastien | |
50 | */ | |
7b72df0d | 51 | public class CpuUsageView extends TmfChartView { |
dffc234f GB |
52 | |
53 | /** ID string */ | |
143e0680 | 54 | public static final @NonNull String ID = "org.eclipse.tracecompass.analysis.os.linux.views.cpuusage"; //$NON-NLS-1$ |
dffc234f | 55 | |
178d3c0e JCK |
56 | /** ID of the selected thread in the map of data in {@link TmfTraceContext} */ |
57 | public static final @NonNull String CPU_USAGE_SELECTED_THREAD = ID + ".CPU_USAGE_SELECTED_TRHEAD"; //$NON-NLS-1$ | |
58 | ||
59 | /** ID of the followed CPU in the map data in {@link TmfTraceContext} */ | |
60 | public static final @NonNull String CPU_USAGE_FOLLOW_CPU = ID + ".FOLLOW_CPU"; //$NON-NLS-1$ | |
61 | ||
7b72df0d BH |
62 | private @Nullable CpuUsageComposite fTreeViewer = null; |
63 | private @Nullable CpuUsageXYViewer fXYViewer = null; | |
54404589 | 64 | |
dffc234f GB |
65 | /** |
66 | * Constructor | |
67 | */ | |
68 | public CpuUsageView() { | |
69 | super(Messages.CpuUsageView_Title); | |
70 | } | |
71 | ||
72 | @Override | |
73 | public void createPartControl(Composite parent) { | |
54404589 MAL |
74 | super.createPartControl(parent); |
75 | ||
7b72df0d BH |
76 | /* Initialize the viewers with the currently selected trace */ |
77 | ITmfTrace trace = TmfTraceManager.getInstance().getActiveTrace(); | |
78 | if (trace != null) { | |
79 | TmfTraceSelectedSignal signal = new TmfTraceSelectedSignal(this, trace); | |
80 | if (fTreeViewer != null) { | |
81 | fTreeViewer.traceSelected(signal); | |
82 | } | |
83 | if (fXYViewer != null) { | |
84 | fXYViewer.traceSelected(signal); | |
85 | } | |
86 | } | |
87 | } | |
dffc234f | 88 | |
7b72df0d BH |
89 | @Override |
90 | protected TmfXYChartViewer createChartViewer(Composite parent) { | |
91 | CpuUsageXYViewer viewer = new CpuUsageXYViewer(parent); | |
92 | viewer.setSendTimeAlignSignals(true); | |
93 | fXYViewer = viewer; | |
94 | return viewer; | |
95 | } | |
dffc234f | 96 | |
7b72df0d BH |
97 | @Override |
98 | public TmfViewer createLeftChildViewer(Composite parent) { | |
99 | final CpuUsageComposite viewer = new CpuUsageComposite(parent); | |
e9a0d1cb GB |
100 | |
101 | /* Add selection listener to tree viewer */ | |
7b72df0d | 102 | viewer.addSelectionChangeListener(new ISelectionChangedListener() { |
e9a0d1cb GB |
103 | @Override |
104 | public void selectionChanged(SelectionChangedEvent event) { | |
105 | ISelection selection = event.getSelection(); | |
106 | if (selection instanceof IStructuredSelection) { | |
107 | Object structSelection = ((IStructuredSelection) selection).getFirstElement(); | |
108 | if (structSelection instanceof CpuUsageEntry) { | |
109 | CpuUsageEntry entry = (CpuUsageEntry) structSelection; | |
7b72df0d BH |
110 | if (fTreeViewer != null) { |
111 | fTreeViewer.setSelectedThread(entry.getTid()); | |
112 | } | |
113 | if (fXYViewer != null) { | |
114 | fXYViewer.setSelectedThread(Long.valueOf(entry.getTid())); | |
115 | } | |
178d3c0e | 116 | saveData(CPU_USAGE_SELECTED_THREAD, entry.getTid()); |
e9a0d1cb GB |
117 | } |
118 | } | |
119 | } | |
120 | }); | |
dffc234f | 121 | |
7b72df0d | 122 | viewer.getControl().addControlListener(new ControlAdapter() { |
54404589 MAL |
123 | @Override |
124 | public void controlResized(ControlEvent e) { | |
125 | super.controlResized(e); | |
126 | } | |
127 | }); | |
dffc234f | 128 | |
7b72df0d BH |
129 | fTreeViewer = viewer; |
130 | return fTreeViewer; | |
dffc234f GB |
131 | } |
132 | ||
178d3c0e JCK |
133 | /** |
134 | * Save a data in the data map of {@link TmfTraceContext} | |
135 | */ | |
136 | private static void saveData(@NonNull String key, Object data) { | |
137 | TmfTraceContext ctx = TmfTraceManager.getInstance().getCurrentTraceContext(); | |
138 | ctx.setData(key, checkNotNull(data)); | |
139 | } | |
140 | ||
141 | private static Object getData(@NonNull String key) { | |
142 | TmfTraceContext ctx = TmfTraceManager.getInstance().getCurrentTraceContext(); | |
143 | return ctx.getData(key); | |
144 | } | |
145 | ||
dffc234f GB |
146 | @Override |
147 | public void setFocus() { | |
3df2eabe | 148 | if (fXYViewer != null) { |
7b72df0d | 149 | fXYViewer.getControl().setFocus(); |
54404589 | 150 | } |
54404589 MAL |
151 | } |
152 | ||
3fda290f MK |
153 | /** |
154 | * Signal handler for when a cpu is selected | |
155 | * | |
156 | * @param signal | |
157 | * the cpu being selected | |
158 | * @since 2.0 | |
159 | */ | |
160 | @TmfSignalHandler | |
161 | public void cpuSelect(TmfCpuSelectedSignal signal) { | |
162 | final @Nullable CpuUsageXYViewer xyViewer = fXYViewer; | |
163 | final @Nullable CpuUsageComposite treeViewer = fTreeViewer; | |
164 | if (xyViewer != null && treeViewer != null) { | |
178d3c0e JCK |
165 | Object data = getData(CPU_USAGE_FOLLOW_CPU); |
166 | if (data == null) { | |
167 | data = new TreeSet<Integer>(); | |
168 | } | |
169 | if (data instanceof Set<?>) { | |
170 | Set<?> set = (Set<?>) data; | |
171 | int core = signal.getCore(); | |
172 | if (core >= 0) { | |
173 | xyViewer.addCpu(core); | |
174 | treeViewer.addCpu(core); | |
175 | if (Iterables.all(set, Predicates.instanceOf(Integer.class))) { | |
176 | @SuppressWarnings("unchecked") | |
177 | Set<Integer> intSet = (Set<Integer>) set; | |
178 | intSet.add(core); | |
179 | } | |
180 | } else { | |
181 | xyViewer.clearCpu(); | |
182 | treeViewer.clearCpu(); | |
183 | ((Set<?>) data).clear(); | |
184 | } | |
185 | saveData(CPU_USAGE_FOLLOW_CPU, data); | |
3fda290f | 186 | } else { |
178d3c0e | 187 | Activator.getDefault().logError("The followed cores should have been store in a Set"); //$NON-NLS-1$ |
3fda290f MK |
188 | } |
189 | } | |
190 | } | |
191 | ||
dffc234f | 192 | } |