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