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