TMF: Change abstract class TmfNetworkMatchDefinition to interface
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / event / matching / TmfNetworkEventMatching.java
CommitLineData
e73a4ba5
GB
1/*******************************************************************************
2 * Copyright (c) 2013 École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 * Geneviève Bastien - Initial implementation and API
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.core.event.matching;
14
15import java.util.ArrayList;
16import java.util.HashMap;
17import java.util.List;
18import java.util.Map;
19
20import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
21import org.eclipse.linuxtools.tmf.core.event.matching.IMatchProcessingUnit;
22import org.eclipse.linuxtools.tmf.core.event.matching.TmfEventDependency;
23import org.eclipse.linuxtools.tmf.core.event.matching.TmfEventMatching;
24import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
25
26/**
27 * This class matches events typically network-style, ie. where some events are
28 * 'send' events and the other 'receive' events or out/in events
29 *
30 * @author Geneviève Bastien
31 * @since 3.0
32 */
33public class TmfNetworkEventMatching extends TmfEventMatching {
34
35 /**
36 * Hashtables for unmatches incoming events
37 */
38 private final List<Map<List<Object>, ITmfEvent>> fUnmatchedIn = new ArrayList<Map<List<Object>, ITmfEvent>>();
39
40 /**
41 * Hashtables for unmatches outgoing events
42 */
43 private final List<Map<List<Object>, ITmfEvent>> fUnmatchedOut = new ArrayList<Map<List<Object>, ITmfEvent>>();
44
45 /**
46 * Enum for in and out types
47 */
48 public enum Direction {
49 /**
50 * The event is a 'receive' type of event
51 */
52 IN,
53 /**
54 * The event is a 'send' type of event
55 */
56 OUT,
57 }
58
59 /**
60 * Constructor with multiple traces and match processing object
61 *
62 * @param traces
63 * The set of traces for which to match events
64 */
65 public TmfNetworkEventMatching(ITmfTrace[] traces) {
66 this(traces, new TmfEventMatches());
67 }
68
69 /**
70 * Constructor with multiple traces and match processing object
71 *
72 * @param traces
73 * The set of traces for which to match events
74 * @param tmfEventMatches
75 * The match processing class
76 */
77 public TmfNetworkEventMatching(ITmfTrace[] traces, IMatchProcessingUnit tmfEventMatches) {
78 super(traces, tmfEventMatches);
79 }
80
81 /**
82 * Method that initializes any data structure for the event matching
83 */
84 @Override
85 public void initMatching() {
86 // Initialize the matching infrastructure (unmatched event lists)
87 fUnmatchedIn.clear();
88 fUnmatchedOut.clear();
89 for (int i = 0; i < getTraces().length; i++) {
90 fUnmatchedIn.add(new HashMap<List<Object>, ITmfEvent>());
91 fUnmatchedOut.add(new HashMap<List<Object>, ITmfEvent>());
92 }
93 super.initMatching();
94 }
95
96 /**
97 * Function that counts the events in a hashtable.
98 *
99 * @param tbl
100 * The table to count events for
101 * @return The number of events
102 */
103 protected int countEvents(Map<List<Object>, ITmfEvent> tbl) {
104 return tbl.size();
105 }
106
107 @Override
108 protected MatchingType getMatchingType() {
109 return MatchingType.NETWORK;
110 }
111
112 /**
113 * Matches one event
114 *
115 * @param event
116 * The event to match
117 * @param traceno
118 * The number of the trace this event belongs to
119 */
120 @Override
121 public void matchEvent(ITmfEvent event, int traceno) {
fa5993ff 122 if (!(getEventDefinition(event.getTrace()) instanceof ITmfNetworkMatchDefinition)) {
e73a4ba5
GB
123 return;
124 }
fa5993ff 125 ITmfNetworkMatchDefinition def = (ITmfNetworkMatchDefinition) getEventDefinition(event.getTrace());
e73a4ba5
GB
126
127 Direction evType = def.getDirection(event);
128 if (evType == null) {
129 return;
130 }
131
132 /* Get the event's unique fields */
133 List<Object> eventKey = def.getUniqueField(event);
134 List<Map<List<Object>, ITmfEvent>> unmatchedTbl, companionTbl;
135
136 /* Point to the appropriate table */
137 switch (evType) {
138 case IN:
139 unmatchedTbl = fUnmatchedIn;
140 companionTbl = fUnmatchedOut;
141 break;
142 case OUT:
143 unmatchedTbl = fUnmatchedOut;
144 companionTbl = fUnmatchedIn;
145 break;
146 default:
147 return;
148 }
149
150 boolean found = false;
151 TmfEventDependency dep = null;
152 /* Search for the event in the companion table */
153 for (Map<List<Object>, ITmfEvent> map : companionTbl) {
154 if (map.containsKey(eventKey)) {
155 found = true;
156 ITmfEvent companionEvent = map.get(eventKey);
157
158 /* Remove the element from the companion table */
159 map.remove(eventKey);
160
161 /* Create the dependency object */
162 switch (evType) {
163 case IN:
164 dep = new TmfEventDependency(companionEvent, event);
165 break;
166 case OUT:
167 dep = new TmfEventDependency(event, companionEvent);
168 break;
169 default:
170 break;
171
172 }
173 }
174 }
175
176 /*
177 * If no companion was found, add the event to the appropriate unMatched
178 * lists
179 */
180 if (found) {
181 getProcessingUnit().addMatch(dep);
182 } else {
183 /*
184 * If an event is already associated with this key, do not add it
185 * again, we keep the first event chronologically, so if its match
186 * is eventually found, it is associated with the first send or
187 * receive event. At best, it is a good guess, at worst, the match
188 * will be too far off to be accurate. Too bad!
189 *
190 * TODO: maybe instead of just one event, we could have a list of
191 * events as value for the unmatched table. Not necessary right now
192 * though
193 */
194 if (!unmatchedTbl.get(traceno).containsKey(eventKey)) {
195 unmatchedTbl.get(traceno).put(eventKey, event);
196 }
197 }
198
199 }
200
201 /**
202 * Prints stats from the matching
203 *
204 * @return string of statistics
205 */
206 @SuppressWarnings("nls")
207 @Override
208 public String toString() {
209 final String cr = System.getProperty("line.separator");
210 StringBuilder b = new StringBuilder();
211 b.append(getProcessingUnit());
212 for (int i = 0; i < getTraces().length; i++) {
213 b.append("Trace " + i + ":" + cr +
214 " " + countEvents(fUnmatchedIn.get(i)) + " unmatched incoming events" + cr +
215 " " + countEvents(fUnmatchedOut.get(i)) + " unmatched outgoing events" + cr);
216 }
217
218 return b.toString();
219 }
220
221}
This page took 0.032706 seconds and 5 git commands to generate.