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"
34 // Functions common to all matching modules
35 static void initMatchingTCP(SyncState
* const syncState
);
36 static void destroyMatchingTCP(SyncState
* const syncState
);
38 static void matchEventTCP(SyncState
* const syncState
, Event
* const event
);
39 static GArray
* finalizeMatchingTCP(SyncState
* const syncState
);
40 static void printMatchingStatsTCP(SyncState
* const syncState
);
41 static void writeMatchingGraphsPlotsTCPMessages(SyncState
* const syncState
,
42 const unsigned int i
, const unsigned int j
);
44 // Functions specific to this module
45 static void registerMatchingTCP() __attribute__((constructor (101)));
47 static void matchEvents(SyncState
* const syncState
, Event
* const event
,
48 GHashTable
* const unMatchedList
, GHashTable
* const
49 unMatchedOppositeList
, const size_t fieldOffset
, const size_t
51 static void partialDestroyMatchingTCP(SyncState
* const syncState
);
53 static bool isAck(const Message
* const message
);
54 static bool needsAck(const Message
* const message
);
55 static void buildReversedConnectionKey(ConnectionKey
* const
56 reversedConnectionKey
, const ConnectionKey
* const connectionKey
);
58 static void openGraphDataFiles(SyncState
* const syncState
);
59 static void closeGraphDataFiles(SyncState
* const syncState
);
60 static void writeMessagePoint(FILE* stream
, const Message
* const message
);
63 static MatchingModule matchingModuleTCP
= {
66 .canMatch
[UDP
]= false,
67 .initMatching
= &initMatchingTCP
,
68 .destroyMatching
= &destroyMatchingTCP
,
69 .matchEvent
= &matchEventTCP
,
70 .finalizeMatching
= &finalizeMatchingTCP
,
71 .printMatchingStats
= &printMatchingStatsTCP
,
73 .writeTraceTraceForePlots
= &writeMatchingGraphsPlotsTCPMessages
,
79 * Matching module registering function
81 static void registerMatchingTCP()
83 g_queue_push_tail(&matchingModules
, &matchingModuleTCP
);
88 * Matching init function
90 * This function is called at the beginning of a synchronization run for a set
93 * Allocate the matching specific data structures
96 * syncState container for synchronization data.
97 * This function allocates these matchingData members:
103 static void initMatchingTCP(SyncState
* const syncState
)
105 MatchingDataTCP
* matchingData
;
107 matchingData
= malloc(sizeof(MatchingDataTCP
));
108 syncState
->matchingData
= matchingData
;
110 matchingData
->unMatchedInE
= g_hash_table_new_full(&ghfSegmentKeyHash
,
111 &gefSegmentKeyEqual
, NULL
, &gdnDestroyEvent
);
112 matchingData
->unMatchedOutE
= g_hash_table_new_full(&ghfSegmentKeyHash
,
113 &gefSegmentKeyEqual
, NULL
, &gdnDestroyEvent
);
114 matchingData
->unAcked
= g_hash_table_new_full(&ghfConnectionKeyHash
,
115 &gefConnectionKeyEqual
, &gdnConnectionKeyDestroy
,
116 &gdnTCPSegmentListDestroy
);
118 if (syncState
->stats
)
122 matchingData
->stats
= calloc(1, sizeof(MatchingStatsTCP
));
123 matchingData
->stats
->totMessageArray
= malloc(syncState
->traceNb
*
124 sizeof(unsigned int*));
125 for (i
= 0; i
< syncState
->traceNb
; i
++)
127 matchingData
->stats
->totMessageArray
[i
]=
128 calloc(syncState
->traceNb
, sizeof(unsigned int));
133 matchingData
->stats
= NULL
;
136 if (syncState
->graphsStream
)
138 openGraphDataFiles(syncState
);
142 matchingData
->messagePoints
= NULL
;
148 * Matching destroy function
150 * Free the matching specific data structures
153 * syncState container for synchronization data.
154 * This function deallocates these matchingData members:
157 static void destroyMatchingTCP(SyncState
* const syncState
)
159 MatchingDataTCP
* matchingData
;
161 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
163 if (matchingData
== NULL
)
168 partialDestroyMatchingTCP(syncState
);
170 if (syncState
->stats
)
174 for (i
= 0; i
< syncState
->traceNb
; i
++)
176 free(matchingData
->stats
->totMessageArray
[i
]);
178 free(matchingData
->stats
->totMessageArray
);
179 free(matchingData
->stats
);
182 free(syncState
->matchingData
);
183 syncState
->matchingData
= NULL
;
188 * Free some of the matching specific data structures
190 * This function can be called right after the events have been processed to
191 * free some data structures that are not needed for finalization.
194 * syncState container for synchronization data.
195 * This function deallocates these matchingData members:
200 static void partialDestroyMatchingTCP(SyncState
* const syncState
)
202 MatchingDataTCP
* matchingData
;
204 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
206 if (matchingData
== NULL
|| matchingData
->unMatchedInE
== NULL
)
211 g_hash_table_destroy(matchingData
->unMatchedInE
);
212 matchingData
->unMatchedInE
= NULL
;
213 g_hash_table_destroy(matchingData
->unMatchedOutE
);
214 g_hash_table_destroy(matchingData
->unAcked
);
216 if (syncState
->graphsStream
&& matchingData
->messagePoints
)
218 closeGraphDataFiles(syncState
);
224 * Try to match one event from a trace with the corresponding event from
228 * syncState container for synchronization data.
229 * event new event to match
231 static void matchEventTCP(SyncState
* const syncState
, Event
* const event
)
233 MatchingDataTCP
* matchingData
;
235 g_assert(event
->type
== TCP
);
237 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
239 if (event
->event
.tcpEvent
->direction
== IN
)
241 matchEvents(syncState
, event
, matchingData
->unMatchedInE
,
242 matchingData
->unMatchedOutE
, offsetof(Message
, inE
),
243 offsetof(Message
, outE
));
247 matchEvents(syncState
, event
, matchingData
->unMatchedOutE
,
248 matchingData
->unMatchedInE
, offsetof(Message
, outE
),
249 offsetof(Message
, inE
));
255 * Call the partial matching destroyer and Obtain the factors from downstream
258 * syncState container for synchronization data.
261 * Factors[traceNb] synchronization factors for each trace
263 static GArray
* finalizeMatchingTCP(SyncState
* const syncState
)
265 partialDestroyMatchingTCP(syncState
);
267 return syncState
->analysisModule
->finalizeAnalysis(syncState
);
272 * Print statistics related to matching. Must be called after
276 * syncState container for synchronization data.
278 static void printMatchingStatsTCP(SyncState
* const syncState
)
281 MatchingDataTCP
* matchingData
;
283 if (!syncState
->stats
)
288 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
290 printf("TCP matching stats:\n");
291 printf("\ttotal input and output events matched together to form a packet: %u\n",
292 matchingData
->stats
->totPacket
);
294 printf("\tMessage traffic:\n");
296 for (i
= 0; i
< syncState
->traceNb
; i
++)
298 for (j
= i
+ 1; j
< syncState
->traceNb
; j
++)
300 printf("\t\t%3d - %-3d: sent %-10u received %-10u\n", i
, j
,
301 matchingData
->stats
->totMessageArray
[j
][i
],
302 matchingData
->stats
->totMessageArray
[i
][j
]);
306 if (syncState
->analysisModule
->analyzeExchange
!= NULL
)
308 printf("\ttotal packets identified needing an acknowledge: %u\n",
309 matchingData
->stats
->totPacketNeedAck
);
310 printf("\ttotal exchanges (four events matched together): %u\n",
311 matchingData
->stats
->totExchangeEffective
);
312 printf("\ttotal synchronization exchanges: %u\n",
313 matchingData
->stats
->totExchangeSync
);
319 * Implementation of a packet matching algorithm for TCP
322 * event: new event to match
323 * unMatchedList: list of unmatched events of the same type (send or
325 * unMatchedOppositeList: list of unmatched events of the opposite type of
327 * fieldOffset: offset of the Event field in the Message struct for the
328 * field of the type of event
329 * oppositeFieldOffset: offset of the Event field in the Message struct
330 * for the field of the opposite type of event
332 static void matchEvents(SyncState
* const syncState
, Event
* const event
,
333 GHashTable
* const unMatchedList
, GHashTable
* const unMatchedOppositeList
,
334 const size_t fieldOffset
, const size_t oppositeFieldOffset
)
336 Event
* companionEvent
;
338 MatchingDataTCP
* matchingData
;
341 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
343 companionEvent
= g_hash_table_lookup(unMatchedOppositeList
, event
->event
.tcpEvent
->segmentKey
);
344 if (companionEvent
!= NULL
)
346 g_debug("Found matching companion event, ");
348 // If it's there, remove it and create a Message
349 g_hash_table_steal(unMatchedOppositeList
, event
->event
.tcpEvent
->segmentKey
);
350 packet
= malloc(sizeof(Message
));
351 *((Event
**) ((void*) packet
+ fieldOffset
))= event
;
352 *((Event
**) ((void*) packet
+ oppositeFieldOffset
))= companionEvent
;
353 packet
->print
= &printTCPSegment
;
354 // Both events can now share the same segmentKey
355 free(packet
->outE
->event
.tcpEvent
->segmentKey
);
356 packet
->outE
->event
.tcpEvent
->segmentKey
= packet
->inE
->event
.tcpEvent
->segmentKey
;
358 if (syncState
->stats
)
360 matchingData
->stats
->totPacket
++;
361 matchingData
->stats
->totMessageArray
[packet
->inE
->traceNum
][packet
->outE
->traceNum
]++;
364 // Discard loopback traffic
365 if (packet
->inE
->traceNum
== packet
->outE
->traceNum
)
367 destroyTCPSegment(packet
);
371 if (syncState
->graphsStream
)
373 writeMessagePoint(matchingData
->messagePoints
[packet
->inE
->traceNum
][packet
->outE
->traceNum
],
377 if (syncState
->analysisModule
->analyzeMessage
!= NULL
)
379 syncState
->analysisModule
->analyzeMessage(syncState
, packet
);
382 // We can skip the rest of the algorithm if the analysis module is not
383 // interested in exchanges
384 if (syncState
->analysisModule
->analyzeExchange
== NULL
)
386 destroyTCPSegment(packet
);
390 // If this packet acknowleges some data ...
393 ConnectionKey oppositeConnectionKey
;
395 buildReversedConnectionKey(&oppositeConnectionKey
,
396 &event
->event
.tcpEvent
->segmentKey
->connectionKey
);
397 conUnAcked
= g_hash_table_lookup(matchingData
->unAcked
,
398 &oppositeConnectionKey
);
399 if (conUnAcked
!= NULL
)
401 Message
* ackedPacket
;
407 result
= g_queue_find_custom(conUnAcked
, packet
, &gcfTCPSegmentAckCompare
);
409 while (result
!= NULL
)
411 // Remove the acknowledged packet from the unAcked list
412 // and keep it for later offset calculations
413 g_debug("Found matching unAcked packet, ");
415 ackedPacket
= (Message
*) result
->data
;
416 g_queue_delete_link(conUnAcked
, result
);
418 if (syncState
->stats
)
420 matchingData
->stats
->totExchangeEffective
++;
423 if (exchange
== NULL
)
425 exchange
= malloc(sizeof(Exchange
));
426 exchange
->message
= packet
;
427 exchange
->acks
= g_queue_new();
430 g_queue_push_tail(exchange
->acks
, ackedPacket
);
432 result
= g_queue_find_custom(conUnAcked
, packet
,
433 &gcfTCPSegmentAckCompare
);
436 // It might be possible to do an offset calculation
437 if (exchange
!= NULL
)
439 ackedPacket
= g_queue_peek_tail(exchange
->acks
);
440 if (ackedPacket
->outE
->traceNum
!= packet
->inE
->traceNum
441 || ackedPacket
->inE
->traceNum
!=
442 packet
->outE
->traceNum
|| packet
->inE
->traceNum
==
443 packet
->outE
->traceNum
)
445 ackedPacket
->print(ackedPacket
);
446 packet
->print(packet
);
447 g_error("Disorganized exchange encountered during "
452 if (syncState
->stats
)
454 matchingData
->stats
->totExchangeSync
++;
457 syncState
->analysisModule
->analyzeExchange(syncState
,
461 exchange
->message
= NULL
;
462 destroyTCPExchange(exchange
);
467 if (needsAck(packet
))
469 if (syncState
->stats
)
471 matchingData
->stats
->totPacketNeedAck
++;
474 // If this packet will generate an ack, add it to the unAcked list
475 g_debug("Adding to unAcked, ");
476 conUnAcked
= g_hash_table_lookup(matchingData
->unAcked
,
477 &event
->event
.tcpEvent
->segmentKey
->connectionKey
);
478 if (conUnAcked
== NULL
)
480 ConnectionKey
* connectionKey
;
482 connectionKey
= malloc(sizeof(ConnectionKey
));
483 memcpy(connectionKey
, &event
->event
.tcpEvent
->segmentKey
->connectionKey
,
484 sizeof(ConnectionKey
));
485 g_hash_table_insert(matchingData
->unAcked
, connectionKey
,
486 conUnAcked
= g_queue_new());
488 g_queue_push_tail(conUnAcked
, packet
);
492 destroyTCPSegment(packet
);
497 // If there's no corresponding event, add the event to the unmatched
498 // list for this type of event
499 g_debug("Adding to unmatched event list, ");
500 g_hash_table_replace(unMatchedList
, event
->event
.tcpEvent
->segmentKey
, event
);
506 * Check if a packet is an acknowledge
515 static bool isAck(const Message
* const packet
)
517 if (packet
->inE
->event
.tcpEvent
->segmentKey
->ack
== 1)
529 * Check if a packet will increment the sequence number, thus needing an
536 * true if the packet will need an acknowledge
539 static bool needsAck(const Message
* const packet
)
541 if (packet
->inE
->event
.tcpEvent
->segmentKey
->syn
|| packet
->inE
->event
.tcpEvent
->segmentKey
->fin
||
542 packet
->inE
->event
.tcpEvent
->segmentKey
->tot_len
- packet
->inE
->event
.tcpEvent
->segmentKey
->ihl
* 4 -
543 packet
->inE
->event
.tcpEvent
->segmentKey
->doff
* 4 > 0)
555 * Populate a connection key structure for the opposite direction of a
559 * reversedConnectionKey the result, must be pre-allocated
560 * connectionKey the connection key to reverse
562 static void buildReversedConnectionKey(ConnectionKey
* const
563 reversedConnectionKey
, const ConnectionKey
* const connectionKey
)
565 reversedConnectionKey
->saddr
= connectionKey
->daddr
;
566 reversedConnectionKey
->daddr
= connectionKey
->saddr
;
567 reversedConnectionKey
->source
= connectionKey
->dest
;
568 reversedConnectionKey
->dest
= connectionKey
->source
;
573 * Create and open files used to store message points to genereate
574 * graphs. Allocate and populate array to store file pointers.
577 * syncState: container for synchronization data
579 static void openGraphDataFiles(SyncState
* const syncState
)
585 MatchingDataTCP
* matchingData
;
587 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
589 cwd
= changeToGraphsDir(syncState
->graphsDir
);
591 matchingData
->messagePoints
= malloc(syncState
->traceNb
* sizeof(FILE**));
592 for (i
= 0; i
< syncState
->traceNb
; i
++)
594 matchingData
->messagePoints
[i
]= malloc(syncState
->traceNb
*
596 for (j
= 0; j
< syncState
->traceNb
; j
++)
600 retval
= snprintf(name
, sizeof(name
),
601 "matching_tcp-%03u_to_%03u.data", j
, i
);
602 if (retval
> sizeof(name
) - 1)
604 name
[sizeof(name
) - 1]= '\0';
606 if ((matchingData
->messagePoints
[i
][j
]= fopen(name
, "w")) ==
609 g_error(strerror(errno
));
618 g_error(strerror(errno
));
625 * Write a message point to a file used to generate graphs
628 * stream: FILE*, file pointer where to write the point
629 * message: message for which to write the point
631 static void writeMessagePoint(FILE* stream
, const Message
* const message
)
635 if (message
->inE
->traceNum
< message
->outE
->traceNum
)
637 // CA is inE->traceNum
638 x
= message
->inE
->cpuTime
;
639 y
= message
->outE
->cpuTime
;
643 // CA is outE->traceNum
644 x
= message
->outE
->cpuTime
;
645 y
= message
->inE
->cpuTime
;
648 fprintf(stream
, "%20llu %20llu\n", x
, y
);
653 * Close files used to store convex hull points to genereate graphs.
654 * Deallocate array to store file pointers.
657 * syncState: container for synchronization data
659 static void closeGraphDataFiles(SyncState
* const syncState
)
662 MatchingDataTCP
* matchingData
;
665 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
667 if (matchingData
->messagePoints
== NULL
)
672 for (i
= 0; i
< syncState
->traceNb
; i
++)
674 for (j
= 0; j
< syncState
->traceNb
; j
++)
678 retval
= fclose(matchingData
->messagePoints
[i
][j
]);
681 g_error(strerror(errno
));
685 free(matchingData
->messagePoints
[i
]);
687 free(matchingData
->messagePoints
);
689 matchingData
->messagePoints
= NULL
;
694 * Write the matching-specific graph lines in the gnuplot script.
697 * syncState: container for synchronization data
698 * i: first trace number
699 * j: second trace number, garanteed to be larger than i
701 static void writeMatchingGraphsPlotsTCPMessages(SyncState
* const syncState
,
702 const unsigned int i
, const unsigned int j
)
704 fprintf(syncState
->graphsStream
,
705 "\t\"matching_tcp-%1$03d_to_%2$03d.data\" "
706 "title \"Sent messages\" with points linetype 4 "
707 "linecolor rgb \"#98fc66\" pointtype 9 pointsize 2, \\\n"
708 "\t\"matching_tcp-%2$03d_to_%1$03d.data\" "
709 "title \"Received messages\" with points linetype 4 "
710 "linecolor rgb \"#6699cc\" pointtype 11 pointsize 2, \\\n", i
, j
);