tmf: Drop generics from ITmfTrace and TmfExperiment
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.core / src / org / eclipse / linuxtools / internal / lttng / core / state / trace / StateTraceManager.java
CommitLineData
5d10d135
ASL
1/*******************************************************************************
2 * Copyright (c) 2009, 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 *******************************************************************************/
5945cec9 12package org.eclipse.linuxtools.internal.lttng.core.state.trace;
5d10d135
ASL
13
14import java.util.Collections;
15import java.util.HashMap;
16import java.util.Vector;
17
9fa32496 18import org.eclipse.linuxtools.internal.lttng.core.Activator;
5945cec9
FC
19import org.eclipse.linuxtools.internal.lttng.core.TraceDebug;
20import org.eclipse.linuxtools.internal.lttng.core.event.LttngEvent;
21import org.eclipse.linuxtools.internal.lttng.core.event.LttngSyntheticEvent;
5945cec9 22import org.eclipse.linuxtools.internal.lttng.core.event.LttngSyntheticEvent.SequenceInd;
d905a64a 23import org.eclipse.linuxtools.internal.lttng.core.event.LttngTimestamp;
5945cec9
FC
24import org.eclipse.linuxtools.internal.lttng.core.model.LTTngTreeNode;
25import org.eclipse.linuxtools.internal.lttng.core.request.ILttngSyntEventRequest;
26import org.eclipse.linuxtools.internal.lttng.core.request.IRequestStatusListener;
27import org.eclipse.linuxtools.internal.lttng.core.request.LttngSyntEventRequest;
28import org.eclipse.linuxtools.internal.lttng.core.state.LttngStateException;
29import org.eclipse.linuxtools.internal.lttng.core.state.evProcessor.ITransEventProcessor;
30import org.eclipse.linuxtools.internal.lttng.core.state.evProcessor.state.StateEventToHandlerFactory;
31import org.eclipse.linuxtools.internal.lttng.core.state.model.LttngTraceState;
32import org.eclipse.linuxtools.internal.lttng.core.state.model.StateModelFactory;
33import org.eclipse.linuxtools.internal.lttng.core.state.resource.ILttngStateContext;
34import org.eclipse.linuxtools.internal.lttng.core.trace.LTTngTextTrace;
35import org.eclipse.linuxtools.internal.lttng.core.trace.LTTngTrace;
4df4581d 36import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
6c13869b
FC
37import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
38import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
39import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
6c13869b
FC
40import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
41import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
42import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpoint;
d905a64a 43import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
9e0640dc 44import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
6c13869b 45import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
5d10d135 46
550d787e 47public class StateTraceManager extends LTTngTreeNode implements IStateTraceManager, ILttngStateContext {
5d10d135
ASL
48
49 // constants
50 private static final long DEFAULT_OFFSET = 0L;
51 private static final int DEFAULT_CHUNK = 1;
52
53 // configurable check point interval
b12f4544 54 private static final long LTTNG_CHECK_POINT_INTERVAL = 50000L;
5d10d135
ASL
55 private long fcheckPointInterval = LTTNG_CHECK_POINT_INTERVAL;
56
57 private TmfExperiment<LttngEvent> fExperiment = null;
58
59 // immutable Objects
4df4581d 60 private final ITmfTrace<?> fTrace;
5d10d135
ASL
61 private int fcpuNumber = -1;
62 private final ITransEventProcessor fStateUpdateProcessor;
63
64 // potentially thread shared
65 private final HashMap<Long, LttngTraceState> stateCheckpointsList = new HashMap<Long, LttngTraceState>();
66 private final Vector<TmfCheckpoint> timestampCheckpointsList = new Vector<TmfCheckpoint>();
67 private LttngTraceState fStateModel;
550d787e 68 private LttngTraceState fCheckPointStateModel;
5d10d135
ASL
69
70 // locks
c1c69938
FC
71 private Object fCheckPointsLock = new Object();
72 private Object fStateModelLock = new Object();
5d10d135 73
5d10d135
ASL
74
75
76 // =======================================================================
77 // Constructor
78 // =======================================================================
79 /**
80 * @param id
81 * @param parent
82 * @param name
83 * @param trace
5d10d135
ASL
84 * @throws LttngStateException
85 */
4df4581d 86 public StateTraceManager(Long id, LTTngTreeNode parent, String name, ITmfTrace<?> trace) throws LttngStateException {
5d10d135
ASL
87 super(id, parent, name, trace);
88
89 if (trace == null) {
9c4eb5f7 90 throw new LttngStateException("No TmfTrace object available!"); //$NON-NLS-1$
5d10d135
ASL
91 }
92
93 fTrace = trace;
5d10d135 94 fStateUpdateProcessor = StateEventToHandlerFactory.getInstance();
550d787e 95
5d10d135 96 init();
550d787e
FC
97
98 fStateModel = StateModelFactory.getStateEntryInstance(this);
99 fStateModel.init(this);
100
101 fCheckPointStateModel = StateModelFactory.getStateEntryInstance(this);
102 fCheckPointStateModel.init(this);
5d10d135
ASL
103 }
104
105 // =======================================================================
106 // Methods
107 // =======================================================================
108 @SuppressWarnings("unchecked")
109 private void init() {
110 // resolve the experiment
111 Object obj = getParent().getValue();
112 if (obj != null && obj instanceof TmfExperiment<?>) {
113 fExperiment = (TmfExperiment<LttngEvent>) obj;
114 }
115
116 // initialize the number of cpus
117 if (fTrace instanceof LTTngTrace) {
118 fcpuNumber = ((LTTngTrace) fTrace).getCpuNumber();
119 } else if (fTrace instanceof LTTngTextTrace) {
120 fcpuNumber = ((LTTngTextTrace) fTrace).getCpuNumber();
121 }
122 }
550d787e 123
5d10d135 124
8827c197 125
8827c197 126
5d10d135
ASL
127
128 /*
129 * (non-Javadoc)
130 *
131 * @see org.eclipse.linuxtools.lttng.state.IStateManager#getEventLog()
132 */
d4011df2 133 @Override
4df4581d 134 public ITmfTrace<?> getStateTrace() {
5d10d135
ASL
135 return fTrace;
136 }
137
138 /**
139 * Save a checkpoint if it is needed at that point
140 * <p>
141 * The function will use "eventCount" internally to determine if a save was
142 * needed
143 *
144 * @param eventCounter
145 * The event "count" or event "id" so far
146 * @param eventTime
147 * The timestamp of this event
148 *
149 * @return boolean True if a checkpoint was saved, false otherwise
150 */
4df4581d 151 private void saveCheckPointIfNeeded(Long eventCounter, ITmfTimestamp eventTime) {
5d10d135
ASL
152 // Save a checkpoint every LTTNG_STATE_SAVE_INTERVAL event
153 if ((eventCounter.longValue() % fcheckPointInterval) == 0) {
c1c69938 154
5d10d135 155 LttngTraceState stateCheckPoint;
c1c69938 156 synchronized (fCheckPointsLock) {
550d787e 157 stateCheckPoint = fCheckPointStateModel.clone();
5d10d135
ASL
158 }
159
9c4eb5f7
FC
160 TraceDebug.debug("Check point created here: " + eventCounter //$NON-NLS-1$
161 + " -> " + eventTime.toString() + "************" //$NON-NLS-1$ //$NON-NLS-2$
ce970a71 162 + getStateTrace().getName() + " >>>>> Thread: " //$NON-NLS-1$
5d10d135
ASL
163 + Thread.currentThread().getId());
164
c1c69938 165 synchronized (fCheckPointsLock) {
5d10d135
ASL
166 // Save the checkpoint
167 stateCheckpointsList.put(eventCounter, stateCheckPoint);
168 // Save correlation between timestamp and checkpoint index
169
d905a64a 170 timestampCheckpointsList.add(new TmfCheckpoint(new TmfTimestamp(eventTime), new TmfContext(new TmfLocation<Long>(eventCounter), eventCounter)));
5d10d135
ASL
171 }
172 }
173 }
174
175 /**
176 * @return the lttng_check_point_interval
177 */
178 public long getCheckPointInterval() {
179 return fcheckPointInterval;
180 }
181
182 /**
183 * @param check_point_interval
184 * , the lttng_check_point_interval to set
185 */
186 public void setCheckPointInterval(long check_point_interval) {
187 this.fcheckPointInterval = check_point_interval;
188 }
189
736aecd5
ASL
190 /*
191 * (non-Javadoc)
192 *
550d787e
FC
193 * @see org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager#
194 * restoreCheckPointByTimestamp
736aecd5 195 * (org.eclipse.linuxtools.tmf.event.TmfTimestamp)
5d10d135 196 */
d4011df2 197 @Override
5d10d135 198 @SuppressWarnings("unchecked")
4df4581d 199 public TmfCheckpoint restoreCheckPointByTimestamp(ITmfTimestamp eventTime) {
736aecd5 200 TmfTimeRange experimentRange = fExperiment.getTimeRange();
d905a64a 201 TmfCheckpoint checkpoint = new TmfCheckpoint(fTrace.getStartTime(), new TmfContext(new TmfLocation<Long>(0L), 0));
5d10d135
ASL
202
203 // The GUI can have time limits higher than this log, since GUI can
b626c6f7
BH
204 // handle multiple logs. Ignore special null value of experiment time range.
205 if ((eventTime.getValue() < 0) ||
a4115405 206 (!experimentRange.equals(TmfTimeRange.NULL_RANGE) && (eventTime.getValue() > experimentRange.getEndTime().getValue()))) {
5d10d135
ASL
207 return null;
208 }
209
736aecd5
ASL
210 // The GUI can have time limits lower than this trace, since experiment
211 // can handle multiple traces
212 if ((eventTime.getValue() < fTrace.getStartTime().getValue())) {
213 eventTime = fTrace.getStartTime();
5dbe4d3b 214 }
736aecd5 215
c1c69938
FC
216 LttngTraceState traceState;
217 synchronized (fCheckPointsLock) {
218 Collections.sort(timestampCheckpointsList);
219 // Initiate the compare with a checkpoint containing the target time
220 // stamp to find
d905a64a 221 int index = Collections.binarySearch(timestampCheckpointsList, new TmfCheckpoint(eventTime, new TmfContext(new TmfLocation<Long>(0L), 0)));
c1c69938
FC
222 // adjust index to round down to earlier checkpoint when exact match
223 // not
224 // found
225 index = getPrevIndex(index);
226
227 if (index == 0) {
228 // No checkpoint restore is needed, start with a brand new
229 // TraceState
230 traceState = StateModelFactory.getStateEntryInstance(this);
231 } else {
232
233 // Useful CheckPoint found
a79913eb 234 checkpoint = timestampCheckpointsList.get(index);
c1c69938
FC
235 // get the location associated with the checkpoint
236 TmfLocation<Long> location = (TmfLocation<Long>) checkpoint.getLocation();
237 // reference a new copy of the checkpoint template
238 traceState = stateCheckpointsList.get(location.getLocation()).clone();
239 }
240
5dbe4d3b 241 }
5d10d135 242
5dbe4d3b 243 // Restore the stored traceState
c1c69938 244 synchronized (fStateModelLock) {
5dbe4d3b 245 fStateModel = traceState;
5d10d135
ASL
246 }
247
a79913eb
FC
248 return checkpoint;
249 }
250
251 /* (non-Javadoc)
252 * @see org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager#restoreCheckPointByIndex(long)
253 */
254 @Override
255 public TmfCheckpoint restoreCheckPointByIndex(long eventIndex) {
d905a64a 256 TmfCheckpoint checkpoint = new TmfCheckpoint(fTrace.getStartTime(), new TmfContext(new TmfLocation<Long>(0L), 0));
a79913eb
FC
257
258 LttngTraceState traceState;
259 synchronized (fCheckPointsLock) {
260 Collections.sort(timestampCheckpointsList);
261 // Initiate the compare with a checkpoint containing the target time
262 // stamp to find
d905a64a 263 int index = Collections.binarySearch(timestampCheckpointsList, new TmfCheckpoint(null, new TmfContext(new TmfLocation<Long>(eventIndex), eventIndex)));
a79913eb
FC
264 // adjust index to round down to earlier checkpoint when exact match not found
265 index = getPrevIndex(index);
266
267 if (index == 0) {
268 // No checkpoint restore is needed, start with a brand new
269 // TraceState
270 traceState = StateModelFactory.getStateEntryInstance(this);
271 } else {
272
273 // Useful CheckPoint found
274 checkpoint = timestampCheckpointsList.get(index);
275 // get the location associated with the checkpoint
276 @SuppressWarnings("unchecked")
277 TmfLocation<Long> location = (TmfLocation<Long>) checkpoint.getLocation();
278 // reference a new copy of the checkpoint template
279 traceState = stateCheckpointsList.get(location.getLocation()).clone();
280 }
281
282 }
283
284 // Restore the stored traceState
285 synchronized (fStateModelLock) {
286 fStateModel = traceState;
287 }
288
289 return checkpoint;
5d10d135
ASL
290 }
291
292 /**
293 * Adjust the result from a binary search to the round down position
294 *
295 * @param position
296 * if Negative is: (-(insertion point) -1)
297 * @return position or if no match found, earlier than insertion point
298 */
299 private int getPrevIndex(int position) {
300 int roundDownPosition = position;
301 if (position < 0) {
302 roundDownPosition = -(position + 2);
303 }
304
305 roundDownPosition = roundDownPosition < 0 ? 0 : roundDownPosition;
306 return roundDownPosition;
307 }
308
5d10d135
ASL
309 // TODO: Remove this request type when the UI handle their own requests
310 /**
311 * Request Event data of a specified time range
312 *
313 * @param timeWindow
314 * @param listener
315 * @param processor
316 * @return ILttngEventRequest The request made
317 */
550d787e 318 ILttngSyntEventRequest getDataRequestByTimeRange(TmfTimeRange timeWindow, IRequestStatusListener listener,
5d10d135
ASL
319 final ITransEventProcessor processor) {
320
550d787e
FC
321 ILttngSyntEventRequest request = new StateTraceManagerRequest(timeWindow, DEFAULT_OFFSET,
322 TmfDataRequest.ALL_DATA, DEFAULT_CHUNK, listener, getExperimentTimeWindow(), processor) {
5d10d135
ASL
323 };
324
325 return request;
326 }
327
5d10d135
ASL
328
329 /*
330 * (non-Javadoc)
331 *
332 * @see
333 * org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager#getStateModel
334 * ()
335 */
d4011df2 336 @Override
c1c69938
FC
337 public LttngTraceState getStateModel() {
338 LttngTraceState stateModel = null;
339 synchronized (fStateModelLock) {
340 stateModel = fStateModel;
5d10d135 341 }
c1c69938 342 return stateModel;
5d10d135
ASL
343 }
344
550d787e
FC
345 /*
346 * (non-Javadoc)
347 *
348 * @see org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager#
349 * getCheckPointStateModel()
350 */
d4011df2 351 @Override
c1c69938
FC
352 public LttngTraceState getCheckPointStateModel() {
353 LttngTraceState checkPointStateModel = null;
354 synchronized (fCheckPointsLock) {
355 checkPointStateModel = fCheckPointStateModel;
550d787e 356 }
c1c69938 357 return checkPointStateModel;
550d787e
FC
358 }
359
5d10d135
ASL
360 /**
361 * @return the stateCheckpointsList
362 */
363 HashMap<Long, LttngTraceState> getStateCheckpointsList() {
364 return stateCheckpointsList;
365 }
366
367 /**
368 * @return the timestampCheckpointsList
369 */
370 Vector<TmfCheckpoint> getTimestampCheckpointsList() {
371 return timestampCheckpointsList;
372 }
373 // =======================================================================
374 // Inner Class
375 // =======================================================================
376 class StateTraceManagerRequest extends LttngSyntEventRequest {
377 // =======================================================================
378 // Data
379 // =======================================================================
380 final TmfEvent[] evt = new TmfEvent[1];
381 final ITransEventProcessor fprocessor;
382 LttngSyntheticEvent synEvent;
383 Long fCount = getSynEventCount();
384
385 // =======================================================================
386 // Constructor
387 // =======================================================================
550d787e
FC
388 public StateTraceManagerRequest(TmfTimeRange range, long offset, int nbEvents, int maxBlockSize,
389 IRequestStatusListener listener, TmfTimeRange experimentTimeRange, ITransEventProcessor processor) {
5d10d135 390
550d787e 391 super(range, offset, nbEvents, maxBlockSize, listener, experimentTimeRange, processor);
5d10d135 392 fprocessor = processor;
9c4eb5f7 393 TraceDebug.debug("Instance created for range: " + range.toString()); //$NON-NLS-1$
5d10d135
ASL
394 fCount = 0L;
395 }
396
397 // =======================================================================
398 // Methods
399 // =======================================================================
400 /*
401 * (non-Javadoc)
402 *
403 * @see
404 * org.eclipse.linuxtools.lttng.request.LttngSyntEventRequest#handleData
405 * ()
406 */
407 @Override
f9673903
FC
408 public void handleData(LttngSyntheticEvent event) {
409 super.handleData(event);
410 if (event != null) {
411 synEvent = event;
5d10d135
ASL
412 if (synEvent.getSynType() == SequenceInd.AFTER) {
413 // Note : We call this function before incrementing
414 // eventCount to save a default check point at the "0th"
415 // event
416 saveCheckPoint(fCount, synEvent.getTimestamp());
417 fCount++;
418
419 if (TraceDebug.isDEBUG()) {
420 if (fCount % 1000 == 0) {
9c4eb5f7 421 TraceDebug.debug("handled: " + fCount + " sequence: " + synEvent.getSynType()); //$NON-NLS-1$ //$NON-NLS-2$
5d10d135
ASL
422 }
423 }
424 }
425 }
426 }
427
428 /**
429 * To be overridden by active save e.g. check points, this no action
430 * default is used for requests which do not require rebuilding of
431 * checkpoints e.g. requiring data of a new time range selection
432 *
433 * @param count
434 * @param time
435 */
4df4581d 436 public void saveCheckPoint(Long count, ITmfTimestamp time) {
5d10d135
ASL
437
438 }
439 }
440
441 /*
442 * (non-Javadoc)
443 *
444 * @see org.eclipse.linuxtools.lttng.state.resource.ILttngStateContext#
445 * getNumberOfCpus()
446 */
d4011df2 447 @Override
5d10d135
ASL
448 public int getNumberOfCpus() {
449 return fcpuNumber;
450 }
451
452 /*
453 * (non-Javadoc)
454 *
455 * @see org.eclipse.linuxtools.lttng.state.resource.ILttngStateContext#
456 * getTraceTimeWindow()
457 */
d4011df2 458 @Override
5d10d135
ASL
459 public TmfTimeRange getTraceTimeWindow() {
460 if (fTrace != null) {
461 return fTrace.getTimeRange();
462
463 }
464 return null;
465 }
466
467 /*
468 * (non-Javadoc)
469 *
470 * @see
471 * org.eclipse.linuxtools.lttng.state.resource.ILttngStateContext#getTraceId
472 * ()
473 */
d4011df2 474 @Override
5d10d135
ASL
475 public String getTraceId() {
476 if (fTrace != null) {
477 return fTrace.getName();
478 }
479 return null;
480 }
481
482 /*
483 * (non-Javadoc)
484 *
485 * @see org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager#
486 * getExperimentTimeWindow()
487 */
d4011df2 488 @Override
5d10d135
ASL
489 public TmfTimeRange getExperimentTimeWindow() {
490 if (fExperiment != null) {
491 return fExperiment.getTimeRange();
492 }
493 return null;
494 }
495
496 /*
497 * (non-Javadoc)
498 *
499 * @see
500 * org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager#getExperimentName
501 * ()
502 */
d4011df2 503 @Override
5d10d135
ASL
504 public String getExperimentName() {
505 return fExperiment.getName();
506 }
507
508 /*
509 * (non-Javadoc)
510 *
511 * @see
512 * org.eclipse.linuxtools.lttng.state.resource.ILttngStateContext#getTraceIdRef
513 * ()
514 */
d4011df2 515 @Override
4df4581d 516 public ITmfTrace<?> getTraceIdRef() {
5d10d135
ASL
517 return fTrace;
518 }
550d787e
FC
519
520 /*
521 * (non-Javadoc)
522 *
523 * @see
524 * org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager#clearCheckPoints
525 * ()
526 */
d4011df2 527 @Override
c1c69938
FC
528 public void clearCheckPoints() {
529 synchronized (fCheckPointsLock) {
550d787e
FC
530 stateCheckpointsList.clear();
531 timestampCheckpointsList.clear();
532
533 fCheckPointStateModel = StateModelFactory.getStateEntryInstance(this);
c1c69938 534
550d787e
FC
535 try {
536 fCheckPointStateModel.init(this);
537 } catch (LttngStateException e) {
9fa32496 538 Activator.getDefault().logError("Unexpected Error", e); //$NON-NLS-1$
550d787e
FC
539 }
540 }
541 }
542
543 /*
544 * (non-Javadoc)
545 *
546 * @see
547 * org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager#handleEvent
548 * (org.eclipse.linuxtools.lttng.event.LttngSyntheticEvent, java.lang.Long)
549 */
d4011df2 550 @Override
550d787e
FC
551 public void handleEvent(LttngSyntheticEvent synEvent, Long eventCount) {
552 fStateUpdateProcessor.process(synEvent, fCheckPointStateModel);
553
554 // Save checkpoint as needed
555 saveCheckPointIfNeeded(eventCount - 1, synEvent.getTimestamp());
556 }
557
558 /*
559 * (non-Javadoc)
560 *
561 * @see java.lang.Object#toString()
562 */
cb866e08 563 @Override
3b38ea61 564 @SuppressWarnings("nls")
550d787e
FC
565 public String toString() {
566 StringBuilder sb = new StringBuilder(super.toString());
567 sb.append("\n\tTotal number of processes in the Shared State model: " + fStateModel.getProcesses().length
568 + "\n\t" + "Total number of processes in the Check point State model: "
569 + fCheckPointStateModel.getProcesses().length);
570
571 TmfTimeRange traceTRange = fTrace.getTimeRange();
572 sb.append("\n\tTrace time interval for trace " + fTrace.getName() + "\n\t"
573 + new LttngTimestamp(traceTRange.getStartTime()));
574 sb.append(" - " + new LttngTimestamp(traceTRange.getEndTime()));
575 sb.append("\n\tCheckPoints available at: ");
576 for (TmfCheckpoint cpoint : timestampCheckpointsList) {
577 sb.append("\n\t" + "Location: " + cpoint.getLocation() + " - " + cpoint.getTimestamp());
578 }
579
580 return sb.toString();
581 }
582
b12f4544
FC
583 /*
584 * (non-Javadoc)
585 * @see org.eclipse.linuxtools.lttng.state.resource.ILttngStateContext#getIdentifier()
586 */
587 @Override
588 public long getIdentifier() {
589 return getId().longValue();
590 }
591
550d787e 592}
This page took 0.065133 seconds and 5 git commands to generate.