1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2009 Benjamin Poirier <benjamin.poirier@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 #include "event_analysis.h"
29 #include "sync_chain.h"
31 #include "event_matching_tcp.h"
35 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
39 // Functions common to all matching modules
40 static void initMatchingTCP(SyncState
* const syncState
);
41 static void destroyMatchingTCP(SyncState
* const syncState
);
43 static void matchEventTCP(SyncState
* const syncState
, Event
* const event
);
44 static GArray
* finalizeMatchingTCP(SyncState
* const syncState
);
45 static void printMatchingStatsTCP(SyncState
* const syncState
);
46 static void writeMatchingGraphsPlotsTCP(SyncState
* const syncState
, const
47 unsigned int i
, const unsigned int j
);
49 // Functions specific to this module
50 static void registerMatchingTCP() __attribute__((constructor (101)));
52 static void matchEvents(SyncState
* const syncState
, Event
* const event
,
53 GHashTable
* const unMatchedList
, GHashTable
* const
54 unMatchedOppositeList
, const size_t fieldOffset
, const size_t
56 static void partialDestroyMatchingTCP(SyncState
* const syncState
);
58 static bool isAck(const Message
* const message
);
59 static bool needsAck(const Message
* const message
);
60 static void buildReversedConnectionKey(ConnectionKey
* const
61 reversedConnectionKey
, const ConnectionKey
* const connectionKey
);
63 static void openGraphDataFiles(SyncState
* const syncState
);
64 static void closeGraphDataFiles(SyncState
* const syncState
);
65 static void writeMessagePoint(FILE* stream
, const Message
* const message
);
68 static MatchingModule matchingModuleTCP
= {
71 .canMatch
[UDP
]= false,
72 .initMatching
= &initMatchingTCP
,
73 .destroyMatching
= &destroyMatchingTCP
,
74 .matchEvent
= &matchEventTCP
,
75 .finalizeMatching
= &finalizeMatchingTCP
,
76 .printMatchingStats
= &printMatchingStatsTCP
,
78 .writeTraceTracePlots
= &writeMatchingGraphsPlotsTCP
,
84 * Matching module registering function
86 static void registerMatchingTCP()
88 g_queue_push_tail(&matchingModules
, &matchingModuleTCP
);
93 * Matching init function
95 * This function is called at the beginning of a synchronization run for a set
98 * Allocate the matching specific data structures
101 * syncState container for synchronization data.
102 * This function allocates these matchingData members:
108 static void initMatchingTCP(SyncState
* const syncState
)
110 MatchingDataTCP
* matchingData
;
112 matchingData
= malloc(sizeof(MatchingDataTCP
));
113 syncState
->matchingData
= matchingData
;
115 matchingData
->unMatchedInE
= g_hash_table_new_full(&ghfSegmentKeyHash
,
116 &gefSegmentKeyEqual
, NULL
, &gdnDestroyEvent
);
117 matchingData
->unMatchedOutE
= g_hash_table_new_full(&ghfSegmentKeyHash
,
118 &gefSegmentKeyEqual
, NULL
, &gdnDestroyEvent
);
119 matchingData
->unAcked
= g_hash_table_new_full(&ghfConnectionKeyHash
,
120 &gefConnectionKeyEqual
, &gdnConnectionKeyDestroy
,
121 &gdnTCPSegmentListDestroy
);
123 if (syncState
->stats
)
127 matchingData
->stats
= calloc(1, sizeof(MatchingStatsTCP
));
128 matchingData
->stats
->totMessageArray
= malloc(syncState
->traceNb
*
129 sizeof(unsigned int*));
130 for (i
= 0; i
< syncState
->traceNb
; i
++)
132 matchingData
->stats
->totMessageArray
[i
]=
133 calloc(syncState
->traceNb
, sizeof(unsigned int));
138 matchingData
->stats
= NULL
;
141 if (syncState
->graphsStream
)
143 openGraphDataFiles(syncState
);
147 matchingData
->messagePoints
= NULL
;
153 * Matching destroy function
155 * Free the matching specific data structures
158 * syncState container for synchronization data.
159 * This function deallocates these matchingData members:
162 static void destroyMatchingTCP(SyncState
* const syncState
)
164 MatchingDataTCP
* matchingData
;
166 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
168 if (matchingData
== NULL
)
173 partialDestroyMatchingTCP(syncState
);
175 if (syncState
->stats
)
179 for (i
= 0; i
< syncState
->traceNb
; i
++)
181 free(matchingData
->stats
->totMessageArray
[i
]);
183 free(matchingData
->stats
->totMessageArray
);
184 free(matchingData
->stats
);
187 free(syncState
->matchingData
);
188 syncState
->matchingData
= NULL
;
193 * Free some of the matching specific data structures
195 * This function can be called right after the events have been processed to
196 * free some data structures that are not needed for finalization.
199 * syncState container for synchronization data.
200 * This function deallocates these matchingData members:
205 static void partialDestroyMatchingTCP(SyncState
* const syncState
)
207 MatchingDataTCP
* matchingData
;
209 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
211 if (matchingData
== NULL
|| matchingData
->unMatchedInE
== NULL
)
216 g_hash_table_destroy(matchingData
->unMatchedInE
);
217 matchingData
->unMatchedInE
= NULL
;
218 g_hash_table_destroy(matchingData
->unMatchedOutE
);
219 g_hash_table_destroy(matchingData
->unAcked
);
221 if (syncState
->graphsStream
&& matchingData
->messagePoints
)
223 closeGraphDataFiles(syncState
);
229 * Try to match one event from a trace with the corresponding event from
233 * syncState container for synchronization data.
234 * event new event to match
236 static void matchEventTCP(SyncState
* const syncState
, Event
* const event
)
238 MatchingDataTCP
* matchingData
;
240 g_assert(event
->type
== TCP
);
242 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
244 if (event
->event
.tcpEvent
->direction
== IN
)
246 matchEvents(syncState
, event
, matchingData
->unMatchedInE
,
247 matchingData
->unMatchedOutE
, offsetof(Message
, inE
),
248 offsetof(Message
, outE
));
252 matchEvents(syncState
, event
, matchingData
->unMatchedOutE
,
253 matchingData
->unMatchedInE
, offsetof(Message
, outE
),
254 offsetof(Message
, inE
));
260 * Call the partial matching destroyer and Obtain the factors from downstream
263 * syncState container for synchronization data.
266 * Factors[traceNb] synchronization factors for each trace
268 static GArray
* finalizeMatchingTCP(SyncState
* const syncState
)
270 partialDestroyMatchingTCP(syncState
);
272 return syncState
->analysisModule
->finalizeAnalysis(syncState
);
277 * Print statistics related to matching. Must be called after
281 * syncState container for synchronization data.
283 static void printMatchingStatsTCP(SyncState
* const syncState
)
286 MatchingDataTCP
* matchingData
;
288 if (!syncState
->stats
)
293 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
295 printf("TCP matching stats:\n");
296 printf("\ttotal input and output events matched together to form a packet: %u\n",
297 matchingData
->stats
->totPacket
);
299 printf("\tMessage traffic:\n");
301 for (i
= 0; i
< syncState
->traceNb
; i
++)
303 for (j
= i
+ 1; j
< syncState
->traceNb
; j
++)
305 printf("\t\t%3d - %-3d: sent %-10u received %-10u\n", i
, j
,
306 matchingData
->stats
->totMessageArray
[j
][i
],
307 matchingData
->stats
->totMessageArray
[i
][j
]);
311 if (syncState
->analysisModule
->analyzeExchange
!= NULL
)
313 printf("\ttotal packets identified needing an acknowledge: %u\n",
314 matchingData
->stats
->totPacketNeedAck
);
315 printf("\ttotal exchanges (four events matched together): %u\n",
316 matchingData
->stats
->totExchangeEffective
);
317 printf("\ttotal synchronization exchanges: %u\n",
318 matchingData
->stats
->totExchangeSync
);
324 * Implementation of a packet matching algorithm for TCP
327 * event: new event to match
328 * unMatchedList: list of unmatched events of the same type (send or
330 * unMatchedOppositeList: list of unmatched events of the opposite type of
332 * fieldOffset: offset of the Event field in the Message struct for the
333 * field of the type of event
334 * oppositeFieldOffset: offset of the Event field in the Message struct
335 * for the field of the opposite type of event
337 static void matchEvents(SyncState
* const syncState
, Event
* const event
,
338 GHashTable
* const unMatchedList
, GHashTable
* const unMatchedOppositeList
,
339 const size_t fieldOffset
, const size_t oppositeFieldOffset
)
341 Event
* companionEvent
;
343 MatchingDataTCP
* matchingData
;
346 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
348 companionEvent
= g_hash_table_lookup(unMatchedOppositeList
, event
->event
.tcpEvent
->segmentKey
);
349 if (companionEvent
!= NULL
)
351 g_debug("Found matching companion event, ");
353 // If it's there, remove it and create a Message
354 g_hash_table_steal(unMatchedOppositeList
, event
->event
.tcpEvent
->segmentKey
);
355 packet
= malloc(sizeof(Message
));
356 *((Event
**) ((void*) packet
+ fieldOffset
))= event
;
357 *((Event
**) ((void*) packet
+ oppositeFieldOffset
))= companionEvent
;
358 packet
->print
= &printTCPSegment
;
359 // Both events can now share the same segmentKey
360 free(packet
->outE
->event
.tcpEvent
->segmentKey
);
361 packet
->outE
->event
.tcpEvent
->segmentKey
= packet
->inE
->event
.tcpEvent
->segmentKey
;
363 if (syncState
->stats
)
365 matchingData
->stats
->totPacket
++;
366 matchingData
->stats
->totMessageArray
[packet
->inE
->traceNum
][packet
->outE
->traceNum
]++;
369 // Discard loopback traffic
370 if (packet
->inE
->traceNum
== packet
->outE
->traceNum
)
372 destroyTCPSegment(packet
);
376 if (syncState
->graphsStream
)
378 writeMessagePoint(matchingData
->messagePoints
[packet
->inE
->traceNum
][packet
->outE
->traceNum
],
382 if (syncState
->analysisModule
->analyzeMessage
!= NULL
)
384 syncState
->analysisModule
->analyzeMessage(syncState
, packet
);
387 // We can skip the rest of the algorithm if the analysis module is not
388 // interested in exchanges
389 if (syncState
->analysisModule
->analyzeExchange
== NULL
)
391 destroyTCPSegment(packet
);
395 // If this packet acknowleges some data ...
398 ConnectionKey oppositeConnectionKey
;
400 buildReversedConnectionKey(&oppositeConnectionKey
,
401 &event
->event
.tcpEvent
->segmentKey
->connectionKey
);
402 conUnAcked
= g_hash_table_lookup(matchingData
->unAcked
,
403 &oppositeConnectionKey
);
404 if (conUnAcked
!= NULL
)
406 Message
* ackedPacket
;
412 result
= g_queue_find_custom(conUnAcked
, packet
, &gcfTCPSegmentAckCompare
);
414 while (result
!= NULL
)
416 // Remove the acknowledged packet from the unAcked list
417 // and keep it for later offset calculations
418 g_debug("Found matching unAcked packet, ");
420 ackedPacket
= (Message
*) result
->data
;
421 g_queue_delete_link(conUnAcked
, result
);
423 if (syncState
->stats
)
425 matchingData
->stats
->totExchangeEffective
++;
428 if (exchange
== NULL
)
430 exchange
= malloc(sizeof(Exchange
));
431 exchange
->message
= packet
;
432 exchange
->acks
= g_queue_new();
435 g_queue_push_tail(exchange
->acks
, ackedPacket
);
437 result
= g_queue_find_custom(conUnAcked
, packet
,
438 &gcfTCPSegmentAckCompare
);
441 // It might be possible to do an offset calculation
442 if (exchange
!= NULL
)
444 ackedPacket
= g_queue_peek_tail(exchange
->acks
);
445 if (ackedPacket
->outE
->traceNum
!= packet
->inE
->traceNum
446 || ackedPacket
->inE
->traceNum
!=
447 packet
->outE
->traceNum
|| packet
->inE
->traceNum
==
448 packet
->outE
->traceNum
)
450 ackedPacket
->print(ackedPacket
);
451 packet
->print(packet
);
452 g_error("Disorganized exchange encountered during "
457 if (syncState
->stats
)
459 matchingData
->stats
->totExchangeSync
++;
462 syncState
->analysisModule
->analyzeExchange(syncState
,
466 exchange
->message
= NULL
;
467 destroyTCPExchange(exchange
);
472 if (needsAck(packet
))
474 if (syncState
->stats
)
476 matchingData
->stats
->totPacketNeedAck
++;
479 // If this packet will generate an ack, add it to the unAcked list
480 g_debug("Adding to unAcked, ");
481 conUnAcked
= g_hash_table_lookup(matchingData
->unAcked
,
482 &event
->event
.tcpEvent
->segmentKey
->connectionKey
);
483 if (conUnAcked
== NULL
)
485 ConnectionKey
* connectionKey
;
487 connectionKey
= malloc(sizeof(ConnectionKey
));
488 memcpy(connectionKey
, &event
->event
.tcpEvent
->segmentKey
->connectionKey
,
489 sizeof(ConnectionKey
));
490 g_hash_table_insert(matchingData
->unAcked
, connectionKey
,
491 conUnAcked
= g_queue_new());
493 g_queue_push_tail(conUnAcked
, packet
);
497 destroyTCPSegment(packet
);
502 // If there's no corresponding event, add the event to the unmatched
503 // list for this type of event
504 g_debug("Adding to unmatched event list, ");
505 g_hash_table_replace(unMatchedList
, event
->event
.tcpEvent
->segmentKey
, event
);
511 * Check if a packet is an acknowledge
520 static bool isAck(const Message
* const packet
)
522 if (packet
->inE
->event
.tcpEvent
->segmentKey
->ack
== 1)
534 * Check if a packet will increment the sequence number, thus needing an
541 * true if the packet will need an acknowledge
544 static bool needsAck(const Message
* const packet
)
546 if (packet
->inE
->event
.tcpEvent
->segmentKey
->syn
|| packet
->inE
->event
.tcpEvent
->segmentKey
->fin
||
547 packet
->inE
->event
.tcpEvent
->segmentKey
->tot_len
- packet
->inE
->event
.tcpEvent
->segmentKey
->ihl
* 4 -
548 packet
->inE
->event
.tcpEvent
->segmentKey
->doff
* 4 > 0)
560 * Populate a connection key structure for the opposite direction of a
564 * reversedConnectionKey the result, must be pre-allocated
565 * connectionKey the connection key to reverse
567 static void buildReversedConnectionKey(ConnectionKey
* const
568 reversedConnectionKey
, const ConnectionKey
* const connectionKey
)
570 reversedConnectionKey
->saddr
= connectionKey
->daddr
;
571 reversedConnectionKey
->daddr
= connectionKey
->saddr
;
572 reversedConnectionKey
->source
= connectionKey
->dest
;
573 reversedConnectionKey
->dest
= connectionKey
->source
;
578 * Create and open files used to store message points to genereate
579 * graphs. Allocate and populate array to store file pointers.
582 * syncState: container for synchronization data
584 static void openGraphDataFiles(SyncState
* const syncState
)
590 MatchingDataTCP
* matchingData
;
592 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
594 cwd
= changeToGraphDir(syncState
->graphsDir
);
596 matchingData
->messagePoints
= malloc(syncState
->traceNb
* sizeof(FILE**));
597 for (i
= 0; i
< syncState
->traceNb
; i
++)
599 matchingData
->messagePoints
[i
]= malloc(syncState
->traceNb
*
601 for (j
= 0; j
< syncState
->traceNb
; j
++)
605 retval
= snprintf(name
, sizeof(name
),
606 "matching_tcp-%03u_to_%03u.data", j
, i
);
607 if (retval
> sizeof(name
) - 1)
609 name
[sizeof(name
) - 1]= '\0';
611 if ((matchingData
->messagePoints
[i
][j
]= fopen(name
, "w")) ==
614 g_error(strerror(errno
));
623 g_error(strerror(errno
));
630 * Write a message point to a file used to generate graphs
633 * stream: FILE*, file pointer where to write the point
634 * message: message for which to write the point
636 static void writeMessagePoint(FILE* stream
, const Message
* const message
)
640 if (message
->inE
->traceNum
< message
->outE
->traceNum
)
642 // CA is inE->traceNum
643 x
= message
->inE
->cpuTime
;
644 y
= message
->outE
->cpuTime
;
648 // CA is outE->traceNum
649 x
= message
->outE
->cpuTime
;
650 y
= message
->inE
->cpuTime
;
653 fprintf(stream
, "%20llu %20llu\n", x
, y
);
658 * Close files used to store convex hull points to genereate graphs.
659 * Deallocate array to store file pointers.
662 * syncState: container for synchronization data
664 static void closeGraphDataFiles(SyncState
* const syncState
)
667 MatchingDataTCP
* matchingData
;
670 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
672 if (matchingData
->messagePoints
== NULL
)
677 for (i
= 0; i
< syncState
->traceNb
; i
++)
679 for (j
= 0; j
< syncState
->traceNb
; j
++)
683 retval
= fclose(matchingData
->messagePoints
[i
][j
]);
686 g_error(strerror(errno
));
690 free(matchingData
->messagePoints
[i
]);
692 free(matchingData
->messagePoints
);
694 matchingData
->messagePoints
= NULL
;
699 * Write the matching-specific graph lines in the gnuplot script.
702 * syncState: container for synchronization data
703 * i: first trace number
704 * j: second trace number, garanteed to be larger than i
706 static void writeMatchingGraphsPlotsTCP(SyncState
* const syncState
, const
707 unsigned int i
, const unsigned int j
)
709 fprintf(syncState
->graphsStream
,
710 "\t\"matching_tcp-%1$03d_to_%2$03d.data\" "
711 "title \"Sent messages\" with points linetype 4 "
712 "linecolor rgb \"#98fc66\" pointtype 9 pointsize 2, \\\n"
713 "\t\"matching_tcp-%2$03d_to_%1$03d.data\" "
714 "title \"Received messages\" with points linetype 4 "
715 "linecolor rgb \"#6699cc\" pointtype 11 pointsize 2, \\\n", i
, j
);