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(FILE* stream
, SyncState
* const
47 syncState
, const 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
,
77 .writeMatchingGraphsPlots
= &writeMatchingGraphsPlotsTCP
,
78 .writeMatchingGraphsOptions
= NULL
,
83 * Matching module registering function
85 static void registerMatchingTCP()
87 g_queue_push_tail(&matchingModules
, &matchingModuleTCP
);
92 * Matching init function
94 * This function is called at the beginning of a synchronization run for a set
97 * Allocate the matching specific data structures
100 * syncState container for synchronization data.
101 * This function allocates these matchingData members:
107 static void initMatchingTCP(SyncState
* const syncState
)
109 MatchingDataTCP
* matchingData
;
111 matchingData
= malloc(sizeof(MatchingDataTCP
));
112 syncState
->matchingData
= matchingData
;
114 matchingData
->unMatchedInE
= g_hash_table_new_full(&ghfSegmentKeyHash
,
115 &gefSegmentKeyEqual
, NULL
, &gdnDestroyEvent
);
116 matchingData
->unMatchedOutE
= g_hash_table_new_full(&ghfSegmentKeyHash
,
117 &gefSegmentKeyEqual
, NULL
, &gdnDestroyEvent
);
118 matchingData
->unAcked
= g_hash_table_new_full(&ghfConnectionKeyHash
,
119 &gefConnectionKeyEqual
, &gdnConnectionKeyDestroy
,
120 &gdnTCPSegmentListDestroy
);
122 if (syncState
->stats
)
126 matchingData
->stats
= calloc(1, sizeof(MatchingStatsTCP
));
127 matchingData
->stats
->totMessageArray
= malloc(syncState
->traceNb
*
128 sizeof(unsigned int*));
129 for (i
= 0; i
< syncState
->traceNb
; i
++)
131 matchingData
->stats
->totMessageArray
[i
]=
132 calloc(syncState
->traceNb
, sizeof(unsigned int));
137 matchingData
->stats
= NULL
;
140 if (syncState
->graphs
)
142 openGraphDataFiles(syncState
);
146 matchingData
->messagePoints
= NULL
;
152 * Matching destroy function
154 * Free the matching specific data structures
157 * syncState container for synchronization data.
158 * This function deallocates these matchingData members:
161 static void destroyMatchingTCP(SyncState
* const syncState
)
163 MatchingDataTCP
* matchingData
;
165 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
167 if (matchingData
== NULL
)
172 partialDestroyMatchingTCP(syncState
);
174 if (syncState
->stats
)
178 for (i
= 0; i
< syncState
->traceNb
; i
++)
180 free(matchingData
->stats
->totMessageArray
[i
]);
182 free(matchingData
->stats
->totMessageArray
);
183 free(matchingData
->stats
);
186 free(syncState
->matchingData
);
187 syncState
->matchingData
= NULL
;
192 * Free some of the matching specific data structures
194 * This function can be called right after the events have been processed to
195 * free some data structures that are not needed for finalization.
198 * syncState container for synchronization data.
199 * This function deallocates these matchingData members:
204 static void partialDestroyMatchingTCP(SyncState
* const syncState
)
206 MatchingDataTCP
* matchingData
;
208 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
210 if (matchingData
== NULL
|| matchingData
->unMatchedInE
== NULL
)
215 g_hash_table_destroy(matchingData
->unMatchedInE
);
216 matchingData
->unMatchedInE
= NULL
;
217 g_hash_table_destroy(matchingData
->unMatchedOutE
);
218 g_hash_table_destroy(matchingData
->unAcked
);
220 if (syncState
->graphs
&& matchingData
->messagePoints
)
222 closeGraphDataFiles(syncState
);
228 * Try to match one event from a trace with the corresponding event from
232 * syncState container for synchronization data.
233 * event new event to match
235 static void matchEventTCP(SyncState
* const syncState
, Event
* const event
)
237 MatchingDataTCP
* matchingData
;
239 g_assert(event
->type
== TCP
);
241 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
243 if (event
->event
.tcpEvent
->direction
== IN
)
245 matchEvents(syncState
, event
, matchingData
->unMatchedInE
,
246 matchingData
->unMatchedOutE
, offsetof(Message
, inE
),
247 offsetof(Message
, outE
));
251 matchEvents(syncState
, event
, matchingData
->unMatchedOutE
,
252 matchingData
->unMatchedInE
, offsetof(Message
, outE
),
253 offsetof(Message
, inE
));
259 * Call the partial matching destroyer and Obtain the factors from downstream
262 * syncState container for synchronization data.
265 * Factors[traceNb] synchronization factors for each trace
267 static GArray
* finalizeMatchingTCP(SyncState
* const syncState
)
269 partialDestroyMatchingTCP(syncState
);
271 return syncState
->analysisModule
->finalizeAnalysis(syncState
);
276 * Print statistics related to matching. Must be called after
280 * syncState container for synchronization data.
282 static void printMatchingStatsTCP(SyncState
* const syncState
)
285 MatchingDataTCP
* matchingData
;
287 if (!syncState
->stats
)
292 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
294 printf("TCP matching stats:\n");
295 printf("\ttotal input and output events matched together to form a packet: %u\n",
296 matchingData
->stats
->totPacket
);
298 printf("\tMessage traffic:\n");
300 for (i
= 0; i
< syncState
->traceNb
; i
++)
302 for (j
= i
+ 1; j
< syncState
->traceNb
; j
++)
304 printf("\t\t%3d - %-3d: sent %-10u received %-10u\n", i
, j
,
305 matchingData
->stats
->totMessageArray
[j
][i
],
306 matchingData
->stats
->totMessageArray
[i
][j
]);
310 if (syncState
->analysisModule
->analyzeExchange
!= NULL
)
312 printf("\ttotal packets identified needing an acknowledge: %u\n",
313 matchingData
->stats
->totPacketNeedAck
);
314 printf("\ttotal exchanges (four events matched together): %u\n",
315 matchingData
->stats
->totExchangeEffective
);
316 printf("\ttotal synchronization exchanges: %u\n",
317 matchingData
->stats
->totExchangeSync
);
323 * Implementation of a packet matching algorithm for TCP
326 * event: new event to match
327 * unMatchedList: list of unmatched events of the same type (send or
329 * unMatchedOppositeList: list of unmatched events of the opposite type of
331 * fieldOffset: offset of the Event field in the Message struct for the
332 * field of the type of event
333 * oppositeFieldOffset: offset of the Event field in the Message struct
334 * for the field of the opposite type of event
336 static void matchEvents(SyncState
* const syncState
, Event
* const event
,
337 GHashTable
* const unMatchedList
, GHashTable
* const unMatchedOppositeList
,
338 const size_t fieldOffset
, const size_t oppositeFieldOffset
)
340 Event
* companionEvent
;
342 MatchingDataTCP
* matchingData
;
345 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
347 companionEvent
= g_hash_table_lookup(unMatchedOppositeList
, event
->event
.tcpEvent
->segmentKey
);
348 if (companionEvent
!= NULL
)
350 g_debug("Found matching companion event, ");
352 // If it's there, remove it and create a Message
353 g_hash_table_steal(unMatchedOppositeList
, event
->event
.tcpEvent
->segmentKey
);
354 packet
= malloc(sizeof(Message
));
355 *((Event
**) ((void*) packet
+ fieldOffset
))= event
;
356 *((Event
**) ((void*) packet
+ oppositeFieldOffset
))= companionEvent
;
357 packet
->print
= &printTCPSegment
;
358 // Both events can now share the same segmentKey
359 free(packet
->outE
->event
.tcpEvent
->segmentKey
);
360 packet
->outE
->event
.tcpEvent
->segmentKey
= packet
->inE
->event
.tcpEvent
->segmentKey
;
362 if (syncState
->stats
)
364 matchingData
->stats
->totPacket
++;
365 matchingData
->stats
->totMessageArray
[packet
->inE
->traceNum
][packet
->outE
->traceNum
]++;
368 // Discard loopback traffic
369 if (packet
->inE
->traceNum
== packet
->outE
->traceNum
)
371 destroyTCPSegment(packet
);
375 if (syncState
->graphs
)
377 writeMessagePoint(matchingData
->messagePoints
[packet
->inE
->traceNum
][packet
->outE
->traceNum
],
381 if (syncState
->analysisModule
->analyzeMessage
!= NULL
)
383 syncState
->analysisModule
->analyzeMessage(syncState
, packet
);
386 // We can skip the rest of the algorithm if the analysis module is not
387 // interested in exchanges
388 if (syncState
->analysisModule
->analyzeExchange
== NULL
)
390 destroyTCPSegment(packet
);
394 // If this packet acknowleges some data ...
397 ConnectionKey oppositeConnectionKey
;
399 buildReversedConnectionKey(&oppositeConnectionKey
,
400 &event
->event
.tcpEvent
->segmentKey
->connectionKey
);
401 conUnAcked
= g_hash_table_lookup(matchingData
->unAcked
,
402 &oppositeConnectionKey
);
403 if (conUnAcked
!= NULL
)
405 Message
* ackedPacket
;
411 result
= g_queue_find_custom(conUnAcked
, packet
, &gcfTCPSegmentAckCompare
);
413 while (result
!= NULL
)
415 // Remove the acknowledged packet from the unAcked list
416 // and keep it for later offset calculations
417 g_debug("Found matching unAcked packet, ");
419 ackedPacket
= (Message
*) result
->data
;
420 g_queue_delete_link(conUnAcked
, result
);
422 if (syncState
->stats
)
424 matchingData
->stats
->totExchangeEffective
++;
427 if (exchange
== NULL
)
429 exchange
= malloc(sizeof(Exchange
));
430 exchange
->message
= packet
;
431 exchange
->acks
= g_queue_new();
434 g_queue_push_tail(exchange
->acks
, ackedPacket
);
436 result
= g_queue_find_custom(conUnAcked
, packet
,
437 &gcfTCPSegmentAckCompare
);
440 // It might be possible to do an offset calculation
441 if (exchange
!= NULL
)
443 ackedPacket
= g_queue_peek_tail(exchange
->acks
);
444 if (ackedPacket
->outE
->traceNum
!= packet
->inE
->traceNum
445 || ackedPacket
->inE
->traceNum
!=
446 packet
->outE
->traceNum
|| packet
->inE
->traceNum
==
447 packet
->outE
->traceNum
)
449 ackedPacket
->print(ackedPacket
);
450 packet
->print(packet
);
451 g_error("Disorganized exchange encountered during "
456 if (syncState
->stats
)
458 matchingData
->stats
->totExchangeSync
++;
461 syncState
->analysisModule
->analyzeExchange(syncState
,
465 exchange
->message
= NULL
;
466 destroyTCPExchange(exchange
);
471 if (needsAck(packet
))
473 if (syncState
->stats
)
475 matchingData
->stats
->totPacketNeedAck
++;
478 // If this packet will generate an ack, add it to the unAcked list
479 g_debug("Adding to unAcked, ");
480 conUnAcked
= g_hash_table_lookup(matchingData
->unAcked
,
481 &event
->event
.tcpEvent
->segmentKey
->connectionKey
);
482 if (conUnAcked
== NULL
)
484 ConnectionKey
* connectionKey
;
486 connectionKey
= malloc(sizeof(ConnectionKey
));
487 memcpy(connectionKey
, &event
->event
.tcpEvent
->segmentKey
->connectionKey
,
488 sizeof(ConnectionKey
));
489 g_hash_table_insert(matchingData
->unAcked
, connectionKey
,
490 conUnAcked
= g_queue_new());
492 g_queue_push_tail(conUnAcked
, packet
);
496 destroyTCPSegment(packet
);
501 // If there's no corresponding event, add the event to the unmatched
502 // list for this type of event
503 g_debug("Adding to unmatched event list, ");
504 g_hash_table_replace(unMatchedList
, event
->event
.tcpEvent
->segmentKey
, event
);
510 * Check if a packet is an acknowledge
519 static bool isAck(const Message
* const packet
)
521 if (packet
->inE
->event
.tcpEvent
->segmentKey
->ack
== 1)
533 * Check if a packet will increment the sequence number, thus needing an
540 * true if the packet will need an acknowledge
543 static bool needsAck(const Message
* const packet
)
545 if (packet
->inE
->event
.tcpEvent
->segmentKey
->syn
|| packet
->inE
->event
.tcpEvent
->segmentKey
->fin
||
546 packet
->inE
->event
.tcpEvent
->segmentKey
->tot_len
- packet
->inE
->event
.tcpEvent
->segmentKey
->ihl
* 4 -
547 packet
->inE
->event
.tcpEvent
->segmentKey
->doff
* 4 > 0)
559 * Populate a connection key structure for the opposite direction of a
563 * reversedConnectionKey the result, must be pre-allocated
564 * connectionKey the connection key to reverse
566 static void buildReversedConnectionKey(ConnectionKey
* const
567 reversedConnectionKey
, const ConnectionKey
* const connectionKey
)
569 reversedConnectionKey
->saddr
= connectionKey
->daddr
;
570 reversedConnectionKey
->daddr
= connectionKey
->saddr
;
571 reversedConnectionKey
->source
= connectionKey
->dest
;
572 reversedConnectionKey
->dest
= connectionKey
->source
;
577 * Create and open files used to store message points to genereate
578 * graphs. Allocate and populate array to store file pointers.
581 * syncState: container for synchronization data
583 static void openGraphDataFiles(SyncState
* const syncState
)
589 MatchingDataTCP
* matchingData
;
591 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
593 cwd
= changeToGraphDir(syncState
->graphs
);
595 matchingData
->messagePoints
= malloc(syncState
->traceNb
* sizeof(FILE**));
596 for (i
= 0; i
< syncState
->traceNb
; i
++)
598 matchingData
->messagePoints
[i
]= malloc(syncState
->traceNb
*
600 for (j
= 0; j
< syncState
->traceNb
; j
++)
604 retval
= snprintf(name
, sizeof(name
),
605 "matching_tcp-%03u_to_%03u.data", j
, i
);
606 if (retval
> sizeof(name
) - 1)
608 name
[sizeof(name
) - 1]= '\0';
610 if ((matchingData
->messagePoints
[i
][j
]= fopen(name
, "w")) ==
613 g_error(strerror(errno
));
622 g_error(strerror(errno
));
629 * Write a message point to a file used to generate graphs
632 * stream: FILE*, file pointer where to write the point
633 * message: message for which to write the point
635 static void writeMessagePoint(FILE* stream
, const Message
* const message
)
639 if (message
->inE
->traceNum
< message
->outE
->traceNum
)
641 // CA is inE->traceNum
642 x
= message
->inE
->cpuTime
;
643 y
= message
->outE
->cpuTime
;
647 // CA is outE->traceNum
648 x
= message
->outE
->cpuTime
;
649 y
= message
->inE
->cpuTime
;
652 fprintf(stream
, "%20llu %20llu\n", x
, y
);
657 * Close files used to store convex hull points to genereate graphs.
658 * Deallocate array to store file pointers.
661 * syncState: container for synchronization data
663 static void closeGraphDataFiles(SyncState
* const syncState
)
666 MatchingDataTCP
* matchingData
;
669 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
671 if (matchingData
->messagePoints
== NULL
)
676 for (i
= 0; i
< syncState
->traceNb
; i
++)
678 for (j
= 0; j
< syncState
->traceNb
; j
++)
682 retval
= fclose(matchingData
->messagePoints
[i
][j
]);
685 g_error(strerror(errno
));
689 free(matchingData
->messagePoints
[i
]);
691 free(matchingData
->messagePoints
);
693 matchingData
->messagePoints
= NULL
;
698 * Write the matching-specific graph lines in the gnuplot script.
701 * stream: stream where to write the data
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(FILE* stream
, SyncState
* const
707 syncState
, const unsigned int i
, const unsigned int j
)
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
);