lttng: Handle the case where the trace is empty
[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
6d08acca 21import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
a52fde77 22import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
18ab1d18 23import org.eclipse.linuxtools.tmf.core.statesystem.IStateHistoryBackend;
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
134 @Override
135 public void doQuery(List<ITmfStateInterval> stateInfo, long t)
136 throws TimeRangeException {
137 if (!checkValidTime(t)) {
138 /* We can't possibly have information about this query */
139 throw new TimeRangeException();
140 }
141
142 /* We start by reading the information in the root node */
143 // FIXME using CoreNode for now, we'll have to redo this part to handle
144 // different node types
145 CoreNode currentNode = sht.latestBranch.firstElement();
146 currentNode.writeInfoFromNode(stateInfo, t);
147
148 /* Then we follow the branch down in the relevant children */
149 while (currentNode.getNbChildren() > 0) {
150 currentNode = (CoreNode) sht.selectNextChild(currentNode, t);
151 currentNode.writeInfoFromNode(stateInfo, t);
152 }
153
154 /*
155 * The stateInfo should now be filled with everything needed, we pass
156 * the control back to the State System.
157 */
158 return;
159 }
160
161 @Override
162 public ITmfStateInterval doSingularQuery(long t, int attributeQuark)
163 throws TimeRangeException {
164 return getRelevantInterval(t, attributeQuark);
165 }
166
b255ae4b
AM
167 @Override
168 public boolean checkValidTime(long t) {
a52fde77
AM
169 return (t >= sht.getTreeStart() && t <= sht.getTreeEnd());
170 }
171
172 /**
173 * Inner method to find the interval in the tree containing the requested
174 * key/timestamp pair, wherever in which node it is.
175 *
176 * @param t
177 * @param key
178 * @return The node containing the information we want
179 */
180 private HTInterval getRelevantInterval(long t, int key)
181 throws TimeRangeException {
182 if (!checkValidTime(t)) {
183 throw new TimeRangeException();
184 }
185
186 // FIXME using CoreNode for now, we'll have to redo this part to handle
187 // different node types
188 CoreNode currentNode = sht.latestBranch.firstElement();
189 HTInterval interval = currentNode.getRelevantInterval(key, t);
190
191 while (interval == null && currentNode.getNbChildren() > 0) {
192 currentNode = (CoreNode) sht.selectNextChild(currentNode, t);
193 interval = currentNode.getRelevantInterval(key, t);
194 }
195 /*
196 * Since we should now have intervals at every attribute/timestamp
197 * combination, it should NOT be null here.
198 */
199 assert (interval != null);
200 return interval;
201 }
202
203 /**
204 * Return the size of the tree history file
205 *
206 * @return The current size of the history file in bytes
207 */
208 public long getFileSize() {
209 return sht.getFileSize();
210 }
211
212 /**
213 * Return the current depth of the tree, ie the number of node levels.
214 *
215 * @return The tree depth
216 */
217 public int getTreeDepth() {
218 return sht.latestBranch.size();
219 }
220
221 /**
222 * Return the average node usage as a percentage (between 0 and 100)
223 *
224 * @return Average node usage %
225 */
226 public int getAverageNodeUsage() {
227 HTNode node;
228 long total = 0;
229 long ret;
230
231 for (int seq = 0; seq < sht.getNodeCount(); seq++) {
232 node = treeIO.readNode(seq);
233 total += node.getNodeUsagePRC();
234 }
235
236 ret = total / sht.getNodeCount();
237 assert (ret >= 0 && ret <= 100);
238 return (int) ret;
239 }
240
241 @Override
242 public void debugPrint(PrintWriter writer) {
243 /* By default don't print out all the intervals */
244 this.debugPrint(writer, false);
245 }
246
247 /**
b255ae4b
AM
248 * The basic debugPrint method will print the tree structure, but not their
249 * contents.
a52fde77
AM
250 *
251 * This method here print the contents (the intervals) as well.
252 *
253 * @param writer
254 * @param printIntervals
255 */
256 public void debugPrint(PrintWriter writer, boolean printIntervals) {
257 /* Only used for debugging, shouldn't be externalized */
258 writer.println("------------------------------"); //$NON-NLS-1$
259 writer.println("State History Tree:\n"); //$NON-NLS-1$
260 writer.println(sht.toString());
261 writer.println("Average node utilization: " //$NON-NLS-1$
262 + this.getAverageNodeUsage());
263 writer.println(""); //$NON-NLS-1$
264
265 sht.debugPrintFullTree(writer, printIntervals);
266 }
267}
This page took 0.038386 seconds and 5 git commands to generate.