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
.lttng
.ui
.views
.statistics
.model
;
15 import java
.util
.Collection
;
16 import java
.util
.HashMap
;
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
;
25 * <h4>Base class for the statistics storage.</h4>
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.
31 public abstract class StatisticsData
{
33 * <h4>Define values that can be used like a C++ enumeration.</h4>
35 * The values can be used with binary "or" and "and" to mix them.
38 public static class Values
{
40 * <h4>Indicate the cpu time</h4>
42 * The actual time the cpu as passed in this state making calculations.
45 public static final int CPU_TIME
= 1;
47 * <h4>Indicate the cumulative cpu time</h4>
49 * Include the time the cpu as passed in this state and substate.
54 * <li>PID:1, Mode:USER_MODE</li>
56 * <li>PID:1, Mode:SYSCALL</li>
58 * <li>PID:2, Mode:USER_MODE</li>
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".
66 * TODO Validate values. Not tested in LTTv. TODO Validate description.
68 public static final int CUMULATIVE_CPU_TIME
= 2;
70 * <h4>Elapsed time</h4>
74 * TODO Give a correct description.
76 public static final int ELAPSED_TIME
= 4;
78 * <h4>State cumulative cpu time</h4>
82 * TODO Give a correct description.
84 public static final int STATE_CUMULATIVE_CPU_TIME
= 8;
88 * <h4>String builder used to merge string with more efficacy.</h4>
90 protected static StringBuilder fBuilder
= new StringBuilder();
92 * <h4>Identification of the root.</h4>
94 public static final FixedArray
<String
> ROOT
= new FixedArray
<String
>("root"); //$NON-NLS-1$
97 * <h4>Function to merge many string with more efficacy.</h4>
101 * @return A new string containing all the strings.
103 protected synchronized static String
mergeString(String
... strings
) {
104 fBuilder
.setLength(0);
105 for (String s
: strings
)
107 return fBuilder
.toString();
111 * <h4>Define what child a node can have.</h4>
113 * The management and usage of this map is done by subclass.
116 * HashSet are always faster than TreeSet.
119 private Map
<String
, Set
<String
>> fKeys
;
121 * <h4>The nodes in the tree.</f4>
123 private HashMap
<FixedArray
<String
>, StatisticsTreeNode
> fNodes
;
126 * <h4>Constructor.</h4>
128 public StatisticsData() {
129 fNodes
= new HashMap
<FixedArray
<String
>, StatisticsTreeNode
>();
130 fKeys
= new HashMap
<String
, Set
<String
>>();
134 * <h4>Indicate the end of the traceset</4>
136 * Can be used to trigger necessary calculations.
140 * Event receive (May have timestamp of 0).
142 * State of the trace at that moment.
144 public abstract void endTraceset(LttngEvent event
, LttngTraceState traceState
);
147 * <h4>Get a node.</h4>
151 * @return The node or null.
153 public StatisticsTreeNode
get(final FixedArray
<String
> path
) {
154 return fNodes
.get(path
);
158 * <h4>Get the children of a node.</h4>
162 * @return Collection containing the children.
164 public abstract Collection
<StatisticsTreeNode
> getChildren(final FixedArray
<String
> path
);
167 * <h4>Get the map of existing elements of path classified by parent.</h4>
171 protected Map
<String
, Set
<String
>> getKeys() {
176 * <h4>Get or create a node.</h4>
182 public StatisticsTreeNode
getOrCreate(final FixedArray
<String
> path
) {
183 StatisticsTreeNode current
= fNodes
.get(path
);
184 if (current
== null) {
186 current
= new StatisticsTreeNode(path
, this);
187 fNodes
.put(path
, current
);
193 * <h4>Get the parent of a node.</h4>
197 * @return Parent node or null.
199 public StatisticsTreeNode
getParent(final FixedArray
<String
> path
) {
200 if (path
.size() == 1) {
201 if (path
.equals(ROOT
))
206 // TODO Get or GetOrCreate?
207 return get(path
.subArray(0, path
.size() - 1));
211 * <h4>Get the name of a process.</h4>
215 * @return The name of the process. //TODO Adding the creation time of the
216 * process may be needed to differentiate two process.
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();
226 return mergeString(process
.getName(), " - ", String
.valueOf(process
.getPid())); //$NON-NLS-1$
230 * <h4>Increase some values.</h4>
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}
240 * State of the trace at that moment.
244 public abstract void increase(LttngEvent event
, LttngTraceState traceState
, int values
);
247 * <h4>Register an event.</h4>
249 * This method must be implemented by subclass.
255 * State of the trace at the moment of the event.
257 public abstract void registerEvent(LttngEvent event
, LttngTraceState traceState
);
260 * <h4>Register that a new node was created.</h4>
262 * Must make sure the {@link #getChildren(FixedArray)} on the parent node
263 * will return the newly created node.
267 * Path of the new node.
269 protected abstract void registerName(final FixedArray
<String
> path
);
272 * <h4>Reset a node.</h4>
280 public void reset(final FixedArray
<String
> path
) {
281 for (StatisticsTreeNode node
: getChildren(path
)) {
282 reset(node
.getPath());
283 fNodes
.remove(node
.getPath());
288 * Indicate that the process is finishing.
291 * The event indicating the end of the process.
293 * State of the trace at that moment.
295 public abstract void process_exit(LttngEvent event
, LttngTraceState traceState
);