c09da07ec5195e402031592a606667fbd7d13bcb
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.kernel.ui / src / org / eclipse / tracecompass / internal / lttng2 / kernel / ui / criticalpath / CriticalPathParameterProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2015 École Polytechnique de Montréal
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
10 package org.eclipse.tracecompass.internal.lttng2.kernel.ui.criticalpath;
11
12 import org.eclipse.jface.viewers.ISelection;
13 import org.eclipse.jface.viewers.IStructuredSelection;
14 import org.eclipse.tracecompass.analysis.graph.core.criticalpath.CriticalPathModule;
15 import org.eclipse.tracecompass.analysis.os.linux.core.model.HostThread;
16 import org.eclipse.tracecompass.analysis.os.linux.ui.views.controlflow.ControlFlowEntry;
17 import org.eclipse.tracecompass.analysis.os.linux.ui.views.controlflow.ControlFlowView;
18 import org.eclipse.tracecompass.internal.analysis.graph.ui.criticalpath.view.CriticalPathView;
19 import org.eclipse.tracecompass.internal.lttng2.kernel.core.analysis.graph.model.LttngWorker;
20 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
21 import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisParamProvider;
22 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
23 import org.eclipse.ui.IPartListener2;
24 import org.eclipse.ui.ISelectionListener;
25 import org.eclipse.ui.IViewPart;
26 import org.eclipse.ui.IWorkbench;
27 import org.eclipse.ui.IWorkbenchPage;
28 import org.eclipse.ui.IWorkbenchPart;
29 import org.eclipse.ui.IWorkbenchPartReference;
30 import org.eclipse.ui.IWorkbenchWindow;
31 import org.eclipse.ui.PlatformUI;
32
33 /**
34 * Class that provides parameters to the critical path analysis for lttng kernel
35 * traces
36 *
37 * @author Geneviève Bastien
38 */
39 public class CriticalPathParameterProvider extends TmfAbstractAnalysisParamProvider {
40
41 private final class IPartListener2Impl implements IPartListener2 {
42
43 private final Class<?> fType;
44
45 IPartListener2Impl(Class<?> type) {
46 fType = type;
47 }
48
49 private void toggleState(IWorkbenchPartReference partRef, boolean state) {
50 if (fType.isInstance(partRef.getPart(false))) {
51 toggleActive(state);
52 }
53 }
54
55 @Override
56 public void partActivated(IWorkbenchPartReference partRef) {
57 toggleState(partRef, true);
58 }
59
60 @Override
61 public void partBroughtToTop(IWorkbenchPartReference partRef) {
62
63 }
64
65 @Override
66 public void partClosed(IWorkbenchPartReference partRef) {
67 toggleState(partRef, false);
68 }
69
70 @Override
71 public void partDeactivated(IWorkbenchPartReference partRef) {
72
73 }
74
75 @Override
76 public void partOpened(IWorkbenchPartReference partRef) {
77 toggleState(partRef, true);
78 }
79
80 @Override
81 public void partHidden(IWorkbenchPartReference partRef) {
82 toggleState(partRef, false);
83 }
84
85 @Override
86 public void partVisible(IWorkbenchPartReference partRef) {
87 toggleState(partRef, true);
88 }
89
90 @Override
91 public void partInputChanged(IWorkbenchPartReference partRef) {
92
93 }
94 }
95
96 private ISelectionListener fSelListener = new ISelectionListener() {
97 @Override
98 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
99 if (selection instanceof IStructuredSelection) {
100 Object element = ((IStructuredSelection) selection).getFirstElement();
101 if (element instanceof ControlFlowEntry) {
102 ControlFlowEntry entry = (ControlFlowEntry) element;
103 setCurrentThreadId(entry);
104 }
105 }
106 }
107 };
108
109 private static final String NAME = "Critical Path Lttng kernel parameter provider"; //$NON-NLS-1$
110
111 private final IPartListener2 fPartListener = new IPartListener2Impl(CriticalPathView.class);
112
113 private ControlFlowEntry fCurrentEntry = null;
114
115 private boolean fActive = false;
116 private boolean fEntryChanged = false;
117
118 /**
119 * Constructor
120 */
121 public CriticalPathParameterProvider() {
122 super();
123 registerListener();
124 }
125
126 @Override
127 public String getName() {
128 return NAME;
129 }
130
131 @Override
132 public Object getParameter(String name) {
133 if (fCurrentEntry == null) {
134 return null;
135 }
136 if (name.equals(CriticalPathModule.PARAM_WORKER)) {
137 /* Try to find the worker for the critical path */
138 IAnalysisModule mod = getModule();
139 if ((mod != null) && (mod instanceof CriticalPathModule)) {
140 Integer threadId = fCurrentEntry.getThreadId();
141 HostThread ht = new HostThread(fCurrentEntry.getTrace().getHostId(), threadId);
142 LttngWorker worker = new LttngWorker(ht, "", 0); //$NON-NLS-1$
143 return worker;
144 }
145 return fCurrentEntry;
146 }
147 return null;
148 }
149
150 @Override
151 public boolean appliesToTrace(ITmfTrace trace) {
152 return true;
153 }
154
155 private void setCurrentThreadId(ControlFlowEntry entry) {
156 if (!entry.equals(fCurrentEntry)) {
157 fCurrentEntry = entry;
158 if (fActive) {
159 this.notifyParameterChanged(CriticalPathModule.PARAM_WORKER);
160 } else {
161 fEntryChanged = true;
162 }
163 }
164 }
165
166 private void toggleActive(boolean active) {
167 if (active != fActive) {
168 fActive = active;
169 if (fActive && fEntryChanged) {
170 this.notifyParameterChanged(CriticalPathModule.PARAM_WORKER);
171 fEntryChanged = false;
172 }
173 }
174 }
175
176 private void registerListener() {
177 if (!PlatformUI.isWorkbenchRunning()) {
178 return;
179 }
180 IWorkbench wb = PlatformUI.getWorkbench();
181 if (wb == null) {
182 return;
183 }
184 IWorkbenchWindow wbw = wb.getActiveWorkbenchWindow();
185 if (wbw == null) {
186 return;
187 }
188 final IWorkbenchPage activePage = wbw.getActivePage();
189 if (activePage == null) {
190 return;
191 }
192
193 /* Activate the update if critical path view visible */
194 IViewPart view = activePage.findView(CriticalPathView.ID);
195 if (view != null) {
196 if (activePage.isPartVisible(view)) {
197 toggleActive(true);
198 }
199 }
200
201 /* Add the listener to the control flow view */
202 view = activePage.findView(ControlFlowView.ID);
203 if (view != null) {
204 view.getSite().getWorkbenchWindow().getSelectionService().addPostSelectionListener(fSelListener);
205 view.getSite().getWorkbenchWindow().getPartService().addPartListener(fPartListener);
206 }
207 }
208
209 }
This page took 0.037525 seconds and 4 git commands to generate.