Make the accuracy area easier to see on the broadcast graphs
[lttv.git] / lttv / lttv / sync / event_matching_broadcast.c
CommitLineData
f10c27a8
BP
1/* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2009 Benjamin Poirier <benjamin.poirier@polymtl.ca>
3 *
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;
7 *
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.
12 *
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,
16 * MA 02111-1307, USA.
17 */
18
19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
23#include <errno.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27
28#include "event_analysis.h"
2bd4b3e4 29#include "sync_chain.h"
f10c27a8
BP
30
31#include "event_matching_broadcast.h"
32
33
34#ifndef g_info
35#define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
36#endif
37
38
39// Functions common to all matching modules
40static void initMatchingBroadcast(SyncState* const syncState);
41static void destroyMatchingBroadcast(SyncState* const syncState);
42
43static void matchEventBroadcast(SyncState* const syncState, Event* const event);
44static GArray* finalizeMatchingBroadcast(SyncState* const syncState);
45static void printMatchingStatsBroadcast(SyncState* const syncState);
ffa21cfd
BP
46static void writeMatchingGraphsPlotsBroadcast(SyncState* const syncState, const
47 unsigned int i, const unsigned int j);
f10c27a8
BP
48
49// Functions specific to this module
50static void registerMatchingBroadcast() __attribute__((constructor (101)));
51
52static void partialDestroyMatchingBroadcast(SyncState* const syncState);
53
ffa21cfd
BP
54static void openGraphDataFiles(SyncState* const syncState);
55static void writeAccuracyPoints(MatchingGraphsBroadcast* graphs, const
56 Broadcast* const broadcast);
ffa21cfd
BP
57static void closeGraphDataFiles(SyncState* const syncState);
58
59
f10c27a8
BP
60static MatchingModule matchingModuleBroadcast = {
61 .name= "broadcast",
62 .canMatch[TCP]= false,
63 .canMatch[UDP]= true,
64 .initMatching= &initMatchingBroadcast,
65 .destroyMatching= &destroyMatchingBroadcast,
66 .matchEvent= &matchEventBroadcast,
67 .finalizeMatching= &finalizeMatchingBroadcast,
68 .printMatchingStats= &printMatchingStatsBroadcast,
467066ee 69 .graphFunctions= {
c6356aa7 70 .writeTraceTimeForePlots= &writeMatchingGraphsPlotsBroadcast,
467066ee 71 }
f10c27a8
BP
72};
73
74
75/*
76 * Matching module registering function
77 */
78static void registerMatchingBroadcast()
79{
80 g_queue_push_tail(&matchingModules, &matchingModuleBroadcast);
81}
82
83
84/*
85 * Matching init function
86 *
87 * This function is called at the beginning of a synchronization run for a set
88 * of traces.
89 *
90 * Allocate the matching specific data structures
91 *
92 * Args:
93 * syncState container for synchronization data.
94 * This function allocates these matchingData members:
95 * pendingBroadcasts
96 * stats
97 */
98static void initMatchingBroadcast(SyncState* const syncState)
99{
100 MatchingDataBroadcast* matchingData;
101
102 matchingData= malloc(sizeof(MatchingDataBroadcast));
103 syncState->matchingData= matchingData;
104
105 matchingData->pendingBroadcasts= g_hash_table_new_full(&ghfDatagramKeyHash,
106 &gefDatagramKeyEqual, &gdnDestroyDatagramKey, &gdnDestroyBroadcast);
107
108 if (syncState->stats)
109 {
110 matchingData->stats= calloc(1, sizeof(MatchingStatsBroadcast));
111 }
112 else
113 {
114 matchingData->stats= NULL;
115 }
ffa21cfd
BP
116
117 if (syncState->graphsStream)
118 {
119 matchingData->graphs= malloc(sizeof(MatchingGraphsBroadcast));
120 openGraphDataFiles(syncState);
121 }
122 else
123 {
124 matchingData->graphs= NULL;
125 }
f10c27a8
BP
126}
127
128
129/*
130 * Matching destroy function
131 *
132 * Free the matching specific data structures
133 *
134 * Args:
135 * syncState container for synchronization data.
136 * This function deallocates these matchingData members:
137 * stats
138 */
139static void destroyMatchingBroadcast(SyncState* const syncState)
140{
ffa21cfd
BP
141 MatchingDataBroadcast* matchingData= syncState->matchingData;
142 unsigned int i;
f10c27a8
BP
143
144 if (matchingData == NULL)
145 {
146 return;
147 }
148
149 partialDestroyMatchingBroadcast(syncState);
150
151 if (syncState->stats)
152 {
153 free(matchingData->stats);
154 }
155
ffa21cfd
BP
156 if (syncState->graphsStream)
157 {
158 for (i= 0; i < syncState->traceNb; i++)
159 {
160 free(matchingData->graphs->pointsNb[i]);
161 }
162 free(matchingData->graphs->pointsNb);
163 free(matchingData->graphs);
164 }
165
f10c27a8
BP
166 free(syncState->matchingData);
167 syncState->matchingData= NULL;
168}
169
170
171/*
172 * Free some of the matching specific data structures
173 *
174 * This function can be called right after the events have been processed to
175 * free some data structures that are not needed for finalization.
176 *
177 * Args:
178 * syncState container for synchronization data.
179 * This function deallocates these matchingData members:
180 * pendingBroadcasts
181 */
182static void partialDestroyMatchingBroadcast(SyncState* const syncState)
183{
184 MatchingDataBroadcast* matchingData;
185
186 matchingData= (MatchingDataBroadcast*) syncState->matchingData;
187
188 if (matchingData == NULL || matchingData->pendingBroadcasts == NULL)
189 {
190 return;
191 }
192
193 g_hash_table_destroy(matchingData->pendingBroadcasts);
194 matchingData->pendingBroadcasts= NULL;
ffa21cfd
BP
195
196 if (syncState->graphsStream && matchingData->graphs->accuracyPoints)
197 {
198 closeGraphDataFiles(syncState);
199 }
f10c27a8
BP
200}
201
202
203/*
204 * Try to match one broadcast with previously received broadcasts (based on
205 * the addresses and the fist bytes of data they contain). Deliver them to the
ffa21cfd 206 * analysis module once traceNb events have been accumulated for a broadcast.
f10c27a8
BP
207 *
208 * Args:
209 * syncState container for synchronization data.
210 * event new event to match
211 */
212static void matchEventBroadcast(SyncState* const syncState, Event* const event)
213{
214 MatchingDataBroadcast* matchingData;
215
216 g_assert(event->type == UDP);
217
218 matchingData= (MatchingDataBroadcast*) syncState->matchingData;
219
220 if (!event->event.udpEvent->unicast)
221 {
222 if (event->event.udpEvent->direction == IN)
223 {
224 Broadcast* broadcast;
225 DatagramKey* datagramKey;
226 gboolean result;
227
228 if (matchingData->stats)
229 {
230 matchingData->stats->totReceive++;
231 }
232
d4721e1a
BP
233 /* if event in pendingBroadcasts:
234 * add it to its broadcast
235 * if this broadcast has traceNb events:
236 * remove it from pending and deliver it to analysis
237 * destroy the broadcast (and its elements)
238 * else:
239 * create a broadcast and add it to pending
240 */
f10c27a8
BP
241
242 result=
243 g_hash_table_lookup_extended(matchingData->pendingBroadcasts,
244 event->event.udpEvent->datagramKey, (gpointer)
245 &datagramKey, (gpointer) &broadcast);
246 if (result)
247 {
248 g_queue_push_tail(broadcast->events, event);
249 if (broadcast->events->length == syncState->traceNb)
250 {
2bd4b3e4
BP
251 if (matchingData->stats)
252 {
253 matchingData->stats->totComplete++;
254 }
255
f10c27a8
BP
256 g_hash_table_steal(matchingData->pendingBroadcasts, datagramKey);
257 free(datagramKey);
258 syncState->analysisModule->analyzeBroadcast(syncState, broadcast);
ffa21cfd
BP
259
260 if (syncState->graphsStream)
261 {
262 writeAccuracyPoints(matchingData->graphs, broadcast);
263 }
f10c27a8
BP
264 destroyBroadcast(broadcast);
265 }
266 }
267 else
268 {
269 broadcast= malloc(sizeof(Broadcast));
270 broadcast->events= g_queue_new();
271 g_queue_push_tail(broadcast->events, event);
2bd4b3e4
BP
272
273 datagramKey= malloc(sizeof(DatagramKey));
274 *datagramKey= *event->event.udpEvent->datagramKey;
275
276 g_hash_table_insert(matchingData->pendingBroadcasts,
277 datagramKey, broadcast);
f10c27a8
BP
278 }
279 }
280 else
281 {
282 if (matchingData->stats)
283 {
284 matchingData->stats->totTransmit++;
285 }
286
287 event->destroy(event);
288 }
289 }
290 else
291 {
292 event->destroy(event);
293 }
294
295}
296
297
298/*
299 * Call the partial matching destroyer and Obtain the factors from downstream
300 *
301 * Args:
302 * syncState container for synchronization data.
303 *
304 * Returns:
305 * Factors[traceNb] synchronization factors for each trace
306 */
307static GArray* finalizeMatchingBroadcast(SyncState* const syncState)
308{
309 MatchingDataBroadcast* matchingData;
310
311 matchingData= (MatchingDataBroadcast*) syncState->matchingData;
312
313 if (matchingData->stats)
314 {
315 matchingData->stats->totIncomplete=
316 g_hash_table_size(matchingData->pendingBroadcasts);
317 }
318
319 partialDestroyMatchingBroadcast(syncState);
320
321 return syncState->analysisModule->finalizeAnalysis(syncState);
322}
323
324
325/*
d6ee5003
BP
326 * Print statistics related to matching. Must be called after
327 * finalizeMatching.
f10c27a8
BP
328 *
329 * Args:
330 * syncState container for synchronization data.
331 */
332static void printMatchingStatsBroadcast(SyncState* const syncState)
333{
334 MatchingDataBroadcast* matchingData;
335
336 if (!syncState->stats)
337 {
338 return;
339 }
f10c27a8
BP
340 matchingData= (MatchingDataBroadcast*) syncState->matchingData;
341
342 printf("Broadcast matching stats:\n");
343 printf("\ttotal broadcasts datagrams emitted: %u\n",
344 matchingData->stats->totTransmit);
345 printf("\ttotal broadcasts datagrams received: %u\n",
346 matchingData->stats->totReceive);
76be6fc2 347 printf("\ttotal broadcast groups for which all receptions were identified: %u\n",
f10c27a8 348 matchingData->stats->totComplete);
76be6fc2 349 printf("\ttotal broadcast groups missing some receptions: %u\n",
f10c27a8
BP
350 matchingData->stats->totIncomplete);
351 if (matchingData->stats->totIncomplete > 0)
352 {
353 printf("\taverage number of broadcast datagrams received in incomplete groups: %f\n",
354 (double) (matchingData->stats->totReceive -
355 matchingData->stats->totComplete * syncState->traceNb) /
356 matchingData->stats->totIncomplete);
357 }
f10c27a8 358}
ffa21cfd
BP
359
360
361/*
362 * Create and open files used to store accuracy points to genereate graphs.
363 * Allocate and populate array to store file pointers and counters.
364 *
365 * Args:
366 * syncState: container for synchronization data
367 */
368static void openGraphDataFiles(SyncState* const syncState)
369{
370 unsigned int i, j;
371 int retval;
372 char* cwd;
373 char name[36];
374 MatchingGraphsBroadcast* graphs= ((MatchingDataBroadcast*)
375 syncState->matchingData)->graphs;
376
377 cwd= changeToGraphDir(syncState->graphsDir);
378
379 graphs->accuracyPoints= malloc(syncState->traceNb * sizeof(FILE**));
380 graphs->pointsNb= malloc(syncState->traceNb * sizeof(unsigned int*));
381 for (i= 0; i < syncState->traceNb; i++)
382 {
383 graphs->accuracyPoints[i]= malloc(i * sizeof(FILE*));
384 graphs->pointsNb[i]= calloc(i, sizeof(unsigned int));
385 for (j= 0; j < i; j++)
386 {
387 retval= snprintf(name, sizeof(name),
388 "matching_broadcast-%03u_and_%03u.data", j, i);
389 g_assert_cmpint(retval, <=, sizeof(name) - 1);
390 if ((graphs->accuracyPoints[i][j]= fopen(name, "w")) == NULL)
391 {
392 g_error(strerror(errno));
393 }
394 }
395 }
396
397 retval= chdir(cwd);
398 if (retval == -1)
399 {
400 g_error(strerror(errno));
401 }
402 free(cwd);
403}
404
405
406/*
407 * Calculate and write points used to generate graphs
408 *
409 * Args:
410 * graphs: structure containing array of file pointers and counters
411 * broadcast: broadcast for which to write the points
412 */
413static void writeAccuracyPoints(MatchingGraphsBroadcast* graphs, const
414 Broadcast* const broadcast)
415{
416 unsigned int i, j;
417 GArray* events;
418 unsigned int eventNb= broadcast->events->length;
419
420 events= g_array_sized_new(FALSE, FALSE, sizeof(Event*), eventNb);
66eaf2eb 421 g_queue_foreach(broadcast->events, &gfAddEventToArray, events);
ffa21cfd
BP
422
423 for (i= 0; i < eventNb; i++)
424 {
425 for (j= 0; j < eventNb; j++)
426 {
427 Event* eventI= g_array_index(events, Event*, i), * eventJ=
428 g_array_index(events, Event*, j);
429
430 if (eventI->traceNum < eventJ->traceNum)
431 {
432 fprintf(graphs->accuracyPoints[eventJ->traceNum][eventI->traceNum],
467066ee
BP
433 "%20llu %20.9f\n", eventI->cpuTime,
434 wallTimeSub(&eventJ->wallTime, &eventI->wallTime));
ffa21cfd
BP
435 graphs->pointsNb[eventJ->traceNum][eventI->traceNum]++;
436 }
437 }
438 }
ffa21cfd 439
66eaf2eb 440 g_array_free(events, TRUE);
ffa21cfd
BP
441}
442
443
444/*
445 * Close files used to store accuracy points to genereate graphs. Deallocate
446 * array to store file pointers (but not array for counters).
447 *
448 * Args:
449 * syncState: container for synchronization data
450 */
451static void closeGraphDataFiles(SyncState* const syncState)
452{
453 unsigned int i, j;
454 MatchingGraphsBroadcast* graphs= ((MatchingDataBroadcast*)
455 syncState->matchingData)->graphs;
456 int retval;
457
458 if (graphs->accuracyPoints == NULL)
459 {
460 return;
461 }
462
463 for (i= 0; i < syncState->traceNb; i++)
464 {
465 for (j= 0; j < i; j++)
466 {
467 retval= fclose(graphs->accuracyPoints[i][j]);
468 if (retval != 0)
469 {
470 g_error(strerror(errno));
471 }
472 }
473 free(graphs->accuracyPoints[i]);
474 }
475 free(graphs->accuracyPoints);
476
477 graphs->accuracyPoints= NULL;
478}
479
480
481/*
482 * Write the matching-specific graph lines in the gnuplot script.
483 *
484 * Args:
485 * syncState: container for synchronization data
486 * i: first trace number
487 * j: second trace number, garanteed to be larger than i
488 */
489static void writeMatchingGraphsPlotsBroadcast(SyncState* const syncState, const
490 unsigned int i, const unsigned int j)
491{
492 if (((MatchingDataBroadcast*)
493 syncState->matchingData)->graphs->pointsNb[j][i])
494 {
495 fprintf(syncState->graphsStream,
496 "\t\"matching_broadcast-%03d_and_%03d.data\" "
c5571851 497 "title \"Broadcast differential delays\" with points "
ffa21cfd
BP
498 "linecolor rgb \"black\" pointtype 6 pointsize 2, \\\n", i,
499 j);
500 }
501}
This page took 0.042643 seconds and 4 git commands to generate.