| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012, 2016 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 | |
| 16 | package org.eclipse.tracecompass.internal.statesystem.core.backend.historytree; |
| 17 | |
| 18 | import java.io.File; |
| 19 | import java.io.FileInputStream; |
| 20 | import java.io.IOException; |
| 21 | import java.nio.channels.ClosedChannelException; |
| 22 | import java.util.Deque; |
| 23 | import java.util.LinkedList; |
| 24 | import java.util.List; |
| 25 | import java.util.logging.Logger; |
| 26 | |
| 27 | import org.eclipse.jdt.annotation.NonNull; |
| 28 | import org.eclipse.tracecompass.common.core.log.TraceCompassLog; |
| 29 | import org.eclipse.tracecompass.internal.statesystem.core.Activator; |
| 30 | import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend; |
| 31 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException; |
| 32 | import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException; |
| 33 | import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval; |
| 34 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; |
| 35 | import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue; |
| 36 | |
| 37 | import com.google.common.annotations.VisibleForTesting; |
| 38 | |
| 39 | /** |
| 40 | * History Tree backend for storing a state history. This is the basic version |
| 41 | * that runs in the same thread as the class creating it. |
| 42 | * |
| 43 | * @author Alexandre Montplaisir |
| 44 | */ |
| 45 | public class HistoryTreeBackend implements IStateHistoryBackend { |
| 46 | |
| 47 | private static final Logger LOGGER = TraceCompassLog.getLogger(HistoryTreeBackend.class); |
| 48 | |
| 49 | private final @NonNull String fSsid; |
| 50 | |
| 51 | /** |
| 52 | * The history tree that sits underneath. |
| 53 | */ |
| 54 | private final @NonNull IHistoryTree fSht; |
| 55 | |
| 56 | /** Indicates if the history tree construction is done */ |
| 57 | private volatile boolean fFinishedBuilding = false; |
| 58 | |
| 59 | /** |
| 60 | * Indicates if the history tree construction is done |
| 61 | * |
| 62 | * @return if the history tree construction is done |
| 63 | */ |
| 64 | protected boolean isFinishedBuilding() { |
| 65 | return fFinishedBuilding; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Sets if the history tree is finished building |
| 70 | * |
| 71 | * @param isFinishedBuilding |
| 72 | * is the history tree finished building |
| 73 | */ |
| 74 | protected void setFinishedBuilding(boolean isFinishedBuilding) { |
| 75 | fFinishedBuilding = isFinishedBuilding; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Constructor for new history files. Use this when creating a new history |
| 80 | * from scratch. |
| 81 | * |
| 82 | * @param ssid |
| 83 | * The state system's ID |
| 84 | * @param newStateFile |
| 85 | * The filename/location where to store the state history (Should |
| 86 | * end in .ht) |
| 87 | * @param providerVersion |
| 88 | * Version of of the state provider. We will only try to reopen |
| 89 | * existing files if this version matches the one in the |
| 90 | * framework. |
| 91 | * @param startTime |
| 92 | * The earliest time stamp that will be stored in the history |
| 93 | * @param blockSize |
| 94 | * The size of the blocks in the history file. This should be a |
| 95 | * multiple of 4096. |
| 96 | * @param maxChildren |
| 97 | * The maximum number of children each core node can have |
| 98 | * @throws IOException |
| 99 | * Thrown if we can't create the file for some reason |
| 100 | */ |
| 101 | public HistoryTreeBackend(@NonNull String ssid, |
| 102 | File newStateFile, |
| 103 | int providerVersion, |
| 104 | long startTime, |
| 105 | int blockSize, |
| 106 | int maxChildren) throws IOException { |
| 107 | fSsid = ssid; |
| 108 | final HTConfig conf = new HTConfig(newStateFile, blockSize, maxChildren, |
| 109 | providerVersion, startTime); |
| 110 | fSht = initializeSHT(conf); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Constructor for new history files. Use this when creating a new history |
| 115 | * from scratch. This version supplies sane defaults for the configuration |
| 116 | * parameters. |
| 117 | * |
| 118 | * @param ssid |
| 119 | * The state system's id |
| 120 | * @param newStateFile |
| 121 | * The filename/location where to store the state history (Should |
| 122 | * end in .ht) |
| 123 | * @param providerVersion |
| 124 | * Version of of the state provider. We will only try to reopen |
| 125 | * existing files if this version matches the one in the |
| 126 | * framework. |
| 127 | * @param startTime |
| 128 | * The earliest time stamp that will be stored in the history |
| 129 | * @throws IOException |
| 130 | * Thrown if we can't create the file for some reason |
| 131 | * @since 1.0 |
| 132 | */ |
| 133 | public HistoryTreeBackend(@NonNull String ssid, File newStateFile, int providerVersion, long startTime) |
| 134 | throws IOException { |
| 135 | this(ssid, newStateFile, providerVersion, startTime, 64 * 1024, 50); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Existing history constructor. Use this to open an existing state-file. |
| 140 | * |
| 141 | * @param ssid |
| 142 | * The state system's id |
| 143 | * @param existingStateFile |
| 144 | * Filename/location of the history we want to load |
| 145 | * @param providerVersion |
| 146 | * Expected version of of the state provider plugin. |
| 147 | * @throws IOException |
| 148 | * If we can't read the file, if it doesn't exist, is not |
| 149 | * recognized, or if the version of the file does not match the |
| 150 | * expected providerVersion. |
| 151 | */ |
| 152 | public HistoryTreeBackend(@NonNull String ssid, @NonNull File existingStateFile, int providerVersion) |
| 153 | throws IOException { |
| 154 | fSsid = ssid; |
| 155 | fSht = initializeSHT(existingStateFile, providerVersion); |
| 156 | fFinishedBuilding = true; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * New-tree initializer for the History Tree wrapped by this backend. Can be |
| 161 | * overriden to use different implementations. |
| 162 | * |
| 163 | * @param conf |
| 164 | * The HTConfig configuration object |
| 165 | * @return The new history tree |
| 166 | * @throws IOException |
| 167 | * If there was a problem during creation |
| 168 | */ |
| 169 | @VisibleForTesting |
| 170 | protected @NonNull IHistoryTree initializeSHT(@NonNull HTConfig conf) throws IOException { |
| 171 | return HistoryTreeFactory.createHistoryTree(conf); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Existing-tree initializer for the History Tree wrapped by this backend. |
| 176 | * Can be overriden to use different implementations. |
| 177 | * |
| 178 | * @param existingStateFile |
| 179 | * The file to open |
| 180 | * @param providerVersion |
| 181 | * The expected state provider version |
| 182 | * @return The history tree opened from the given file |
| 183 | * @throws IOException |
| 184 | * If there was a problem during creation |
| 185 | */ |
| 186 | @VisibleForTesting |
| 187 | protected @NonNull IHistoryTree initializeSHT(@NonNull File existingStateFile, int providerVersion) throws IOException { |
| 188 | return HistoryTreeFactory.createFromFile(existingStateFile.toPath(), providerVersion); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Get the History Tree built by this backend. |
| 193 | * |
| 194 | * Note: Do not override this method. If you want to extend the class to use |
| 195 | * a different History Tree implementation, override both variants of |
| 196 | * {@link #initializeSHT} instead. |
| 197 | * |
| 198 | * @return The history tree |
| 199 | */ |
| 200 | protected final @NonNull IHistoryTree getSHT() { |
| 201 | return fSht; |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public String getSSID() { |
| 206 | return fSsid; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public long getStartTime() { |
| 211 | return getSHT().getTreeStart(); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public long getEndTime() { |
| 216 | return getSHT().getTreeEnd(); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void insertPastState(long stateStartTime, long stateEndTime, |
| 221 | int quark, ITmfStateValue value) throws TimeRangeException { |
| 222 | HTInterval interval = new HTInterval(stateStartTime, stateEndTime, |
| 223 | quark, (TmfStateValue) value); |
| 224 | |
| 225 | /* Start insertions at the "latest leaf" */ |
| 226 | getSHT().insertInterval(interval); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void finishedBuilding(long endTime) { |
| 231 | getSHT().closeTree(endTime); |
| 232 | fFinishedBuilding = true; |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public FileInputStream supplyAttributeTreeReader() { |
| 237 | return getSHT().supplyATReader(); |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public File supplyAttributeTreeWriterFile() { |
| 242 | return getSHT().supplyATWriterFile(); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public long supplyAttributeTreeWriterFilePosition() { |
| 247 | return getSHT().supplyATWriterFilePos(); |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public void removeFiles() { |
| 252 | getSHT().deleteFile(); |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public void dispose() { |
| 257 | if (fFinishedBuilding) { |
| 258 | LOGGER.info(() -> "[HistoryTreeBackend:ClosingFile] size=" + getSHT().getFileSize()); //$NON-NLS-1$ |
| 259 | getSHT().closeFile(); |
| 260 | } else { |
| 261 | /* |
| 262 | * The build is being interrupted, delete the file we partially |
| 263 | * built since it won't be complete, so shouldn't be re-used in the |
| 264 | * future (.deleteFile() will close the file first) |
| 265 | */ |
| 266 | getSHT().deleteFile(); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public void doQuery(List<ITmfStateInterval> stateInfo, long t) |
| 272 | throws TimeRangeException, StateSystemDisposedException { |
| 273 | checkValidTime(t); |
| 274 | |
| 275 | /* Queue is a stack of nodes containing nodes intersecting t */ |
| 276 | Deque<HTNode> queue = new LinkedList<>(); |
| 277 | |
| 278 | /* We start by reading the information in the root node */ |
| 279 | queue.add(getSHT().getRootNode()); |
| 280 | |
| 281 | /* Then we follow the down in the relevant children */ |
| 282 | try { |
| 283 | while (!queue.isEmpty()) { |
| 284 | HTNode currentNode = queue.pop(); |
| 285 | if (currentNode.getNodeType() == HTNode.NodeType.CORE) { |
| 286 | /*Here we add the relevant children nodes for BFS*/ |
| 287 | queue.addAll(getSHT().selectNextChildren((ParentNode) currentNode, t)); |
| 288 | } |
| 289 | currentNode.writeInfoFromNode(stateInfo, t); |
| 290 | } |
| 291 | } catch (ClosedChannelException e) { |
| 292 | throw new StateSystemDisposedException(e); |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * The stateInfo should now be filled with everything needed, we pass |
| 297 | * the control back to the State System. |
| 298 | */ |
| 299 | } |
| 300 | |
| 301 | @Override |
| 302 | public ITmfStateInterval doSingularQuery(long t, int attributeQuark) |
| 303 | throws TimeRangeException, StateSystemDisposedException { |
| 304 | try { |
| 305 | return getRelevantInterval(t, attributeQuark); |
| 306 | } catch (ClosedChannelException e) { |
| 307 | throw new StateSystemDisposedException(e); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | private void checkValidTime(long t) { |
| 312 | long startTime = getStartTime(); |
| 313 | long endTime = getEndTime(); |
| 314 | if (t < startTime || t > endTime) { |
| 315 | throw new TimeRangeException(String.format("%s Time:%d, Start:%d, End:%d", //$NON-NLS-1$ |
| 316 | fSsid, t, startTime, endTime)); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Inner method to find the interval in the tree containing the requested |
| 322 | * key/timestamp pair, wherever in which node it is. |
| 323 | */ |
| 324 | private HTInterval getRelevantInterval(long t, int key) |
| 325 | throws TimeRangeException, ClosedChannelException { |
| 326 | checkValidTime(t); |
| 327 | |
| 328 | Deque<HTNode> queue = new LinkedList<>(); |
| 329 | queue.add(getSHT().getRootNode()); |
| 330 | HTInterval interval = null; |
| 331 | while (interval == null && !queue.isEmpty()) { |
| 332 | HTNode currentNode = queue.pop(); |
| 333 | if (currentNode.getNodeType() == HTNode.NodeType.CORE) { |
| 334 | queue.addAll(getSHT().selectNextChildren((ParentNode) currentNode, t)); |
| 335 | } |
| 336 | interval = currentNode.getRelevantInterval(key, t); |
| 337 | } |
| 338 | return interval; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Return the size of the tree history file |
| 343 | * |
| 344 | * @return The current size of the history file in bytes |
| 345 | */ |
| 346 | public long getFileSize() { |
| 347 | return getSHT().getFileSize(); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Return the average node usage as a percentage (between 0 and 100) |
| 352 | * |
| 353 | * @return Average node usage % |
| 354 | */ |
| 355 | public int getAverageNodeUsage() { |
| 356 | HTNode node; |
| 357 | long total = 0; |
| 358 | long ret; |
| 359 | |
| 360 | try { |
| 361 | for (int seq = 0; seq < getSHT().getNodeCount(); seq++) { |
| 362 | node = getSHT().readNode(seq); |
| 363 | total += node.getNodeUsagePercent(); |
| 364 | } |
| 365 | } catch (ClosedChannelException e) { |
| 366 | Activator.getDefault().logError(e.getMessage(), e); |
| 367 | } |
| 368 | |
| 369 | ret = total / getSHT().getNodeCount(); |
| 370 | /* The return value should be a percentage */ |
| 371 | if (ret < 0 || ret > 100) { |
| 372 | throw new IllegalStateException("Average node usage is not a percentage: " + ret); //$NON-NLS-1$ |
| 373 | } |
| 374 | return (int) ret; |
| 375 | } |
| 376 | |
| 377 | } |