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>
26 * <p>It allow to implement a tree structure while avoiding the need to run through the tree each time you need to add a node at a given place.</p>
28 public abstract class StatisticsData
{
30 * <h4>Define values that can be used like a C++ enumeration.</h4>
31 * <p>The values can be used with binary "or" and "and" to mix them.</p>
33 public static class Values
{
35 * <h4>Indicate the cpu time</h4>
36 * <p>The actual time the cpu as passed in this state making calculations.</p>
38 public static final int CPU_TIME
= 1;
40 * <h4>Indicate the cumulative cpu time</h4>
41 * <p>Include the time the cpu as passed in this state and substate.</p>
44 * <li>PID:1, Mode:USER_MODE</li>
46 * <li>PID:1, Mode:SYSCALL</li>
48 * <li>PID:2, Mode:USER_MODE</li>
51 * <p>In this example, the cumulative cpu time for "PID:1, Mode:USER_MODE" would
52 * be equal to its cpu time plus the cpu time of "PID:1, Mode:SYSCALL".</p>
53 * TODO Validate values. Not tested in LTTv.
54 * TODO Validate description.
56 public static final int CUMULATIVE_CPU_TIME
= 2;
58 * <h4>Elapsed time</h4>
59 * <p>Description...</p>
60 * TODO Give a correct description.
62 public static final int ELAPSED_TIME
= 4;
64 * <h4>State cumulative cpu time</h4>
65 * <p>Description...</p>
66 * TODO Give a correct description.
68 public static final int STATE_CUMULATIVE_CPU_TIME
= 8;
71 * <h4>String builder used to merge string with more efficacy.</h4>
73 protected static StringBuilder fBuilder
= new StringBuilder();
75 * <h4>Identification of the root.</h4>
77 public static final FixedArray
<String
> ROOT
= new FixedArray
<String
>("root"); //$NON-NLS-1$
79 * <h4>Function to merge many string with more efficacy.</h4>
80 * @param strings Strings to merge.
81 * @return A new string containing all the strings.
83 protected synchronized static String
mergeString(String
...strings
) {
84 fBuilder
.setLength(0);
85 for(String s
: strings
)
87 return fBuilder
.toString();
90 * <h4>Define what child a node can have. </h4>
91 * <p>The management and usage of this map is done by subclass.</p>
92 * <p>HashSet are always faster than TreeSet.</p>
94 private Map
<String
, Set
<String
>> fKeys
;
96 * <h4>The nodes in the tree.</f4>
98 private HashMap
<FixedArray
<String
>, StatisticsTreeNode
> fNodes
;
100 * <h4>Constructor.</h4>
102 public StatisticsData() {
103 fNodes
= new HashMap
<FixedArray
<String
>,StatisticsTreeNode
>();
104 fKeys
= new HashMap
<String
, Set
<String
>>();
107 * <h4>Indicate the end of the traceset</4>
108 * <p>Can be used to trigger necessary calculations.</p>
109 * @param event Event receive (May have timestamp of 0).
110 * @param traceState State of the trace at that moment.
112 public abstract void endTraceset(LttngEvent event
, LttngTraceState traceState
);
114 * <h4>Get a node.</h4>
115 * @param path Path to the node.
116 * @return The node or null.
118 public StatisticsTreeNode
get(final FixedArray
<String
> path
) {
119 return fNodes
.get(path
);
122 * <h4>Get the children of a node.</h4>
123 * @param path Path to the node.
124 * @return Collection containing the children.
126 public abstract Collection
<StatisticsTreeNode
> getChildren(final FixedArray
<String
> path
);
128 * <h4>Get the map of existing elements of path classified by parent.</h4>
131 protected Map
<String
, Set
<String
>> getKeys() {
135 * <h4>Get or create a node.</h4>
136 * @param path Path to the node.
139 public StatisticsTreeNode
getOrCreate(final FixedArray
<String
> path
) {
140 StatisticsTreeNode current
= fNodes
.get(path
);
141 if(current
== null) {
143 current
= new StatisticsTreeNode(path
,this);
144 fNodes
.put(path
, current
);
149 * <h4>Get the parent of a node.</h4>
150 * @param path Path to the node.
151 * @return Parent node or null.
153 public StatisticsTreeNode
getParent(final FixedArray
<String
> path
) {
154 if(path
.size() == 1) {
155 if(path
.equals(ROOT
))
160 // TODO Get or GetOrCreate?
161 return get(path
.subArray(0,path
.size()-1));
164 * <h4>Get the name of a process.</h4>
165 * @param process The process.
166 * @return The name of the process.
167 * //TODO Adding the creation time of the process may be needed to differentiate two process.
169 protected String
getProcessName(LttngProcessState process
) {
170 if(process
.getPid() == -1)
171 return "Unknown process";
172 if(process
.getName() == null)
173 return mergeString("Unknown process - ", String
.valueOf(process
.getPid()));
174 if(process
.getName().equals("")) //$NON-NLS-1$
175 return process
.getPid().toString();
177 return mergeString(process
.getName(), " - ",String
.valueOf(process
.getPid())); //$NON-NLS-1$
180 * <h4>Increase some values.</h4>
181 * <p>Values is an binary or operation on the desired values between {@link Values#CPU_TIME}, {@link Values#CUMULATIVE_CPU_TIME}, {@link Values#ELAPSED_TIME} and {@link Values#STATE_CUMULATIVE_CPU_TIME} .
182 * @param event Current event.
183 * @param traceState State of the trace at that moment.
184 * @param values Values desired.
186 public abstract void increase(LttngEvent event
, LttngTraceState traceState
, int values
);
188 * <h4>Register an event.</h4>
189 * <p>This method must be implemented by subclass.</p>
190 * @param event Event to process.
191 * @param traceState State of the trace at the moment of the event.
193 public abstract void registerEvent(LttngEvent event
, LttngTraceState traceState
);
195 * <h4>Register that a new node was created.</h4>
196 * <p>Must make sure the {@link #getChildren(FixedArray)} on the parent node will return the newly created node.</p>
197 * @param path Path of the new node.
199 protected abstract void registerName(final FixedArray
<String
> path
);
201 * <h4>Reset a node.</h4>
202 * <p>Work recursively.</p>
203 * @param path Path to the node.
205 public void reset(final FixedArray
<String
> path
) {
206 for(StatisticsTreeNode node
: getChildren(path
)) {
207 reset(node
.getPath());
208 fNodes
.remove(node
.getPath());
212 * Indicate that the process is finishing.
213 * @param event The event indicating the end of the process.
214 * @param traceState State of the trace at that moment.
216 public abstract void process_exit(LttngEvent event
, LttngTraceState traceState
);