Commit | Line | Data |
---|---|---|
5d10d135 ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2010 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 | * Alvaro Sanchez-Leon (alvsan09@gmail.com) - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.lttng.control; | |
14 | ||
15 | import java.util.HashMap; | |
16 | import java.util.Map; | |
17 | ||
18 | import org.eclipse.linuxtools.lttng.TraceDebug; | |
19 | import org.eclipse.linuxtools.lttng.event.LttngEvent; | |
20 | import org.eclipse.linuxtools.lttng.event.LttngEventType; | |
21 | import org.eclipse.linuxtools.lttng.event.LttngSyntheticEvent; | |
22 | import org.eclipse.linuxtools.lttng.event.LttngSyntheticEvent.SequenceInd; | |
23 | import org.eclipse.linuxtools.lttng.event.LttngTimestamp; | |
24 | import org.eclipse.linuxtools.lttng.model.LTTngTreeNode; | |
25 | import org.eclipse.linuxtools.lttng.request.LttngBaseEventRequest; | |
26 | import org.eclipse.linuxtools.lttng.state.evProcessor.ITransEventProcessor; | |
27 | import org.eclipse.linuxtools.lttng.state.evProcessor.state.StateEventToHandlerFactory; | |
28 | import org.eclipse.linuxtools.lttng.state.model.LttngTraceState; | |
29 | import org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager; | |
30 | import org.eclipse.linuxtools.tmf.component.TmfEventProvider; | |
31 | import org.eclipse.linuxtools.tmf.event.TmfEventSource; | |
32 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; | |
33 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; | |
34 | import org.eclipse.linuxtools.tmf.experiment.TmfExperiment; | |
35 | import org.eclipse.linuxtools.tmf.request.ITmfDataRequest; | |
36 | import org.eclipse.linuxtools.tmf.request.ITmfEventRequest; | |
5d10d135 ASL |
37 | import org.eclipse.linuxtools.tmf.request.TmfEventRequest; |
38 | import org.eclipse.linuxtools.tmf.trace.ITmfContext; | |
39 | import org.eclipse.linuxtools.tmf.trace.TmfContext; | |
40 | import org.eclipse.linuxtools.tmf.trace.TmfTrace; | |
41 | ||
42 | /** | |
43 | * @author alvaro | |
44 | * | |
45 | */ | |
46 | public class LttngSyntheticEventProvider extends | |
47 | TmfEventProvider<LttngSyntheticEvent> { | |
48 | ||
49 | // ======================================================================== | |
50 | // Data | |
51 | // ======================================================================== | |
52 | public static final int BLOCK_SIZE = 1; | |
53 | public static final int NB_EVENTS = 1; | |
54 | public static final int QUEUE_SIZE = 1; // lttng specific, one event at a | |
55 | // time | |
56 | ||
57 | // TmfDataProvider<LttngEvent> fExtProvider = null; | |
58 | private ITmfDataRequest<LttngSyntheticEvent> fmainRequest = null; | |
550d787e | 59 | private final Map<IStateTraceManager, LttngBaseEventRequest> fEventProviderRequests = new HashMap<IStateTraceManager, LttngBaseEventRequest>(); |
5d10d135 ASL |
60 | private final LttngSyntheticEvent fStatusEvent; |
61 | private final LttngSyntheticEvent fStatusEventAck; | |
550d787e | 62 | private int fMainReqEventCount = 0; |
5d10d135 ASL |
63 | volatile boolean startIndSent = false; |
64 | private LTTngTreeNode fExperiment = null; | |
65 | private ITransEventProcessor fstateUpdateProcessor = StateEventToHandlerFactory | |
66 | .getInstance(); | |
67 | private boolean waitForRequest = false; | |
68 | ||
69 | // ======================================================================== | |
70 | // Constructor | |
71 | // ======================================================================== | |
72 | /** | |
73 | * Accessibility to package - use factory instead of this constructor | |
74 | * | |
75 | * @param type | |
76 | */ | |
77 | LttngSyntheticEventProvider(Class<LttngSyntheticEvent> type) { | |
78 | super("LttngSyntheticEventProvider", type, QUEUE_SIZE); | |
79 | ||
80 | // prepare empty instance status indicators and allow them to travel via | |
81 | // the framework | |
82 | TmfEventSource source = new TmfEventSource(this); | |
83 | LttngEventType dtype = new LttngEventType(); | |
84 | LttngTimestamp statusTimeStamp = new LttngTimestamp( | |
ae2c17d3 | 85 | TmfTimestamp.Zero); |
5d10d135 ASL |
86 | |
87 | fStatusEvent = new LttngSyntheticEvent(null, statusTimeStamp, source, | |
88 | dtype, null, null, null); | |
89 | fStatusEvent.setSequenceInd(SequenceInd.STARTREQ); | |
90 | ||
91 | fStatusEventAck = new LttngSyntheticEvent(null, statusTimeStamp, | |
92 | source, dtype, null, null, null); | |
93 | fStatusEventAck.setSequenceInd(SequenceInd.ACK); | |
94 | } | |
95 | ||
96 | // ======================================================================== | |
97 | // Methods | |
98 | // ======================================================================== | |
550d787e | 99 | |
5d10d135 ASL |
100 | @SuppressWarnings("unchecked") |
101 | @Override | |
102 | public ITmfContext armRequest( | |
103 | final ITmfDataRequest<LttngSyntheticEvent> request) { | |
104 | // validate | |
105 | // make sure we have the right type of request | |
106 | if (!(request instanceof ITmfEventRequest<?>)) { | |
107 | request.cancel(); | |
108 | TraceDebug.debug("Request is not an instance of ITmfEventRequest"); | |
109 | return null; | |
110 | } | |
111 | ||
112 | if (fExperiment == null) { | |
113 | TraceDebug.debug("Experiment is null"); | |
114 | request.cancel(); | |
115 | return null; | |
116 | } | |
117 | ||
118 | // get ready to start processing | |
119 | reset(fExperiment); | |
120 | ||
121 | // At least one base provider shall be available | |
550d787e | 122 | if (fEventProviderRequests.size() < 1) { |
5d10d135 ASL |
123 | request.cancel(); |
124 | TraceDebug.debug("No Base event providers available"); | |
125 | return null; | |
126 | } | |
127 | ||
128 | fmainRequest = request; | |
129 | // define event data handling | |
130 | ITmfEventRequest<LttngSyntheticEvent> eventRequest = (ITmfEventRequest<LttngSyntheticEvent>) fmainRequest; | |
131 | TmfTimeRange reqWindow = eventRequest.getRange(); | |
550d787e | 132 | |
5d10d135 ASL |
133 | TraceDebug.debug("Main Synthethic event request started on thread: " + Thread.currentThread().getName()); |
134 | ||
135 | // loop for every traceManager in current experiment | |
550d787e FC |
136 | boolean subRequestQueued = false; |
137 | for (IStateTraceManager traceManager : fEventProviderRequests.keySet()) { | |
5d10d135 ASL |
138 | |
139 | // restore trace state system to nearest check point | |
140 | TmfTimestamp checkPoint = traceManager | |
141 | .restoreCheckPointByTimestamp(reqWindow.getStartTime()); | |
550d787e FC |
142 | |
143 | // adjust start time bound to check point | |
144 | ||
5d10d135 ASL |
145 | // validate so checkpoint restore is within requested bounds |
146 | TmfTimeRange traceRange = traceManager.getTrace().getTimeRange(); | |
550d787e FC |
147 | if ((checkPoint != null) && !( |
148 | checkPoint.getValue() >= traceRange.getStartTime().getValue() && | |
149 | checkPoint.getValue() <= traceRange.getEndTime().getValue() && | |
150 | checkPoint.getValue() < reqWindow.getEndTime().getValue()) | |
151 | ) { | |
5d10d135 ASL |
152 | // checkpoint is out of trace bounds |
153 | continue; | |
154 | } | |
550d787e FC |
155 | TmfTimeRange adjustedRange = reqWindow; |
156 | if (checkPoint != null) { | |
157 | adjustedRange = new TmfTimeRange(checkPoint, reqWindow.getEndTime()); | |
158 | } | |
5d10d135 | 159 | |
736aecd5 | 160 | LttngTraceState traceModel = traceManager.getStateModel(); |
5d10d135 ASL |
161 | // create sub-request for one trace within experiment |
162 | final LttngBaseEventRequest subRequest = new LttngBaseEventRequest( | |
163 | adjustedRange, reqWindow.getStartTime(), 0, | |
550d787e | 164 | TmfEventRequest.ALL_DATA, BLOCK_SIZE, traceModel, ITmfDataRequest.ExecutionType.SHORT) { |
5d10d135 ASL |
165 | |
166 | private LttngSyntheticEvent syntheticEvent = null; | |
167 | private LttngSyntheticEvent syntheticAckIndicator = null; | |
168 | long subEventCount = 0L; | |
169 | ||
170 | private final long fDispatchTime = getDispatchTime().getValue(); | |
171 | private final LttngTraceState fTraceModel = getTraceModel(); | |
172 | ||
173 | /* | |
174 | * (non-Javadoc) | |
175 | * | |
176 | * @see | |
177 | * org.eclipse.linuxtools.lttng.control.LttngEventRequest#handleData | |
178 | * () | |
179 | */ | |
180 | @Override | |
181 | public void handleData() { | |
182 | LttngEvent[] events = getData(); | |
183 | if (events.length > 0) { | |
184 | for (LttngEvent e : events) { | |
185 | handleIncomingData(e); | |
186 | } | |
187 | } else { | |
188 | TraceDebug.debug("handle data received with no data"); | |
550d787e FC |
189 | // handleProviderDone(getTraceModel()); |
190 | // done(); | |
5d10d135 ASL |
191 | } |
192 | } | |
193 | ||
194 | /* | |
195 | * (non-Javadoc) | |
196 | * | |
197 | * @see org.eclipse.linuxtools.tmf.request.TmfDataRequest#done() | |
198 | */ | |
199 | @Override | |
200 | public void done() { | |
201 | // mark this sub-request as completed | |
202 | super.done(); | |
203 | handleProviderDone(getTraceModel()); | |
550d787e | 204 | // super.done(); |
5d10d135 ASL |
205 | } |
206 | ||
207 | /** | |
208 | * Trigger the Analysis and sequential control of the events. | |
209 | * | |
210 | * @param e | |
211 | */ | |
212 | private void handleIncomingData(LttngEvent e) { | |
213 | long eventTime = e.getTimestamp().getValue(); | |
214 | ||
215 | // if (eventTime == 13589777932952L) { | |
216 | // // syscall entry id 78 expected | |
217 | // System.out.println("debug mark at 13589777932952L"); | |
218 | // } | |
219 | ||
220 | // queue the new event data and an ACK | |
221 | updateSynEvent(e); | |
222 | ||
5d10d135 | 223 | // If time at or above requested time, update application |
550d787e FC |
224 | try { |
225 | if (eventTime >= fDispatchTime) { | |
226 | // Before update | |
227 | syntheticEvent.setSequenceInd(SequenceInd.BEFORE); | |
228 | queueResult(syntheticEvent); | |
229 | queueResult(syntheticAckIndicator); | |
230 | ||
231 | // Update state locally | |
232 | syntheticEvent.setSequenceInd(SequenceInd.UPDATE); | |
233 | fstateUpdateProcessor.process(syntheticEvent, fTraceModel); | |
234 | ||
235 | // After Update | |
236 | syntheticEvent.setSequenceInd(SequenceInd.AFTER); | |
237 | queueResult(syntheticEvent); | |
238 | queueResult(syntheticAckIndicator); | |
239 | ||
240 | // increment once per dispatch | |
241 | incrementSynEvenCount(); | |
242 | subEventCount++; | |
243 | } else { | |
244 | // event time is between checkpoint adjusted time and | |
245 | // requested time i.e. application does not expect the | |
246 | // event, however the state system needs to be re-built | |
247 | // to the dispatch point | |
248 | syntheticEvent.setSequenceInd(SequenceInd.UPDATE); | |
249 | fstateUpdateProcessor.process(syntheticEvent, fTraceModel); | |
250 | } | |
251 | } catch (InterruptedException e1) { | |
cb866e08 FC |
252 | // TODO: Cancel this request |
253 | request.cancel(); | |
254 | // e1.printStackTrace(); | |
5d10d135 ASL |
255 | } |
256 | } | |
257 | ||
258 | /** | |
259 | * Create a synthetic event from the received new reference, if | |
260 | * the reference is the same there is no need for a new instance | |
261 | * | |
262 | * if this is the first event for this request, call start | |
263 | * handler | |
264 | * | |
265 | * @param e | |
266 | * @return | |
267 | */ | |
268 | private LttngSyntheticEvent updateSynEvent(LttngEvent e) { | |
269 | if (syntheticEvent == null | |
270 | || syntheticEvent.getBaseEvent() != e) { | |
271 | syntheticEvent = new LttngSyntheticEvent(e); | |
272 | syntheticAckIndicator = new LttngSyntheticEvent(e); | |
273 | syntheticAckIndicator.setSequenceInd(SequenceInd.ACK); | |
274 | } | |
275 | ||
276 | // Trace model needed by application handlers | |
277 | syntheticEvent.setTraceModel(fTraceModel); | |
278 | ||
279 | // send the start request indication once per request thread | |
280 | if (!startIndSent) { | |
281 | TraceDebug.debug("Thread started: " + Thread.currentThread().getName()); | |
282 | handleProviderStarted(getTraceModel()); | |
283 | startIndSent = true; | |
284 | } | |
285 | ||
286 | return syntheticEvent; | |
287 | } | |
288 | }; | |
550d787e | 289 | |
5d10d135 ASL |
290 | // preserve the associated sub request to control it e.g. |
291 | // cancellation | |
550d787e | 292 | fEventProviderRequests.put(traceManager, subRequest); |
5d10d135 ASL |
293 | |
294 | // start request | |
295 | TmfTrace<LttngEvent> provider = (TmfTrace<LttngEvent>) traceManager | |
296 | .getTrace(); | |
550d787e | 297 | // provider.sendRequest(subRequest, ExecutionType.LONG); |
8827c197 | 298 | provider.sendRequest(subRequest); |
550d787e | 299 | subRequestQueued = true; |
5d10d135 ASL |
300 | } |
301 | ||
302 | // Return a dummy context, not used for relay provider | |
550d787e | 303 | return (subRequestQueued) ? new TmfContext() : null; |
5d10d135 ASL |
304 | } |
305 | ||
306 | /** | |
307 | * Notify listeners to prepare to receive data e.g. clean previous data etc. | |
308 | */ | |
309 | public void handleProviderStarted(LttngTraceState traceModel) { | |
310 | LttngSyntheticEvent startIndEvent = new LttngSyntheticEvent( | |
311 | fStatusEvent); | |
312 | startIndEvent.setSequenceInd(SequenceInd.STARTREQ); | |
313 | ||
314 | // Notify application | |
550d787e FC |
315 | try { |
316 | queueResult(startIndEvent); | |
317 | queueResult(fStatusEventAck); | |
318 | } catch (InterruptedException e) { | |
cb866e08 FC |
319 | // TODO: cancel this request |
320 | // e.printStackTrace(); | |
550d787e | 321 | } |
5d10d135 ASL |
322 | |
323 | // Notify state event processor | |
324 | fstateUpdateProcessor.process(startIndEvent, null); | |
325 | } | |
326 | ||
327 | /** | |
328 | * Notify listeners, no more events for the current request will be | |
329 | * distributed e.g. update view. | |
330 | */ | |
331 | public synchronized void handleProviderDone(LttngTraceState traceModel) { | |
332 | // TODO: The use of a thread per main request and thread per sub-request | |
333 | // requires | |
334 | // to make sure the proper main request is marked completed. So a | |
335 | // relationship of sub-requests to parent needs to be established to | |
336 | // handle completion and cancellations properly | |
337 | ||
338 | // Close the main request when all sub-requests are marked completed | |
550d787e | 339 | for (LttngBaseEventRequest subRequest : fEventProviderRequests.values()) { |
5d10d135 ASL |
340 | if (subRequest != null) { |
341 | if (!subRequest.isCompleted()) { | |
342 | // Not ready to complete main request | |
343 | return; | |
344 | } | |
345 | } | |
346 | } | |
347 | ||
348 | // All sub-requests are marked completed so the main request can be | |
349 | // completed as well | |
350 | // Notify application, | |
ae2c17d3 FC |
351 | LttngSyntheticEvent finishEvent = new LttngSyntheticEvent(fStatusEvent); |
352 | finishEvent.setSequenceInd(SequenceInd.ENDREQ); | |
353 | finishEvent.setTraceModel(traceModel); | |
550d787e FC |
354 | |
355 | try { | |
ae2c17d3 FC |
356 | queueResult(finishEvent); |
357 | queueResult(fStatusEventAck); | |
550d787e FC |
358 | // End the loop in the main request |
359 | queueResult(LttngSyntheticEvent.NullEvent); | |
360 | } catch (InterruptedException e) { | |
361 | // System.out.println(getName() + | |
362 | // ":handleProviderDone() failed to queue request"); | |
cb866e08 FC |
363 | // TODO: Cancel the request |
364 | // e.printStackTrace(); | |
550d787e | 365 | } |
5d10d135 ASL |
366 | } |
367 | ||
368 | /** | |
369 | * Increment the global event counter i.e. events from any sub requests | |
370 | */ | |
371 | private synchronized void incrementSynEvenCount() { | |
550d787e | 372 | fMainReqEventCount++; |
5d10d135 ASL |
373 | } |
374 | ||
375 | /** | |
376 | * @return | |
377 | */ | |
378 | public synchronized int getSynEvenCount() { | |
550d787e | 379 | return fMainReqEventCount; |
5d10d135 ASL |
380 | } |
381 | ||
382 | /** | |
383 | * Reset provider to a state ready to begin thread execution | |
384 | * | |
385 | * @param experimentNode | |
386 | */ | |
550d787e | 387 | public synchronized void reset(LTTngTreeNode experimentNode) { |
5d10d135 ASL |
388 | |
389 | fmainRequest = null; | |
390 | ||
391 | // Make sure previous request are terminated | |
550d787e | 392 | for (LttngBaseEventRequest tmpRequest : fEventProviderRequests.values()) { |
5d10d135 ASL |
393 | if (tmpRequest != null && !tmpRequest.isCompleted()) { |
394 | tmpRequest.cancel(); | |
395 | } | |
396 | } | |
397 | ||
550d787e FC |
398 | fEventProviderRequests.clear(); |
399 | fMainReqEventCount = 0; | |
5d10d135 ASL |
400 | startIndSent = false; |
401 | ||
402 | // set of base event providers | |
403 | if (fExperiment != null) { | |
404 | LTTngTreeNode[] traces = fExperiment.getChildren(); | |
405 | for (LTTngTreeNode trace : traces) { | |
406 | IStateTraceManager traceBaseEventProvider = (IStateTraceManager) trace; | |
550d787e | 407 | fEventProviderRequests.put(traceBaseEventProvider, null); |
5d10d135 ASL |
408 | } |
409 | } | |
410 | ||
411 | if (fExperiment != experimentNode) { | |
412 | updateExperimentNode(experimentNode); | |
413 | } | |
414 | } | |
415 | ||
416 | /** | |
417 | * Point to a new experiment reference | |
418 | * | |
419 | * @param experiment | |
420 | */ | |
421 | private synchronized void updateExperimentNode(LTTngTreeNode experiment) { | |
422 | if (experiment != null | |
423 | && experiment.getValue() instanceof TmfExperiment<?>) { | |
424 | fExperiment = experiment; | |
425 | } else { | |
426 | TraceDebug | |
427 | .debug("Experiment received is not instance of TmfExperiment: " | |
428 | + experiment.getClass().getName()); | |
429 | } | |
430 | } | |
431 | ||
432 | /* | |
433 | * (non-Javadoc) | |
434 | * | |
435 | * @see | |
436 | * org.eclipse.linuxtools.tmf.component.TmfDataProvider#sendRequest(org. | |
437 | * eclipse.linuxtools.tmf.request.TmfDataRequest) | |
438 | */ | |
cb866e08 | 439 | @Override |
550d787e | 440 | public void sendRequest(final ITmfDataRequest<LttngSyntheticEvent> request) { |
5d10d135 ASL |
441 | super.sendRequest(request); |
442 | if (waitForRequest) { | |
443 | try { | |
444 | request.waitForCompletion(); | |
445 | } catch (InterruptedException e) { | |
446 | e.printStackTrace(); | |
447 | } | |
448 | } | |
449 | } | |
450 | ||
451 | /** | |
452 | * @return the waitForRequest | |
453 | */ | |
454 | public boolean isWaitForRequest() { | |
455 | return waitForRequest; | |
456 | } | |
457 | ||
458 | /** | |
459 | * @param waitForRequest | |
460 | * configures the provider to wait for the request completion | |
461 | */ | |
462 | public void setWaitForRequest(boolean waitForRequest) { | |
463 | this.waitForRequest = waitForRequest; | |
464 | } | |
465 | ||
cb866e08 FC |
466 | @Override |
467 | public LttngSyntheticEvent getNext(ITmfContext context) throws InterruptedException { | |
468 | return super.getNext(context); | |
469 | } | |
470 | ||
471 | @Override | |
472 | public void queueResult(LttngSyntheticEvent data) throws InterruptedException { | |
473 | super.queueResult(data); | |
474 | } | |
475 | ||
5d10d135 | 476 | } |