| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012, 2016 Ericsson and others. |
| 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 | * Alexandre Montplaisir - Initial API |
| 11 | ******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.tracecompass.statesystem.core; |
| 14 | |
| 15 | import java.util.List; |
| 16 | |
| 17 | import org.eclipse.jdt.annotation.NonNull; |
| 18 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; |
| 19 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException; |
| 20 | import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException; |
| 21 | import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval; |
| 22 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * This is the read-only interface to the generic state system. It contains all |
| 27 | * the read-only quark-getting methods, as well as the history-querying ones. |
| 28 | * |
| 29 | * @author Alexandre Montplaisir |
| 30 | * @noimplement Only the internal StateSystem class should implement this |
| 31 | * interface. |
| 32 | */ |
| 33 | public interface ITmfStateSystem { |
| 34 | |
| 35 | /** Root attribute quark |
| 36 | * @since 2.0*/ |
| 37 | int ROOT_ATTRIBUTE = -1; |
| 38 | /** Invalid attribute quark |
| 39 | * @since 2.0*/ |
| 40 | int INVALID_ATTRIBUTE = -2; |
| 41 | |
| 42 | /** |
| 43 | * Get the ID of this state system. |
| 44 | * |
| 45 | * @return The state system's ID |
| 46 | */ |
| 47 | String getSSID(); |
| 48 | |
| 49 | /** |
| 50 | * Return the start time of this history. It usually matches the start time |
| 51 | * of the original trace. |
| 52 | * |
| 53 | * @return The history's registered start time |
| 54 | */ |
| 55 | long getStartTime(); |
| 56 | |
| 57 | /** |
| 58 | * Return the current end time of the history. |
| 59 | * |
| 60 | * @return The current end time of this state history |
| 61 | */ |
| 62 | long getCurrentEndTime(); |
| 63 | |
| 64 | /** |
| 65 | * Check if the construction of this state system was cancelled or not. If |
| 66 | * false is returned, it can mean that the building was finished |
| 67 | * successfully, or that it is still ongoing. You can check independently |
| 68 | * with {@link #waitUntilBuilt()} if it is finished or not. |
| 69 | * |
| 70 | * @return If the construction was cancelled or not. In true is returned, no |
| 71 | * queries should be run afterwards. |
| 72 | */ |
| 73 | boolean isCancelled(); |
| 74 | |
| 75 | /** |
| 76 | * While it's possible to query a state history that is being built, |
| 77 | * sometimes we might want to wait until the construction is finished before |
| 78 | * we start doing queries. |
| 79 | * |
| 80 | * This method blocks the calling thread until the history back-end is done |
| 81 | * building. If it's already built (ie, opening a pre-existing file) this |
| 82 | * should return immediately. |
| 83 | * |
| 84 | * You should always check with {@link #isCancelled()} if it is safe to |
| 85 | * query this state system before doing queries. |
| 86 | */ |
| 87 | void waitUntilBuilt(); |
| 88 | |
| 89 | /** |
| 90 | * Wait until the state system construction is finished. Similar to |
| 91 | * {@link #waitUntilBuilt()}, but we also specify a timeout. If the timeout |
| 92 | * elapses before the construction is finished, the method will return. |
| 93 | * |
| 94 | * The return value determines if the return was due to the construction |
| 95 | * finishing (true), or the timeout elapsing (false). |
| 96 | * |
| 97 | * This can be useful, for example, for a component doing queries |
| 98 | * periodically to the system while it is being built. |
| 99 | * |
| 100 | * @param timeout |
| 101 | * Timeout value in milliseconds |
| 102 | * @return True if the return was due to the construction finishing, false |
| 103 | * if it was because the timeout elapsed. Same logic as |
| 104 | * {@link java.util.concurrent.CountDownLatch#await(long, java.util.concurrent.TimeUnit)} |
| 105 | */ |
| 106 | boolean waitUntilBuilt(long timeout); |
| 107 | |
| 108 | /** |
| 109 | * Notify the state system that the trace is being closed, so it should |
| 110 | * clean up, close its files, etc. |
| 111 | */ |
| 112 | void dispose(); |
| 113 | |
| 114 | // ------------------------------------------------------------------------ |
| 115 | // Read-only quark-getting methods |
| 116 | // ------------------------------------------------------------------------ |
| 117 | |
| 118 | /** |
| 119 | * Return the current total amount of attributes in the system. This is also |
| 120 | * equal to the quark that will be assigned to the next attribute that's |
| 121 | * created. |
| 122 | * |
| 123 | * @return The current number of attributes in the system |
| 124 | */ |
| 125 | int getNbAttributes(); |
| 126 | |
| 127 | /** |
| 128 | * Basic quark-retrieving method. Pass an attribute in parameter as an array |
| 129 | * of strings, the matching quark will be returned. |
| 130 | * <p> |
| 131 | * This version will NOT create any new attributes. If an invalid attribute |
| 132 | * is requested, an exception will be thrown. |
| 133 | * <p> |
| 134 | * If it is expected that the requested attribute might be absent, it is |
| 135 | * recommended to use {@link #optQuarkAbsolute(String...)} instead. |
| 136 | * |
| 137 | * @param attribute |
| 138 | * Attribute given as its full path in the Attribute Tree |
| 139 | * @return The quark of the requested attribute, if it existed. |
| 140 | * @throws AttributeNotFoundException |
| 141 | * This exception is thrown if the requested attribute simply |
| 142 | * did not exist in the system. |
| 143 | */ |
| 144 | int getQuarkAbsolute(String... attribute) |
| 145 | throws AttributeNotFoundException; |
| 146 | |
| 147 | /** |
| 148 | * Quark-retrieving method for an optional attribute that may or may not be |
| 149 | * present. Pass an attribute in parameter as an array of strings, if it |
| 150 | * exists, the matching quark will be returned. |
| 151 | * <p> |
| 152 | * This version will NOT create any new attributes. If an attribute that |
| 153 | * does not exist is requested, {@link #INVALID_ATTRIBUTE} will be returned. |
| 154 | * |
| 155 | * @param attribute |
| 156 | * Attribute given as its full path in the Attribute Tree |
| 157 | * @return The quark of the requested attribute, or |
| 158 | * {@link #INVALID_ATTRIBUTE} if it does not exist. |
| 159 | * @since 2.0 |
| 160 | */ |
| 161 | int optQuarkAbsolute(String... attribute); |
| 162 | |
| 163 | /** |
| 164 | * "Relative path" quark-getting method. Instead of specifying a full path, |
| 165 | * if you know the path is relative to another attribute for which you |
| 166 | * already have the quark, use this for better performance. |
| 167 | * <p> |
| 168 | * This is useful for cases where a lot of modifications or queries will |
| 169 | * originate from the same branch of the attribute tree : the common part of |
| 170 | * the path won't have to be re-hashed for every access. |
| 171 | * <p> |
| 172 | * This version will NOT create any new attributes. If an invalid attribute |
| 173 | * is requested, an exception will be thrown. |
| 174 | * <p> |
| 175 | * If it is expected that the requested sub-attribute might be absent, it is |
| 176 | * recommended to use {@link #optQuarkRelative(int, String...)} instead. |
| 177 | * |
| 178 | * @param startingNodeQuark |
| 179 | * The quark of the attribute from which 'subPath' originates. |
| 180 | * @param subPath |
| 181 | * "Rest" of the path to get to the final attribute |
| 182 | * @return The matching quark, if it existed |
| 183 | * @throws IndexOutOfBoundsException |
| 184 | * If the starting node quark is out of range |
| 185 | * @throws AttributeNotFoundException |
| 186 | * If the sub-attribute does not exist |
| 187 | */ |
| 188 | int getQuarkRelative(int startingNodeQuark, String... subPath) |
| 189 | throws AttributeNotFoundException; |
| 190 | |
| 191 | /** |
| 192 | * "Relative path" quark-getting method for an optional attribute that may |
| 193 | * or may not be present. Instead of specifying a full path, if you know the |
| 194 | * path is relative to another attribute for which you already have the |
| 195 | * quark, use this for better performance. |
| 196 | * <p> |
| 197 | * This is useful for cases where a lot of modifications or queries will |
| 198 | * originate from the same branch of the attribute tree : the common part of |
| 199 | * the path won't have to be re-hashed for every access. |
| 200 | * <p> |
| 201 | * This version will NOT create any new attributes. If a sub-attribute that |
| 202 | * does not exist is requested, {@link #INVALID_ATTRIBUTE} will be returned. |
| 203 | * |
| 204 | * @param startingNodeQuark |
| 205 | * The quark of the attribute from which 'subPath' originates. |
| 206 | * @param subPath |
| 207 | * "Rest" of the path to get to the final attribute |
| 208 | * @return The quark of the requested sub-attribute, or |
| 209 | * {@link #INVALID_ATTRIBUTE} if it does not exist. |
| 210 | * @throws IndexOutOfBoundsException |
| 211 | * If the starting node quark is out of range |
| 212 | * @since 2.0 |
| 213 | */ |
| 214 | int optQuarkRelative(int startingNodeQuark, String... subPath); |
| 215 | |
| 216 | /** |
| 217 | * Return the sub-attributes of the target attribute, as a List of quarks. |
| 218 | * |
| 219 | * @param quark |
| 220 | * The attribute of which you want to sub-attributes. You can use |
| 221 | * {@link #ROOT_ATTRIBUTE} here to specify the root node. |
| 222 | * @param recursive |
| 223 | * True if you want all recursive sub-attributes, false if you |
| 224 | * only want the first level. |
| 225 | * @return A List of integers, matching the quarks of the sub-attributes. |
| 226 | * @throws AttributeNotFoundException |
| 227 | * If the quark was not existing or invalid. |
| 228 | */ |
| 229 | @NonNull List<@NonNull Integer> getSubAttributes(int quark, boolean recursive) |
| 230 | throws AttributeNotFoundException; |
| 231 | |
| 232 | /** |
| 233 | * Return the sub-attributes of the target attribute, as a List of quarks, |
| 234 | * similarly to {@link #getSubAttributes(int, boolean)}, but with an added |
| 235 | * regex pattern to filter on the return attributes. |
| 236 | * |
| 237 | * @param quark |
| 238 | * The attribute of which you want to sub-attributes. You can use |
| 239 | * {@link #ROOT_ATTRIBUTE} here to specify the root node. |
| 240 | * @param recursive |
| 241 | * True if you want all recursive sub-attributes, false if you |
| 242 | * only want the first level. Note that the returned value will |
| 243 | * be flattened. |
| 244 | * @param pattern |
| 245 | * The regular expression to match the attribute base name. |
| 246 | * @return A List of integers, matching the quarks of the sub-attributes |
| 247 | * that match the regex. An empty list is returned if there is no |
| 248 | * matching attribute. |
| 249 | * @throws AttributeNotFoundException |
| 250 | * If the 'quark' was not existing or invalid. |
| 251 | */ |
| 252 | @NonNull List<@NonNull Integer> getSubAttributes(int quark, boolean recursive, String pattern) |
| 253 | throws AttributeNotFoundException; |
| 254 | |
| 255 | /** |
| 256 | * Batch quark-retrieving method. This method allows you to specify a path |
| 257 | * pattern which can include wildcard "*" or parent ".." elements. It will |
| 258 | * check all the existing attributes in the attribute tree and return those |
| 259 | * who match the pattern. |
| 260 | * <p> |
| 261 | * For example, passing ("Threads", "*", "Exec_mode") will return the list |
| 262 | * of quarks for attributes "Threads/1000/Exec_mode", |
| 263 | * "Threads/1500/Exec_mode", and so on, depending on what exists at this |
| 264 | * time in the attribute tree. |
| 265 | * <p> |
| 266 | * If no wildcard or parent element is specified, the behavior is the same |
| 267 | * as getQuarkAbsolute() (except it will return a List with one entry, or an |
| 268 | * empty list if there is no match instead of throwing an exception). This |
| 269 | * method will never create new attributes. |
| 270 | * |
| 271 | * @param pattern |
| 272 | * The array of strings representing the pattern to look for. |
| 273 | * @return A List of unique attribute quarks, representing attributes that |
| 274 | * matched the pattern. If no attribute matched, the list will be |
| 275 | * empty (but not null). If the pattern is empty, |
| 276 | * {@link #ROOT_ATTRIBUTE} is returned in the list. |
| 277 | */ |
| 278 | @NonNull List<@NonNull Integer> getQuarks(String... pattern); |
| 279 | |
| 280 | /** |
| 281 | * Relative batch quark-retrieving method. This method allows you to specify |
| 282 | * a path pattern which can include wildcard "*" or parent ".." elements. It |
| 283 | * will check all the existing attributes in the attribute tree and return |
| 284 | * those who match the pattern. |
| 285 | * <p> |
| 286 | * For example, passing (5, "Threads", "*", "Exec_mode") will return the |
| 287 | * list of quarks for attributes "<path of quark 5>/Threads/1000/Exec_mode", |
| 288 | * "<path of quark 5>/Threads/1500/Exec_mode", and so on, depending on what |
| 289 | * exists at this time in the attribute tree. |
| 290 | * <p> |
| 291 | * If no wildcard or parent element is specified, the behavior is the same |
| 292 | * as getQuarkRelative() (except it will return a List with one entry, or an |
| 293 | * empty list if there is no match instead of throwing an exception). This |
| 294 | * method will never create new attributes. |
| 295 | * |
| 296 | * @param startingNodeQuark |
| 297 | * The quark of the attribute from which 'pattern' originates. |
| 298 | * @param pattern |
| 299 | * The array of strings representing the pattern to look for. |
| 300 | * @return A List of unique attribute quarks, representing attributes that |
| 301 | * matched the pattern. If no attribute matched, the list will be |
| 302 | * empty (but not null). If the pattern is empty, the starting node |
| 303 | * quark is returned in the list. |
| 304 | * @throws IndexOutOfBoundsException |
| 305 | * If the starting node quark is out of range |
| 306 | * @since 2.0 |
| 307 | */ |
| 308 | @NonNull List<@NonNull Integer> getQuarks(int startingNodeQuark, String... pattern); |
| 309 | |
| 310 | /** |
| 311 | * Return the name assigned to this quark. This returns only the "basename", |
| 312 | * not the complete path to this attribute. |
| 313 | * |
| 314 | * @param attributeQuark |
| 315 | * The quark for which we want the name |
| 316 | * @return The name of the quark |
| 317 | * @throws IndexOutOfBoundsException |
| 318 | * If the attribute quark is out of range |
| 319 | */ |
| 320 | @NonNull String getAttributeName(int attributeQuark); |
| 321 | |
| 322 | /** |
| 323 | * This returns the slash-separated path of an attribute by providing its |
| 324 | * quark |
| 325 | * |
| 326 | * @param attributeQuark |
| 327 | * The quark of the attribute we want |
| 328 | * @return One single string separated with '/', like a filesystem path |
| 329 | * @throws IndexOutOfBoundsException |
| 330 | * If the attribute quark is out of range |
| 331 | */ |
| 332 | @NonNull String getFullAttributePath(int attributeQuark); |
| 333 | |
| 334 | /** |
| 335 | * Return the full attribute path, as an array of strings representing each |
| 336 | * element. |
| 337 | * |
| 338 | * @param attributeQuark |
| 339 | * The quark of the attribute we want. |
| 340 | * @return The array of path elements |
| 341 | * @throws IndexOutOfBoundsException |
| 342 | * If the attribute quark is out of range |
| 343 | * @since 1.0 |
| 344 | */ |
| 345 | String @NonNull [] getFullAttributePathArray(int attributeQuark); |
| 346 | |
| 347 | /** |
| 348 | * Returns the parent quark of the attribute. |
| 349 | * |
| 350 | * @param attributeQuark |
| 351 | * The quark of the attribute |
| 352 | * @return Quark of the parent attribute or {@link #ROOT_ATTRIBUTE} if root |
| 353 | * quark or no parent. |
| 354 | * @throws IndexOutOfBoundsException |
| 355 | * If the attribute quark is out of range |
| 356 | */ |
| 357 | int getParentAttributeQuark(int attributeQuark); |
| 358 | |
| 359 | // ------------------------------------------------------------------------ |
| 360 | // Query methods |
| 361 | // ------------------------------------------------------------------------ |
| 362 | |
| 363 | /** |
| 364 | * Returns the current state value we have (in the Transient State) for the |
| 365 | * given attribute. |
| 366 | * |
| 367 | * This is useful even for a StateHistorySystem, as we are guaranteed it |
| 368 | * will only do a memory access and not go look on disk (and we don't even |
| 369 | * have to provide a timestamp!) |
| 370 | * |
| 371 | * @param attributeQuark |
| 372 | * For which attribute we want the current state |
| 373 | * @return The State value that's "current" for this attribute |
| 374 | * @throws AttributeNotFoundException |
| 375 | * If the requested attribute is invalid |
| 376 | */ |
| 377 | @NonNull ITmfStateValue queryOngoingState(int attributeQuark) |
| 378 | throws AttributeNotFoundException; |
| 379 | |
| 380 | /** |
| 381 | * Get the start time of the current ongoing state, for the specified |
| 382 | * attribute. |
| 383 | * |
| 384 | * @param attribute |
| 385 | * Quark of the attribute |
| 386 | * @return The current start time of the ongoing state |
| 387 | * @throws AttributeNotFoundException |
| 388 | * If the attribute is invalid |
| 389 | */ |
| 390 | long getOngoingStartTime(int attribute) |
| 391 | throws AttributeNotFoundException; |
| 392 | |
| 393 | /** |
| 394 | * Load the complete state information at time 't' into the returned List. |
| 395 | * You can then get the intervals for single attributes by using |
| 396 | * List.get(n), where 'n' is the quark of the attribute. |
| 397 | * |
| 398 | * On average if you need around 10 or more queries for the same timestamps, |
| 399 | * use this method. If you need less than 10 (for example, running many |
| 400 | * queries for the same attributes but at different timestamps), you might |
| 401 | * be better using the querySingleState() methods instead. |
| 402 | * |
| 403 | * @param t |
| 404 | * We will recreate the state information to what it was at time |
| 405 | * t. |
| 406 | * @return The List of intervals, where the offset = the quark |
| 407 | * @throws TimeRangeException |
| 408 | * If the 't' parameter is outside of the range of the state |
| 409 | * history. |
| 410 | * @throws StateSystemDisposedException |
| 411 | * If the query is sent after the state system has been disposed |
| 412 | */ |
| 413 | @NonNull List<@NonNull ITmfStateInterval> queryFullState(long t) |
| 414 | throws StateSystemDisposedException; |
| 415 | |
| 416 | /** |
| 417 | * Singular query method. This one does not update the whole stateInfo |
| 418 | * vector, like queryFullState() does. It only searches for one specific |
| 419 | * entry in the state history. |
| 420 | * |
| 421 | * It should be used when you only want very few entries, instead of the |
| 422 | * whole state (or many entries, but all at different timestamps). If you do |
| 423 | * request many entries all at the same time, you should use the |
| 424 | * conventional queryFullState() + List.get() method. |
| 425 | * |
| 426 | * @param t |
| 427 | * The timestamp at which we want the state |
| 428 | * @param attributeQuark |
| 429 | * Which attribute we want to get the state of |
| 430 | * @return The StateInterval representing the state |
| 431 | * @throws TimeRangeException |
| 432 | * If 't' is invalid |
| 433 | * @throws AttributeNotFoundException |
| 434 | * If the requested quark does not exist in the model |
| 435 | * @throws StateSystemDisposedException |
| 436 | * If the query is sent after the state system has been disposed |
| 437 | */ |
| 438 | @NonNull ITmfStateInterval querySingleState(long t, int attributeQuark) |
| 439 | throws AttributeNotFoundException, StateSystemDisposedException; |
| 440 | } |