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