Commit | Line | Data |
---|---|---|
6e512b93 ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009 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: | |
b513223f | 10 | * Alvaro Sanchez-Leon - Initial API and implementation |
860cadcf | 11 | * Michel Dagenais (michel.dagenais@polymtl.ca) - Reference C implementation, used with permission |
6e512b93 ASL |
12 | *******************************************************************************/ |
13 | package org.eclipse.linuxtools.lttng.ui.views.controlflow.model; | |
14 | ||
b513223f FC |
15 | import java.util.HashMap; |
16 | import java.util.Iterator; | |
6e512b93 | 17 | |
5945cec9 | 18 | import org.eclipse.linuxtools.internal.lttng.core.TraceDebug; |
8827c197 | 19 | import org.eclipse.linuxtools.lttng.ui.model.trange.ItemContainer; |
6e512b93 ASL |
20 | import org.eclipse.linuxtools.lttng.ui.model.trange.TimeRangeEventProcess; |
21 | ||
22 | /** | |
b513223f | 23 | * Contains the processes in use by the Control flow view |
6e512b93 ASL |
24 | * |
25 | * @author alvaro | |
26 | * | |
27 | */ | |
8827c197 | 28 | public class FlowProcessContainer implements ItemContainer<TimeRangeEventProcess> { |
6e512b93 ASL |
29 | // ======================================================================== |
30 | // Data | |
31 | // ======================================================================== | |
b513223f FC |
32 | private final HashMap<ProcessKey, TimeRangeEventProcess> allProcesses = new HashMap<ProcessKey, TimeRangeEventProcess>(); |
33 | private static Integer uniqueId = 0; | |
34 | ||
6e512b93 ASL |
35 | // ======================================================================== |
36 | // Constructor | |
37 | // ======================================================================== | |
38 | ||
39 | /** | |
40 | * Package level constructor | |
41 | */ | |
42 | FlowProcessContainer() { | |
43 | ||
44 | } | |
45 | ||
46 | // ======================================================================== | |
47 | // Methods | |
48 | // ======================================================================== | |
49 | /** | |
b513223f FC |
50 | * Interface to add a new process.<p> |
51 | * | |
52 | * Note : Process with the same key will be overwritten, it's calling function job to make sure the new process is unique. | |
6e512b93 | 53 | * |
b513223f | 54 | * @param newProcess The process to add |
6e512b93 | 55 | */ |
d4011df2 | 56 | @Override |
8827c197 FC |
57 | public void addItem(TimeRangeEventProcess newItem) { |
58 | if (newItem != null) { | |
59 | allProcesses.put(new ProcessKey(newItem), newItem); | |
6e512b93 ASL |
60 | } |
61 | } | |
b513223f | 62 | |
6e512b93 | 63 | /** |
b513223f FC |
64 | * Request a unique ID |
65 | * | |
66 | * @return Integer | |
67 | */ | |
d4011df2 FC |
68 | @Override |
69 | public Integer getUniqueId() { | |
b513223f FC |
70 | return uniqueId++; |
71 | } | |
72 | ||
73 | /** | |
74 | * This method is intended for read only purposes in order to keep the | |
75 | * internal data structure in synch | |
76 | * | |
77 | * @return TimeRangeEventProcess[] | |
78 | */ | |
d4011df2 | 79 | @Override |
8827c197 | 80 | public TimeRangeEventProcess[] readItems() { |
28b94d61 | 81 | |
b513223f FC |
82 | // This allow us to return an Array of the correct type of the exact correct dimension, without looping |
83 | return allProcesses.values().toArray(new TimeRangeEventProcess[allProcesses.size()]); | |
6e512b93 | 84 | } |
b513223f | 85 | |
6e512b93 | 86 | /** |
8827c197 FC |
87 | * Clear the children information for processes e.g. just before refreshing |
88 | * data with a new time range | |
89 | */ | |
d4011df2 | 90 | @Override |
8827c197 | 91 | public void clearChildren() { |
b513223f FC |
92 | TimeRangeEventProcess process = null; |
93 | Iterator<ProcessKey> iterator = allProcesses.keySet().iterator(); | |
94 | ||
95 | while (iterator.hasNext()) { | |
96 | process = allProcesses.get(iterator.next()); | |
8827c197 | 97 | process.reset(); |
b513223f | 98 | } |
6e512b93 | 99 | } |
b513223f | 100 | |
d712a5f3 | 101 | /** |
b513223f FC |
102 | * Clear all process items |
103 | */ | |
d4011df2 FC |
104 | @Override |
105 | public void clearItems() { | |
b513223f FC |
106 | allProcesses.clear(); |
107 | } | |
108 | ||
109 | /** | |
110 | * Remove the process related to a specific trace e.g. during trace | |
111 | * removal | |
112 | * | |
113 | * @param traceId The trace unique id (trace name?) on which we want to remove process | |
114 | */ | |
d4011df2 | 115 | @Override |
8827c197 | 116 | public void removeItems(String traceId) { |
b513223f FC |
117 | ProcessKey iterKey = null; |
118 | ||
119 | Iterator<ProcessKey> iterator = allProcesses.keySet().iterator(); | |
120 | while (iterator.hasNext()) { | |
121 | iterKey = iterator.next(); | |
122 | ||
123 | if (allProcesses.get(iterKey).getTraceID().equals(traceId)) { | |
124 | allProcesses.remove(iterKey); | |
125 | } | |
126 | } | |
dfaf8391 | 127 | } |
b513223f FC |
128 | |
129 | /** | |
130 | * Search by keys (pid, cpuId, traceId and creationTime)<p> | |
131 | * | |
132 | * A match is returned if the four arguments received match an entry | |
133 | * Otherwise null is returned | |
134 | * | |
135 | * @param searchedPid The processId (Pid) we are looking for | |
136 | * @param searchedCpuId The cpu Id we are looking for | |
137 | * @param searchedTraceID The traceId (trace name?) we are looking for | |
138 | * @param searchedCreationtime The creation time we are looking for | |
139 | * | |
140 | * @return TimeRangeEventProcess | |
141 | */ | |
142 | public TimeRangeEventProcess findProcess(Long searchedPid, Long searchedCpuId, String searchedTraceID, Long searchedCreationtime) { | |
28b94d61 | 143 | // Get the TimeRangeEventProcess associated to a key we create here |
b513223f | 144 | TimeRangeEventProcess foundProcess = allProcesses.get( new ProcessKey(searchedPid, searchedCpuId, searchedTraceID, searchedCreationtime) ); |
28b94d61 | 145 | |
b513223f FC |
146 | return foundProcess; |
147 | } | |
148 | } | |
dfaf8391 | 149 | |
6e512b93 | 150 | |
b513223f FC |
151 | class ProcessKey { |
152 | private TimeRangeEventProcess valueRef = null; | |
153 | ||
154 | private Long pid = null; | |
155 | private Long cpuId = null; | |
156 | private String traceId = null; | |
157 | private Long creationtime = null; | |
158 | ||
159 | @SuppressWarnings("unused") | |
160 | private ProcessKey() { } | |
161 | ||
162 | public ProcessKey(TimeRangeEventProcess newRef) { | |
163 | valueRef = newRef; | |
164 | } | |
165 | ||
166 | public ProcessKey(Long newPid, Long newCpuId, String newTraceId, Long newCreationTime) { | |
167 | pid = newPid; | |
168 | cpuId = newCpuId; | |
169 | traceId = newTraceId; | |
170 | creationtime = newCreationTime; | |
171 | } | |
172 | ||
173 | @Override | |
174 | public boolean equals(Object obj) { | |
1cceddbe | 175 | if (obj == this) |
176 | return true; | |
177 | if (obj == null) | |
178 | return false; | |
179 | ||
b513223f FC |
180 | boolean isSame = false; |
181 | ||
182 | if ( obj instanceof ProcessKey ) { | |
183 | ProcessKey procKey = (ProcessKey) obj; | |
2211df66 | 184 | |
ba1bc132 ASL |
185 | if (valueRef != null) { |
186 | if ((procKey.getPid().equals(valueRef.getPid())) | |
187 | && (procKey.getTraceId().equals(valueRef.getTraceID())) | |
188 | && (procKey.getCreationtime().equals(valueRef.getCreationTime()))) { | |
189 | // use the cpu value to validate pid 0 | |
190 | if (valueRef.getPid().longValue() == 0L && !procKey.getCpuId().equals(valueRef.getCpu())) { | |
191 | isSame = false; | |
192 | } else { | |
193 | isSame = true; | |
194 | } | |
195 | } | |
196 | } else { | |
197 | if ((procKey.getPid().equals(this.pid)) && (procKey.getTraceId().equals(this.traceId)) | |
198 | && (procKey.getCreationtime().equals(this.creationtime))) { | |
199 | // use the cpu value to validate pid 0 | |
200 | if (this.pid.longValue() == 0L && !procKey.getCpuId().equals(this.cpuId)) { | |
201 | isSame = false; | |
202 | } else { | |
203 | isSame = true; | |
204 | } | |
205 | } | |
206 | } | |
2211df66 FC |
207 | } |
208 | else { | |
9c4eb5f7 | 209 | TraceDebug.debug("ERROR : The given key is not of the type ProcessKey!" + obj.getClass().toString()); //$NON-NLS-1$ |
b513223f FC |
210 | } |
211 | ||
212 | return isSame; | |
213 | } | |
214 | ||
215 | // *** WARNING : Everything in there work because the check "valueRef != null" is the same for ALL getter | |
216 | // Do NOT change this check without checking. | |
217 | public Long getPid() { | |
218 | if ( valueRef != null ) { | |
219 | return valueRef.getPid(); | |
220 | } | |
221 | else { | |
222 | return pid; | |
223 | } | |
224 | } | |
225 | ||
226 | public Long getCpuId() { | |
227 | if ( valueRef != null ) { | |
228 | return valueRef.getCpu(); | |
229 | } | |
230 | else { | |
231 | return cpuId; | |
232 | } | |
233 | } | |
234 | ||
235 | public String getTraceId() { | |
236 | if ( valueRef != null ) { | |
237 | return valueRef.getTraceID(); | |
238 | } | |
239 | else { | |
240 | return traceId; | |
241 | } | |
242 | } | |
243 | ||
244 | public Long getCreationtime() { | |
245 | if ( valueRef != null ) { | |
246 | return valueRef.getCreationTime(); | |
247 | } | |
248 | else { | |
249 | return creationtime; | |
250 | } | |
251 | } | |
252 | ||
253 | @Override | |
254 | public int hashCode() { | |
255 | return this.toString().hashCode(); | |
256 | } | |
257 | ||
258 | ||
3b38ea61 FC |
259 | @Override |
260 | @SuppressWarnings("nls") | |
b513223f FC |
261 | public String toString() { |
262 | if ( valueRef != null ) { | |
ba1bc132 ASL |
263 | // return (valueRef.getPid().toString() + ":" + |
264 | // valueRef.getCpu().toString() + ":" | |
265 | // + valueRef.getTraceID().toString() + ":" + | |
266 | // valueRef.getCreationTime().toString()); | |
267 | return (valueRef.getPid().toString() + ":" + valueRef.getTraceID().toString() + ":" + valueRef | |
268 | .getCreationTime().toString()); | |
b513223f FC |
269 | } |
270 | ||
ba1bc132 ASL |
271 | // return (pid.toString() + ":" + cpuId.toString() + ":" + |
272 | // traceId.toString() + ":" + creationtime.toString()); | |
273 | ||
274 | return (pid.toString() + ":" + traceId.toString() + ":" + creationtime.toString()); | |
b513223f | 275 | } |
6e512b93 | 276 | } |