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