Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 01be4b955f0c41ee51b75168123d70043932fbcc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*******************************************************************************
 * Copyright (c) 2011 Ericsson
 * 
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *   Polytechnique Montréal - Initial API and implementation
 *   Bernd Hufmann - Productification, enhancements and fixes
 *   
 *******************************************************************************/
package org.eclipse.linuxtools.lttng.ui.tracecontrol.actions;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.linuxtools.lttng.core.tracecontrol.model.TargetResource;
import org.eclipse.linuxtools.lttng.core.tracecontrol.model.TraceResource;
import org.eclipse.linuxtools.lttng.core.tracecontrol.model.TraceResource.TraceState;
import org.eclipse.linuxtools.lttng.core.tracecontrol.model.config.TraceConfig;
import org.eclipse.linuxtools.lttng.core.tracecontrol.service.ILttControllerService;
import org.eclipse.linuxtools.lttng.ui.LTTngUiPlugin;
import org.eclipse.linuxtools.lttng.ui.tracecontrol.Messages;
import org.eclipse.linuxtools.lttng.ui.tracecontrol.TraceControlConstants;
import org.eclipse.linuxtools.lttng.ui.tracecontrol.dialogs.NewTraceDialog;
import org.eclipse.linuxtools.lttng.ui.tracecontrol.subsystems.TraceSubSystem;
import org.eclipse.rse.core.events.ISystemRemoteChangeEvents;
import org.eclipse.rse.core.model.ISystemRegistry;
import org.eclipse.rse.core.model.SystemStartHere;
import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
import org.eclipse.rse.ui.SystemBasePlugin;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.tm.tcf.protocol.IToken;
import org.eclipse.tm.tcf.util.TCFTask;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

/**
 * <b><u>CreateNewTrace</u></b>
 * <p>
 * Action implementation to create a new trace.
 * </p>
 */
public class CreateNewTrace implements IObjectActionDelegate, IWorkbenchWindowActionDelegate, IViewActionDelegate {

    // ------------------------------------------------------------------------
    // Attributes
    // ------------------------------------------------------------------------

    private final List<TargetResource> fSelectedFiles;

    // ------------------------------------------------------------------------
    // Constructors
    // ------------------------------------------------------------------------

    /**
     * Constructor for CreateNewTrace.
     */
    public CreateNewTrace() {
        fSelectedFiles = new ArrayList<TargetResource>();
    }

