Tmf: Trace synchronization using network events
[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) {
122 /*
123 * TODO: Should find a way to assert the type is right here. For now we
124 * just hope developers register NetworkMatchDefinition for Network
125 * match types...
126 */
127 TmfNetworkMatchDefinition def = (TmfNetworkMatchDefinition) getEventDefinition(event.getTrace());
128 if (def == null) {
129 return;
130 }
131
132 Direction evType = def.getDirection(event);
133 if (evType == null) {
134 return;
135 }
136
137 /* Get the event's unique fields */
138 List<Object> eventKey = def.getUniqueField(event);
139 List<Map<List<Object>, ITmfEvent>> unmatchedTbl, companionTbl;
140
141 /* Point to the appropriate table */
142 switch (evType) {
143 case IN:
144 unmatchedTbl = fUnmatchedIn;
145 companionTbl = fUnmatchedOut;
146 break;
147 case OUT:
148 unmatchedTbl = fUnmatchedOut;
149 companionTbl = fUnmatchedIn;
150 break;
151 default:
152 return;
153 }
154
155 boolean found = false;
156 TmfEventDependency dep = null;
157 /* Search for the event in the companion table */
158 for (Map<List<Object>, ITmfEvent> map : companionTbl) {
159 if (map.containsKey(eventKey)) {
160 found = true;
161 ITmfEvent companionEvent = map.get(eventKey);
162
163 /* Remove the element from the companion table */
164 map.remove(eventKey);
165
166 /* Create the dependency object */
167 switch (evType) {
168 case IN:
169 dep = new TmfEventDependency(companionEvent, event);
170 break;
171 case OUT:
172 dep = new TmfEventDependency(event, companionEvent);
173 break;
174 default:
175 break;
176
177 }
178 }
179 }
180
181 /*
182 * If no companion was found, add the event to the appropriate unMatched
183 * lists
184 */
185 if (found) {
186 getProcessingUnit().addMatch(dep);
187 } else {
188 /*
189 * If an event is already associated with this key, do not add it
190 * again, we keep the first event chronologically, so if its match
191 * is eventually found, it is associated with the first send or
192 * receive event. At best, it is a good guess, at worst, the match
193 * will be too far off to be accurate. Too bad!
194 *
195 * TODO: maybe instead of just one event, we could have a list of
196 * events as value for the unmatched table. Not necessary right now
197 * though
198 */
199 if (!unmatchedTbl.get(traceno).containsKey(eventKey)) {
200 unmatchedTbl.get(traceno).put(eventKey, event);
201 }
202 }
203
204 }
205
206 /**
207 * Prints stats from the matching
208 *
209 * @return string of statistics
210 */
211 @SuppressWarnings("nls")
212 @Override
213 public String toString() {
214 final String cr = System.getProperty("line.separator");
215 StringBuilder b = new StringBuilder();
216 b.append(getProcessingUnit());
217 for (int i = 0; i < getTraces().length; i++) {
218 b.append("Trace " + i + ":" + cr +
219 " " + countEvents(fUnmatchedIn.get(i)) + " unmatched incoming events" + cr +
220 " " + countEvents(fUnmatchedOut.get(i)) + " unmatched outgoing events" + cr);
221 }
222
223 return b.toString();
224 }
225
226}
This page took 0.038451 seconds and 5 git commands to generate.