Commit | Line | Data |
---|---|---|
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 | *******************************************************************************/ | |
12 | package org.eclipse.linuxtools.lttng.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.TraceDebug; | |
19 | import org.eclipse.linuxtools.lttng.event.LttngEvent; | |
20 | import org.eclipse.linuxtools.lttng.event.LttngSyntheticEvent; | |
21 | import org.eclipse.linuxtools.lttng.event.LttngSyntheticEvent.SequenceInd; | |
22 | import org.eclipse.linuxtools.lttng.event.LttngTimestamp; | |
23 | import org.eclipse.linuxtools.lttng.model.LTTngTreeNode; | |
24 | import org.eclipse.linuxtools.lttng.request.ILttngSyntEventRequest; | |
25 | import org.eclipse.linuxtools.lttng.request.IRequestStatusListener; | |
26 | import org.eclipse.linuxtools.lttng.request.LttngSyntEventRequest; | |
27 | import org.eclipse.linuxtools.lttng.state.LttngStateException; | |
28 | import org.eclipse.linuxtools.lttng.state.evProcessor.ITransEventProcessor; | |
29 | import org.eclipse.linuxtools.lttng.state.evProcessor.state.StateEventToHandlerFactory; | |
30 | import org.eclipse.linuxtools.lttng.state.model.LttngTraceState; | |
31 | import org.eclipse.linuxtools.lttng.state.model.StateModelFactory; | |
32 | import org.eclipse.linuxtools.lttng.state.resource.ILttngStateContext; | |
33 | import org.eclipse.linuxtools.lttng.trace.LTTngTextTrace; | |
34 | import org.eclipse.linuxtools.lttng.trace.LTTngTrace; | |
5d10d135 ASL |
35 | import org.eclipse.linuxtools.tmf.event.TmfEvent; |
36 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; | |
37 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; | |
38 | import org.eclipse.linuxtools.tmf.experiment.TmfExperiment; | |
39 | import org.eclipse.linuxtools.tmf.request.TmfDataRequest; | |
5d10d135 ASL |
40 | import org.eclipse.linuxtools.tmf.trace.ITmfTrace; |
41 | import org.eclipse.linuxtools.tmf.trace.TmfCheckpoint; | |
42 | import org.eclipse.linuxtools.tmf.trace.TmfLocation; | |
43 | ||
736aecd5 | 44 | public 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; | |
9aae0442 | 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 | */ | |
736aecd5 | 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(); |
9aae0442 | 91 | |
5d10d135 | 92 | init(); |
736aecd5 | 93 | |
9aae0442 ASL |
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 | } | |
736aecd5 | 119 | |
5d10d135 | 120 | |
5d10d135 | 121 | |
5d10d135 | 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 | */ | |
736aecd5 | 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; | |
9aae0442 ASL |
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 | ||
736aecd5 ASL |
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 | * | |
188 | * @see org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager# | |
189 | * restoreCheckPointByTimestamp | |
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 | |
736aecd5 | 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(); | |
207 | ||
208 | Collections.sort(timestampCheckpointsList); | |
209 | // Initiate the compare with a checkpoint containing the target time | |
210 | // stamp to find | |
211 | int index = Collections.binarySearch(timestampCheckpointsList, new TmfCheckpoint(eventTime, | |
212 | new TmfLocation<Long>(0L))); | |
213 | // adjust index to round down to earlier checkpoint when exact match | |
214 | // not | |
215 | // found | |
216 | index = getPrevIndex(index); | |
217 | ||
218 | LttngTraceState traceState; | |
219 | if (index == 0) { | |
220 | // No checkpoint restore is needed, start with a brand new | |
221 | // TraceState | |
222 | traceState = StateModelFactory.getStateEntryInstance(this); | |
223 | } else { | |
224 | synchronized (checkPointsLock) { | |
225 | // Useful CheckPoint found | |
226 | TmfCheckpoint checkpoint = timestampCheckpointsList.get(index); | |
227 | nearestTimeStamp = checkpoint.getTimestamp(); | |
228 | // get the location associated with the checkpoint | |
229 | TmfLocation<Long> location = (TmfLocation<Long>) checkpoint.getLocation(); | |
230 | // reference a new copy of the checkpoint template | |
231 | traceState = stateCheckpointsList.get(location.getLocation()).clone(); | |
232 | } | |
5d10d135 | 233 | } |
5d10d135 | 234 | |
736aecd5 ASL |
235 | // Restore the stored traceState |
236 | synchronized (fStateModel) { | |
237 | fStateModel = traceState; | |
9aae0442 | 238 | } |
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 | */ | |
736aecd5 | 270 | ILttngSyntEventRequest getDataRequestByTimeRange(TmfTimeRange timeWindow, IRequestStatusListener listener, |
5d10d135 ASL |
271 | final ITransEventProcessor processor) { |
272 | ||
736aecd5 ASL |
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 | ||
9aae0442 ASL |
294 | /* |
295 | * (non-Javadoc) | |
296 | * | |
736aecd5 ASL |
297 | * @see org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager# |
298 | * getCheckPointStateModel() | |
9aae0442 | 299 | */ |
736aecd5 ASL |
300 | public LttngTraceState getCheckPointStateModel() { |
301 | synchronized (fStateModel) { | |
302 | return fCheckPointStateModel; | |
9aae0442 ASL |
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 | // ======================================================================= | |
736aecd5 ASL |
334 | public StateTraceManagerRequest(TmfTimeRange range, long offset, int nbEvents, int maxBlockSize, |
335 | IRequestStatusListener listener, TmfTimeRange experimentTimeRange, ITransEventProcessor processor) { | |
5d10d135 | 336 | |
736aecd5 | 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 | |
354 | public void handleData() { | |
355 | TmfEvent[] result = getData(); | |
356 | ||
357 | evt[0] = (result.length > 0) ? result[0] : null; | |
358 | if (evt[0] != null) { | |
359 | synEvent = (LttngSyntheticEvent) evt[0]; | |
360 | if (synEvent.getSynType() == SequenceInd.AFTER) { | |
361 | // Note : We call this function before incrementing | |
362 | // eventCount to save a default check point at the "0th" | |
363 | // event | |
364 | saveCheckPoint(fCount, synEvent.getTimestamp()); | |
365 | fCount++; | |
366 | ||
367 | if (TraceDebug.isDEBUG()) { | |
368 | if (fCount % 1000 == 0) { | |
736aecd5 | 369 | TraceDebug.debug("handled: " + fCount + " sequence: " + synEvent.getSynType()); |
5d10d135 ASL |
370 | } |
371 | } | |
372 | } | |
373 | } | |
374 | } | |
375 | ||
376 | /** | |
377 | * To be overridden by active save e.g. check points, this no action | |
378 | * default is used for requests which do not require rebuilding of | |
379 | * checkpoints e.g. requiring data of a new time range selection | |
380 | * | |
381 | * @param count | |
382 | * @param time | |
383 | */ | |
384 | public void saveCheckPoint(Long count, TmfTimestamp time) { | |
385 | ||
386 | } | |
387 | } | |
388 | ||
389 | /* | |
390 | * (non-Javadoc) | |
391 | * | |
392 | * @see org.eclipse.linuxtools.lttng.state.resource.ILttngStateContext# | |
393 | * getNumberOfCpus() | |
394 | */ | |
395 | public int getNumberOfCpus() { | |
396 | return fcpuNumber; | |
397 | } | |
398 | ||
399 | /* | |
400 | * (non-Javadoc) | |
401 | * | |
402 | * @see org.eclipse.linuxtools.lttng.state.resource.ILttngStateContext# | |
403 | * getTraceTimeWindow() | |
404 | */ | |
405 | public TmfTimeRange getTraceTimeWindow() { | |
406 | if (fTrace != null) { | |
407 | return fTrace.getTimeRange(); | |
408 | ||
409 | } | |
410 | return null; | |
411 | } | |
412 | ||
413 | /* | |
414 | * (non-Javadoc) | |
415 | * | |
416 | * @see | |
417 | * org.eclipse.linuxtools.lttng.state.resource.ILttngStateContext#getTraceId | |
418 | * () | |
419 | */ | |
420 | public String getTraceId() { | |
421 | if (fTrace != null) { | |
422 | return fTrace.getName(); | |
423 | } | |
424 | return null; | |
425 | } | |
426 | ||
427 | /* | |
428 | * (non-Javadoc) | |
429 | * | |
430 | * @see org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager# | |
431 | * getExperimentTimeWindow() | |
432 | */ | |
433 | public TmfTimeRange getExperimentTimeWindow() { | |
434 | if (fExperiment != null) { | |
435 | return fExperiment.getTimeRange(); | |
436 | } | |
437 | return null; | |
438 | } | |
439 | ||
440 | /* | |
441 | * (non-Javadoc) | |
442 | * | |
443 | * @see | |
444 | * org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager#getExperimentName | |
445 | * () | |
446 | */ | |
447 | public String getExperimentName() { | |
448 | return fExperiment.getName(); | |
449 | } | |
450 | ||
451 | /* | |
452 | * (non-Javadoc) | |
453 | * | |
454 | * @see | |
455 | * org.eclipse.linuxtools.lttng.state.resource.ILttngStateContext#getTraceIdRef | |
456 | * () | |
457 | */ | |
458 | public ITmfTrace getTraceIdRef() { | |
459 | return fTrace; | |
460 | } | |
9aae0442 ASL |
461 | |
462 | /* | |
463 | * (non-Javadoc) | |
464 | * | |
465 | * @see | |
466 | * org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager#clearCheckPoints | |
467 | * () | |
468 | */ | |
469 | public void clearCheckPoints() { | |
470 | synchronized (checkPointsLock) { | |
471 | stateCheckpointsList.clear(); | |
472 | timestampCheckpointsList.clear(); | |
736aecd5 ASL |
473 | |
474 | fCheckPointStateModel = StateModelFactory.getStateEntryInstance(this); | |
9aae0442 ASL |
475 | try { |
476 | fCheckPointStateModel.init(this); | |
477 | } catch (LttngStateException e) { | |
478 | e.printStackTrace(); | |
479 | } | |
480 | } | |
481 | } | |
736aecd5 | 482 | |
9aae0442 ASL |
483 | /* |
484 | * (non-Javadoc) | |
485 | * | |
486 | * @see | |
487 | * org.eclipse.linuxtools.lttng.state.trace.IStateTraceManager#handleEvent | |
488 | * (org.eclipse.linuxtools.lttng.event.LttngSyntheticEvent, java.lang.Long) | |
489 | */ | |
490 | public void handleEvent(LttngSyntheticEvent synEvent, Long eventCount) { | |
491 | fStateUpdateProcessor.process(synEvent, fCheckPointStateModel); | |
736aecd5 | 492 | |
9aae0442 ASL |
493 | // Save checkpoint as needed |
494 | saveCheckPointIfNeeded(eventCount - 1, synEvent.getTimestamp()); | |
495 | } | |
736aecd5 | 496 | |
9aae0442 ASL |
497 | /* |
498 | * (non-Javadoc) | |
499 | * | |
500 | * @see java.lang.Object#toString() | |
501 | */ | |
502 | public String toString() { | |
503 | StringBuilder sb = new StringBuilder(super.toString()); | |
504 | sb.append("\n\tTotal number of processes in the Shared State model: " + fStateModel.getProcesses().length | |
505 | + "\n\t" + "Total number of processes in the Check point State model: " | |
506 | + fCheckPointStateModel.getProcesses().length); | |
736aecd5 | 507 | |
9aae0442 ASL |
508 | TmfTimeRange traceTRange = fTrace.getTimeRange(); |
509 | sb.append("\n\tTrace time interval for trace " + fTrace.getName() + "\n\t" | |
510 | + new LttngTimestamp(traceTRange.getStartTime())); | |
511 | sb.append(" - " + new LttngTimestamp(traceTRange.getEndTime())); | |
512 | sb.append("\n\tCheckPoints available at: "); | |
513 | for (TmfCheckpoint cpoint : timestampCheckpointsList) { | |
514 | sb.append("\n\t" + "Location: " + cpoint.getLocation() + " - " + cpoint.getTimestamp()); | |
515 | } | |
736aecd5 | 516 | |
9aae0442 ASL |
517 | return sb.toString(); |
518 | } | |
519 | ||
520 | } |