2010-11-09 Francois Chouinard <fchouinard@gmail.com> 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>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>
27 */
28 public abstract class StatisticsData {
29 /**
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>
32 */
33 public static class Values {
34 /**
35 * <h4>Indicate the cpu time</h4>
36 * <p>The actual time the cpu as passed in this state making calculations.</p>
37 */
38 public static final int CPU_TIME = 1;
39 /**
40 * <h4>Indicate the cumulative cpu time</h4>
41 * <p>Include the time the cpu as passed in this state and substate.</p>
42 * <p>Example:
43 * <ul>
44 * <li>PID:1, Mode:USER_MODE</li>
45 * <ul>
46 * <li>PID:1, Mode:SYSCALL</li>
47 * </ul>
48 * <li>PID:2, Mode:USER_MODE</li>
49 * </ul>
50 * </p>
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.
55 */
56 public static final int CUMULATIVE_CPU_TIME = 2;
57 /**
58 * <h4>Elapsed time</h4>
59 * <p>Description...</p>
60 * TODO Give a correct description.
61 */
62 public static final int ELAPSED_TIME = 4;
63 /**
64 * <h4>State cumulative cpu time</h4>
65 * <p>Description...</p>
66 * TODO Give a correct description.
67 */
68 public static final int STATE_CUMULATIVE_CPU_TIME = 8;
69 }
70 /**
71 * <h4>String builder used to merge string with more efficacy.</h4>
72 */
73 protected static StringBuilder fBuilder = new StringBuilder();
74 /**
75 * <h4>Identification of the root.</h4>
76 */
77 public static final FixedArray<String> ROOT = new FixedArray<String>("root"); //$NON-NLS-1$
78 /**
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.
82 */
83 protected synchronized static String mergeString(String...strings) {
84 fBuilder.setLength(0);
85 for(String s : strings)
86 fBuilder.append(s);
87 return fBuilder.toString();
88 }
89 /**
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>
93 */
94 private Map<String, Set<String>> fKeys;
95 /**
96 * <h4>The nodes in the tree.</f4>
97 */
98 private HashMap<FixedArray<String>, StatisticsTreeNode> fNodes;
99 /**
100 * <h4>Constructor.</h4>
101 */
102 public StatisticsData() {
103 fNodes = new HashMap<FixedArray<String>,StatisticsTreeNode>();
104 fKeys = new HashMap<String, Set<String>>();
105 }
106 /**
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.
111 */
112 public abstract void endTraceset(LttngEvent event, LttngTraceState traceState);
113 /**
114 * <h4>Get a node.</h4>
115 * @param path Path to the node.
116 * @return The node or null.
117 */
118 public StatisticsTreeNode get(final FixedArray<String> path) {
119 return fNodes.get(path);
120 }
121 /**
122 * <h4>Get the children of a node.</h4>
123 * @param path Path to the node.
124 * @return Collection containing the children.
125 */
126 public abstract Collection<StatisticsTreeNode> getChildren(final FixedArray<String> path);
127 /**
128 * <h4>Get the map of existing elements of path classified by parent.</h4>
129 * @return The map.
130 */
131 protected Map<String, Set<String>> getKeys() {
132 return fKeys;
133 }
134 /**
135 * <h4>Get or create a node.</h4>
136 * @param path Path to the node.
137 * @return The node.
138 */
139 public StatisticsTreeNode getOrCreate(final FixedArray<String> path) {
140 StatisticsTreeNode current = fNodes.get(path);
141 if(current == null) {
142 registerName(path);
143 current = new StatisticsTreeNode(path,this);
144 fNodes.put(path, current);
145 }
146 return current;
147 }
148 /**
149 * <h4>Get the parent of a node.</h4>
150 * @param path Path to the node.
151 * @return Parent node or null.
152 */
153 public StatisticsTreeNode getParent(final FixedArray<String> path) {
154 if(path.size() == 1) {
155 if(path.equals(ROOT))
156 return null;
157 else
158 return get(ROOT);
159 }
160 // TODO Get or GetOrCreate?
161 return get(path.subArray(0,path.size()-1));
162 }
163 /**
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.
168 */
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();
176 else
177 return mergeString(process.getName(), " - ",String.valueOf(process.getPid())); //$NON-NLS-1$
178 }
179 /**
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.
185 */
186 public abstract void increase(LttngEvent event, LttngTraceState traceState, int values);
187 /**
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.
192 */
193 public abstract void registerEvent(LttngEvent event, LttngTraceState traceState);
194 /**
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.
198 */
199 protected abstract void registerName(final FixedArray<String> path);
200 /**
201 * <h4>Reset a node.</h4>
202 * <p>Work recursively.</p>
203 * @param path Path to the node.
204 */
205 public void reset(final FixedArray<String> path) {
206 for(StatisticsTreeNode node : getChildren(path)) {
207 reset(node.getPath());
208 fNodes.remove(node.getPath());
209 }
210 }
211 /**
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.
215 */
216 public abstract void process_exit(LttngEvent event, LttngTraceState traceState);
217 }
This page took 0.034219 seconds and 5 git commands to generate.