Update State History construction tests to the newest test trace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / statesystem / backend / 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
13package org.eclipse.linuxtools.tmf.core.statesystem.backend.historytree;
14
15import java.io.File;
16import java.io.FileInputStream;
17import java.io.IOException;
18import java.io.PrintWriter;
19import java.util.List;
20
21import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
22import org.eclipse.linuxtools.tmf.core.statesystem.TimeRangeException;
23import org.eclipse.linuxtools.tmf.core.statesystem.helpers.IStateHistoryBackend;
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 *
83 * @param existingFileName Filename/location of the history we want to load
84 * @throws IOException If we can't read the file, if it doesn't exist or is not recognized
85 */
86 public HistoryTreeBackend(File existingStateFile) throws IOException {
87 sht = new HistoryTree(existingStateFile);
88 treeIO = sht.getTreeIO();
89 }
90
91 @Override
92 public long getStartTime() {
93 return sht.getTreeStart();
94 }
95
96 @Override
97 public long getEndTime() {
98 return sht.getTreeEnd();
99 }
100
101 @Override
102 public void insertPastState(long stateStartTime, long stateEndTime,
103 int quark, ITmfStateValue value) throws TimeRangeException {
104 HTInterval interval = new HTInterval(stateStartTime, stateEndTime,
105 quark, (TmfStateValue) value);
106
107 /* Start insertions at the "latest leaf" */
108 sht.insertInterval(interval);
109 }
110
111 @Override
112 public void finishedBuilding(long endTime) throws TimeRangeException {
113 sht.closeTree();
114 }
115
116 @Override
117 public FileInputStream supplyAttributeTreeReader() {
118 return treeIO.supplyATReader();
119 }
120
121 @Override
122 public File supplyAttributeTreeWriterFile() {
123 return treeIO.supplyATWriterFile();
124 }
125
126 @Override
127 public long supplyAttributeTreeWriterFilePosition() {
128 return treeIO.supplyATWriterFilePos();
129 }
130
131 @Override
132 public void doQuery(List<ITmfStateInterval> stateInfo, long t)
133 throws TimeRangeException {
134 if (!checkValidTime(t)) {
135 /* We can't possibly have information about this query */
136 throw new TimeRangeException();
137 }
138
139 /* We start by reading the information in the root node */
140 // FIXME using CoreNode for now, we'll have to redo this part to handle
141 // different node types
142 CoreNode currentNode = sht.latestBranch.firstElement();
143 currentNode.writeInfoFromNode(stateInfo, t);
144
145 /* Then we follow the branch down in the relevant children */
146 while (currentNode.getNbChildren() > 0) {
147 currentNode = (CoreNode) sht.selectNextChild(currentNode, t);
148 currentNode.writeInfoFromNode(stateInfo, t);
149 }
150
151 /*
152 * The stateInfo should now be filled with everything needed, we pass
153 * the control back to the State System.
154 */
155 return;
156 }
157
158 @Override
159 public ITmfStateInterval doSingularQuery(long t, int attributeQuark)
160 throws TimeRangeException {
161 return getRelevantInterval(t, attributeQuark);
162 }
163
164 /**
165 * Simple check to make sure the requested timestamps are within the borders
166 * of this tree.
167 *
168 * @param t
169 * The queried timestamp
170 * @return True if it's within range, false if not.
171 */
172 private boolean checkValidTime(long t) {
173 return (t >= sht.getTreeStart() && t <= sht.getTreeEnd());
174 }
175
176 /**
177 * Inner method to find the interval in the tree containing the requested
178 * key/timestamp pair, wherever in which node it is.
179 *
180 * @param t
181 * @param key
182 * @return The node containing the information we want
183 */
184 private HTInterval getRelevantInterval(long t, int key)
185 throws TimeRangeException {
186 if (!checkValidTime(t)) {
187 throw new TimeRangeException();
188 }
189
190 // FIXME using CoreNode for now, we'll have to redo this part to handle
191 // different node types
192 CoreNode currentNode = sht.latestBranch.firstElement();
193 HTInterval interval = currentNode.getRelevantInterval(key, t);
194
195 while (interval == null && currentNode.getNbChildren() > 0) {
196 currentNode = (CoreNode) sht.selectNextChild(currentNode, t);
197 interval = currentNode.getRelevantInterval(key, t);
198 }
199 /*
200 * Since we should now have intervals at every attribute/timestamp
201 * combination, it should NOT be null here.
202 */
203 assert (interval != null);
204 return interval;
205 }
206
207 /**
208 * Return the size of the tree history file
209 *
210 * @return The current size of the history file in bytes
211 */
212 public long getFileSize() {
213 return sht.getFileSize();
214 }
215
216 /**
217 * Return the current depth of the tree, ie the number of node levels.
218 *
219 * @return The tree depth
220 */
221 public int getTreeDepth() {
222 return sht.latestBranch.size();
223 }
224
225 /**
226 * Return the average node usage as a percentage (between 0 and 100)
227 *
228 * @return Average node usage %
229 */
230 public int getAverageNodeUsage() {
231 HTNode node;
232 long total = 0;
233 long ret;
234
235 for (int seq = 0; seq < sht.getNodeCount(); seq++) {
236 node = treeIO.readNode(seq);
237 total += node.getNodeUsagePRC();
238 }
239
240 ret = total / sht.getNodeCount();
241 assert (ret >= 0 && ret <= 100);
242 return (int) ret;
243 }
244
245 @Override
246 public void debugPrint(PrintWriter writer) {
247 /* By default don't print out all the intervals */
248 this.debugPrint(writer, false);
249 }
250
251 /**
252 * The basic debugPrint method will print the tree structure, but not
253 * their contents.
254 *
255 * This method here print the contents (the intervals) as well.
256 *
257 * @param writer
258 * @param printIntervals
259 */
260 public void debugPrint(PrintWriter writer, boolean printIntervals) {
261 /* Only used for debugging, shouldn't be externalized */
262 writer.println("------------------------------"); //$NON-NLS-1$
263 writer.println("State History Tree:\n"); //$NON-NLS-1$
264 writer.println(sht.toString());
265 writer.println("Average node utilization: " //$NON-NLS-1$
266 + this.getAverageNodeUsage());
267 writer.println(""); //$NON-NLS-1$
268
269 sht.debugPrintFullTree(writer, printIntervals);
270 }
271}
This page took 0.034419 seconds and 5 git commands to generate.