Revisit the TmfExperiment structure
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.kernel.ui / src / org / eclipse / linuxtools / internal / lttng2 / kernel / ui / views / resources / ResourcesView.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 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.lttng2.kernel.ui.views.resources;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Map;
20
21 import org.eclipse.linuxtools.lttng2.kernel.core.trace.Attributes;
22 import org.eclipse.linuxtools.lttng2.kernel.core.trace.CtfKernelTrace;
23 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
24 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
25 import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException;
26 import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
27 import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
28 import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
29 import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentSelectedSignal;
30 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
31 import org.eclipse.linuxtools.tmf.core.statesystem.IStateSystemQuerier;
32 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
33 import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
34 import org.eclipse.linuxtools.tmf.ui.views.TmfView;
35 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.TimeGraphProvider;
36 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.TimeGraphViewer;
37 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;
38 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
39 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeEvent;
40 import org.eclipse.swt.SWT;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.swt.widgets.Display;
43
44 public class ResourcesView extends TmfView {
45
46 // ------------------------------------------------------------------------
47 // Constants
48 // ------------------------------------------------------------------------
49
50 /**
51 * View ID.
52 */
53 public static final String ID = "org.eclipse.linuxtools.lttng2.kernel.ui.views.resources"; //$NON-NLS-1$
54
55 // ------------------------------------------------------------------------
56 // Fields
57 // ------------------------------------------------------------------------
58
59 // The time graph viewer
60 TimeGraphViewer fTimeGraphViewer;
61
62 // The selected experiment
63 private TmfExperiment<ITmfEvent> fSelectedExperiment;
64
65 // The time graph entry list
66 private ArrayList<ITimeGraphEntry> fEntryList;
67
68 // The start time
69 private long fStartTime;
70
71 // The end time
72 private long fEndTime;
73
74 // The display width
75 private int fDisplayWidth;
76
77 // ------------------------------------------------------------------------
78 // Classes
79 // ------------------------------------------------------------------------
80
81 private class GroupEntry implements ITimeGraphEntry {
82 public ITimeGraphEntry fParent;
83 public ArrayList<ITimeGraphEntry> fChildren;
84 public String fName;
85
86 public GroupEntry(ITimeGraphEntry parent, String name) {
87 fParent = parent;
88 fChildren = new ArrayList<ITimeGraphEntry>();
89 fName = name;
90 }
91
92 @Override
93 public ITimeGraphEntry getParent() {
94 return fParent;
95 }
96
97 @Override
98 public boolean hasChildren() {
99 return fChildren != null && fChildren.size() > 0;
100 }
101
102 @Override
103 public ITimeGraphEntry[] getChildren() {
104 return fChildren.toArray(new ITimeGraphEntry[0]);
105 }
106
107 @Override
108 public String getName() {
109 return fName;
110 }
111
112 @Override
113 public long getStartTime() {
114 return -1;
115 }
116
117 @Override
118 public long getStopTime() {
119 return -1;
120 }
121
122 @Override
123 public Iterator<ITimeEvent> getTimeEventsIterator() {
124 return null;
125 }
126
127 @Override
128 public <T extends ITimeEvent> Iterator<T> getTimeEventsIterator(long startTime, long stopTime, long visibleDuration) {
129 return null;
130 }
131
132 public void addChild(ITimeGraphEntry entry) {
133 fChildren.add(entry);
134 }
135 }
136
137 // ------------------------------------------------------------------------
138 // Constructors
139 // ------------------------------------------------------------------------
140
141 public ResourcesView() {
142 super(ID);
143 fDisplayWidth = Display.getDefault().getBounds().width;
144 }
145
146 // ------------------------------------------------------------------------
147 // ViewPart
148 // ------------------------------------------------------------------------
149
150 /* (non-Javadoc)
151 * @see org.eclipse.linuxtools.tmf.ui.views.TmfView#createPartControl(org.eclipse.swt.widgets.Composite)
152 */
153 @Override
154 public void createPartControl(Composite parent) {
155 fTimeGraphViewer = new TimeGraphViewer(parent, SWT.NONE);
156
157 fTimeGraphViewer.setTimeGraphProvider(new TimeGraphProvider() {
158 @Override
159 public String getTraceClassName(ITimeGraphEntry trace) {
160 return "trace class"; //$NON-NLS-1$
161 }
162
163 @Override
164 public String getStateName(StateColor color) {
165 return "state name"; //$NON-NLS-1$
166 }
167
168 @Override
169 public String getEventName(ITimeEvent event, boolean upper, boolean extInfo) {
170 return "event name"; //$NON-NLS-1$
171 }
172
173 @Override
174 public Map<String, String> getEventHoverToolTipInfo(ITimeEvent event) {
175 return new HashMap<String, String>();
176 }
177
178 @Override
179 public StateColor getEventColor(ITimeEvent event) {
180 if (event.getTime() % 2 == 0) {
181 return StateColor.BLACK;
182 } else {
183 return StateColor.GRAY;
184 }
185 }
186 });
187
188 final Thread thread = new Thread() {
189 @Override
190 public void run() {
191 if (TmfExperiment.getCurrentExperiment() != null) {
192 selectExperiment(TmfExperiment.getCurrentExperiment());
193 }
194 }
195 };
196 thread.start();
197 }
198
199 /* (non-Javadoc)
200 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
201 */
202 @Override
203 public void setFocus() {
204 fTimeGraphViewer.setFocus();
205 }
206
207 // ------------------------------------------------------------------------
208 // Signal handlers
209 // ------------------------------------------------------------------------
210
211 @TmfSignalHandler
212 public void experimentSelected(final TmfExperimentSelectedSignal<? extends TmfEvent> signal) {
213 if (signal.getExperiment().equals(fSelectedExperiment)) {
214 return;
215 }
216
217 final Thread thread = new Thread() {
218 @Override
219 public void run() {
220 selectExperiment(signal.getExperiment());
221 }};
222 thread.start();
223 }
224
225 @SuppressWarnings("unchecked")
226 private void selectExperiment(TmfExperiment<?> experiment) {
227 fStartTime = Long.MAX_VALUE;
228 fEndTime = Long.MIN_VALUE;
229 fSelectedExperiment = (TmfExperiment<ITmfEvent>) experiment;
230 fEntryList = new ArrayList<ITimeGraphEntry>();
231 for (ITmfTrace<?> trace : experiment.getTraces()) {
232 GroupEntry groupEntry = new GroupEntry(null, trace.getPath());
233 fEntryList.add(groupEntry);
234 refresh();
235 if (trace instanceof CtfKernelTrace) {
236 CtfKernelTrace ctfKernelTrace = (CtfKernelTrace) trace;
237 IStateSystemQuerier ssq = ctfKernelTrace.getStateSystem();
238 long start = ssq.getStartTime();
239 long end = ssq.getCurrentEndTime();
240 fStartTime = Math.min(fStartTime, start);
241 fEndTime = Math.max(fEndTime, end);
242 List<Integer> cpuQuarks = ssq.getQuarks(Attributes.CPUS, "*"); //$NON-NLS-1$
243 for (int cpuQuark : cpuQuarks) {
244 String cpuName = "CPU " + ssq.getAttributeName(cpuQuark);
245 ResourcesEntry entry = new ResourcesEntry(groupEntry, ctfKernelTrace, cpuName);
246 try {
247 int currentThreadQuark = ssq.getQuarkRelative(cpuQuark, Attributes.CURRENT_THREAD);
248 long resolution = (end - start) / fDisplayWidth;
249 List<ITmfStateInterval> currentThreadIntervals = ssq.queryHistoryRange(currentThreadQuark, start, end, resolution);
250 for (ITmfStateInterval currentThreadInterval : currentThreadIntervals) {
251 if (!currentThreadInterval.getStateValue().isNull() && currentThreadInterval.getStateValue().getType() == 0) {
252 int currentThread = currentThreadInterval.getStateValue().unboxInt();
253 long startTime = currentThreadInterval.getStartTime();
254 long endTime = currentThreadInterval.getEndTime();
255 entry.addTraceEvent(new TimeEvent(entry, startTime, endTime - startTime));
256 }
257 }
258 } catch (AttributeNotFoundException e) {
259 e.printStackTrace();
260 } catch (TimeRangeException e) {
261 e.printStackTrace();
262 } catch (StateValueTypeException e) {
263 e.printStackTrace();
264 }
265 groupEntry.addChild(entry);
266 refresh();
267 }
268 }
269 }
270 }
271
272 private void refresh() {
273 Display.getDefault().asyncExec(new Runnable() {
274 @Override
275 public void run() {
276 if (fTimeGraphViewer.getControl().isDisposed()) {
277 return;
278 }
279 fTimeGraphViewer.setInput(fEntryList.toArray(new ITimeGraphEntry[0]));
280 fTimeGraphViewer.setTimeBounds(fStartTime, fEndTime);
281 fTimeGraphViewer.setStartFinishTime(fStartTime, fEndTime);
282 }
283 });
284 }
285
286 }
This page took 0.036985 seconds and 6 git commands to generate.