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