1 /*******************************************************************************
2 * Copyright (c) 2012 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
60 * The size of the interval insertion queue. 2000 - 10000 usually
63 * If there was a problem opening the history file for writing
65 public ThreadedHistoryTreeBackend(File newStateFile
, int blockSize
,
66 int maxChildren
, long startTime
, int queueSize
) throws IOException
{
67 super(newStateFile
, blockSize
, maxChildren
, startTime
);
69 intervalQueue
= new ArrayBlockingQueue
<HTInterval
>(queueSize
);
70 shtThread
= new Thread(this, "History Tree Thread"); //$NON-NLS-1$
75 * New State History constructor. This version provides default values for
76 * blockSize and maxChildren.
79 * The name of the history file that will be created. Should end
82 * The earliest timestamp stored in the history
84 * The size of the interval insertion queue. 2000 - 10000 usually
87 * If there was a problem opening the history file for writing
89 public ThreadedHistoryTreeBackend(File newStateFile
, long startTime
,
90 int queueSize
) throws IOException
{
91 super(newStateFile
, startTime
);
93 intervalQueue
= new ArrayBlockingQueue
<HTInterval
>(queueSize
);
94 shtThread
= new Thread(this, "History Tree Thread"); //$NON-NLS-1$
99 * The Threaded version does not specify an "existing file" constructor,
100 * since the history is already built (and we only use the other thread
101 * during building). Just use a plain HistoryTreeProvider in this case.
103 * TODO but what about streaming??
107 public void insertPastState(long stateStartTime
, long stateEndTime
,
108 int quark
, ITmfStateValue value
) throws TimeRangeException
{
110 * Here, instead of directly inserting the elements in the History Tree
111 * underneath, we'll put them in the Queue. They will then be taken and
112 * processed by the other thread executing the run() method.
114 HTInterval interval
= new HTInterval(stateStartTime
, stateEndTime
,
115 quark
, (TmfStateValue
) value
);
117 intervalQueue
.put(interval
);
118 } catch (InterruptedException e
) {
119 /* We should not get interrupted here */
120 System
.out
.println("State system got interrupted!"); //$NON-NLS-1$
126 public void finishedBuilding(long endTime
) {
128 * We need to commit everything in the History Tree and stop the
129 * standalone thread before returning to the StateHistorySystem. (SHS
130 * will then write the Attribute Tree to the file, that must not happen
131 * at the same time we are writing the last nodes!)
134 stopRunningThread(endTime
);
135 isFinishedBuilding
= true;
140 public void dispose() {
141 if (!isFinishedBuilding
) {
142 stopRunningThread(TmfTimestamp
.PROJECT_IS_CANNED
.getValue());
145 * isFinishedBuilding remains false, so the superclass will ask the
146 * back-end to delete the file.
151 private void stopRunningThread(long endTime
) {
152 if (!shtThread
.isAlive()) {
157 * Send a "poison pill" in the queue, then wait for the HT to finish
161 HTInterval pill
= new HTInterval(-1, endTime
, -1, TmfStateValue
.nullValue());
162 intervalQueue
.put(pill
);
164 } catch (TimeRangeException e
) {
166 } catch (InterruptedException e
) {
173 if (intervalQueue
== null) {
174 System
.err
.println("Cannot start the storage backend without its interval queue."); //$NON-NLS-1$
177 HTInterval currentInterval
;
179 currentInterval
= intervalQueue
.take();
180 while (currentInterval
.getStartTime() != -1) {
181 /* Send the interval to the History Tree */
182 sht
.insertInterval(currentInterval
);
183 currentInterval
= intervalQueue
.take();
185 assert (currentInterval
.getAttribute() == -1);
187 * We've been told we're done, let's write down everything and quit.
188 * The end time of this "signal interval" is actually correct.
190 sht
.closeTree(currentInterval
.getEndTime());
192 } catch (InterruptedException e
) {
193 /* We've been interrupted abnormally */
194 System
.out
.println("State History Tree interrupted!"); //$NON-NLS-1$
196 } catch (TimeRangeException e
) {
197 /* This also should not happen */