tmf.core: make tracer follow naming convention
[deliverable/tracecompass.git] / org.eclipse.tracecompass.statesystem.core / src / org / eclipse / tracecompass / internal / statesystem / core / StateSystem.java
... / ...
CommitLineData
1/*******************************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
5 *
6 * All rights reserved. This program and the accompanying materials are
7 * made available under the terms of the Eclipse Public License v1.0 which
8 * accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *
11 * Contributors:
12 * Alexandre Montplaisir - Initial API and implementation
13 * Patrick Tasse - Add message to exceptions
14 *******************************************************************************/
15
16package org.eclipse.tracecompass.internal.statesystem.core;
17
18import java.io.File;
19import java.io.IOException;
20import java.io.PrintWriter;
21import java.util.ArrayList;
22import java.util.LinkedList;
23import java.util.List;
24import java.util.concurrent.CountDownLatch;
25import java.util.concurrent.TimeUnit;
26
27import org.eclipse.jdt.annotation.NonNull;
28import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
29import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend;
30import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
31import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
32import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
33import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
34import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
35import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
36import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue.Type;
37import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
38
39/**
40 * This is the core class of the Generic State System. It contains all the
41 * methods to build and query a state history. It's exposed externally through
42 * the IStateSystemQuerier and IStateSystemBuilder interfaces, depending if the
43 * user needs read-only access or read-write access.
44 *
45 * When building, DON'T FORGET to call .closeHistory() when you are done
46 * inserting intervals, or the storage backend will have no way of knowing it
47 * can close and write itself to disk, and its thread will keep running.
48 *
49 * @author alexmont
50 *
51 */
52public class StateSystem implements ITmfStateSystemBuilder {
53
54 /* References to the inner structures */
55 private final AttributeTree attributeTree;
56 private final TransientState transState;
57 private final IStateHistoryBackend backend;
58
59 /* Latch tracking if the state history is done building or not */
60 private final CountDownLatch finishedLatch = new CountDownLatch(1);
61
62 private boolean buildCancelled = false;
63 private boolean isDisposed = false;
64
65 /**
66 * New-file constructor. For when you build a state system with a new file,
67 * or if the back-end does not require a file on disk.
68 *
69 * @param backend
70 * Back-end plugin to use
71 */
72 public StateSystem(@NonNull IStateHistoryBackend backend) {
73 this.backend = backend;
74 this.transState = new TransientState(backend);
75 this.attributeTree = new AttributeTree(this);
76 }
77
78 /**
79 * General constructor
80 *
81 * @param backend
82 * The "state history storage" back-end to use.
83 * @param newFile
84 * Put true if this is a new history started from scratch. It is
85 * used to tell the state system where to get its attribute tree.
86 * @throws IOException
87 * If there was a problem creating the new history file
88 */
89 public StateSystem(@NonNull IStateHistoryBackend backend, boolean newFile)
90 throws IOException {
91 this.backend = backend;
92 this.transState = new TransientState(backend);
93
94 if (newFile) {
95 attributeTree = new AttributeTree(this);
96 } else {
97 /* We're opening an existing file */
98 this.attributeTree = new AttributeTree(this, backend.supplyAttributeTreeReader());
99 transState.setInactive();
100 finishedLatch.countDown(); /* The history is already built */
101 }
102 }
103
104 @Override
105 public String getSSID() {
106 return backend.getSSID();
107 }
108
109 @Override
110 public boolean isCancelled() {
111 return buildCancelled;
112 }
113
114 @Override
115 public void waitUntilBuilt() {
116 try {
117 finishedLatch.await();
118 } catch (InterruptedException e) {
119 e.printStackTrace();
120 }
121 }
122
123 @Override
124 public boolean waitUntilBuilt(long timeout) {
125 boolean ret = false;
126 try {
127 ret = finishedLatch.await(timeout, TimeUnit.MILLISECONDS);
128 } catch (InterruptedException e) {
129 e.printStackTrace();
130 }
131 return ret;
132 }
133
134 @Override
135 public synchronized void dispose() {
136 isDisposed = true;
137 if (transState.isActive()) {
138 transState.setInactive();
139 buildCancelled = true;
140 }
141 backend.dispose();
142 }
143
144 //--------------------------------------------------------------------------
145 // General methods related to the attribute tree
146 //--------------------------------------------------------------------------
147
148 /**
149 * Get the attribute tree associated with this state system. This should be
150 * the only way of accessing it (and if subclasses want to point to a
151 * different attribute tree than their own, they should only need to
152 * override this).
153 *
154 * @return The attribute tree
155 */
156 public AttributeTree getAttributeTree() {
157 return attributeTree;
158 }
159
160 /**
161 * Method used by the attribute tree when creating new attributes, to keep
162 * the attribute count in the transient state in sync.
163 */
164 public void addEmptyAttribute() {
165 transState.addEmptyEntry();
166 }
167
168 @Override
169 public int getNbAttributes() {
170 return getAttributeTree().getNbAttributes();
171 }
172
173 @Override
174 public String getAttributeName(int attributeQuark) {
175 return getAttributeTree().getAttributeName(attributeQuark);
176 }
177
178 @Override
179 public String getFullAttributePath(int attributeQuark) {
180 return getAttributeTree().getFullAttributeName(attributeQuark);
181 }
182
183 //--------------------------------------------------------------------------
184 // Methods related to the storage backend
185 //--------------------------------------------------------------------------
186
187 @Override
188 public long getStartTime() {
189 return backend.getStartTime();
190 }
191
192 @Override
193 public long getCurrentEndTime() {
194 return backend.getEndTime();
195 }
196
197 @Override
198 public void closeHistory(long endTime) throws TimeRangeException {
199 File attributeTreeFile;
200 long attributeTreeFilePos;
201 long realEndTime = endTime;
202
203 if (realEndTime < backend.getEndTime()) {
204 /*
205 * This can happen (empty nodes pushing the border further, etc.)
206 * but shouldn't be too big of a deal.
207 */
208 realEndTime = backend.getEndTime();
209 }
210 transState.closeTransientState(realEndTime);
211 backend.finishedBuilding(realEndTime);
212
213 attributeTreeFile = backend.supplyAttributeTreeWriterFile();
214 attributeTreeFilePos = backend.supplyAttributeTreeWriterFilePosition();
215 if (attributeTreeFile != null) {
216 /*
217 * If null was returned, we simply won't save the attribute tree,
218 * too bad!
219 */
220 getAttributeTree().writeSelf(attributeTreeFile, attributeTreeFilePos);
221 }
222 finishedLatch.countDown(); /* Mark the history as finished building */
223 }
224
225 //--------------------------------------------------------------------------
226 // Quark-retrieving methods
227 //--------------------------------------------------------------------------
228
229 @Override
230 public int getQuarkAbsolute(String... attribute)
231 throws AttributeNotFoundException {
232 return getAttributeTree().getQuarkDontAdd(-1, attribute);
233 }
234
235 @Override
236 public int getQuarkAbsoluteAndAdd(String... attribute) {
237 return getAttributeTree().getQuarkAndAdd(-1, attribute);
238 }
239
240 @Override
241 public int getQuarkRelative(int startingNodeQuark, String... subPath)
242 throws AttributeNotFoundException {
243 return getAttributeTree().getQuarkDontAdd(startingNodeQuark, subPath);
244 }
245
246 @Override
247 public int getQuarkRelativeAndAdd(int startingNodeQuark, String... subPath) {
248 return getAttributeTree().getQuarkAndAdd(startingNodeQuark, subPath);
249 }
250
251 @Override
252 public List<Integer> getSubAttributes(int quark, boolean recursive)
253 throws AttributeNotFoundException {
254 return getAttributeTree().getSubAttributes(quark, recursive);
255 }
256
257 @Override
258 public List<Integer> getSubAttributes(int quark, boolean recursive, String pattern)
259 throws AttributeNotFoundException {
260 List<Integer> all = getSubAttributes(quark, recursive);
261 List<Integer> ret = new LinkedList<>();
262 for (Integer attQuark : all) {
263 String name = getAttributeName(attQuark.intValue());
264 if (name.matches(pattern)) {
265 ret.add(attQuark);
266 }
267 }
268 return ret;
269 }
270
271 @Override
272 public int getParentAttributeQuark(int quark) {
273 return getAttributeTree().getParentAttributeQuark(quark);
274 }
275
276 @Override
277 public List<Integer> getQuarks(String... pattern) {
278 List<Integer> quarks = new LinkedList<>();
279 List<String> prefix = new LinkedList<>();
280 List<String> suffix = new LinkedList<>();
281 boolean split = false;
282 String[] prefixStr;
283 String[] suffixStr;
284 List<Integer> directChildren;
285 int startingAttribute;
286
287 /* Fill the "prefix" and "suffix" parts of the pattern around the '*' */
288 for (String entry : pattern) {
289 if (entry.equals("*")) { //$NON-NLS-1$
290 if (split) {
291 /*
292 * Split was already true? This means there was more than
293 * one wildcard. This is not supported, return an empty
294 * list.
295 */
296 return quarks;
297 }
298 split = true;
299 continue;
300 }
301
302 if (split) {
303 suffix.add(entry);
304 } else {
305 prefix.add(entry);
306 }
307 }
308 prefixStr = prefix.toArray(new String[prefix.size()]);
309 suffixStr = suffix.toArray(new String[suffix.size()]);
310
311 /*
312 * If there was no wildcard, we'll only return the one matching
313 * attribute, if there is one.
314 */
315 if (!split) {
316 int quark;
317 try {
318 quark = getQuarkAbsolute(prefixStr);
319 } catch (AttributeNotFoundException e) {
320 /* It's fine, we'll just return the empty List */
321 return quarks;
322 }
323 quarks.add(quark);
324 return quarks;
325 }
326
327 try {
328 if (prefix.size() == 0) {
329 /*
330 * If 'prefix' is empty, this means the wildcard was the first
331 * element. Look for the root node's sub-attributes.
332 */
333 startingAttribute = -1;
334 } else {
335 startingAttribute = getQuarkAbsolute(prefixStr);
336 }
337 directChildren = getSubAttributes(startingAttribute, false);
338 } catch (AttributeNotFoundException e) {
339 /* That attribute path did not exist, return the empty array */
340 return quarks;
341 }
342
343 /*
344 * Iterate of all the sub-attributes, and only keep those who match the
345 * 'suffix' part of the initial pattern.
346 */
347 for (int childQuark : directChildren) {
348 int matchingQuark;
349 try {
350 matchingQuark = getQuarkRelative(childQuark, suffixStr);
351 } catch (AttributeNotFoundException e) {
352 continue;
353 }
354 quarks.add(matchingQuark);
355 }
356
357 return quarks;
358 }
359
360 //--------------------------------------------------------------------------
361 // Methods related to insertions in the history
362 //--------------------------------------------------------------------------
363
364 @Override
365 public void modifyAttribute(long t, ITmfStateValue value, int attributeQuark)
366 throws TimeRangeException, AttributeNotFoundException,
367 StateValueTypeException {
368 if (value == null) {
369 /*
370 * TODO Replace with @NonNull parameter (will require fixing all the
371 * state providers!)
372 */
373 throw new IllegalArgumentException();
374 }
375 transState.processStateChange(t, value, attributeQuark);
376 }
377
378 @Override
379 public void incrementAttribute(long t, int attributeQuark)
380 throws StateValueTypeException, TimeRangeException,
381 AttributeNotFoundException {
382 ITmfStateValue stateValue = queryOngoingState(attributeQuark);
383 int prevValue = 0;
384 /* if the attribute was previously null, start counting at 0 */
385 if (!stateValue.isNull()) {
386 prevValue = stateValue.unboxInt();
387 }
388 modifyAttribute(t, TmfStateValue.newValueInt(prevValue + 1),
389 attributeQuark);
390 }
391
392 @Override
393 public void pushAttribute(long t, ITmfStateValue value, int attributeQuark)
394 throws TimeRangeException, AttributeNotFoundException,
395 StateValueTypeException {
396 int stackDepth;
397 int subAttributeQuark;
398 ITmfStateValue previousSV = transState.getOngoingStateValue(attributeQuark);
399
400 if (previousSV.isNull()) {
401 /*
402 * If the StateValue was null, this means this is the first time we
403 * use this attribute. Leave stackDepth at 0.
404 */
405 stackDepth = 0;
406 } else if (previousSV.getType() == Type.INTEGER) {
407 /* Previous value was an integer, all is good, use it */
408 stackDepth = previousSV.unboxInt();
409 } else {
410 /* Previous state of this attribute was another type? Not good! */
411 throw new StateValueTypeException(getSSID() + " Quark:" + attributeQuark + ", Type:" + previousSV.getType() + ", Expected:" + Type.INTEGER); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
412 }
413
414 if (stackDepth >= 100000) {
415 /*
416 * Limit stackDepth to 100000, to avoid having Attribute Trees grow
417 * out of control due to buggy insertions
418 */
419 String message = " Stack limit reached, not pushing"; //$NON-NLS-1$
420 throw new AttributeNotFoundException(getSSID() + " Quark:" + attributeQuark + message); //$NON-NLS-1$
421 }
422
423 stackDepth++;
424 subAttributeQuark = getQuarkRelativeAndAdd(attributeQuark, String.valueOf(stackDepth));
425
426 modifyAttribute(t, TmfStateValue.newValueInt(stackDepth), attributeQuark);
427 modifyAttribute(t, value, subAttributeQuark);
428 }
429
430 @Override
431 public ITmfStateValue popAttribute(long t, int attributeQuark)
432 throws AttributeNotFoundException, TimeRangeException,
433 StateValueTypeException {
434 /* These are the state values of the stack-attribute itself */
435 ITmfStateValue previousSV = transState.getOngoingStateValue(attributeQuark);
436
437 if (previousSV.isNull()) {
438 /*
439 * Trying to pop an empty stack. This often happens at the start of
440 * traces, for example when we see a syscall_exit, without having
441 * the corresponding syscall_entry in the trace. Just ignore
442 * silently.
443 */
444 return null;
445 }
446 if (previousSV.getType() != Type.INTEGER) {
447 /*
448 * The existing value was not an integer (which is expected for
449 * stack tops), this doesn't look like a valid stack attribute.
450 */
451 throw new StateValueTypeException(getSSID() + " Quark:" + attributeQuark + ", Type:" + previousSV.getType() + ", Expected:" + Type.INTEGER); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
452 }
453
454 int stackDepth = previousSV.unboxInt();
455
456 if (stackDepth <= 0) {
457 /* This on the other hand should not happen... */
458 throw new StateValueTypeException(getSSID() + " Quark:" + attributeQuark + ", Stack depth:" + stackDepth); //$NON-NLS-1$//$NON-NLS-2$
459 }
460
461 /* The attribute should already exist at this point */
462 int subAttributeQuark = getQuarkRelative(attributeQuark, String.valueOf(stackDepth));
463 ITmfStateValue poppedValue = queryOngoingState(subAttributeQuark);
464
465 /* Update the state value of the stack-attribute */
466 ITmfStateValue nextSV;
467 if (--stackDepth == 0) {
468 /* Store a null state value */
469 nextSV = TmfStateValue.nullValue();
470 } else {
471 nextSV = TmfStateValue.newValueInt(stackDepth);
472 }
473 modifyAttribute(t, nextSV, attributeQuark);
474
475 /* Delete the sub-attribute that contained the user's state value */
476 removeAttribute(t, subAttributeQuark);
477
478 return poppedValue;
479 }
480
481 @Override
482 public void removeAttribute(long t, int attributeQuark)
483 throws TimeRangeException, AttributeNotFoundException {
484 if (attributeQuark < 0) {
485 throw new IllegalArgumentException();
486 }
487
488 /*
489 * Nullify our children first, recursively. We pass 'false' because we
490 * handle the recursion ourselves.
491 */
492 List<Integer> childAttributes = getSubAttributes(attributeQuark, false);
493 for (int childNodeQuark : childAttributes) {
494 if (attributeQuark == childNodeQuark) {
495 /* Something went very wrong when building out attribute tree */
496 throw new IllegalStateException();
497 }
498 removeAttribute(t, childNodeQuark);
499 }
500 /* Nullify ourselves */
501 try {
502 transState.processStateChange(t, TmfStateValue.nullValue(), attributeQuark);
503 } catch (StateValueTypeException e) {
504 /*
505 * Will not happen since we're inserting null values only, but poor
506 * compiler has no way of knowing this...
507 */
508 throw new IllegalStateException(e);
509 }
510 }
511
512 //--------------------------------------------------------------------------
513 // "Current" query/update methods
514 //--------------------------------------------------------------------------
515
516 @Override
517 public ITmfStateValue queryOngoingState(int attributeQuark)
518 throws AttributeNotFoundException {
519 return transState.getOngoingStateValue(attributeQuark);
520 }
521
522 @Override
523 public long getOngoingStartTime(int attribute)
524 throws AttributeNotFoundException {
525 return transState.getOngoingStartTime(attribute);
526 }
527
528 @Override
529 public void updateOngoingState(ITmfStateValue newValue, int attributeQuark)
530 throws AttributeNotFoundException {
531 transState.changeOngoingStateValue(attributeQuark, newValue);
532 }
533
534 /**
535 * Modify the whole "ongoing state" (state values + start times). This can
536 * be used when "seeking" a state system to a different point in the trace
537 * (and restoring the known stateInfo at this location). Use with care!
538 *
539 * @param newStateIntervals
540 * The new List of state values to use as ongoing state info
541 */
542 protected void replaceOngoingState(@NonNull List<ITmfStateInterval> newStateIntervals) {
543 transState.replaceOngoingState(newStateIntervals);
544 }
545
546 //--------------------------------------------------------------------------
547 // Regular query methods (sent to the back-end)
548 //--------------------------------------------------------------------------
549
550 @Override
551 public synchronized List<ITmfStateInterval> queryFullState(long t)
552 throws TimeRangeException, StateSystemDisposedException {
553 if (isDisposed) {
554 throw new StateSystemDisposedException();
555 }
556
557 final int nbAttr = getNbAttributes();
558 List<ITmfStateInterval> stateInfo = new ArrayList<>(nbAttr);
559
560 /* Bring the size of the array to the current number of attributes */
561 for (int i = 0; i < nbAttr; i++) {
562 stateInfo.add(null);
563 }
564
565 /*
566 * If we are currently building the history, also query the "ongoing"
567 * states for stuff that might not yet be written to the history.
568 */
569 if (transState.isActive()) {
570 transState.doQuery(stateInfo, t);
571 }
572
573 /* Query the storage backend */
574 backend.doQuery(stateInfo, t);
575
576 /*
577 * We should have previously inserted an interval for every attribute.
578 */
579 for (ITmfStateInterval interval : stateInfo) {
580 if (interval == null) {
581 throw new IllegalStateException("Incoherent interval storage"); //$NON-NLS-1$
582 }
583 }
584 return stateInfo;
585 }
586
587 @Override
588 public ITmfStateInterval querySingleState(long t, int attributeQuark)
589 throws AttributeNotFoundException, TimeRangeException,
590 StateSystemDisposedException {
591 if (isDisposed) {
592 throw new StateSystemDisposedException();
593 }
594
595 ITmfStateInterval ret = transState.getIntervalAt(t, attributeQuark);
596 if (ret == null) {
597 /*
598 * The transient state did not have the information, let's look into
599 * the backend next.
600 */
601 ret = backend.doSingularQuery(t, attributeQuark);
602 }
603
604 if (ret == null) {
605 /*
606 * If we did our job correctly, there should be intervals for every
607 * possible attribute, over all the valid time range.
608 */
609 throw new IllegalStateException("Incoherent interval storage"); //$NON-NLS-1$
610 }
611 return ret;
612 }
613
614 //--------------------------------------------------------------------------
615 // Debug methods
616 //--------------------------------------------------------------------------
617
618 static void logMissingInterval(int attribute, long timestamp) {
619 Activator.getDefault().logInfo("No data found in history for attribute " + //$NON-NLS-1$
620 attribute + " at time " + timestamp + //$NON-NLS-1$
621 ", returning dummy interval"); //$NON-NLS-1$
622 }
623
624 /**
625 * Print out the contents of the inner structures.
626 *
627 * @param writer
628 * The PrintWriter in which to print the output
629 */
630 public void debugPrint(@NonNull PrintWriter writer) {
631 getAttributeTree().debugPrint(writer);
632 transState.debugPrint(writer);
633 backend.debugPrint(writer);
634 }
635
636}
This page took 0.031545 seconds and 5 git commands to generate.