1 /*******************************************************************************
2 * Copyright (c) 2011 Ericsson
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
10 * Polytechnique Montréal - Initial API and implementation
11 * Bernd Hufmann - Productification, enhancements and fixes
13 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.lttng
.ui
.tracecontrol
.actions
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Iterator
;
19 import java
.util
.List
;
20 import java
.util
.concurrent
.TimeUnit
;
22 import org
.eclipse
.jface
.action
.IAction
;
23 import org
.eclipse
.jface
.viewers
.ISelection
;
24 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
25 import org
.eclipse
.linuxtools
.lttng
.tracecontrol
.model
.TargetResource
;
26 import org
.eclipse
.linuxtools
.lttng
.tracecontrol
.model
.TraceResource
;
27 import org
.eclipse
.linuxtools
.lttng
.tracecontrol
.model
.TraceResource
.TraceState
;
28 import org
.eclipse
.linuxtools
.lttng
.tracecontrol
.model
.config
.TraceConfig
;
29 import org
.eclipse
.linuxtools
.lttng
.tracecontrol
.service
.ILttControllerService
;
30 import org
.eclipse
.linuxtools
.lttng
.ui
.LTTngUiPlugin
;
31 import org
.eclipse
.linuxtools
.lttng
.ui
.tracecontrol
.TraceControlConstants
;
32 import org
.eclipse
.linuxtools
.lttng
.ui
.tracecontrol
.Messages
;
33 import org
.eclipse
.linuxtools
.lttng
.ui
.tracecontrol
.dialogs
.NewTraceDialog
;
34 import org
.eclipse
.linuxtools
.lttng
.ui
.tracecontrol
.subsystems
.TraceSubSystem
;
35 import org
.eclipse
.rse
.core
.events
.ISystemRemoteChangeEvents
;
36 import org
.eclipse
.rse
.core
.model
.ISystemRegistry
;
37 import org
.eclipse
.rse
.core
.model
.SystemStartHere
;
38 import org
.eclipse
.rse
.services
.clientserver
.messages
.SystemMessageException
;
39 import org
.eclipse
.rse
.ui
.SystemBasePlugin
;
40 import org
.eclipse
.swt
.widgets
.Shell
;
41 import org
.eclipse
.tm
.tcf
.protocol
.IToken
;
42 import org
.eclipse
.tm
.tcf
.util
.TCFTask
;
43 import org
.eclipse
.ui
.IObjectActionDelegate
;
44 import org
.eclipse
.ui
.IViewActionDelegate
;
45 import org
.eclipse
.ui
.IViewPart
;
46 import org
.eclipse
.ui
.IWorkbenchPart
;
47 import org
.eclipse
.ui
.IWorkbenchWindow
;
48 import org
.eclipse
.ui
.IWorkbenchWindowActionDelegate
;
51 * <b><u>CreateNewTrace</u></b>
53 * Action implementation to create a new trace.
56 public class CreateNewTrace
implements IObjectActionDelegate
, IWorkbenchWindowActionDelegate
, IViewActionDelegate
{
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 private List
<TargetResource
> fSelectedFiles
;
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
69 * Constructor for CreateNewTrace.
71 public CreateNewTrace() {
72 fSelectedFiles
= new ArrayList
<TargetResource
>();
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
81 * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
84 public void setActivePart(IAction action
, IWorkbenchPart targetPart
) {
88 * Returns the first selected target resource.
90 * @return first selected target resource
92 protected TargetResource
getFirstSelectedTarget() {
93 if (fSelectedFiles
.size() > 0) {
94 return (TargetResource
) fSelectedFiles
.get(0);
101 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
104 public void run(IAction action
) {
105 Shell shell
= getShell();
106 final TargetResource targetResource
= getFirstSelectedTarget();
107 TraceSubSystem subSystem
= (TraceSubSystem
)targetResource
.getSubSystem();
108 NewTraceDialog dialog
= new NewTraceDialog(shell
, subSystem
, targetResource
);
110 final TraceConfig traceConfig
= dialog
.open();
112 if (traceConfig
== null) {
117 final ILttControllerService service
= subSystem
.getControllerService();
119 TraceResource trace
= new TraceResource(targetResource
.getSubSystem(), service
);
120 trace
.setName(traceConfig
.getTraceName());
121 trace
.setParent(targetResource
);
122 trace
.setTraceConfig(traceConfig
);
124 if (targetResource
.isUst()) {
125 boolean ok
= setupUstLocation(service
, targetResource
, traceConfig
);
133 if (!targetResource
.isUst()) {
135 // Enable all channels by default
136 trace
.setChannelEnable(TraceControlConstants
.Lttng_Control_AllChannels
, true);
138 // Set overwrite mode for all channels according to user selection (true for flight recorder, false for normal)
139 trace
.setChannelOverwrite(TraceControlConstants
.Lttng_Control_AllChannels
, traceConfig
.getMode() == TraceConfig
.FLIGHT_RECORDER_MODE
);
141 // Set channel timer for all channels
142 final long period
= 5000;
143 trace
.setChannelTimer(TraceControlConstants
.Lttng_Control_AllChannels
, period
);
145 // Set subbuffer size for all channels
146 final long subbufSize
= 16384;
147 trace
.setChannelSubbufSize(TraceControlConstants
.Lttng_Control_AllChannels
, subbufSize
);
149 // Set number of subbuffers for all channels
150 final long subbufNum
= 2;
151 trace
.setChannelSubbufNum(TraceControlConstants
.Lttng_Control_AllChannels
, subbufNum
);
154 if (traceConfig
.isNetworkTrace()) {
156 File newDir
= new File(traceConfig
.getTracePath());
157 if (!newDir
.exists()) {
158 boolean created
= newDir
.mkdirs();
160 throw new Exception(Messages
.Lttng_Control_ErrorCreateTracePath
+ ": " + traceConfig
.getTracePath()); //$NON-NLS-1$
166 // in UST the tracing is started after setupTrace!!
167 trace
.setTraceState(TraceState
.STARTED
);
170 trace
.setTraceState(TraceState
.CONFIGURED
);
173 targetResource
.addTrace(trace
);
175 ISystemRegistry registry
= SystemStartHere
.getSystemRegistry();
176 registry
.fireRemoteResourceChangeEvent(ISystemRemoteChangeEvents
.SYSTEM_REMOTE_RESOURCE_CREATED
, trace
, targetResource
, subSystem
, null);
178 } catch (Exception e
) {
179 SystemMessageException sysExp
;
180 if (e
instanceof SystemMessageException
) {
181 sysExp
= (SystemMessageException
)e
;
183 sysExp
= new SystemMessageException(LTTngUiPlugin
.getDefault().getMessage(e
));
185 SystemBasePlugin
.logError(Messages
.Lttng_Control_ErrorNewTrace
+ " (" + //$NON-NLS-1$
186 Messages
.Lttng_Resource_Trace
+ ": " + traceConfig
.getTraceName() + ")", sysExp
); //$NON-NLS-1$ //$NON-NLS-2$
195 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
198 @SuppressWarnings("unchecked")
199 public void selectionChanged(IAction action
, ISelection selection
) {
200 if (selection
instanceof IStructuredSelection
) {
201 fSelectedFiles
.clear();
202 // store the selected targets to be used when running
203 Iterator
<IStructuredSelection
> theSet
= ((IStructuredSelection
) selection
).iterator();
204 while (theSet
.hasNext()) {
205 Object obj
= theSet
.next();
206 if (obj
instanceof TargetResource
) {
207 fSelectedFiles
.add((TargetResource
)obj
);
214 * Returns the active workbench shell of this plug-in.
216 * @return active workbench shell.
218 protected Shell
getShell() {
219 return SystemBasePlugin
.getActiveWorkbenchShell();
224 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
227 public void init(IWorkbenchWindow window
) {
233 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
236 public void dispose() {
241 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
244 public void init(IViewPart view
) {
248 * Setup the trace location for UST.
250 private boolean setupUstLocation(final ILttControllerService service
, final TargetResource targetResource
, final TraceConfig traceConfig
) throws Exception
{
251 if (traceConfig
.isNetworkTrace()) {
252 File localDir
= new File(traceConfig
.getTracePath());
253 if (!localDir
.exists()) {
254 boolean success
= localDir
.mkdirs();
260 // Create future task
261 boolean ok
= new TCFTask
<Boolean
>() {
265 // Setup trace location using Lttng controller service proxy
266 service
.writeTraceNetwork(targetResource
.getParent().getName(),
267 targetResource
.getName(),
268 traceConfig
.getTraceName(),
269 traceConfig
.getNumChannel(),
270 traceConfig
.getIsAppend(),
271 traceConfig
.getMode() == TraceConfig
.FLIGHT_RECORDER_MODE
,
272 traceConfig
.getMode() == TraceConfig
.NORMAL_MODE
,
273 new ILttControllerService
.DoneWriteTraceNetwork() {
276 public void doneWriteTraceNetwork(IToken token
, Exception error
, Object str
) {
283 // Notify about success
287 }}.get(TraceControlConstants
.DEFAULT_TCF_TASK_TIMEOUT
, TimeUnit
.SECONDS
);
290 // Create future task
291 boolean ok
= new TCFTask
<Boolean
>() {
295 // Setup trace location using Lttng controller service proxy
296 service
.writeTraceLocal(targetResource
.getParent().getName(),
297 targetResource
.getName(),
298 traceConfig
.getTraceName(),
299 traceConfig
.getTracePath(),
300 traceConfig
.getNumChannel(),
301 traceConfig
.getIsAppend(),
302 traceConfig
.getMode() == TraceConfig
.NORMAL_MODE
,
303 traceConfig
.getMode() == TraceConfig
.FLIGHT_RECORDER_MODE
,
304 new ILttControllerService
.DoneWriteTraceLocal() {
307 public void doneWriteTraceLocal(IToken token
, Exception error
, Object str
) {
314 // Notify about success
318 }}.get(TraceControlConstants
.DEFAULT_TCF_TASK_TIMEOUT
, TimeUnit
.SECONDS
);