1 /*******************************************************************************
2 * Copyright (c) 2010 Ericsson
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
10 * Francois Godin (copelnug@gmail.com) - Initial design and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.lttng
.ui
.views
.statistics
.model
;
15 import java
.util
.Collection
;
16 import java
.util
.HashMap
;
20 import org
.eclipse
.linuxtools
.internal
.lttng
.core
.event
.LttngEvent
;
21 import org
.eclipse
.linuxtools
.internal
.lttng
.core
.state
.model
.LttngTraceState
;
24 * <h4>Base class for the statistics storage.</h4>
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.
30 public abstract class StatisticsData
{
32 * <h4>Define values that can be used like a C++ enumeration.</h4>
34 * The values can be used with binary "or" and "and" to mix them.
37 public static class Values
{
39 * <h4>Indicate the cpu time</h4>
41 * The actual time the cpu as passed in this state making calculations.
44 public static final int CPU_TIME
= 1;
46 * <h4>Indicate the cumulative cpu time</h4>
48 * Include the time the cpu as passed in this state and substate.
53 * <li>PID:1, Mode:USER_MODE</li>
55 * <li>PID:1, Mode:SYSCALL</li>
57 * <li>PID:2, Mode:USER_MODE</li>
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".
65 * TODO Validate values. Not tested in LTTv. TODO Validate description.
67 public static final int CUMULATIVE_CPU_TIME
= 2;
69 * <h4>Elapsed time</h4>
73 * TODO Give a correct description.
75 public static final int ELAPSED_TIME
= 4;
77 * <h4>State cumulative cpu time</h4>
81 * TODO Give a correct description.
83 public static final int STATE_CUMULATIVE_CPU_TIME
= 8;
87 * <h4>String builder used to merge string with more efficacy.</h4>
89 protected static final StringBuilder fBuilder
= new StringBuilder();
91 * <h4>Identification of the root.</h4>
93 public static final FixedArray ROOT
= new FixedArray(-1);
96 * <h4>Function to merge many string with more efficacy.</h4>
100 * @return A new string containing all the strings.
102 protected synchronized static String
mergeString(String
... strings
) {
103 fBuilder
.setLength(0);
104 for (String s
: strings
)
106 return fBuilder
.toString();
110 * <h4>Define what child a node can have.</h4>
112 * The management and usage of this map is done by subclass.
115 * HashSet are always faster than TreeSet.
118 private Map
<Integer
, Set
<Integer
>> fKeys
;
120 * <h4>The nodes in the tree.</f4>
122 private HashMap
<FixedArray
, StatisticsTreeNode
> fNodes
;
125 * <h4>Constructor.</h4>
127 public StatisticsData() {
128 fNodes
= new HashMap
<FixedArray
, StatisticsTreeNode
>();
129 fKeys
= new HashMap
<Integer
, Set
<Integer
>>();
133 * <h4>Indicate the end of the traceset</4>
135 * Can be used to trigger necessary calculations.
139 * Event receive (May have timestamp of 0).
141 * State of the trace at that moment.
143 public abstract void endTraceset(LttngEvent event
, LttngTraceState traceState
);
146 * <h4>Get a node.</h4>
150 * @return The node or null.
152 public StatisticsTreeNode
get(final FixedArray path
) {
153 return fNodes
.get(path
);
157 * <h4>Put a node.</h4>
163 * @return node if replaced.
165 public StatisticsTreeNode
put(final FixedArray path
, StatisticsTreeNode node
) {
166 return fNodes
.put(path
, node
);
170 * <h4>Get the children of a node.</h4>
174 * @return Collection containing the children.
176 public abstract Collection
<StatisticsTreeNode
> getChildren(final FixedArray path
);
179 * <h4>Get the map of existing elements of path classified by parent.</h4>
183 protected Map
<Integer
, Set
<Integer
>> getKeys() {
188 * <h4>Get or create a node.</h4>
194 public StatisticsTreeNode
getOrCreate(final FixedArray path
) {
195 StatisticsTreeNode current
= fNodes
.get(path
);
196 if (current
== null) {
198 current
= new StatisticsTreeNode(path
, this);
199 fNodes
.put(path
, current
);
205 * <h4>Get the parent of a node.</h4>
209 * @return Parent node or null.
211 public StatisticsTreeNode
getParent(final FixedArray path
) {
212 if (path
.size() == 1) {
213 if (path
.equals(ROOT
))
218 // TODO Get or GetOrCreate?
219 return get(path
.subArray(0, path
.size() - 1));
223 * <h4>Increase some values.</h4>
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}
233 * State of the trace at that moment.
237 public abstract void increase(LttngEvent event
, LttngTraceState traceState
, int values
);
240 * <h4>Register an event.</h4>
242 * This method must be implemented by subclass.
248 * State of the trace at the moment of the event.
250 public abstract void registerEvent(LttngEvent event
, LttngTraceState traceState
);
253 * <h4>Register that a new node was created.</h4>
255 * Must make sure the {@link #getChildren(FixedArray)} on the parent node
256 * will return the newly created node.
260 * Path of the new node.
262 protected abstract void registerName(final FixedArray path
);
265 * <h4>Reset a node.</h4>
273 public void reset(final FixedArray path
) {
274 for (StatisticsTreeNode node
: getChildren(path
)) {
275 reset(node
.getPath());
276 fNodes
.remove(node
.getPath());
281 * Indicate that the process is finishing.
284 * The event indicating the end of the process.
286 * State of the trace at that moment.
288 public abstract void process_exit(LttngEvent event
, LttngTraceState traceState
);