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,
29 #include "event_analysis.h"
30 #include "sync_chain.h"
32 #include "event_matching_tcp.h"
35 // Functions common to all matching modules
36 static void initMatchingTCP(SyncState
* const syncState
);
37 static void destroyMatchingTCP(SyncState
* const syncState
);
39 static void matchEventTCP(SyncState
* const syncState
, Event
* const event
);
40 static GArray
* finalizeMatchingTCP(SyncState
* const syncState
);
41 static void printMatchingStatsTCP(SyncState
* const syncState
);
42 static void writeMatchingGraphsPlotsTCPMessages(SyncState
* const syncState
,
43 const unsigned int i
, const unsigned int j
);
45 // Functions specific to this module
46 static void matchEvents(SyncState
* const syncState
, Event
* const event
,
47 GHashTable
* const unMatchedList
, GHashTable
* const
48 unMatchedOppositeList
, const size_t fieldOffset
, const size_t
50 static void partialDestroyMatchingTCP(SyncState
* const syncState
);
52 static bool isAck(const Message
* const message
);
53 static bool needsAck(const Message
* const message
);
54 static void buildReversedConnectionKey(ConnectionKey
* const
55 reversedConnectionKey
, const ConnectionKey
* const connectionKey
);
57 static void openGraphDataFiles(SyncState
* const syncState
);
58 static void closeGraphDataFiles(SyncState
* const syncState
);
59 static void writeMessagePoint(FILE* stream
, const Message
* const message
);
62 static MatchingModule matchingModuleTCP
= {
65 .canMatch
[UDP
]= false,
66 .initMatching
= &initMatchingTCP
,
67 .destroyMatching
= &destroyMatchingTCP
,
68 .matchEvent
= &matchEventTCP
,
69 .finalizeMatching
= &finalizeMatchingTCP
,
70 .printMatchingStats
= &printMatchingStatsTCP
,
72 .writeTraceTraceForePlots
= &writeMatchingGraphsPlotsTCPMessages
,
78 * Matching module registering function
80 void registerMatchingTCP()
82 g_queue_push_tail(&matchingModules
, &matchingModuleTCP
);
87 * Matching init function
89 * This function is called at the beginning of a synchronization run for a set
92 * Allocate the matching specific data structures
95 * syncState container for synchronization data.
96 * This function allocates these matchingData members:
102 static void initMatchingTCP(SyncState
* const syncState
)
104 MatchingDataTCP
* matchingData
;
106 matchingData
= malloc(sizeof(MatchingDataTCP
));
107 syncState
->matchingData
= matchingData
;
109 matchingData
->unMatchedInE
= g_hash_table_new_full(&ghfSegmentKeyHash
,
110 &gefSegmentKeyEqual
, NULL
, &gdnDestroyEvent
);
111 matchingData
->unMatchedOutE
= g_hash_table_new_full(&ghfSegmentKeyHash
,
112 &gefSegmentKeyEqual
, NULL
, &gdnDestroyEvent
);
113 matchingData
->unAcked
= g_hash_table_new_full(&ghfConnectionKeyHash
,
114 &gefConnectionKeyEqual
, &gdnConnectionKeyDestroy
,
115 &gdnTCPSegmentListDestroy
);
117 if (syncState
->stats
)
121 matchingData
->stats
= calloc(1, sizeof(MatchingStatsTCP
));
122 matchingData
->stats
->totMessageArray
= malloc(syncState
->traceNb
*
123 sizeof(unsigned int*));
124 for (i
= 0; i
< syncState
->traceNb
; i
++)
126 matchingData
->stats
->totMessageArray
[i
]=
127 calloc(syncState
->traceNb
, sizeof(unsigned int));
132 matchingData
->stats
= NULL
;
135 if (syncState
->graphsStream
)
137 openGraphDataFiles(syncState
);
141 matchingData
->messagePoints
= NULL
;
147 * Matching destroy function
149 * Free the matching specific data structures
152 * syncState container for synchronization data.
153 * This function deallocates these matchingData members:
156 static void destroyMatchingTCP(SyncState
* const syncState
)
158 MatchingDataTCP
* matchingData
;
160 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
162 if (matchingData
== NULL
)
167 partialDestroyMatchingTCP(syncState
);
169 if (syncState
->stats
)
173 for (i
= 0; i
< syncState
->traceNb
; i
++)
175 free(matchingData
->stats
->totMessageArray
[i
]);
177 free(matchingData
->stats
->totMessageArray
);
178 free(matchingData
->stats
);
181 free(syncState
->matchingData
);
182 syncState
->matchingData
= NULL
;
187 * Free some of the matching specific data structures
189 * This function can be called right after the events have been processed to
190 * free some data structures that are not needed for finalization.
193 * syncState container for synchronization data.
194 * This function deallocates these matchingData members:
199 static void partialDestroyMatchingTCP(SyncState
* const syncState
)
201 MatchingDataTCP
* matchingData
;
203 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
205 if (matchingData
== NULL
|| matchingData
->unMatchedInE
== NULL
)
210 g_hash_table_destroy(matchingData
->unMatchedInE
);
211 matchingData
->unMatchedInE
= NULL
;
212 g_hash_table_destroy(matchingData
->unMatchedOutE
);
213 g_hash_table_destroy(matchingData
->unAcked
);
215 if (syncState
->graphsStream
&& matchingData
->messagePoints
)
217 closeGraphDataFiles(syncState
);
223 * Try to match one event from a trace with the corresponding event from
227 * syncState container for synchronization data.
228 * event new event to match
230 static void matchEventTCP(SyncState
* const syncState
, Event
* const event
)
232 MatchingDataTCP
* matchingData
;
234 g_assert(event
->type
== TCP
);
236 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
238 if (event
->event
.tcpEvent
->direction
== IN
)
240 matchEvents(syncState
, event
, matchingData
->unMatchedInE
,
241 matchingData
->unMatchedOutE
, offsetof(Message
, inE
),
242 offsetof(Message
, outE
));
246 matchEvents(syncState
, event
, matchingData
->unMatchedOutE
,
247 matchingData
->unMatchedInE
, offsetof(Message
, outE
),
248 offsetof(Message
, inE
));
254 * Call the partial matching destroyer and Obtain the factors from downstream
257 * syncState container for synchronization data.
260 * Factors[traceNb] synchronization factors for each trace
262 static GArray
* finalizeMatchingTCP(SyncState
* const syncState
)
264 partialDestroyMatchingTCP(syncState
);
266 return syncState
->analysisModule
->finalizeAnalysis(syncState
);
271 * Print statistics related to matching. Must be called after
275 * syncState container for synchronization data.
277 static void printMatchingStatsTCP(SyncState
* const syncState
)
280 MatchingDataTCP
* matchingData
;
282 if (!syncState
->stats
)
287 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
289 printf("TCP matching stats:\n");
290 printf("\ttotal input and output events matched together to form a packet: %u\n",
291 matchingData
->stats
->totPacket
);
293 printf("\tMessage traffic:\n");
295 for (i
= 0; i
< syncState
->traceNb
; i
++)
297 for (j
= i
+ 1; j
< syncState
->traceNb
; j
++)
299 printf("\t\t%3d - %-3d: sent %-10u received %-10u\n", i
, j
,
300 matchingData
->stats
->totMessageArray
[j
][i
],
301 matchingData
->stats
->totMessageArray
[i
][j
]);
305 if (syncState
->analysisModule
->analyzeExchange
!= NULL
)
307 printf("\ttotal packets identified needing an acknowledge: %u\n",
308 matchingData
->stats
->totPacketNeedAck
);
309 printf("\ttotal exchanges (four events matched together): %u\n",
310 matchingData
->stats
->totExchangeEffective
);
311 printf("\ttotal synchronization exchanges: %u\n",
312 matchingData
->stats
->totExchangeSync
);
318 * Implementation of a packet matching algorithm for TCP
321 * event: new event to match
322 * unMatchedList: list of unmatched events of the same type (send or
324 * unMatchedOppositeList: list of unmatched events of the opposite type of
326 * fieldOffset: offset of the Event field in the Message struct for the
327 * field of the type of event
328 * oppositeFieldOffset: offset of the Event field in the Message struct
329 * for the field of the opposite type of event
331 static void matchEvents(SyncState
* const syncState
, Event
* const event
,
332 GHashTable
* const unMatchedList
, GHashTable
* const unMatchedOppositeList
,
333 const size_t fieldOffset
, const size_t oppositeFieldOffset
)
335 Event
* companionEvent
;
337 MatchingDataTCP
* matchingData
;
340 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
342 companionEvent
= g_hash_table_lookup(unMatchedOppositeList
, event
->event
.tcpEvent
->segmentKey
);
343 if (companionEvent
!= NULL
)
345 g_debug("Found matching companion event, ");
347 // If it's there, remove it and create a Message
348 g_hash_table_steal(unMatchedOppositeList
, event
->event
.tcpEvent
->segmentKey
);
349 packet
= malloc(sizeof(Message
));
350 *((Event
**) ((void*) packet
+ fieldOffset
))= event
;
351 *((Event
**) ((void*) packet
+ oppositeFieldOffset
))= companionEvent
;
352 packet
->print
= &printTCPSegment
;
353 // Both events can now share the same segmentKey
354 free(packet
->outE
->event
.tcpEvent
->segmentKey
);
355 packet
->outE
->event
.tcpEvent
->segmentKey
= packet
->inE
->event
.tcpEvent
->segmentKey
;
357 if (syncState
->stats
)
359 matchingData
->stats
->totPacket
++;
360 matchingData
->stats
->totMessageArray
[packet
->inE
->traceNum
][packet
->outE
->traceNum
]++;
363 // Discard loopback traffic
364 if (packet
->inE
->traceNum
== packet
->outE
->traceNum
)
366 destroyTCPSegment(packet
);
370 if (syncState
->graphsStream
)
372 writeMessagePoint(matchingData
->messagePoints
[packet
->inE
->traceNum
][packet
->outE
->traceNum
],
376 if (syncState
->analysisModule
->analyzeMessage
!= NULL
)
378 syncState
->analysisModule
->analyzeMessage(syncState
, packet
);
381 // We can skip the rest of the algorithm if the analysis module is not
382 // interested in exchanges
383 if (syncState
->analysisModule
->analyzeExchange
== NULL
)
385 destroyTCPSegment(packet
);
389 // If this packet acknowleges some data ...
392 ConnectionKey oppositeConnectionKey
;
394 buildReversedConnectionKey(&oppositeConnectionKey
,
395 &event
->event
.tcpEvent
->segmentKey
->connectionKey
);
396 conUnAcked
= g_hash_table_lookup(matchingData
->unAcked
,
397 &oppositeConnectionKey
);
398 if (conUnAcked
!= NULL
)
400 Message
* ackedPacket
;
406 result
= g_queue_find_custom(conUnAcked
, packet
, &gcfTCPSegmentAckCompare
);
408 while (result
!= NULL
)
410 // Remove the acknowledged packet from the unAcked list
411 // and keep it for later offset calculations
412 g_debug("Found matching unAcked packet, ");
414 ackedPacket
= (Message
*) result
->data
;
415 g_queue_delete_link(conUnAcked
, result
);
417 if (syncState
->stats
)
419 matchingData
->stats
->totExchangeEffective
++;
422 if (exchange
== NULL
)
424 exchange
= malloc(sizeof(Exchange
));
425 exchange
->message
= packet
;
426 exchange
->acks
= g_queue_new();
429 g_queue_push_tail(exchange
->acks
, ackedPacket
);
431 result
= g_queue_find_custom(conUnAcked
, packet
,
432 &gcfTCPSegmentAckCompare
);
435 // It might be possible to do an offset calculation
436 if (exchange
!= NULL
)
438 ackedPacket
= g_queue_peek_tail(exchange
->acks
);
439 if (ackedPacket
->outE
->traceNum
!= packet
->inE
->traceNum
440 || ackedPacket
->inE
->traceNum
!=
441 packet
->outE
->traceNum
|| packet
->inE
->traceNum
==
442 packet
->outE
->traceNum
)
444 ackedPacket
->print(ackedPacket
);
445 packet
->print(packet
);
446 g_error("Disorganized exchange encountered during "
451 if (syncState
->stats
)
453 matchingData
->stats
->totExchangeSync
++;
456 syncState
->analysisModule
->analyzeExchange(syncState
,
460 exchange
->message
= NULL
;
461 destroyTCPExchange(exchange
);
466 if (needsAck(packet
))
468 if (syncState
->stats
)
470 matchingData
->stats
->totPacketNeedAck
++;
473 // If this packet will generate an ack, add it to the unAcked list
474 g_debug("Adding to unAcked, ");
475 conUnAcked
= g_hash_table_lookup(matchingData
->unAcked
,
476 &event
->event
.tcpEvent
->segmentKey
->connectionKey
);
477 if (conUnAcked
== NULL
)
479 ConnectionKey
* connectionKey
;
481 connectionKey
= malloc(sizeof(ConnectionKey
));
482 memcpy(connectionKey
, &event
->event
.tcpEvent
->segmentKey
->connectionKey
,
483 sizeof(ConnectionKey
));
484 g_hash_table_insert(matchingData
->unAcked
, connectionKey
,
485 conUnAcked
= g_queue_new());
487 g_queue_push_tail(conUnAcked
, packet
);
491 destroyTCPSegment(packet
);
496 // If there's no corresponding event, add the event to the unmatched
497 // list for this type of event
498 g_debug("Adding to unmatched event list, ");
499 g_hash_table_replace(unMatchedList
, event
->event
.tcpEvent
->segmentKey
, event
);
505 * Check if a packet is an acknowledge
514 static bool isAck(const Message
* const packet
)
516 if (packet
->inE
->event
.tcpEvent
->segmentKey
->ack
== 1)
528 * Check if a packet will increment the sequence number, thus needing an
535 * true if the packet will need an acknowledge
538 static bool needsAck(const Message
* const packet
)
540 if (packet
->inE
->event
.tcpEvent
->segmentKey
->syn
|| packet
->inE
->event
.tcpEvent
->segmentKey
->fin
||
541 packet
->inE
->event
.tcpEvent
->segmentKey
->tot_len
- packet
->inE
->event
.tcpEvent
->segmentKey
->ihl
* 4 -
542 packet
->inE
->event
.tcpEvent
->segmentKey
->doff
* 4 > 0)
554 * Populate a connection key structure for the opposite direction of a
558 * reversedConnectionKey the result, must be pre-allocated
559 * connectionKey the connection key to reverse
561 static void buildReversedConnectionKey(ConnectionKey
* const
562 reversedConnectionKey
, const ConnectionKey
* const connectionKey
)
564 reversedConnectionKey
->saddr
= connectionKey
->daddr
;
565 reversedConnectionKey
->daddr
= connectionKey
->saddr
;
566 reversedConnectionKey
->source
= connectionKey
->dest
;
567 reversedConnectionKey
->dest
= connectionKey
->source
;
572 * Create and open files used to store message points to genereate
573 * graphs. Allocate and populate array to store file pointers.
576 * syncState: container for synchronization data
578 static void openGraphDataFiles(SyncState
* const syncState
)
584 MatchingDataTCP
* matchingData
;
586 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
588 cwd
= changeToGraphsDir(syncState
->graphsDir
);
590 matchingData
->messagePoints
= malloc(syncState
->traceNb
* sizeof(FILE**));
591 for (i
= 0; i
< syncState
->traceNb
; i
++)
593 matchingData
->messagePoints
[i
]= malloc(syncState
->traceNb
*
595 for (j
= 0; j
< syncState
->traceNb
; j
++)
599 retval
= snprintf(name
, sizeof(name
),
600 "matching_tcp-%03u_to_%03u.data", j
, i
);
601 if (retval
> sizeof(name
) - 1)
603 name
[sizeof(name
) - 1]= '\0';
605 if ((matchingData
->messagePoints
[i
][j
]= fopen(name
, "w")) ==
608 g_error(strerror(errno
));
617 g_error(strerror(errno
));
624 * Write a message point to a file used to generate graphs
627 * stream: FILE*, file pointer where to write the point
628 * message: message for which to write the point
630 static void writeMessagePoint(FILE* stream
, const Message
* const message
)
634 if (message
->inE
->traceNum
< message
->outE
->traceNum
)
636 // CA is inE->traceNum
637 x
= message
->inE
->cpuTime
;
638 y
= message
->outE
->cpuTime
;
642 // CA is outE->traceNum
643 x
= message
->outE
->cpuTime
;
644 y
= message
->inE
->cpuTime
;
647 fprintf(stream
, "%20" PRIu64
" %20" PRIu64
"\n", x
, y
);
652 * Close files used to store convex hull points to genereate graphs.
653 * Deallocate array to store file pointers.
656 * syncState: container for synchronization data
658 static void closeGraphDataFiles(SyncState
* const syncState
)
661 MatchingDataTCP
* matchingData
;
664 matchingData
= (MatchingDataTCP
*) syncState
->matchingData
;
666 if (matchingData
->messagePoints
== NULL
)
671 for (i
= 0; i
< syncState
->traceNb
; i
++)
673 for (j
= 0; j
< syncState
->traceNb
; j
++)
677 retval
= fclose(matchingData
->messagePoints
[i
][j
]);
680 g_error(strerror(errno
));
684 free(matchingData
->messagePoints
[i
]);
686 free(matchingData
->messagePoints
);
688 matchingData
->messagePoints
= NULL
;
693 * Write the matching-specific graph lines in the gnuplot script.
696 * syncState: container for synchronization data
697 * i: first trace number
698 * j: second trace number, garanteed to be larger than i
700 static void writeMatchingGraphsPlotsTCPMessages(SyncState
* const syncState
,
701 const unsigned int i
, const unsigned int j
)
703 fprintf(syncState
->graphsStream
,
704 "\t\"matching_tcp-%1$03d_to_%2$03d.data\" "
705 "title \"Sent messages\" with points linetype 4 "
706 "linecolor rgb \"#98fc66\" pointtype 9 pointsize 2, \\\n"
707 "\t\"matching_tcp-%2$03d_to_%1$03d.data\" "
708 "title \"Received messages\" with points linetype 4 "
709 "linecolor rgb \"#6699cc\" pointtype 11 pointsize 2, \\\n", i
, j
);