Re-structure LTTng sub-project as per the Linux Tools guidelines
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.core / src / org / eclipse / linuxtools / lttng / core / state / trace / StateTraceManager.java
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 *******************************************************************************/
12 package org.eclipse.linuxtools.lttng.core.state.trace;
13
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.Vector;
17
18 import org.eclipse.linuxtools.lttng.core.TraceDebug;
19 import org.eclipse.linuxtools.lttng.core.event.LttngEvent;
20 import org.eclipse.linuxtools.lttng.core.event.LttngSyntheticEvent;
21 import org.eclipse.linuxtools.lttng.core.event.LttngTimestamp;
22 import org.eclipse.linuxtools.lttng.core.event.LttngSyntheticEvent.SequenceInd;
23 import org.eclipse.linuxtools.lttng.core.model.LTTngTreeNode;
24 import org.eclipse.linuxtools.lttng.core.request.ILttngSyntEventRequest;
25 import org.eclipse.linuxtools.lttng.core.request.IRequestStatusListener;
26 import org.eclipse.linuxtools.lttng.core.request.LttngSyntEventRequest;
27 import org.eclipse.linuxtools.lttng.core.state.LttngStateException;
28 import org.eclipse.linuxtools.lttng.core.state.evProcessor.ITransEventProcessor;
29 import org.eclipse.linuxtools.lttng.core.state.evProcessor.state.StateEventToHandlerFactory;
30 import org.eclipse.linuxtools.lttng.core.state.model.LttngTraceState;
31 import org.eclipse.linuxtools.lttng.core.state.model.StateModelFactory;
32 import org.eclipse.linuxtools.lttng.core.state.resource.ILttngStateContext;
33 import org.eclipse.linuxtools.lttng.core.trace.LTTngTextTrace;
34 import org.eclipse.linuxtools.lttng.core.trace.LTTngTrace;
35 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
36 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
37 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
38 import org.eclipse.linuxtools.tmf.core.experiment.TmfExperiment;
39 import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
40 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
41 import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpoint;
42 import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
43
44 public class StateTraceManager extends LTTngTreeNode implements IStateTraceManager, ILttngStateContext {
45
46 // constants
47 private static final long DEFAULT_OFFSET = 0L;
48 private static final int DEFAULT_CHUNK = 1;
49
50 // configurable check point interval
51 private static final long LTTNG_CHECK_POINT_INTERVAL = 50000L;
52 private long fcheckPointInterval = LTTNG_CHECK_POINT_INTERVAL;
53
54 private TmfExperiment<LttngEvent> fExperiment = null;
55
56 // immutable Objects
57 private final ITmfTrace fTrace;
58 private int fcpuNumber = -1;
59 private final ITransEventProcessor fStateUpdateProcessor;
60
61 // potentially thread shared
62 private final HashMap<Long, LttngTraceState> stateCheckpointsList = new HashMap<Long, LttngTraceState>();
63 private final Vector<TmfCheckpoint> timestampCheckpointsList = new Vector<TmfCheckpoint>();
64 private LttngTraceState fStateModel;
65 private LttngTraceState fCheckPointStateModel;
66
67 // locks
68 private Object fCheckPointsLock = new Object();
69 private Object fStateModelLock = new Object();
70
71
72
73 // =======================================================================
74 // Constructor
75 // =======================================================================
76 /**
77 * @param id
78 * @param parent
79 * @param name
80 * @param trace
81 * @throws LttngStateException
82 */
83 public StateTraceManager(Long id, LTTngTreeNode parent, String name, ITmfTrace trace) throws LttngStateException {
84 super(id, parent, name, trace);
85
86 if (trace == null) {
87 throw new LttngStateException("No TmfTrace object available!"); //$NON-NLS-1$
88 }
89
90 fTrace = trace;
91 fStateUpdateProcessor = StateEventToHandlerFactory.getInstance();
92
93 init();
94
95 fStateModel = StateModelFactory.getStateEntryInstance(this);
96 fStateModel.init(this);
97
98 fCheckPointStateModel = StateModelFactory.getStateEntryInstance(this);
99 fCheckPointStateModel.init(this);
100 }
101
102 // =======================================================================
103 // Methods
104 // =======================================================================
105 @SuppressWarnings("unchecked")
106 private void init() {
107 // resolve the experiment
108 Object obj = getParent().getValue();
109 if (obj != null && obj instanceof TmfExperiment<?>) {
110 fExperiment = (TmfExperiment<LttngEvent>) obj;
111 }
112
113 // initialize the number of cpus
114 if (fTrace instanceof LTTngTrace) {
115 fcpuNumber = ((LTTngTrace) fTrace).getCpuNumber();
116 } else if (fTrace instanceof LTTngTextTrace) {
117 fcpuNumber = ((LTTngTextTrace) fTrace).getCpuNumber();
118 }
119 }
120
121
122
123
124
125 /*
126 * (non-Javadoc)
127 *
128 * @see org.eclipse.linuxtools.lttng.state.IStateManager#getEventLog()
129 */
130 @Override
131 public ITmfTrace getTrace() {
132 return fTrace;
133 }
134
135 /**
136 * Save a checkpoint if it is needed at that point
137 * <p>
138 * The function will use "eventCount" internally to determine if a save was
139 * needed
140 *
141 * @param eventCounter
142 * The event "count" or event "id" so far
143 * @param eventTime
144 * The timestamp of this event
145 *
146 * @return boolean True if a checkpoint was saved, false otherwise
147 */
148 private void saveCheckPointIfNeeded(Long eventCounter, TmfTimestamp eventTime) {
149 // Save a checkpoint every LTTNG_STATE_SAVE_INTERVAL event
150 if ((eventCounter.longValue() % fcheckPointInterval) == 0) {
151
152 LttngTraceState stateCheckPoint;
153 synchronized (fCheckPointsLock) {
154 stateCheckPoint = fCheckPointStateModel.clone();
155 }
156
157 TraceDebug.debug("Check point created here: " + eventCounter //$NON-NLS-1$
158 + " -> " + eventTime.toString() + "************" //$NON-NLS-1$ //$NON-NLS-2$
159 + getTrace().getName() + " >>>>> Thread: " //$NON-NLS-1$
160 + Thread.currentThread().getId());
161
162 synchronized (fCheckPointsLock) {
163 // Save the checkpoint
164 stateCheckpointsList.put(eventCounter, stateCheckPoint);
165 // Save correlation between timestamp and checkpoint index
166
167 timestampCheckpointsList.add(new TmfCheckpoint(new TmfTimestamp(eventTime), new TmfLocation<Long>(
168 eventCounter)));
169 }
170 }
171 }
172
173 /**
174 * @return the lttng_check_point_interval
175 */
176 public long getCheckPointInterval() {
177 return fcheckPointInterval;
178 }
179
180 /**
181 * @param check_point_interval
182 * , the lttng_check_point_interval to set
183 */
184 public void setCheckPointInterval(long check_point_interval) {
185 this.fcheckPointInterval = check_point_interval;
186 }
187
188 /*
189 * (non-Javadoc)
190 *
191 * @see org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager#
192 * restoreCheckPointByTimestamp
193 * (org.eclipse.linuxtools.tmf.event.TmfTimestamp)
194 */
195 @Override
196 @SuppressWarnings("unchecked")
197 public TmfCheckpoint restoreCheckPointByTimestamp(TmfTimestamp eventTime) {
198 TmfTimeRange experimentRange = fExperiment.getTimeRange();
199 TmfCheckpoint checkpoint = new TmfCheckpoint(fTrace.getStartTime(), new TmfLocation<Long>(0L));
200
201 // The GUI can have time limits higher than this log, since GUI can
202 // handle multiple logs. Ignore special null value of experiment time range.
203 if ((eventTime.getValue() < 0) ||
204 (!experimentRange.equals(TmfTimeRange.Null) && (eventTime.getValue() > experimentRange.getEndTime().getValue()))) {
205 return null;
206 }
207
208 // The GUI can have time limits lower than this trace, since experiment
209 // can handle multiple traces
210 if ((eventTime.getValue() < fTrace.getStartTime().getValue())) {
211 eventTime = fTrace.getStartTime();
212 }
213
214 LttngTraceState traceState;
215 synchronized (fCheckPointsLock) {
216 Collections.sort(timestampCheckpointsList);
217 // Initiate the compare with a checkpoint containing the target time
218 // stamp to find
219 int index = Collections.binarySearch(timestampCheckpointsList, new TmfCheckpoint(eventTime,
220 new TmfLocation<Long>(0L)));
221 // adjust index to round down to earlier checkpoint when exact match
222 // not
223 // found
224 index = getPrevIndex(index);
225
226 if (index == 0) {
227 // No checkpoint restore is needed, start with a brand new
228 // TraceState
229 traceState = StateModelFactory.getStateEntryInstance(this);
230 } else {
231
232 // Useful CheckPoint found
233 checkpoint = timestampCheckpointsList.get(index);
234 // get the location associated with the checkpoint
235 TmfLocation<Long> location = (TmfLocation<Long>) checkpoint.getLocation();
236 // reference a new copy of the checkpoint template
237 traceState = stateCheckpointsList.get(location.getLocation()).clone();
238 }
239
240 }
241
242 // Restore the stored traceState
243 synchronized (fStateModelLock) {
244 fStateModel = traceState;
245 }
246
247 return checkpoint;
248 }
249
250 /* (non-Javadoc)
251 * @see org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager#restoreCheckPointByIndex(long)
252 */
253 @Override
254 public TmfCheckpoint restoreCheckPointByIndex(long eventIndex) {
255 TmfCheckpoint checkpoint = new TmfCheckpoint(fTrace.getStartTime(), new TmfLocation<Long>(0L));
256
257 LttngTraceState traceState;
258 synchronized (fCheckPointsLock) {
259 Collections.sort(timestampCheckpointsList);
260 // Initiate the compare with a checkpoint containing the target time
261 // stamp to find
262 int index = Collections.binarySearch(timestampCheckpointsList, new TmfCheckpoint(null,
263 new TmfLocation<Long>(eventIndex)));
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;
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
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 */
318 ILttngSyntEventRequest getDataRequestByTimeRange(TmfTimeRange timeWindow, IRequestStatusListener listener,
319 final ITransEventProcessor processor) {
320
321 ILttngSyntEventRequest request = new StateTraceManagerRequest(timeWindow, DEFAULT_OFFSET,
322 TmfDataRequest.ALL_DATA, DEFAULT_CHUNK, listener, getExperimentTimeWindow(), processor) {
323 };
324
325 return request;
326 }
327
328
329 /*
330 * (non-Javadoc)
331 *
332 * @see
333 * org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager#getStateModel
334 * ()
335 */
336 @Override
337 public LttngTraceState getStateModel() {
338 LttngTraceState stateModel = null;
339 synchronized (fStateModelLock) {
340 stateModel = fStateModel;
341 }
342 return stateModel;
343 }
344
345 /*
346 * (non-Javadoc)
347 *
348 * @see org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager#
349 * getCheckPointStateModel()
350 */
351 @Override
352 public LttngTraceState getCheckPointStateModel() {
353 LttngTraceState checkPointStateModel = null;
354 synchronized (fCheckPointsLock) {
355 checkPointStateModel = fCheckPointStateModel;
356 }
357 return checkPointStateModel;
358 }
359
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 // =======================================================================
388 public StateTraceManagerRequest(TmfTimeRange range, long offset, int nbEvents, int maxBlockSize,
389 IRequestStatusListener listener, TmfTimeRange experimentTimeRange, ITransEventProcessor processor) {
390
391 super(range, offset, nbEvents, maxBlockSize, listener, experimentTimeRange, processor);
392 fprocessor = processor;
393 TraceDebug.debug("Instance created for range: " + range.toString()); //$NON-NLS-1$
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
408 public void handleData(LttngSyntheticEvent event) {
409 super.handleData(event);
410 if (event != null) {
411 synEvent = event;
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) {
421 TraceDebug.debug("handled: " + fCount + " sequence: " + synEvent.getSynType()); //$NON-NLS-1$ //$NON-NLS-2$
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 */
436 public void saveCheckPoint(Long count, TmfTimestamp time) {
437
438 }
439 }
440
441 /*
442 * (non-Javadoc)
443 *
444 * @see org.eclipse.linuxtools.lttng.state.resource.ILttngStateContext#
445 * getNumberOfCpus()
446 */
447 @Override
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 */
458 @Override
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 */
474 @Override
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 */
488 @Override
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 */
503 @Override
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 */
515 @Override
516 public ITmfTrace getTraceIdRef() {
517 return fTrace;
518 }
519
520 /*
521 * (non-Javadoc)
522 *
523 * @see
524 * org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager#clearCheckPoints
525 * ()
526 */
527 @Override
528 public void clearCheckPoints() {
529 synchronized (fCheckPointsLock) {
530 stateCheckpointsList.clear();
531 timestampCheckpointsList.clear();
532
533 fCheckPointStateModel = StateModelFactory.getStateEntryInstance(this);
534
535 try {
536 fCheckPointStateModel.init(this);
537 } catch (LttngStateException e) {
538 e.printStackTrace();
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 */
550 @Override
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 */
563 @Override
564 @SuppressWarnings("nls")
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
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
592 }
This page took 0.043061 seconds and 5 git commands to generate.