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>
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
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.backends
.historytree
;
16 import java
.io
.IOException
;
17 import java
.util
.concurrent
.ArrayBlockingQueue
;
18 import java
.util
.concurrent
.BlockingQueue
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.ITmfStateValue
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.TmfStateValue
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
26 * Variant of the HistoryTreeBackend which runs all the interval-insertion logic
27 * in a separate thread.
32 public final class ThreadedHistoryTreeBackend
extends HistoryTreeBackend
38 * protected final StateHistoryTree sht;
41 private BlockingQueue
<HTInterval
> intervalQueue
;
42 private final Thread shtThread
;
45 * New state history constructor
47 * Note that it usually doesn't make sense to use a Threaded HT if you're
48 * opening an existing state-file, but you know what you're doing...
51 * The name of the history file that will be created. Should end
54 * The size of the blocks in the file
56 * The maximum number of children allowed for each core node
58 * The earliest timestamp stored in the history
59 * @param providerVersion
60 * Version of of the state provider. We will only try to reopen
61 * existing files if this version matches the one in the
64 * The size of the interval insertion queue. 2000 - 10000 usually
67 * If there was a problem opening the history file for writing
69 public ThreadedHistoryTreeBackend(File newStateFile
, int blockSize
,
70 int maxChildren
, long startTime
, int providerVersion
, int queueSize
)
72 super(newStateFile
, blockSize
, maxChildren
, providerVersion
, startTime
);
74 intervalQueue
= new ArrayBlockingQueue
<HTInterval
>(queueSize
);
75 shtThread
= new Thread(this, "History Tree Thread"); //$NON-NLS-1$
80 * New State History constructor. This version provides default values for
81 * blockSize and maxChildren.
84 * The name of the history file that will be created. Should end
87 * The earliest timestamp stored in the history
88 * @param providerVersion
89 * Version of of the state provider. We will only try to reopen
90 * existing files if this version matches the one in the
93 * The size of the interval insertion queue. 2000 - 10000 usually
96 * If there was a problem opening the history file for writing
98 public ThreadedHistoryTreeBackend(File newStateFile
, long startTime
,
99 int providerVersion
, int queueSize
) throws IOException
{
100 super(newStateFile
, providerVersion
, startTime
);
102 intervalQueue
= new ArrayBlockingQueue
<HTInterval
>(queueSize
);
103 shtThread
= new Thread(this, "History Tree Thread"); //$NON-NLS-1$
108 * The Threaded version does not specify an "existing file" constructor,
109 * since the history is already built (and we only use the other thread
110 * during building). Just use a plain HistoryTreeProvider in this case.
112 * TODO but what about streaming??
116 public void insertPastState(long stateStartTime
, long stateEndTime
,
117 int quark
, ITmfStateValue value
) throws TimeRangeException
{
119 * Here, instead of directly inserting the elements in the History Tree
120 * underneath, we'll put them in the Queue. They will then be taken and
121 * processed by the other thread executing the run() method.
123 HTInterval interval
= new HTInterval(stateStartTime
, stateEndTime
,
124 quark
, (TmfStateValue
) value
);
126 intervalQueue
.put(interval
);
127 } catch (InterruptedException e
) {
128 /* We should not get interrupted here */
129 System
.out
.println("State system got interrupted!"); //$NON-NLS-1$
135 public void finishedBuilding(long endTime
) {
137 * We need to commit everything in the History Tree and stop the
138 * standalone thread before returning to the StateHistorySystem. (SHS
139 * will then write the Attribute Tree to the file, that must not happen
140 * at the same time we are writing the last nodes!)
143 stopRunningThread(endTime
);
144 isFinishedBuilding
= true;
149 public void dispose() {
150 if (!isFinishedBuilding
) {
151 stopRunningThread(TmfTimestamp
.PROJECT_IS_CANNED
.getValue());
154 * isFinishedBuilding remains false, so the superclass will ask the
155 * back-end to delete the file.
160 private void stopRunningThread(long endTime
) {
161 if (!shtThread
.isAlive()) {
166 * Send a "poison pill" in the queue, then wait for the HT to finish
170 HTInterval pill
= new HTInterval(-1, endTime
, -1, TmfStateValue
.nullValue());
171 intervalQueue
.put(pill
);
173 } catch (TimeRangeException e
) {
175 } catch (InterruptedException e
) {
182 if (intervalQueue
== null) {
183 System
.err
.println("Cannot start the storage backend without its interval queue."); //$NON-NLS-1$
186 HTInterval currentInterval
;
188 currentInterval
= intervalQueue
.take();
189 while (currentInterval
.getStartTime() != -1) {
190 /* Send the interval to the History Tree */
191 sht
.insertInterval(currentInterval
);
192 currentInterval
= intervalQueue
.take();
194 assert (currentInterval
.getAttribute() == -1);
196 * We've been told we're done, let's write down everything and quit.
197 * The end time of this "signal interval" is actually correct.
199 sht
.closeTree(currentInterval
.getEndTime());
201 } catch (InterruptedException e
) {
202 /* We've been interrupted abnormally */
203 System
.out
.println("State History Tree interrupted!"); //$NON-NLS-1$
205 } catch (TimeRangeException e
) {
206 /* This also should not happen */