b85a4639fba233397dc3386a745c791bde0e8b4d
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / statesystem / backends / historytree / HistoryTreeBackend.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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.FileInputStream;
17 import java.io.IOException;
18 import java.io.PrintWriter;
19 import java.nio.channels.ClosedChannelException;
20 import java.util.List;
21
22 import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.IStateHistoryBackend;
23 import org.eclipse.linuxtools.tmf.core.exceptions.StateSystemDisposedException;
24 import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
25 import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
26 import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue;
27 import org.eclipse.linuxtools.tmf.core.statevalue.TmfStateValue;
28
29 /**
30 * History Tree backend for storing a state history. This is the basic version
31 * that runs in the same thread as the class creating it.
32 *
33 * @author Alexandre Montplaisir
34 *
35 */
36 public class HistoryTreeBackend implements IStateHistoryBackend {
37
38 /** The history tree that sits underneath */
39 protected final HistoryTree sht;
40
41 /** Indicates if the history tree construction is done */
42 protected boolean isFinishedBuilding = false;
43
44 /**
45 * Constructor for new history files. Use this when creating a new history
46 * from scratch.
47 *
48 * @param newStateFile
49 * The filename/location where to store the state history (Should
50 * end in .ht)
51 * @param blockSize
52 * The size of the blocks in the history file. This should be a
53 * multiple of 4096.
54 * @param maxChildren
55 * The maximum number of children each core node can have
56 * @param providerVersion
57 * Version of of the state provider. We will only try to reopen
58 * existing files if this version matches the one in the
59 * framework.
60 * @param startTime
61 * The earliest time stamp that will be stored in the history
62 * @throws IOException
63 * Thrown if we can't create the file for some reason
64 */
65 public HistoryTreeBackend(File newStateFile, int blockSize,
66 int maxChildren, int providerVersion, long startTime) throws IOException {
67 final HTConfig conf = new HTConfig(newStateFile, blockSize, maxChildren,
68 providerVersion, startTime);
69 sht = new HistoryTree(conf);
70 }
71
72 /**
73 * Constructor for new history files. Use this when creating a new history
74 * from scratch. This version supplies sane defaults for the configuration
75 * parameters.
76 *
77 * @param newStateFile
78 * The filename/location where to store the state history (Should
79 * end in .ht)
80 * @param providerVersion
81 * Version of of the state provider. We will only try to reopen
82 * existing files if this version matches the one in the
83 * framework.
84 * @param startTime
85 * The earliest time stamp that will be stored in the history
86 * @throws IOException
87 * Thrown if we can't create the file for some reason
88 */
89 public HistoryTreeBackend(File newStateFile, int providerVersion, long startTime)
90 throws IOException {
91 this(newStateFile, 64 * 1024, 50, providerVersion, startTime);
92 }
93
94 /**
95 * Existing history constructor. Use this to open an existing state-file.
96 *
97 * @param existingStateFile
98 * Filename/location of the history we want to load
99 * @param providerVersion
100 * Expected version of of the state provider plugin.
101 * @throws IOException
102 * If we can't read the file, if it doesn't exist, is not
103 * recognized, or if the version of the file does not match the
104 * expected providerVersion.
105 */
106 public HistoryTreeBackend(File existingStateFile, int providerVersion)
107 throws IOException {
108 sht = new HistoryTree(existingStateFile, providerVersion);
109 isFinishedBuilding = true;
110 }
111
112 @Override
113 public long getStartTime() {
114 return sht.getTreeStart();
115 }
116
117 @Override
118 public long getEndTime() {
119 return sht.getTreeEnd();
120 }
121
122 @Override
123 public void insertPastState(long stateStartTime, long stateEndTime,
124 int quark, ITmfStateValue value) throws TimeRangeException {
125 HTInterval interval = new HTInterval(stateStartTime, stateEndTime,
126 quark, (TmfStateValue) value);
127
128 /* Start insertions at the "latest leaf" */
129 sht.insertInterval(interval);
130 }
131
132 @Override
133 public void finishedBuilding(long endTime) {
134 sht.closeTree(endTime);
135 isFinishedBuilding = true;
136 }
137
138 @Override
139 public FileInputStream supplyAttributeTreeReader() {
140 return sht.supplyATReader();
141 }
142
143 @Override
144 public File supplyAttributeTreeWriterFile() {
145 return sht.supplyATWriterFile();
146 }
147
148 @Override
149 public long supplyAttributeTreeWriterFilePosition() {
150 return sht.supplyATWriterFilePos();
151 }
152
153 @Override
154 public void removeFiles() {
155 sht.deleteFile();
156 }
157
158 @Override
159 public void dispose() {
160 if (isFinishedBuilding) {
161 sht.closeFile();
162 } else {
163 /*
164 * The build is being interrupted, delete the file we partially
165 * built since it won't be complete, so shouldn't be re-used in the
166 * future (.deleteFile() will close the file first)
167 */
168 sht.deleteFile();
169 }
170 }
171
172 @Override
173 public void doQuery(List<ITmfStateInterval> stateInfo, long t)
174 throws TimeRangeException, StateSystemDisposedException {
175 if (!checkValidTime(t)) {
176 /* We can't possibly have information about this query */
177 throw new TimeRangeException();
178 }
179
180 /* We start by reading the information in the root node */
181 HTNode currentNode = sht.getRootNode();
182 currentNode.writeInfoFromNode(stateInfo, t);
183
184 /* Then we follow the branch down in the relevant children */
185 try {
186 while (currentNode.getNodeType() == HTNode.NodeType.CORE) {
187 currentNode = sht.selectNextChild((CoreNode) currentNode, t);
188 currentNode.writeInfoFromNode(stateInfo, t);
189 }
190 } catch (ClosedChannelException e) {
191 throw new StateSystemDisposedException(e);
192 }
193
194 /*
195 * The stateInfo should now be filled with everything needed, we pass
196 * the control back to the State System.
197 */
198 return;
199 }
200
201 @Override
202 public ITmfStateInterval doSingularQuery(long t, int attributeQuark)
203 throws TimeRangeException, StateSystemDisposedException {
204 return getRelevantInterval(t, attributeQuark);
205 }
206
207 @Override
208 public boolean checkValidTime(long t) {
209 return (t >= sht.getTreeStart() && t <= sht.getTreeEnd());
210 }
211
212 /**
213 * Inner method to find the interval in the tree containing the requested
214 * key/timestamp pair, wherever in which node it is.
215 *
216 * @param t
217 * @param key
218 * @return The node containing the information we want
219 */
220 private HTInterval getRelevantInterval(long t, int key)
221 throws TimeRangeException, StateSystemDisposedException {
222 if (!checkValidTime(t)) {
223 throw new TimeRangeException();
224 }
225
226 HTNode currentNode = sht.getRootNode();
227 HTInterval interval = currentNode.getRelevantInterval(key, t);
228
229 try {
230 while (interval == null && currentNode.getNodeType() == HTNode.NodeType.CORE) {
231 currentNode = sht.selectNextChild((CoreNode)currentNode, t);
232 interval = currentNode.getRelevantInterval(key, t);
233 }
234 } catch (ClosedChannelException e) {
235 throw new StateSystemDisposedException(e);
236 }
237 /*
238 * Since we should now have intervals at every attribute/timestamp
239 * combination, it should NOT be null here.
240 */
241 assert (interval != null);
242 return interval;
243 }
244
245 /**
246 * Return the size of the tree history file
247 *
248 * @return The current size of the history file in bytes
249 */
250 public long getFileSize() {
251 return sht.getFileSize();
252 }
253
254 /**
255 * Return the average node usage as a percentage (between 0 and 100)
256 *
257 * @return Average node usage %
258 */
259 public int getAverageNodeUsage() {
260 HTNode node;
261 long total = 0;
262 long ret;
263
264 try {
265 for (int seq = 0; seq < sht.getNodeCount(); seq++) {
266 node = sht.readNode(seq);
267 total += node.getNodeUsagePercent();
268 }
269 } catch (ClosedChannelException e) {
270 e.printStackTrace();
271 }
272
273 ret = total / sht.getNodeCount();
274 assert (ret >= 0 && ret <= 100);
275 return (int) ret;
276 }
277
278 @Override
279 public void debugPrint(PrintWriter writer) {
280 /* By default don't print out all the intervals */
281 this.debugPrint(writer, false);
282 }
283
284 /**
285 * The basic debugPrint method will print the tree structure, but not their
286 * contents.
287 *
288 * This method here print the contents (the intervals) as well.
289 *
290 * @param writer
291 * The PrintWriter to which the debug info will be written
292 * @param printIntervals
293 * Should we also print every contained interval individually?
294 */
295 public void debugPrint(PrintWriter writer, boolean printIntervals) {
296 /* Only used for debugging, shouldn't be externalized */
297 writer.println("------------------------------"); //$NON-NLS-1$
298 writer.println("State History Tree:\n"); //$NON-NLS-1$
299 writer.println(sht.toString());
300 writer.println("Average node utilization: " //$NON-NLS-1$
301 + this.getAverageNodeUsage());
302 writer.println(""); //$NON-NLS-1$
303
304 sht.debugPrintFullTree(writer, printIntervals);
305 }
306 }
This page took 0.038484 seconds and 4 git commands to generate.