1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial implementation and API
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.event
.matching
;
15 import java
.util
.Collection
;
17 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
18 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
20 import com
.google
.common
.collect
.HashBasedTable
;
21 import com
.google
.common
.collect
.Table
;
24 * This class matches events typically network-style, ie. where some events are
25 * 'send' events and the other 'receive' events or out/in events
27 * @author Geneviève Bastien
30 public class TmfNetworkEventMatching
extends TmfEventMatching
{
33 * Hashtables for unmatches incoming events
35 private final Table
<ITmfTrace
, IEventMatchingKey
, ITmfEvent
> fUnmatchedIn
= HashBasedTable
.create();
38 * Hashtables for unmatches outgoing events
40 private final Table
<ITmfTrace
, IEventMatchingKey
, ITmfEvent
> fUnmatchedOut
= HashBasedTable
.create();
43 * Enum for in and out types
45 public enum Direction
{
47 * The event is a 'receive' type of event
51 * The event is a 'send' type of event
57 * Constructor with multiple traces and match processing object
60 * The set of traces for which to match events
62 public TmfNetworkEventMatching(Collection
<ITmfTrace
> traces
) {
63 this(traces
, new TmfEventMatches());
67 * Constructor with multiple traces and match processing object
70 * The set of traces for which to match events
71 * @param tmfEventMatches
72 * The match processing class
74 public TmfNetworkEventMatching(Collection
<ITmfTrace
> traces
, IMatchProcessingUnit tmfEventMatches
) {
75 super(traces
, tmfEventMatches
);
79 * Method that initializes any data structure for the event matching
82 public void initMatching() {
83 // Initialize the matching infrastructure (unmatched event lists)
85 fUnmatchedOut
.clear();
90 protected MatchingType
getMatchingType() {
91 return MatchingType
.NETWORK
;
95 public synchronized void matchEvent(ITmfEvent event
, ITmfTrace trace
) {
96 ITmfNetworkMatchDefinition def
= null;
97 Direction evType
= null;
98 for (ITmfMatchEventDefinition oneDef
: getEventDefinitions(event
.getTrace())) {
99 if (oneDef
instanceof ITmfNetworkMatchDefinition
) {
100 def
= (ITmfNetworkMatchDefinition
) oneDef
;
101 evType
= def
.getDirection(event
);
102 if (evType
!= null) {
108 if (def
== null || evType
== null) {
112 /* Get the event's unique fields */
113 IEventMatchingKey eventKey
= def
.getEventKey(event
);
115 if (eventKey
== null) {
118 Table
<ITmfTrace
, IEventMatchingKey
, ITmfEvent
> unmatchedTbl
, companionTbl
;
120 /* Point to the appropriate table */
123 unmatchedTbl
= fUnmatchedIn
;
124 companionTbl
= fUnmatchedOut
;
127 unmatchedTbl
= fUnmatchedOut
;
128 companionTbl
= fUnmatchedIn
;
134 boolean found
= false;
135 TmfEventDependency dep
= null;
136 /* Search for the event in the companion table */
137 for (ITmfTrace mTrace
: getIndividualTraces()) {
138 if (companionTbl
.contains(mTrace
, eventKey
)) {
140 ITmfEvent companionEvent
= companionTbl
.get(mTrace
, eventKey
);
142 /* Remove the element from the companion table */
143 companionTbl
.remove(mTrace
, eventKey
);
145 /* Create the dependency object */
148 dep
= new TmfEventDependency(companionEvent
, event
);
151 dep
= new TmfEventDependency(event
, companionEvent
);
161 * If no companion was found, add the event to the appropriate unMatched
165 getProcessingUnit().addMatch(dep
);
168 * If an event is already associated with this key, do not add it
169 * again, we keep the first event chronologically, so if its match
170 * is eventually found, it is associated with the first send or
171 * receive event. At best, it is a good guess, at worst, the match
172 * will be too far off to be accurate. Too bad!
174 * TODO: maybe instead of just one event, we could have a list of
175 * events as value for the unmatched table. Not necessary right now
178 if (!unmatchedTbl
.contains(event
.getTrace(), eventKey
)) {
179 unmatchedTbl
.put(event
.getTrace(), eventKey
, event
);
186 * Prints stats from the matching
188 * @return string of statistics
190 @SuppressWarnings("nls")
192 public String
toString() {
193 final String cr
= System
.getProperty("line.separator");
194 StringBuilder b
= new StringBuilder();
195 b
.append(getProcessingUnit());
197 for (ITmfTrace trace
: getIndividualTraces()) {
198 b
.append("Trace " + i
++ + ":" + cr
+
199 " " + fUnmatchedIn
.row(trace
).size() + " unmatched incoming events" + cr
+
200 " " + fUnmatchedOut
.row(trace
).size() + " unmatched outgoing events" + cr
);