| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012, 2013 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 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.historytree; |
| 14 | |
| 15 | import java.io.File; |
| 16 | import java.io.IOException; |
| 17 | import java.util.concurrent.ArrayBlockingQueue; |
| 18 | import java.util.concurrent.BlockingQueue; |
| 19 | |
| 20 | import org.eclipse.linuxtools.internal.tmf.core.Activator; |
| 21 | import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException; |
| 22 | import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue; |
| 23 | import org.eclipse.linuxtools.tmf.core.statevalue.TmfStateValue; |
| 24 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; |
| 25 | |
| 26 | /** |
| 27 | * Variant of the HistoryTreeBackend which runs all the interval-insertion logic |
| 28 | * in a separate thread. |
| 29 | * |
| 30 | * @author alexmont |
| 31 | * |
| 32 | */ |
| 33 | public final class ThreadedHistoryTreeBackend extends HistoryTreeBackend |
| 34 | implements Runnable { |
| 35 | |
| 36 | private BlockingQueue<HTInterval> intervalQueue; |
| 37 | private final Thread shtThread; |
| 38 | |
| 39 | /** |
| 40 | * New state history constructor |
| 41 | * |
| 42 | * Note that it usually doesn't make sense to use a Threaded HT if you're |
| 43 | * opening an existing state-file, but you know what you're doing... |
| 44 | * |
| 45 | * @param newStateFile |
| 46 | * The name of the history file that will be created. Should end |
| 47 | * in ".ht" |
| 48 | * @param blockSize |
| 49 | * The size of the blocks in the file |
| 50 | * @param maxChildren |
| 51 | * The maximum number of children allowed for each core node |
| 52 | * @param startTime |
| 53 | * The earliest timestamp stored in the history |
| 54 | * @param providerVersion |
| 55 | * Version of of the state provider. We will only try to reopen |
| 56 | * existing files if this version matches the one in the |
| 57 | * framework. |
| 58 | * @param queueSize |
| 59 | * The size of the interval insertion queue. 2000 - 10000 usually |
| 60 | * works well |
| 61 | * @throws IOException |
| 62 | * If there was a problem opening the history file for writing |
| 63 | */ |
| 64 | public ThreadedHistoryTreeBackend(File newStateFile, int blockSize, |
| 65 | int maxChildren, long startTime, int providerVersion, int queueSize) |
| 66 | throws IOException { |
| 67 | super(newStateFile, blockSize, maxChildren, providerVersion, startTime); |
| 68 | |
| 69 | intervalQueue = new ArrayBlockingQueue<HTInterval>(queueSize); |
| 70 | shtThread = new Thread(this, "History Tree Thread"); //$NON-NLS-1$ |
| 71 | shtThread.start(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * New State History constructor. This version provides default values for |
| 76 | * blockSize and maxChildren. |
| 77 | * |
| 78 | * @param newStateFile |
| 79 | * The name of the history file that will be created. Should end |
| 80 | * in ".ht" |
| 81 | * @param startTime |
| 82 | * The earliest timestamp stored in the history |
| 83 | * @param providerVersion |
| 84 | * Version of of the state provider. We will only try to reopen |
| 85 | * existing files if this version matches the one in the |
| 86 | * framework. |
| 87 | * @param queueSize |
| 88 | * The size of the interval insertion queue. 2000 - 10000 usually |
| 89 | * works well |
| 90 | * @throws IOException |
| 91 | * If there was a problem opening the history file for writing |
| 92 | */ |
| 93 | public ThreadedHistoryTreeBackend(File newStateFile, long startTime, |
| 94 | int providerVersion, int queueSize) throws IOException { |
| 95 | super(newStateFile, providerVersion, startTime); |
| 96 | |
| 97 | intervalQueue = new ArrayBlockingQueue<HTInterval>(queueSize); |
| 98 | shtThread = new Thread(this, "History Tree Thread"); //$NON-NLS-1$ |
| 99 | shtThread.start(); |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * The Threaded version does not specify an "existing file" constructor, |
| 104 | * since the history is already built (and we only use the other thread |
| 105 | * during building). Just use a plain HistoryTreeProvider in this case. |
| 106 | * |
| 107 | * TODO but what about streaming?? |
| 108 | */ |
| 109 | |
| 110 | @Override |
| 111 | public void insertPastState(long stateStartTime, long stateEndTime, |
| 112 | int quark, ITmfStateValue value) throws TimeRangeException { |
| 113 | /* |
| 114 | * Here, instead of directly inserting the elements in the History Tree |
| 115 | * underneath, we'll put them in the Queue. They will then be taken and |
| 116 | * processed by the other thread executing the run() method. |
| 117 | */ |
| 118 | HTInterval interval = new HTInterval(stateStartTime, stateEndTime, |
| 119 | quark, (TmfStateValue) value); |
| 120 | try { |
| 121 | intervalQueue.put(interval); |
| 122 | } catch (InterruptedException e) { |
| 123 | Activator.logError("State system interrupted", e); //$NON-NLS-1$ |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void finishedBuilding(long endTime) { |
| 129 | /* |
| 130 | * We need to commit everything in the History Tree and stop the |
| 131 | * standalone thread before returning to the StateHistorySystem. (SHS |
| 132 | * will then write the Attribute Tree to the file, that must not happen |
| 133 | * at the same time we are writing the last nodes!) |
| 134 | */ |
| 135 | |
| 136 | stopRunningThread(endTime); |
| 137 | isFinishedBuilding = true; |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void dispose() { |
| 143 | if (!isFinishedBuilding) { |
| 144 | stopRunningThread(TmfTimestamp.PROJECT_IS_CANNED.getValue()); |
| 145 | } |
| 146 | /* |
| 147 | * isFinishedBuilding remains false, so the superclass will ask the |
| 148 | * back-end to delete the file. |
| 149 | */ |
| 150 | super.dispose(); |
| 151 | } |
| 152 | |
| 153 | private void stopRunningThread(long endTime) { |
| 154 | if (!shtThread.isAlive()) { |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Send a "poison pill" in the queue, then wait for the HT to finish |
| 160 | * its closeTree() |
| 161 | */ |
| 162 | try { |
| 163 | HTInterval pill = new HTInterval(-1, endTime, -1, TmfStateValue.nullValue()); |
| 164 | intervalQueue.put(pill); |
| 165 | shtThread.join(); |
| 166 | } catch (TimeRangeException e) { |
| 167 | Activator.logError("Error closing state system", e); //$NON-NLS-1$ |
| 168 | } catch (InterruptedException e) { |
| 169 | Activator.logError("State system interrupted", e); //$NON-NLS-1$ |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void run() { |
| 175 | if (intervalQueue == null) { |
| 176 | Activator.logError("Cannot start the storage backend without its interval queue."); //$NON-NLS-1$ |
| 177 | return; |
| 178 | } |
| 179 | HTInterval currentInterval; |
| 180 | try { |
| 181 | currentInterval = intervalQueue.take(); |
| 182 | while (currentInterval.getStartTime() != -1) { |
| 183 | /* Send the interval to the History Tree */ |
| 184 | sht.insertInterval(currentInterval); |
| 185 | currentInterval = intervalQueue.take(); |
| 186 | } |
| 187 | assert (currentInterval.getAttribute() == -1); |
| 188 | /* |
| 189 | * We've been told we're done, let's write down everything and quit. |
| 190 | * The end time of this "signal interval" is actually correct. |
| 191 | */ |
| 192 | sht.closeTree(currentInterval.getEndTime()); |
| 193 | return; |
| 194 | } catch (InterruptedException e) { |
| 195 | /* We've been interrupted abnormally */ |
| 196 | Activator.logError("State History Tree interrupted!", e); //$NON-NLS-1$ |
| 197 | } catch (TimeRangeException e) { |
| 198 | /* This also should not happen */ |
| 199 | Activator.logError("Error starting the state system", e); //$NON-NLS-1$ |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | } |