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: | |
10 | * Alvaro Sanchez-Leon - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | package org.eclipse.linuxtools.lttng.ui.views.controlflow.model; | |
13 | ||
14 | import java.util.HashMap; | |
15 | import java.util.Iterator; | |
16 | ||
17 | import org.eclipse.linuxtools.lttng.TraceDebug; | |
6e512b93 ASL |
18 | import org.eclipse.linuxtools.lttng.ui.model.trange.TimeRangeEventProcess; |
19 | ||
20 | /** | |
21 | * Contains the processes in use by the Control flow view | |
22 | * | |
23 | * @author alvaro | |
24 | * | |
25 | */ | |
8035003b | 26 | public class FlowProcessContainer { |
6e512b93 ASL |
27 | // ======================================================================== |
28 | // Data | |
29 | // ======================================================================== | |
30 | private final HashMap<ProcessKey, TimeRangeEventProcess> allProcesses = new HashMap<ProcessKey, TimeRangeEventProcess>(); | |
31 | private static Integer uniqueId = 0; | |
32 | ||
33 | // ======================================================================== | |
34 | // Constructor | |
35 | // ======================================================================== | |
36 | ||
37 | /** | |
38 | * Package level constructor | |
39 | */ | |
40 | FlowProcessContainer() { | |
41 | ||
42 | } | |
43 | ||
44 | // ======================================================================== | |
45 | // Methods | |
46 | // ======================================================================== | |
47 | /** | |
48 | * Interface to add a new process.<p> | |
49 | * | |
50 | * Note : Process with the same key will be overwritten, it's calling function job to make sure the new process is unique. | |
51 | * | |
52 | * @param newProcess The process to add | |
53 | */ | |
8035003b ASL |
54 | public void addProcess(TimeRangeEventProcess newProcess) { |
55 | if (newProcess != null) { | |
56 | allProcesses.put(new ProcessKey(newProcess), newProcess); | |
6e512b93 ASL |
57 | } |
58 | } | |
59 | ||
60 | /** | |
61 | * Request a unique ID | |
62 | * | |
63 | * @return Integer | |
64 | */ | |
65 | public Integer getUniqueId() { | |
66 | return uniqueId++; | |
67 | } | |
68 | ||
69 | /** | |
70 | * This method is intended for read only purposes in order to keep the | |
71 | * internal data structure in synch | |
72 | * | |
73 | * @return TimeRangeEventProcess[] | |
74 | */ | |
8035003b | 75 | public TimeRangeEventProcess[] readProcesses() { |
6e512b93 ASL |
76 | |
77 | // This allow us to return an Array of the correct type of the exact correct dimension, without looping | |
78 | return allProcesses.values().toArray(new TimeRangeEventProcess[allProcesses.size()]); | |
79 | } | |
80 | ||
81 | /** | |
8035003b ASL |
82 | * Clear the children information for processes related to a specific trace |
83 | * e.g. just before refreshing data with a new time range | |
84 | * | |
85 | * @param traceId The trace unique id (trace name?) on which we need to eliminate children. | |
86 | */ | |
87 | public void clearChildren(String traceId) { | |
6e512b93 ASL |
88 | TimeRangeEventProcess process = null; |
89 | Iterator<ProcessKey> iterator = allProcesses.keySet().iterator(); | |
90 | ||
91 | while (iterator.hasNext()) { | |
92 | process = allProcesses.get(iterator.next()); | |
8035003b ASL |
93 | |
94 | if (process.getTraceID().equals(traceId)) { | |
95 | // Reset clear childEventComposites() and traceEvents() | |
96 | // Also restore the nextGoodTime to the insertionTime for the drawing | |
97 | process.reset(); | |
98 | } | |
6e512b93 ASL |
99 | } |
100 | } | |
101 | ||
102 | /** | |
103 | * Clear all process items | |
104 | */ | |
8035003b | 105 | public void clearProcesses() { |
6e512b93 ASL |
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 | */ | |
8035003b | 115 | public void removeProcesses(String traceId) { |
6e512b93 ASL |
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 | } | |
126 | } | |
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) { | |
142 | // Get the TimeRangeEventProcess associated to a key we create here | |
143 | TimeRangeEventProcess foundProcess = allProcesses.get( new ProcessKey(searchedPid, searchedCpuId, searchedTraceID, searchedCreationtime) ); | |
144 | ||
145 | return foundProcess; | |
146 | } | |
147 | } | |
148 | ||
149 | ||
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; | |
178 | ||
179 | if ( valueRef != null ) { | |
180 | if ( (procKey.getPid().equals(valueRef.getPid()) ) && | |
181 | (procKey.getTraceId().equals(valueRef.getTraceID()) ) && | |
182 | (procKey.getCpuId().equals(valueRef.getCpu()) ) && | |
183 | (procKey.getCreationtime().equals(valueRef.getCreationTime()) ) ) | |
184 | { | |
185 | isSame = true; | |
186 | } | |
187 | } | |
188 | else { | |
189 | if ( (procKey.getPid().equals(this.pid ) ) && | |
190 | (procKey.getTraceId().equals(this.traceId ) ) && | |
191 | (procKey.getCpuId().equals(this.cpuId ) ) && | |
192 | (procKey.getCreationtime().equals(this.creationtime ) ) ) | |
193 | { | |
194 | isSame = true; | |
195 | } | |
196 | } | |
197 | } | |
198 | else { | |
199 | TraceDebug.debug("ERROR : The given key is not of the type ProcessKey!" + obj.getClass().toString()); | |
200 | } | |
201 | ||
202 | return isSame; | |
203 | } | |
204 | ||
205 | // *** WARNING : Everything in there work because the check "valueRef != null" is the same for ALL getter | |
206 | // Do NOT change this check without checking. | |
207 | public Long getPid() { | |
208 | if ( valueRef != null ) { | |
209 | return valueRef.getPid(); | |
210 | } | |
211 | else { | |
212 | return pid; | |
213 | } | |
214 | } | |
215 | ||
216 | public Long getCpuId() { | |
217 | if ( valueRef != null ) { | |
218 | return valueRef.getCpu(); | |
219 | } | |
220 | else { | |
221 | return cpuId; | |
222 | } | |
223 | } | |
224 | ||
225 | public String getTraceId() { | |
226 | if ( valueRef != null ) { | |
227 | return valueRef.getTraceID(); | |
228 | } | |
229 | else { | |
230 | return traceId; | |
231 | } | |
232 | } | |
233 | ||
234 | public Long getCreationtime() { | |
235 | if ( valueRef != null ) { | |
236 | return valueRef.getCreationTime(); | |
237 | } | |
238 | else { | |
239 | return creationtime; | |
240 | } | |
241 | } | |
242 | ||
243 | @Override | |
244 | public int hashCode() { | |
245 | return this.toString().hashCode(); | |
246 | } | |
247 | ||
248 | ||
249 | @Override | |
250 | public String toString() { | |
251 | if ( valueRef != null ) { | |
252 | return (valueRef.getPid().toString() + ":" + valueRef.getCpu().toString() + ":" + valueRef.getTraceID().toString() + ":" + valueRef.getCreationTime().toString()); | |
253 | } | |
254 | ||
255 | return (pid.toString() + ":" + cpuId.toString() + ":" + traceId.toString() + ":" + creationtime.toString()); | |
256 | } | |
257 | } |