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