Commit | Line | Data |
---|---|---|
a52fde77 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2012, 2014 Ericsson |
a52fde77 AM |
3 | * Copyright (c) 2010, 2011 École Polytechnique de Montréal |
4 | * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com> | |
5df842b3 | 5 | * |
a52fde77 AM |
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 | |
5df842b3 | 10 | * |
a52fde77 AM |
11 | *******************************************************************************/ |
12 | ||
18ab1d18 | 13 | package org.eclipse.linuxtools.internal.tmf.core.statesystem; |
a52fde77 | 14 | |
8d1346f0 AM |
15 | import java.io.File; |
16 | import java.io.IOException; | |
a52fde77 | 17 | import java.io.PrintWriter; |
8d1346f0 | 18 | import java.util.ArrayList; |
f94a0bac | 19 | import java.util.LinkedList; |
a52fde77 | 20 | import java.util.List; |
16576a7e | 21 | import java.util.concurrent.CountDownLatch; |
9287b6a2 | 22 | import java.util.concurrent.TimeUnit; |
a52fde77 | 23 | |
8d1346f0 AM |
24 | import org.eclipse.core.runtime.IProgressMonitor; |
25 | import org.eclipse.core.runtime.NullProgressMonitor; | |
2e21b6d8 | 26 | import org.eclipse.jdt.annotation.NonNull; |
5500a7f0 | 27 | import org.eclipse.linuxtools.internal.tmf.core.Activator; |
f9a76cac | 28 | import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.IStateHistoryBackend; |
6d08acca | 29 | import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException; |
96345c5a | 30 | import org.eclipse.linuxtools.tmf.core.exceptions.StateSystemDisposedException; |
6d08acca AM |
31 | import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException; |
32 | import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException; | |
8d1346f0 AM |
33 | import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval; |
34 | import org.eclipse.linuxtools.tmf.core.interval.TmfStateInterval; | |
f1f86dfb | 35 | import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystemBuilder; |
a52fde77 | 36 | import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue; |
b67a2540 | 37 | import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue.Type; |
359eeba0 | 38 | import org.eclipse.linuxtools.tmf.core.statevalue.TmfStateValue; |
a52fde77 AM |
39 | |
40 | /** | |
8d1346f0 AM |
41 | * This is the core class of the Generic State System. It contains all the |
42 | * methods to build and query a state history. It's exposed externally through | |
43 | * the IStateSystemQuerier and IStateSystemBuilder interfaces, depending if the | |
44 | * user needs read-only access or read-write access. | |
5df842b3 | 45 | * |
8d1346f0 AM |
46 | * When building, DON'T FORGET to call .closeHistory() when you are done |
47 | * inserting intervals, or the storage backend will have no way of knowing it | |
48 | * can close and write itself to disk, and its thread will keep running. | |
5df842b3 | 49 | * |
a52fde77 | 50 | * @author alexmont |
5df842b3 | 51 | * |
a52fde77 | 52 | */ |
f1f86dfb | 53 | public class StateSystem implements ITmfStateSystemBuilder { |
a52fde77 | 54 | |
84a9548a AM |
55 | private final String ssid; |
56 | ||
a52fde77 | 57 | /* References to the inner structures */ |
8d1346f0 AM |
58 | private final AttributeTree attributeTree; |
59 | private final TransientState transState; | |
60 | private final IStateHistoryBackend backend; | |
a52fde77 | 61 | |
16576a7e AM |
62 | /* Latch tracking if the state history is done building or not */ |
63 | private final CountDownLatch finishedLatch = new CountDownLatch(1); | |
64 | ||
1a4205d9 | 65 | private boolean buildCancelled = false; |
96345c5a | 66 | private boolean isDisposed = false; |
1a4205d9 | 67 | |
f9a76cac AM |
68 | /** |
69 | * New-file constructor. For when you build a state system with a new file, | |
70 | * or if the back-end does not require a file on disk. | |
71 | * | |
84a9548a AM |
72 | * @param ssid |
73 | * The ID of this statesystem. It should be unique. | |
f9a76cac AM |
74 | * @param backend |
75 | * Back-end plugin to use | |
76 | */ | |
84a9548a AM |
77 | public StateSystem(@NonNull String ssid, @NonNull IStateHistoryBackend backend) { |
78 | this.ssid = ssid; | |
f9a76cac AM |
79 | this.backend = backend; |
80 | this.transState = new TransientState(backend); | |
81 | this.attributeTree = new AttributeTree(this); | |
82 | } | |
83 | ||
a52fde77 | 84 | /** |
8d1346f0 AM |
85 | * General constructor |
86 | * | |
84a9548a AM |
87 | * @param ssid |
88 | * The ID of this statesystem. It should be unique. | |
8d1346f0 | 89 | * @param backend |
f9a76cac | 90 | * The "state history storage" back-end to use. |
8d1346f0 AM |
91 | * @param newFile |
92 | * Put true if this is a new history started from scratch. It is | |
93 | * used to tell the state system where to get its attribute tree. | |
94 | * @throws IOException | |
95 | * If there was a problem creating the new history file | |
a52fde77 | 96 | */ |
84a9548a | 97 | public StateSystem(@NonNull String ssid, @NonNull IStateHistoryBackend backend, boolean newFile) |
8d1346f0 | 98 | throws IOException { |
84a9548a | 99 | this.ssid = ssid; |
8d1346f0 AM |
100 | this.backend = backend; |
101 | this.transState = new TransientState(backend); | |
a52fde77 | 102 | |
8d1346f0 AM |
103 | if (newFile) { |
104 | attributeTree = new AttributeTree(this); | |
105 | } else { | |
106 | /* We're opening an existing file */ | |
107 | this.attributeTree = new AttributeTree(this, backend.supplyAttributeTreeReader()); | |
108 | transState.setInactive(); | |
16576a7e AM |
109 | finishedLatch.countDown(); /* The history is already built */ |
110 | } | |
111 | } | |
112 | ||
84a9548a AM |
113 | @Override |
114 | public String getSSID() { | |
115 | return ssid; | |
116 | } | |
117 | ||
16576a7e | 118 | @Override |
2002c638 AM |
119 | public boolean isCancelled() { |
120 | return buildCancelled; | |
121 | } | |
122 | ||
123 | @Override | |
124 | public void waitUntilBuilt() { | |
16576a7e AM |
125 | try { |
126 | finishedLatch.await(); | |
127 | } catch (InterruptedException e) { | |
128 | e.printStackTrace(); | |
8d1346f0 | 129 | } |
1a4205d9 AM |
130 | } |
131 | ||
9287b6a2 AM |
132 | @Override |
133 | public boolean waitUntilBuilt(long timeout) { | |
134 | boolean ret = false; | |
135 | try { | |
136 | ret = finishedLatch.await(timeout, TimeUnit.MILLISECONDS); | |
137 | } catch (InterruptedException e) { | |
138 | e.printStackTrace(); | |
139 | } | |
140 | return ret; | |
141 | } | |
142 | ||
1a4205d9 AM |
143 | @Override |
144 | public synchronized void dispose() { | |
96345c5a | 145 | isDisposed = true; |
1a4205d9 AM |
146 | if (transState.isActive()) { |
147 | transState.setInactive(); | |
148 | buildCancelled = true; | |
149 | } | |
150 | backend.dispose(); | |
a52fde77 AM |
151 | } |
152 | ||
8d1346f0 AM |
153 | //-------------------------------------------------------------------------- |
154 | // General methods related to the attribute tree | |
155 | //-------------------------------------------------------------------------- | |
156 | ||
339d27b4 AM |
157 | /** |
158 | * Get the attribute tree associated with this state system. This should be | |
159 | * the only way of accessing it (and if subclasses want to point to a | |
160 | * different attribute tree than their own, they should only need to | |
161 | * override this). | |
162 | * | |
163 | * @return The attribute tree | |
164 | */ | |
165 | public AttributeTree getAttributeTree() { | |
166 | return attributeTree; | |
167 | } | |
168 | ||
8d1346f0 AM |
169 | /** |
170 | * Method used by the attribute tree when creating new attributes, to keep | |
171 | * the attribute count in the transient state in sync. | |
172 | */ | |
339d27b4 | 173 | protected void addEmptyAttribute() { |
8d1346f0 AM |
174 | transState.addEmptyEntry(); |
175 | } | |
176 | ||
177 | @Override | |
4623f57f | 178 | public int getNbAttributes() { |
339d27b4 | 179 | return getAttributeTree().getNbAttributes(); |
4623f57f AM |
180 | } |
181 | ||
8d1346f0 AM |
182 | @Override |
183 | public String getAttributeName(int attributeQuark) { | |
339d27b4 | 184 | return getAttributeTree().getAttributeName(attributeQuark); |
8d1346f0 AM |
185 | } |
186 | ||
187 | @Override | |
188 | public String getFullAttributePath(int attributeQuark) { | |
339d27b4 | 189 | return getAttributeTree().getFullAttributeName(attributeQuark); |
8d1346f0 AM |
190 | } |
191 | ||
192 | //-------------------------------------------------------------------------- | |
193 | // Methods related to the storage backend | |
194 | //-------------------------------------------------------------------------- | |
a52fde77 | 195 | |
8d1346f0 AM |
196 | @Override |
197 | public long getStartTime() { | |
198 | return backend.getStartTime(); | |
199 | } | |
200 | ||
201 | @Override | |
202 | public long getCurrentEndTime() { | |
203 | return backend.getEndTime(); | |
204 | } | |
205 | ||
206 | @Override | |
207 | public void closeHistory(long endTime) throws TimeRangeException { | |
208 | File attributeTreeFile; | |
209 | long attributeTreeFilePos; | |
210 | long realEndTime = endTime; | |
211 | ||
212 | if (realEndTime < backend.getEndTime()) { | |
213 | /* | |
214 | * This can happen (empty nodes pushing the border further, etc.) | |
215 | * but shouldn't be too big of a deal. | |
216 | */ | |
217 | realEndTime = backend.getEndTime(); | |
218 | } | |
219 | transState.closeTransientState(realEndTime); | |
220 | backend.finishedBuilding(realEndTime); | |
221 | ||
222 | attributeTreeFile = backend.supplyAttributeTreeWriterFile(); | |
223 | attributeTreeFilePos = backend.supplyAttributeTreeWriterFilePosition(); | |
224 | if (attributeTreeFile != null) { | |
225 | /* | |
226 | * If null was returned, we simply won't save the attribute tree, | |
227 | * too bad! | |
228 | */ | |
339d27b4 | 229 | getAttributeTree().writeSelf(attributeTreeFile, attributeTreeFilePos); |
8d1346f0 | 230 | } |
16576a7e | 231 | finishedLatch.countDown(); /* Mark the history as finished building */ |
8d1346f0 AM |
232 | } |
233 | ||
234 | //-------------------------------------------------------------------------- | |
235 | // Quark-retrieving methods | |
236 | //-------------------------------------------------------------------------- | |
237 | ||
238 | @Override | |
a52fde77 AM |
239 | public int getQuarkAbsolute(String... attribute) |
240 | throws AttributeNotFoundException { | |
339d27b4 | 241 | return getAttributeTree().getQuarkDontAdd(-1, attribute); |
a52fde77 AM |
242 | } |
243 | ||
8d1346f0 | 244 | @Override |
a52fde77 | 245 | public int getQuarkAbsoluteAndAdd(String... attribute) { |
339d27b4 | 246 | return getAttributeTree().getQuarkAndAdd(-1, attribute); |
a52fde77 AM |
247 | } |
248 | ||
8d1346f0 | 249 | @Override |
a52fde77 AM |
250 | public int getQuarkRelative(int startingNodeQuark, String... subPath) |
251 | throws AttributeNotFoundException { | |
339d27b4 | 252 | return getAttributeTree().getQuarkDontAdd(startingNodeQuark, subPath); |
a52fde77 AM |
253 | } |
254 | ||
8d1346f0 | 255 | @Override |
a52fde77 | 256 | public int getQuarkRelativeAndAdd(int startingNodeQuark, String... subPath) { |
339d27b4 | 257 | return getAttributeTree().getQuarkAndAdd(startingNodeQuark, subPath); |
a52fde77 AM |
258 | } |
259 | ||
8d1346f0 | 260 | @Override |
c66426fd | 261 | public List<Integer> getSubAttributes(int quark, boolean recursive) |
0a9de3d2 | 262 | throws AttributeNotFoundException { |
339d27b4 | 263 | return getAttributeTree().getSubAttributes(quark, recursive); |
0a9de3d2 AM |
264 | } |
265 | ||
5206c858 AM |
266 | @Override |
267 | public List<Integer> getSubAttributes(int quark, boolean recursive, String pattern) | |
268 | throws AttributeNotFoundException { | |
269 | List<Integer> all = getSubAttributes(quark, recursive); | |
270 | List<Integer> ret = new LinkedList<>(); | |
271 | for (Integer attQuark : all) { | |
272 | String name = getAttributeName(attQuark.intValue()); | |
273 | if (name.matches(pattern)) { | |
274 | ret.add(attQuark); | |
275 | } | |
276 | } | |
277 | return ret; | |
278 | } | |
279 | ||
0fdd2c45 FG |
280 | @Override |
281 | public int getParentAttributeQuark(int quark) { | |
282 | return getAttributeTree().getParentAttributeQuark(quark); | |
283 | } | |
284 | ||
8d1346f0 | 285 | @Override |
f94a0bac | 286 | public List<Integer> getQuarks(String... pattern) { |
a4524c1b AM |
287 | List<Integer> quarks = new LinkedList<>(); |
288 | List<String> prefix = new LinkedList<>(); | |
289 | List<String> suffix = new LinkedList<>(); | |
f94a0bac AM |
290 | boolean split = false; |
291 | String[] prefixStr; | |
292 | String[] suffixStr; | |
293 | List<Integer> directChildren; | |
294 | int startingAttribute; | |
295 | ||
296 | /* Fill the "prefix" and "suffix" parts of the pattern around the '*' */ | |
297 | for (String entry : pattern) { | |
298 | if (entry.equals("*")) { //$NON-NLS-1$ | |
299 | if (split) { | |
300 | /* | |
301 | * Split was already true? This means there was more than | |
302 | * one wildcard. This is not supported, return an empty | |
303 | * list. | |
304 | */ | |
305 | return quarks; | |
306 | } | |
307 | split = true; | |
308 | continue; | |
309 | } | |
310 | ||
311 | if (split) { | |
312 | suffix.add(entry); | |
313 | } else { | |
314 | prefix.add(entry); | |
315 | } | |
316 | } | |
317 | prefixStr = prefix.toArray(new String[prefix.size()]); | |
318 | suffixStr = suffix.toArray(new String[suffix.size()]); | |
319 | ||
320 | /* | |
321 | * If there was no wildcard, we'll only return the one matching | |
322 | * attribute, if there is one. | |
323 | */ | |
cb42195c | 324 | if (!split) { |
f94a0bac AM |
325 | int quark; |
326 | try { | |
327 | quark = getQuarkAbsolute(prefixStr); | |
328 | } catch (AttributeNotFoundException e) { | |
329 | /* It's fine, we'll just return the empty List */ | |
330 | return quarks; | |
331 | } | |
332 | quarks.add(quark); | |
333 | return quarks; | |
334 | } | |
335 | ||
336 | try { | |
337 | if (prefix.size() == 0) { | |
338 | /* | |
339 | * If 'prefix' is empty, this means the wildcard was the first | |
340 | * element. Look for the root node's sub-attributes. | |
341 | */ | |
342 | startingAttribute = -1; | |
343 | } else { | |
344 | startingAttribute = getQuarkAbsolute(prefixStr); | |
345 | } | |
339d27b4 | 346 | directChildren = getSubAttributes(startingAttribute, false); |
f94a0bac AM |
347 | } catch (AttributeNotFoundException e) { |
348 | /* That attribute path did not exist, return the empty array */ | |
349 | return quarks; | |
350 | } | |
351 | ||
352 | /* | |
353 | * Iterate of all the sub-attributes, and only keep those who match the | |
354 | * 'suffix' part of the initial pattern. | |
355 | */ | |
356 | for (int childQuark : directChildren) { | |
357 | int matchingQuark; | |
358 | try { | |
359 | matchingQuark = getQuarkRelative(childQuark, suffixStr); | |
360 | } catch (AttributeNotFoundException e) { | |
361 | continue; | |
362 | } | |
363 | quarks.add(matchingQuark); | |
364 | } | |
365 | ||
366 | return quarks; | |
367 | } | |
368 | ||
8d1346f0 AM |
369 | //-------------------------------------------------------------------------- |
370 | // Methods related to insertions in the history | |
371 | //-------------------------------------------------------------------------- | |
a52fde77 | 372 | |
8d1346f0 | 373 | @Override |
a52fde77 | 374 | public void modifyAttribute(long t, ITmfStateValue value, int attributeQuark) |
7e0b2b56 AM |
375 | throws TimeRangeException, AttributeNotFoundException, |
376 | StateValueTypeException { | |
a52fde77 AM |
377 | transState.processStateChange(t, value, attributeQuark); |
378 | } | |
379 | ||
8d1346f0 | 380 | @Override |
a52fde77 AM |
381 | public void incrementAttribute(long t, int attributeQuark) |
382 | throws StateValueTypeException, TimeRangeException, | |
383 | AttributeNotFoundException { | |
359eeba0 PT |
384 | ITmfStateValue stateValue = queryOngoingState(attributeQuark); |
385 | int prevValue = 0; | |
386 | /* if the attribute was previously null, start counting at 0 */ | |
387 | if (!stateValue.isNull()) { | |
388 | prevValue = stateValue.unboxInt(); | |
280bbdbb | 389 | } |
a52fde77 AM |
390 | modifyAttribute(t, TmfStateValue.newValueInt(prevValue + 1), |
391 | attributeQuark); | |
392 | } | |
393 | ||
8d1346f0 | 394 | @Override |
a52fde77 AM |
395 | public void pushAttribute(long t, ITmfStateValue value, int attributeQuark) |
396 | throws TimeRangeException, AttributeNotFoundException, | |
397 | StateValueTypeException { | |
0126a8ca | 398 | int stackDepth; |
a52fde77 AM |
399 | int subAttributeQuark; |
400 | ITmfStateValue previousSV = transState.getOngoingStateValue(attributeQuark); | |
401 | ||
402 | if (previousSV.isNull()) { | |
403 | /* | |
404 | * If the StateValue was null, this means this is the first time we | |
405 | * use this attribute. Leave stackDepth at 0. | |
406 | */ | |
cb42195c | 407 | stackDepth = 0; |
b67a2540 | 408 | } else if (previousSV.getType() == Type.INTEGER) { |
a52fde77 AM |
409 | /* Previous value was an integer, all is good, use it */ |
410 | stackDepth = previousSV.unboxInt(); | |
a52fde77 AM |
411 | } else { |
412 | /* Previous state of this attribute was another type? Not good! */ | |
90a25ebe AM |
413 | throw new StateValueTypeException(); |
414 | } | |
415 | ||
e8251298 | 416 | if (stackDepth >= 100000) { |
90a25ebe | 417 | /* |
a0f8fb9b FW |
418 | * Limit stackDepth to 100000, to avoid having Attribute Trees grow |
419 | * out of control due to buggy insertions | |
90a25ebe AM |
420 | */ |
421 | String message = "Stack limit reached, not pushing"; //$NON-NLS-1$ | |
422 | throw new AttributeNotFoundException(message); | |
a52fde77 AM |
423 | } |
424 | ||
425 | stackDepth++; | |
0126a8ca | 426 | subAttributeQuark = getQuarkRelativeAndAdd(attributeQuark, String.valueOf(stackDepth)); |
a52fde77 | 427 | |
5896eb76 | 428 | modifyAttribute(t, TmfStateValue.newValueInt(stackDepth), attributeQuark); |
90a25ebe | 429 | modifyAttribute(t, value, subAttributeQuark); |
a52fde77 AM |
430 | } |
431 | ||
8d1346f0 | 432 | @Override |
5896eb76 | 433 | public ITmfStateValue popAttribute(long t, int attributeQuark) |
a52fde77 AM |
434 | throws AttributeNotFoundException, TimeRangeException, |
435 | StateValueTypeException { | |
e2eac108 | 436 | /* These are the state values of the stack-attribute itself */ |
5896eb76 | 437 | ITmfStateValue previousSV = queryOngoingState(attributeQuark); |
a52fde77 AM |
438 | |
439 | if (previousSV.isNull()) { | |
e2eac108 AM |
440 | /* |
441 | * Trying to pop an empty stack. This often happens at the start of | |
442 | * traces, for example when we see a syscall_exit, without having | |
443 | * the corresponding syscall_entry in the trace. Just ignore | |
444 | * silently. | |
445 | */ | |
5896eb76 | 446 | return null; |
90a25ebe | 447 | } |
b67a2540 | 448 | if (previousSV.getType() != Type.INTEGER) { |
a52fde77 | 449 | /* |
b67a2540 AM |
450 | * The existing value was not an integer (which is expected for |
451 | * stack tops), this doesn't look like a valid stack attribute. | |
a52fde77 | 452 | */ |
90a25ebe | 453 | throw new StateValueTypeException(); |
a52fde77 AM |
454 | } |
455 | ||
0126a8ca | 456 | int stackDepth = previousSV.unboxInt(); |
90a25ebe | 457 | |
e2eac108 | 458 | if (stackDepth <= 0) { |
a52fde77 | 459 | /* This on the other hand should not happen... */ |
e2eac108 | 460 | String message = "A top-level stack attribute cannot " + //$NON-NLS-1$ |
359eeba0 | 461 | "have a value of 0 or less."; //$NON-NLS-1$ |
90a25ebe | 462 | throw new StateValueTypeException(message); |
a52fde77 AM |
463 | } |
464 | ||
e2eac108 | 465 | /* The attribute should already exist at this point */ |
0126a8ca | 466 | int subAttributeQuark = getQuarkRelative(attributeQuark, String.valueOf(stackDepth)); |
5896eb76 | 467 | ITmfStateValue poppedValue = queryOngoingState(subAttributeQuark); |
a52fde77 | 468 | |
e2eac108 AM |
469 | /* Update the state value of the stack-attribute */ |
470 | ITmfStateValue nextSV; | |
a0f8fb9b | 471 | if (--stackDepth == 0) { |
359eeba0 | 472 | /* Store a null state value */ |
e2eac108 AM |
473 | nextSV = TmfStateValue.nullValue(); |
474 | } else { | |
475 | nextSV = TmfStateValue.newValueInt(stackDepth); | |
476 | } | |
477 | modifyAttribute(t, nextSV, attributeQuark); | |
478 | ||
479 | /* Delete the sub-attribute that contained the user's state value */ | |
a52fde77 | 480 | removeAttribute(t, subAttributeQuark); |
e2eac108 | 481 | |
5896eb76 | 482 | return poppedValue; |
a52fde77 AM |
483 | } |
484 | ||
8d1346f0 | 485 | @Override |
a52fde77 AM |
486 | public void removeAttribute(long t, int attributeQuark) |
487 | throws TimeRangeException, AttributeNotFoundException { | |
488 | assert (attributeQuark >= 0); | |
c66426fd AM |
489 | List<Integer> childAttributes; |
490 | ||
491 | /* | |
492 | * "Nullify our children first, recursively. We pass 'false' because we | |
493 | * handle the recursion ourselves. | |
494 | */ | |
339d27b4 | 495 | childAttributes = getSubAttributes(attributeQuark, false); |
0126a8ca | 496 | for (int childNodeQuark : childAttributes) { |
a52fde77 AM |
497 | assert (attributeQuark != childNodeQuark); |
498 | removeAttribute(t, childNodeQuark); | |
499 | } | |
500 | /* Nullify ourselves */ | |
7e0b2b56 AM |
501 | try { |
502 | transState.processStateChange(t, TmfStateValue.nullValue(), | |
503 | attributeQuark); | |
504 | } catch (StateValueTypeException e) { | |
50678114 AM |
505 | /* |
506 | * Will not happen since we're inserting null values only, but poor | |
507 | * compiler has no way of knowing this... | |
7e0b2b56 | 508 | */ |
cb42195c | 509 | throw new IllegalStateException(e); |
7e0b2b56 | 510 | } |
a52fde77 AM |
511 | } |
512 | ||
8d1346f0 AM |
513 | //-------------------------------------------------------------------------- |
514 | // "Current" query/update methods | |
515 | //-------------------------------------------------------------------------- | |
a52fde77 | 516 | |
8d1346f0 | 517 | @Override |
a52fde77 AM |
518 | public ITmfStateValue queryOngoingState(int attributeQuark) |
519 | throws AttributeNotFoundException { | |
520 | return transState.getOngoingStateValue(attributeQuark); | |
521 | } | |
522 | ||
602c0697 AM |
523 | @Override |
524 | public long getOngoingStartTime(int attribute) | |
525 | throws AttributeNotFoundException { | |
526 | return transState.getOngoingStartTime(attribute); | |
527 | } | |
528 | ||
8d1346f0 | 529 | @Override |
a52fde77 AM |
530 | public void updateOngoingState(ITmfStateValue newValue, int attributeQuark) |
531 | throws AttributeNotFoundException { | |
532 | transState.changeOngoingStateValue(attributeQuark, newValue); | |
533 | } | |
534 | ||
66866869 AM |
535 | /** |
536 | * Modify the whole "ongoing state" (state values + start times). This can | |
537 | * be used when "seeking" a state system to a different point in the trace | |
538 | * (and restoring the known stateInfo at this location). Use with care! | |
539 | * | |
540 | * @param newStateIntervals | |
541 | * The new List of state values to use as ongoing state info | |
542 | */ | |
543 | protected void replaceOngoingState(List<ITmfStateInterval> newStateIntervals) { | |
544 | transState.replaceOngoingState(newStateIntervals); | |
a0f8fb9b | 545 | } |
66866869 | 546 | |
8d1346f0 AM |
547 | //-------------------------------------------------------------------------- |
548 | // Regular query methods (sent to the back-end) | |
549 | //-------------------------------------------------------------------------- | |
550 | ||
551 | @Override | |
552 | public synchronized List<ITmfStateInterval> queryFullState(long t) | |
96345c5a AM |
553 | throws TimeRangeException, StateSystemDisposedException { |
554 | if (isDisposed) { | |
555 | throw new StateSystemDisposedException(); | |
556 | } | |
557 | ||
a4524c1b | 558 | List<ITmfStateInterval> stateInfo = new ArrayList<>(getNbAttributes()); |
8d1346f0 AM |
559 | |
560 | /* Bring the size of the array to the current number of attributes */ | |
339d27b4 | 561 | for (int i = 0; i < getNbAttributes(); i++) { |
8d1346f0 AM |
562 | stateInfo.add(null); |
563 | } | |
564 | ||
565 | /* Query the storage backend */ | |
566 | backend.doQuery(stateInfo, t); | |
567 | ||
568 | /* | |
569 | * If we are currently building the history, also query the "ongoing" | |
570 | * states for stuff that might not yet be written to the history. | |
571 | */ | |
572 | if (transState.isActive()) { | |
573 | transState.doQuery(stateInfo, t); | |
574 | } | |
575 | ||
576 | /* | |
577 | * We should have previously inserted an interval for every attribute. | |
578 | * If we do happen do see a 'null' object here, just replace it with a a | |
579 | * dummy internal with a null value, to avoid NPE's further up. | |
580 | */ | |
581 | for (int i = 0; i < stateInfo.size(); i++) { | |
582 | if (stateInfo.get(i) == null) { | |
8d1346f0 AM |
583 | stateInfo.set(i, new TmfStateInterval(t, t, i, TmfStateValue.nullValue())); |
584 | } | |
585 | } | |
586 | return stateInfo; | |
50678114 AM |
587 | } |
588 | ||
8d1346f0 AM |
589 | @Override |
590 | public ITmfStateInterval querySingleState(long t, int attributeQuark) | |
96345c5a AM |
591 | throws AttributeNotFoundException, TimeRangeException, |
592 | StateSystemDisposedException { | |
593 | if (isDisposed) { | |
594 | throw new StateSystemDisposedException(); | |
595 | } | |
8d1346f0 | 596 | |
09e6fd9b AM |
597 | ITmfStateInterval ret = transState.getIntervalAt(t, attributeQuark); |
598 | if (ret == null) { | |
599 | /* | |
600 | * The transient state did not have the information, let's look into | |
601 | * the backend next. | |
602 | */ | |
8d1346f0 AM |
603 | ret = backend.doSingularQuery(t, attributeQuark); |
604 | } | |
605 | ||
606 | /* | |
607 | * Return a fake interval if we could not find anything in the history. | |
608 | * We do NOT want to return 'null' here. | |
609 | */ | |
610 | if (ret == null) { | |
8d1346f0 AM |
611 | return new TmfStateInterval(t, this.getCurrentEndTime(), |
612 | attributeQuark, TmfStateValue.nullValue()); | |
613 | } | |
614 | return ret; | |
615 | } | |
616 | ||
4bff6e6e AM |
617 | @Override |
618 | public ITmfStateInterval querySingleStackTop(long t, int stackAttributeQuark) | |
619 | throws StateValueTypeException, AttributeNotFoundException, | |
96345c5a | 620 | TimeRangeException, StateSystemDisposedException { |
359eeba0 | 621 | ITmfStateValue curStackStateValue = querySingleState(t, stackAttributeQuark).getStateValue(); |
4bff6e6e | 622 | |
359eeba0 | 623 | if (curStackStateValue.isNull()) { |
4bff6e6e AM |
624 | /* There is nothing stored in this stack at this moment */ |
625 | return null; | |
359eeba0 | 626 | } |
0126a8ca | 627 | int curStackDepth = curStackStateValue.unboxInt(); |
359eeba0 | 628 | if (curStackDepth <= 0) { |
4bff6e6e AM |
629 | /* |
630 | * This attribute is an integer attribute, but it doesn't seem like | |
631 | * it's used as a stack-attribute... | |
632 | */ | |
633 | throw new StateValueTypeException(); | |
634 | } | |
635 | ||
0126a8ca | 636 | int subAttribQuark = getQuarkRelative(stackAttributeQuark, String.valueOf(curStackDepth)); |
cb42195c | 637 | return querySingleState(t, subAttribQuark); |
4bff6e6e AM |
638 | } |
639 | ||
8d1346f0 AM |
640 | @Override |
641 | public List<ITmfStateInterval> queryHistoryRange(int attributeQuark, | |
642 | long t1, long t2) throws TimeRangeException, | |
96345c5a AM |
643 | AttributeNotFoundException, StateSystemDisposedException { |
644 | if (isDisposed) { | |
645 | throw new StateSystemDisposedException(); | |
646 | } | |
647 | ||
8d1346f0 AM |
648 | List<ITmfStateInterval> intervals; |
649 | ITmfStateInterval currentInterval; | |
650 | long ts, tEnd; | |
651 | ||
652 | /* Make sure the time range makes sense */ | |
1cf25311 | 653 | if (t2 < t1) { |
8d1346f0 AM |
654 | throw new TimeRangeException(); |
655 | } | |
656 | ||
657 | /* Set the actual, valid end time of the range query */ | |
658 | if (t2 > this.getCurrentEndTime()) { | |
659 | tEnd = this.getCurrentEndTime(); | |
660 | } else { | |
661 | tEnd = t2; | |
662 | } | |
663 | ||
664 | /* Get the initial state at time T1 */ | |
a4524c1b | 665 | intervals = new ArrayList<>(); |
8d1346f0 AM |
666 | currentInterval = querySingleState(t1, attributeQuark); |
667 | intervals.add(currentInterval); | |
668 | ||
669 | /* Get the following state changes */ | |
670 | ts = currentInterval.getEndTime(); | |
671 | while (ts != -1 && ts < tEnd) { | |
672 | ts++; /* To "jump over" to the next state in the history */ | |
673 | currentInterval = querySingleState(ts, attributeQuark); | |
674 | intervals.add(currentInterval); | |
675 | ts = currentInterval.getEndTime(); | |
676 | } | |
677 | return intervals; | |
678 | } | |
679 | ||
680 | @Override | |
681 | public List<ITmfStateInterval> queryHistoryRange(int attributeQuark, | |
b5a8d0cc | 682 | long t1, long t2, long resolution, IProgressMonitor monitor) |
96345c5a AM |
683 | throws TimeRangeException, AttributeNotFoundException, |
684 | StateSystemDisposedException { | |
685 | if (isDisposed) { | |
686 | throw new StateSystemDisposedException(); | |
687 | } | |
688 | ||
a0f8fb9b FW |
689 | List<ITmfStateInterval> intervals = new LinkedList<>(); |
690 | ITmfStateInterval currentInterval = null; | |
8d1346f0 AM |
691 | long ts, tEnd; |
692 | ||
41b5c37f AM |
693 | IProgressMonitor mon = monitor; |
694 | if (mon == null) { | |
695 | mon = new NullProgressMonitor(); | |
b5a8d0cc AM |
696 | } |
697 | ||
8d1346f0 AM |
698 | /* Make sure the time range makes sense */ |
699 | if (t2 < t1 || resolution <= 0) { | |
700 | throw new TimeRangeException(); | |
701 | } | |
702 | ||
703 | /* Set the actual, valid end time of the range query */ | |
704 | if (t2 > this.getCurrentEndTime()) { | |
705 | tEnd = this.getCurrentEndTime(); | |
706 | } else { | |
707 | tEnd = t2; | |
708 | } | |
709 | ||
8d1346f0 AM |
710 | /* |
711 | * Iterate over the "resolution points". We skip unneeded queries in the | |
712 | * case the current interval is longer than the resolution. | |
713 | */ | |
a0f8fb9b FW |
714 | for (ts = t1; ts <= tEnd; |
715 | ts += ((currentInterval.getEndTime() - ts) / resolution + 1) * resolution) { | |
41b5c37f | 716 | if (mon.isCanceled()) { |
8d1346f0 AM |
717 | return intervals; |
718 | } | |
8d1346f0 AM |
719 | currentInterval = querySingleState(ts, attributeQuark); |
720 | intervals.add(currentInterval); | |
721 | } | |
722 | ||
723 | /* Add the interval at t2, if it wasn't included already. */ | |
a0f8fb9b | 724 | if (currentInterval != null && currentInterval.getEndTime() < tEnd) { |
8d1346f0 AM |
725 | currentInterval = querySingleState(tEnd, attributeQuark); |
726 | intervals.add(currentInterval); | |
727 | } | |
728 | return intervals; | |
729 | } | |
730 | ||
731 | //-------------------------------------------------------------------------- | |
732 | // Debug methods | |
733 | //-------------------------------------------------------------------------- | |
734 | ||
735 | static void logMissingInterval(int attribute, long timestamp) { | |
5500a7f0 | 736 | Activator.logInfo("No data found in history for attribute " + //$NON-NLS-1$ |
8d1346f0 AM |
737 | attribute + " at time " + timestamp + //$NON-NLS-1$ |
738 | ", returning dummy interval"); //$NON-NLS-1$ | |
a52fde77 AM |
739 | } |
740 | ||
741 | /** | |
742 | * Print out the contents of the inner structures. | |
5df842b3 | 743 | * |
a52fde77 AM |
744 | * @param writer |
745 | * The PrintWriter in which to print the output | |
746 | */ | |
747 | public void debugPrint(PrintWriter writer) { | |
339d27b4 | 748 | getAttributeTree().debugPrint(writer); |
a52fde77 | 749 | transState.debugPrint(writer); |
8d1346f0 | 750 | backend.debugPrint(writer); |
a52fde77 AM |
751 | } |
752 | ||
8d1346f0 | 753 | } |