    // ------------------------------------------------------------------------
    // Operations
    // ------------------------------------------------------------------------

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.
     * action.IAction, org.eclipse.ui.IWorkbenchPart)
     */
    @Override
    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
    }

    /**
     * Returns the first selected target resource.
     * 
     * @return first selected target resource
     */
    protected TargetResource getFirstSelectedTarget() {
        if (fSelectedFiles.size() > 0) {
            return fSelectedFiles.get(0);
        }
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
     */
    @Override
    public void run(IAction action) {
        Shell shell = getShell();
        final TargetResource targetResource = getFirstSelectedTarget();
        TraceSubSystem subSystem = (TraceSubSystem) targetResource.getSubSystem();
        NewTraceDialog dialog = new NewTraceDialog(shell, subSystem, targetResource);

        final TraceConfig traceConfig = dialog.open();

        if (traceConfig == null) {
            return;
        }

        try {
            final ILttControllerService service = subSystem.getControllerService();

            TraceResource trace = new TraceResource(targetResource.getSubSystem(), service);
            trace.setName(traceConfig.getTraceName());
            trace.setParent(targetResource);
            trace.setTraceConfig(traceConfig);

            if (targetResource.isUst()) {
                boolean ok = setupUstLocation(service, targetResource, traceConfig);
                if (!ok) {
                    return;
                }
            }

            trace.setupTrace();

            if (!targetResource.isUst()) {

                // Enable all channels by default
                trace.setChannelEnable(TraceControlConstants.Lttng_Control_AllChannels, true);

                // Set overwrite mode for all channels according to user
                // selection (true for flight recorder, false for normal)
                trace.setChannelOverwrite(TraceControlConstants.Lttng_Control_AllChannels, traceConfig.getMode() == TraceConfig.FLIGHT_RECORDER_MODE);

                // Set channel timer for all channels
                final long period = 1000;
                trace.setChannelTimer(TraceControlConstants.Lttng_Control_AllChannels, period);

                // Set subbuffer size for all channels
                final long subbufSize = 16384;
                trace.setChannelSubbufSize(TraceControlConstants.Lttng_Control_AllChannels, subbufSize);

                // Set number of subbuffers for all channels
                final long subbufNum = 2;
                trace.setChannelSubbufNum(TraceControlConstants.Lttng_Control_AllChannels, subbufNum);
            }

            if (traceConfig.isNetworkTrace()) {

                File newDir = new File(traceConfig.getTracePath());
                if (!newDir.exists()) {
                    boolean created = newDir.mkdirs();
                    if (!created) {
                        throw new Exception(Messages.Lttng_Control_ErrorCreateTracePath + ": " + traceConfig.getTracePath()); //$NON-NLS-1$
                    }
                }
            }

            if (trace.isUst()) {
                // in UST the tracing is started after setupTrace!!
                trace.setTraceState(TraceState.STARTED);
            } else {
                trace.setTraceState(TraceState.CONFIGURED);
            }

            targetResource.addTrace(trace);

            ISystemRegistry registry = SystemStartHere.getSystemRegistry();
            registry.fireRemoteResourceChangeEvent(ISystemRemoteChangeEvents.SYSTEM_REMOTE_RESOURCE_CREATED, trace, targetResource, subSystem, null);

        } catch (Exception e) {
            SystemMessageException sysExp;
            if (e instanceof SystemMessageException) {
                sysExp = (SystemMessageException) e;
            } else {
                sysExp = new SystemMessageException(LTTngUiPlugin.getDefault().getMessage(e));
            }
            SystemBasePlugin.logError(Messages.Lttng_Control_ErrorNewTrace + " (" + //$NON-NLS-1$
                    Messages.Lttng_Resource_Trace + ": " + traceConfig.getTraceName() + ")", sysExp); //$NON-NLS-1$ //$NON-NLS-2$

            return;
        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action
     * .IAction, org.eclipse.jface.viewers.ISelection)
     */
    @Override
    @SuppressWarnings("unchecked")
    public void selectionChanged(IAction action, ISelection selection) {
        if (selection instanceof IStructuredSelection) {
            fSelectedFiles.clear();
            // store the selected targets to be used when running
            Iterator<IStructuredSelection> theSet = ((IStructuredSelection) selection).iterator();
            while (theSet.hasNext()) {
                Object obj = theSet.next();
                if (obj instanceof TargetResource) {
                    fSelectedFiles.add((TargetResource) obj);
                }
            }
        }
    }

    /**
     * Returns the active workbench shell of this plug-in.
     * 
     * @return active workbench shell.
     */
    protected Shell getShell() {
        return SystemBasePlugin.getActiveWorkbenchShell();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.
     * IWorkbenchWindow)
     */
    @Override
    public void init(IWorkbenchWindow window) {

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
     */
    @Override
    public void dispose() {
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
     */
    @Override
    public void init(IViewPart view) {
    }

    /*
     * Setup the trace location for UST.
     */
    private boolean setupUstLocation(final ILttControllerService service, final TargetResource targetResource, final TraceConfig traceConfig)
            throws Exception {
        if (traceConfig.isNetworkTrace()) {
            File localDir = new File(traceConfig.getTracePath());
            if (!localDir.exists()) {
                boolean success = localDir.mkdirs();
                if (!success) {
                    return false;
                }
            }

            // Create future task
            boolean ok = new TCFTask<Boolean>() {
                @Override
                public void run() {

                    // Setup trace location using Lttng controller service proxy
                    service.writeTraceNetwork(targetResource.getParent().getName(), targetResource.getName(), traceConfig.getTraceName(),
                            traceConfig.getNumChannel(), traceConfig.getIsAppend(), traceConfig.getMode() == TraceConfig.FLIGHT_RECORDER_MODE,
                            traceConfig.getMode() == TraceConfig.NORMAL_MODE, new ILttControllerService.DoneWriteTraceNetwork() {

                                @Override
                                public void doneWriteTraceNetwork(IToken token, Exception error, Object str) {
                                    if (error != null) {
                                        // Notify with error
                                        error(error);
                                        return;
                                    }

                                    // Notify about success
                                    done(true);
                                }
                            });
                }
            }.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
            return ok;
        } else {
            // Create future task
            boolean ok = new TCFTask<Boolean>() {
                @Override
                public void run() {

                    // Setup trace location using Lttng controller service proxy
                    service.writeTraceLocal(targetResource.getParent().getName(), targetResource.getName(), traceConfig.getTraceName(),
                            traceConfig.getTracePath(), traceConfig.getNumChannel(), traceConfig.getIsAppend(),
                            traceConfig.getMode() == TraceConfig.NORMAL_MODE, traceConfig.getMode() == TraceConfig.FLIGHT_RECORDER_MODE,
                            new ILttControllerService.DoneWriteTraceLocal() {

                                @Override
                                public void doneWriteTraceLocal(IToken token, Exception error, Object str) {
                                    if (error != null) {
                                        // Notify with error
                                        error(error);
                                        return;
                                    }

                                    // Notify about success
                                    done(true);
                                }
                            });
                }
            }.get(TraceControlConstants.DEFAULT_TCF_TASK_TIMEOUT, TimeUnit.SECONDS);
            return ok;
        }
    }

}

Back to the top