Re-structure LTTng sub-project as per the Linux Tools guidelines
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / statistics / model / StatisticsData.java
1 /*******************************************************************************
2 * Copyright (c) 2010 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Francois Godin (copelnug@gmail.com) - Initial design and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.lttng.ui.views.statistics.model;
14
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.eclipse.linuxtools.lttng.core.event.LttngEvent;
21 import org.eclipse.linuxtools.lttng.core.state.model.LttngTraceState;
22
23 /**
24 * <h4>Base class for the statistics storage.</h4>
25 * <p>
26 * It allow to implement a tree structure while avoiding the need to run through
27 * the tree each time you need to add a node at a given place.
28 * </p>
29 */
30 public abstract class StatisticsData {
31 /**
32 * <h4>Define values that can be used like a C++ enumeration.</h4>
33 * <p>
34 * The values can be used with binary "or" and "and" to mix them.
35 * </p>
36 */
37 public static class Values {
38 /**
39 * <h4>Indicate the cpu time</h4>
40 * <p>
41 * The actual time the cpu as passed in this state making calculations.
42 * </p>
43 */
44 public static final int CPU_TIME = 1;
45 /**
46 * <h4>Indicate the cumulative cpu time</h4>
47 * <p>
48 * Include the time the cpu as passed in this state and substate.
49 * </p>
50 * <p>
51 * Example:
52 * <ul>
53 * <li>PID:1, Mode:USER_MODE</li>
54 * <ul>
55 * <li>PID:1, Mode:SYSCALL</li>
56 * </ul>
57 * <li>PID:2, Mode:USER_MODE</li>
58 * </ul>
59 * </p>
60 * <p>
61 * In this example, the cumulative cpu time for "PID:1, Mode:USER_MODE"
62 * would be equal to its cpu time plus the cpu time of
63 * "PID:1, Mode:SYSCALL".
64 * </p>
65 * TODO Validate values. Not tested in LTTv. TODO Validate description.
66 */
67 public static final int CUMULATIVE_CPU_TIME = 2;
68 /**
69 * <h4>Elapsed time</h4>
70 * <p>
71 * Description...
72 * </p>
73 * TODO Give a correct description.
74 */
75 public static final int ELAPSED_TIME = 4;
76 /**
77 * <h4>State cumulative cpu time</h4>
78 * <p>
79 * Description...
80 * </p>
81 * TODO Give a correct description.
82 */
83 public static final int STATE_CUMULATIVE_CPU_TIME = 8;
84 }
85
86 /**
87 * <h4>String builder used to merge string with more efficacy.</h4>
88 */
89 protected static StringBuilder fBuilder = new StringBuilder();
90 /**
91 * <h4>Identification of the root.</h4>
92 */
93 public static final FixedArray ROOT = new FixedArray(-1);
94
95 /**
96 * <h4>Function to merge many string with more efficacy.</h4>
97 *
98 * @param strings
99 * Strings to merge.
100 * @return A new string containing all the strings.
101 */
102 protected synchronized static String mergeString(String... strings) {
103 fBuilder.setLength(0);
104 for (String s : strings)
105 fBuilder.append(s);
106 return fBuilder.toString();
107 }
108
109 /**
110 * <h4>Define what child a node can have.</h4>
111 * <p>
112 * The management and usage of this map is done by subclass.
113 * </p>
114 * <p>
115 * HashSet are always faster than TreeSet.
116 * </p>
117 */
118 private Map<Integer, Set<Integer>> fKeys;
119 /**
120 * <h4>The nodes in the tree.</f4>
121 */
122 private HashMap<FixedArray, StatisticsTreeNode> fNodes;
123
124 /**
125 * <h4>Constructor.</h4>
126 */
127 public StatisticsData() {
128 fNodes = new HashMap<FixedArray, StatisticsTreeNode>();
129 fKeys = new HashMap<Integer, Set<Integer>>();
130 }
131
132 /**
133 * <h4>Indicate the end of the traceset</4>
134 * <p>
135 * Can be used to trigger necessary calculations.
136 * </p>
137 *
138 * @param event
139 * Event receive (May have timestamp of 0).
140 * @param traceState
141 * State of the trace at that moment.
142 */
143 public abstract void endTraceset(LttngEvent event, LttngTraceState traceState);
144
145 /**
146 * <h4>Get a node.</h4>
147 *
148 * @param path
149 * Path to the node.
150 * @return The node or null.
151 */
152 public StatisticsTreeNode get(final FixedArray path) {
153 return fNodes.get(path);
154 }
155
156 /**
157 * <h4>Put a node.</h4>
158 *
159 * @param path
160 * Path to the node.
161 * @param node
162 * Node to put.
163 * @return node if replaced.
164 */
165 public StatisticsTreeNode put(final FixedArray path, StatisticsTreeNode node) {
166 return fNodes.put(path, node);
167 }
168
169 /**
170 * <h4>Get the children of a node.</h4>
171 *
172 * @param path
173 * Path to the node.
174 * @return Collection containing the children.
175 */
176 public abstract Collection<StatisticsTreeNode> getChildren(final FixedArray path);
177
178 /**
179 * <h4>Get the map of existing elements of path classified by parent.</h4>
180 *
181 * @return The map.
182 */
183 protected Map<Integer, Set<Integer>> getKeys() {
184 return fKeys;
185 }
186
187 /**
188 * <h4>Get or create a node.</h4>
189 *
190 * @param path
191 * Path to the node.
192 * @return The node.
193 */
194 public StatisticsTreeNode getOrCreate(final FixedArray path) {
195 StatisticsTreeNode current = fNodes.get(path);
196 if (current == null) {
197 registerName(path);
198 current = new StatisticsTreeNode(path, this);
199 fNodes.put(path, current);
200 }
201 return current;
202 }
203
204 /**
205 * <h4>Get the parent of a node.</h4>
206 *
207 * @param path
208 * Path to the node.
209 * @return Parent node or null.
210 */
211 public StatisticsTreeNode getParent(final FixedArray path) {
212 if (path.size() == 1) {
213 if (path.equals(ROOT))
214 return null;
215 else
216 return get(ROOT);
217 }
218 // TODO Get or GetOrCreate?
219 return get(path.subArray(0, path.size() - 1));
220 }
221
222 /**
223 * <h4>Increase some values.</h4>
224 * <p>
225 * Values is an binary or operation on the desired values between
226 * {@link Values#CPU_TIME}, {@link Values#CUMULATIVE_CPU_TIME},
227 * {@link Values#ELAPSED_TIME} and {@link Values#STATE_CUMULATIVE_CPU_TIME}
228 * .
229 *
230 * @param event
231 * Current event.
232 * @param traceState
233 * State of the trace at that moment.
234 * @param values
235 * Values desired.
236 */
237 public abstract void increase(LttngEvent event, LttngTraceState traceState, int values);
238
239 /**
240 * <h4>Register an event.</h4>
241 * <p>
242 * This method must be implemented by subclass.
243 * </p>
244 *
245 * @param event
246 * Event to process.
247 * @param traceState
248 * State of the trace at the moment of the event.
249 */
250 public abstract void registerEvent(LttngEvent event, LttngTraceState traceState);
251
252 /**
253 * <h4>Register that a new node was created.</h4>
254 * <p>
255 * Must make sure the {@link #getChildren(FixedArray)} on the parent node
256 * will return the newly created node.
257 * </p>
258 *
259 * @param path
260 * Path of the new node.
261 */
262 protected abstract void registerName(final FixedArray path);
263
264 /**
265 * <h4>Reset a node.</h4>
266 * <p>
267 * Work recursively.
268 * </p>
269 *
270 * @param path
271 * Path to the node.
272 */
273 public void reset(final FixedArray path) {
274 for (StatisticsTreeNode node : getChildren(path)) {
275 reset(node.getPath());
276 fNodes.remove(node.getPath());
277 }
278 }
279
280 /**
281 * Indicate that the process is finishing.
282 *
283 * @param event
284 * The event indicating the end of the process.
285 * @param traceState
286 * State of the trace at that moment.
287 */
288 public abstract void process_exit(LttngEvent event, LttngTraceState traceState);
289 }
This page took 0.037455 seconds and 5 git commands to generate.