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,
18 #define _ISOC99_SOURCE
31 #include "sync_chain.h"
33 #include "event_analysis_chull.h"
37 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
55 // Functions common to all analysis modules
56 static void initAnalysisCHull(SyncState
* const syncState
);
57 static void destroyAnalysisCHull(SyncState
* const syncState
);
59 static void analyzeMessageCHull(SyncState
* const syncState
, Message
* const
61 static GArray
* finalizeAnalysisCHull(SyncState
* const syncState
);
62 static void printAnalysisStatsCHull(SyncState
* const syncState
);
63 static void writeAnalysisGraphsPlotsCHull(SyncState
* const syncState
, const
64 unsigned int i
, const unsigned int j
);
66 // Functions specific to this module
67 static void registerAnalysisCHull() __attribute__((constructor (101)));
69 static void openGraphFiles(SyncState
* const syncState
);
70 static void closeGraphFiles(SyncState
* const syncState
);
71 static void writeGraphFiles(SyncState
* const syncState
);
72 static void gfDumpHullToFile(gpointer data
, gpointer userData
);
74 static void grahamScan(GQueue
* const hull
, Point
* const newPoint
, const
76 static int jointCmp(const Point
* const p1
, const Point
* const p2
, const Point
*
77 const p3
) __attribute__((pure
));
78 static double crossProductK(const Point
const* p1
, const Point
const* p2
,
79 const Point
const* p3
, const Point
const* p4
) __attribute__((pure
));
80 static FactorsCHull
** calculateAllFactors(SyncState
* const syncState
);
81 static Factors
* calculateFactorsExact(GQueue
* const cu
, GQueue
* const cl
, const
82 LineType lineType
) __attribute__((pure
));
83 static void calculateFactorsMiddle(FactorsCHull
* factors
);
84 static void calculateFactorsFallback(GQueue
* const cr
, GQueue
* const cs
,
85 FactorsCHull
* const result
);
86 static double slope(const Point
* const p1
, const Point
* const p2
)
87 __attribute__((pure
));
88 static double intercept(const Point
* const p1
, const Point
* const p2
)
89 __attribute__((pure
));
90 static GArray
* reduceFactors(SyncState
* const syncState
, FactorsCHull
**
92 static void freeAllFactors(const SyncState
* const syncState
, FactorsCHull
**
94 static double verticalDistance(Point
* p1
, Point
* p2
, Point
* const point
)
95 __attribute__((pure
));
96 static void floydWarshall(SyncState
* const syncState
, FactorsCHull
** const
97 allFactors
, double*** const distances
, unsigned int*** const
99 static void getFactors(FactorsCHull
** const allFactors
, unsigned int** const
100 predecessors
, unsigned int* const references
, const unsigned int traceNum
,
101 Factors
* const factors
);
103 static void gfPointDestroy(gpointer data
, gpointer userData
);
106 static AnalysisModule analysisModuleCHull
= {
108 .initAnalysis
= &initAnalysisCHull
,
109 .destroyAnalysis
= &destroyAnalysisCHull
,
110 .analyzeMessage
= &analyzeMessageCHull
,
111 .finalizeAnalysis
= &finalizeAnalysisCHull
,
112 .printAnalysisStats
= &printAnalysisStatsCHull
,
114 .writeTraceTracePlots
= &writeAnalysisGraphsPlotsCHull
,
120 * Analysis module registering function
122 static void registerAnalysisCHull()
124 g_queue_push_tail(&analysisModules
, &analysisModuleCHull
);
129 * Analysis init function
131 * This function is called at the beginning of a synchronization run for a set
134 * Allocate some of the analysis specific data structures
137 * syncState container for synchronization data.
138 * This function allocates or initializes these analysisData
143 static void initAnalysisCHull(SyncState
* const syncState
)
146 AnalysisDataCHull
* analysisData
;
148 analysisData
= malloc(sizeof(AnalysisDataCHull
));
149 syncState
->analysisData
= analysisData
;
151 analysisData
->hullArray
= malloc(syncState
->traceNb
* sizeof(GQueue
**));
152 for (i
= 0; i
< syncState
->traceNb
; i
++)
154 analysisData
->hullArray
[i
]= malloc(syncState
->traceNb
* sizeof(GQueue
*));
156 for (j
= 0; j
< syncState
->traceNb
; j
++)
158 analysisData
->hullArray
[i
][j
]= g_queue_new();
162 if (syncState
->stats
)
164 analysisData
->stats
= malloc(sizeof(AnalysisStatsCHull
));
165 analysisData
->stats
->dropped
= 0;
166 analysisData
->stats
->allFactors
= NULL
;
169 if (syncState
->graphsStream
)
171 analysisData
->graphsData
= malloc(sizeof(AnalysisGraphsDataCHull
));
172 openGraphFiles(syncState
);
173 analysisData
->graphsData
->allFactors
= NULL
;
179 * Create and open files used to store convex hull points to genereate
180 * graphs. Allocate and populate array to store file pointers.
183 * syncState: container for synchronization data
185 static void openGraphFiles(SyncState
* const syncState
)
191 AnalysisDataCHull
* analysisData
;
193 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
195 cwd
= changeToGraphDir(syncState
->graphsDir
);
197 analysisData
->graphsData
->hullPoints
= malloc(syncState
->traceNb
*
199 for (i
= 0; i
< syncState
->traceNb
; i
++)
201 analysisData
->graphsData
->hullPoints
[i
]= malloc(syncState
->traceNb
*
203 for (j
= 0; j
< syncState
->traceNb
; j
++)
207 retval
= snprintf(name
, sizeof(name
),
208 "analysis_chull-%03u_to_%03u.data", j
, i
);
209 if (retval
> sizeof(name
) - 1)
211 name
[sizeof(name
) - 1]= '\0';
213 if ((analysisData
->graphsData
->hullPoints
[i
][j
]= fopen(name
, "w")) ==
216 g_error(strerror(errno
));
225 g_error(strerror(errno
));
232 * Write hull points to files to generate graphs.
235 * syncState: container for synchronization data
237 static void writeGraphFiles(SyncState
* const syncState
)
240 AnalysisDataCHull
* analysisData
;
242 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
244 for (i
= 0; i
< syncState
->traceNb
; i
++)
246 for (j
= 0; j
< syncState
->traceNb
; j
++)
250 g_queue_foreach(analysisData
->hullArray
[i
][j
],
252 analysisData
->graphsData
->hullPoints
[i
][j
]);
260 * A GFunc for g_queue_foreach. Write a hull point to a file used to generate
264 * data: Point*, point to write to the file
265 * userData: FILE*, file pointer where to write the point
267 static void gfDumpHullToFile(gpointer data
, gpointer userData
)
271 point
= (Point
*) data
;
272 fprintf((FILE*) userData
, "%20llu %20llu\n", point
->x
, point
->y
);
277 * Close files used to store convex hull points to generate graphs.
278 * Deallocate array to store file pointers.
281 * syncState: container for synchronization data
283 static void closeGraphFiles(SyncState
* const syncState
)
286 AnalysisDataCHull
* analysisData
;
289 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
291 if (analysisData
->graphsData
->hullPoints
== NULL
)
296 for (i
= 0; i
< syncState
->traceNb
; i
++)
298 for (j
= 0; j
< syncState
->traceNb
; j
++)
302 retval
= fclose(analysisData
->graphsData
->hullPoints
[i
][j
]);
305 g_error(strerror(errno
));
309 free(analysisData
->graphsData
->hullPoints
[i
]);
311 free(analysisData
->graphsData
->hullPoints
);
312 analysisData
->graphsData
->hullPoints
= NULL
;
317 * Analysis destroy function
319 * Free the analysis specific data structures
322 * syncState container for synchronization data.
323 * This function deallocates these analysisData members:
327 static void destroyAnalysisCHull(SyncState
* const syncState
)
330 AnalysisDataCHull
* analysisData
;
332 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
334 if (analysisData
== NULL
)
339 for (i
= 0; i
< syncState
->traceNb
; i
++)
341 for (j
= 0; j
< syncState
->traceNb
; j
++)
343 g_queue_foreach(analysisData
->hullArray
[i
][j
], gfPointDestroy
, NULL
);
345 free(analysisData
->hullArray
[i
]);
347 free(analysisData
->hullArray
);
349 if (syncState
->stats
)
351 if (analysisData
->stats
->allFactors
!= NULL
)
353 freeAllFactors(syncState
, analysisData
->stats
->allFactors
);
356 free(analysisData
->stats
);
359 if (syncState
->graphsStream
)
361 if (analysisData
->graphsData
->hullPoints
!= NULL
)
363 closeGraphFiles(syncState
);
366 if (!syncState
->stats
&& analysisData
->graphsData
->allFactors
!= NULL
)
368 freeAllFactors(syncState
, analysisData
->graphsData
->allFactors
);
371 free(analysisData
->graphsData
);
374 free(syncState
->analysisData
);
375 syncState
->analysisData
= NULL
;
380 * Perform analysis on an event pair.
383 * syncState container for synchronization data
384 * message structure containing the events
386 static void analyzeMessageCHull(SyncState
* const syncState
, Message
* const message
)
388 AnalysisDataCHull
* analysisData
;
393 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
395 newPoint
= malloc(sizeof(Point
));
396 if (message
->inE
->traceNum
< message
->outE
->traceNum
)
398 // CA is inE->traceNum
399 newPoint
->x
= message
->inE
->cpuTime
;
400 newPoint
->y
= message
->outE
->cpuTime
;
402 g_debug("Reception point hullArray[%lu][%lu] x= inE->time= %llu y= outE->time= %llu",
403 message
->inE
->traceNum
, message
->outE
->traceNum
, newPoint
->x
,
408 // CA is outE->traceNum
409 newPoint
->x
= message
->outE
->cpuTime
;
410 newPoint
->y
= message
->inE
->cpuTime
;
412 g_debug("Send point hullArray[%lu][%lu] x= inE->time= %llu y= outE->time= %llu",
413 message
->inE
->traceNum
, message
->outE
->traceNum
, newPoint
->x
,
418 analysisData
->hullArray
[message
->inE
->traceNum
][message
->outE
->traceNum
];
420 if (hull
->length
>= 1 && newPoint
->x
< ((Point
*)
421 g_queue_peek_tail(hull
))->x
)
423 if (syncState
->stats
)
425 analysisData
->stats
->dropped
++;
432 grahamScan(hull
, newPoint
, hullType
);
438 * Construct one half of a convex hull from abscissa-sorted points
441 * hull: the points already in the hull
442 * newPoint: a new point to consider
443 * type: which half of the hull to construct
445 static void grahamScan(GQueue
* const hull
, Point
* const newPoint
, const
450 g_debug("grahamScan(hull (length: %u), newPoint, %s)", hull
->length
, type
451 == LOWER
? "LOWER" : "UPPER");
462 if (hull
->length
>= 2)
464 g_debug("jointCmp(hull[%u], hull[%u], newPoint) * inversionFactor = %d * %d = %d",
467 jointCmp(g_queue_peek_nth(hull
, hull
->length
- 2),
468 g_queue_peek_tail(hull
), newPoint
),
470 jointCmp(g_queue_peek_nth(hull
, hull
->length
- 2),
471 g_queue_peek_tail(hull
), newPoint
) * inversionFactor
);
473 while (hull
->length
>= 2 && jointCmp(g_queue_peek_nth(hull
, hull
->length
-
474 2), g_queue_peek_tail(hull
), newPoint
) * inversionFactor
<= 0)
476 g_debug("Removing hull[%u]", hull
->length
);
477 free((Point
*) g_queue_pop_tail(hull
));
479 if (hull
->length
>= 2)
481 g_debug("jointCmp(hull[%u], hull[%u], newPoint) * inversionFactor = %d * %d = %d",
484 jointCmp(g_queue_peek_nth(hull
, hull
->length
- 2),
485 g_queue_peek_tail(hull
), newPoint
),
487 jointCmp(g_queue_peek_nth(hull
, hull
->length
- 2),
488 g_queue_peek_tail(hull
), newPoint
) * inversionFactor
);
491 g_queue_push_tail(hull
, newPoint
);
496 * Finalize the factor calculations
499 * syncState container for synchronization data.
502 * Factors[traceNb] synchronization factors for each trace
504 static GArray
* finalizeAnalysisCHull(SyncState
* const syncState
)
506 AnalysisDataCHull
* analysisData
;
508 FactorsCHull
** allFactors
;
510 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
512 if (syncState
->graphsStream
&& analysisData
->graphsData
->hullPoints
!= NULL
)
514 writeGraphFiles(syncState
);
515 closeGraphFiles(syncState
);
518 allFactors
= calculateAllFactors(syncState
);
520 factors
= reduceFactors(syncState
, allFactors
);
522 if (syncState
->stats
|| syncState
->graphsStream
)
524 if (syncState
->stats
)
526 analysisData
->stats
->allFactors
= allFactors
;
529 if (syncState
->graphsStream
)
531 analysisData
->graphsData
->allFactors
= allFactors
;
536 freeAllFactors(syncState
, allFactors
);
544 * Print statistics related to analysis. Must be called after
548 * syncState container for synchronization data.
550 static void printAnalysisStatsCHull(SyncState
* const syncState
)
552 AnalysisDataCHull
* analysisData
;
555 if (!syncState
->stats
)
560 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
562 printf("Convex hull analysis stats:\n");
563 printf("\tout of order packets dropped from analysis: %u\n",
564 analysisData
->stats
->dropped
);
566 printf("\tNumber of points in convex hulls:\n");
568 for (i
= 0; i
< syncState
->traceNb
; i
++)
570 for (j
= i
+ 1; j
< syncState
->traceNb
; j
++)
572 printf("\t\t%3d - %-3d: lower half-hull %-5u upper half-hull %-5u\n",
573 i
, j
, analysisData
->hullArray
[j
][i
]->length
,
574 analysisData
->hullArray
[i
][j
]->length
);
578 printf("\tIndividual synchronization factors:\n");
580 for (i
= 0; i
< syncState
->traceNb
; i
++)
582 for (j
= i
+ 1; j
< syncState
->traceNb
; j
++)
584 FactorsCHull
* factorsCHull
;
586 factorsCHull
= &analysisData
->stats
->allFactors
[j
][i
];
587 printf("\t\t%3d - %-3d: ", i
, j
);
589 if (factorsCHull
->type
== EXACT
)
591 printf("Exact a0= % 7g a1= 1 %c %7g\n",
592 factorsCHull
->approx
->offset
,
593 factorsCHull
->approx
->drift
< 0. ? '-' : '+',
594 fabs(factorsCHull
->approx
->drift
));
596 else if (factorsCHull
->type
== MIDDLE
)
598 printf("Middle a0= % 7g a1= 1 %c %7g accuracy %7g\n",
599 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
600 - 1. < 0. ? '-' : '+', fabs(factorsCHull
->approx
->drift
-
601 1.), factorsCHull
->accuracy
);
602 printf("\t\t a0: % 7g to % 7g (delta= %7g)\n",
603 factorsCHull
->max
->offset
, factorsCHull
->min
->offset
,
604 factorsCHull
->min
->offset
- factorsCHull
->max
->offset
);
605 printf("\t\t a1: 1 %+7g to %+7g (delta= %7g)\n",
606 factorsCHull
->min
->drift
- 1., factorsCHull
->max
->drift
-
607 1., factorsCHull
->max
->drift
- factorsCHull
->min
->drift
);
609 else if (factorsCHull
->type
== FALLBACK
)
611 printf("Fallback a0= % 7g a1= 1 %c %7g error= %7g\n",
612 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
613 - 1. < 0. ? '-' : '+', fabs(factorsCHull
->approx
->drift
-
614 1.), factorsCHull
->accuracy
);
616 else if (factorsCHull
->type
== INCOMPLETE
)
618 printf("Incomplete\n");
620 if (factorsCHull
->min
->drift
!= -INFINITY
)
622 printf("\t\t min: a0: % 7g a1: 1 %c %7g\n",
623 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
-
624 1. < 0 ? '-' : '+', fabs(factorsCHull
->min
->drift
-
627 if (factorsCHull
->max
->drift
!= INFINITY
)
629 printf("\t\t max: a0: % 7g a1: 1 %c %7g\n",
630 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
-
631 1. < 0 ? '-' : '+', fabs(factorsCHull
->max
->drift
-
635 else if (factorsCHull
->type
== SCREWED
)
639 if (factorsCHull
->min
!= NULL
&& factorsCHull
->min
->drift
!= -INFINITY
)
641 printf("\t\t min: a0: % 7g a1: 1 %c %7g\n",
642 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
-
643 1. < 0 ? '-' : '+', fabs(factorsCHull
->min
->drift
-
646 if (factorsCHull
->max
!= NULL
&& factorsCHull
->max
->drift
!= INFINITY
)
648 printf("\t\t max: a0: % 7g a1: 1 %c %7g\n",
649 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
-
650 1. < 0 ? '-' : '+', fabs(factorsCHull
->max
->drift
-
654 else if (factorsCHull
->type
== ABSENT
)
660 g_assert_not_reached();
668 * A GFunc for g_queue_foreach()
671 * data Point*, point to destroy
674 static void gfPointDestroy(gpointer data
, gpointer userData
)
678 point
= (Point
*) data
;
684 * Find out if a sequence of three points constitutes a "left turn" or a
688 * p1, p2, p3: The three points.
692 * 0 colinear (unlikely result since this uses floating point
696 static int jointCmp(const Point
const* p1
, const Point
const* p2
, const
700 const double fuzzFactor
= 0.;
702 result
= crossProductK(p1
, p2
, p1
, p3
);
703 g_debug("crossProductK(p1= (%llu, %llu), p2= (%llu, %llu), p1= (%llu, %llu), p3= (%llu, %llu))= %g",
704 p1
->x
, p1
->y
, p2
->x
, p2
->y
, p1
->x
, p1
->y
, p3
->x
, p3
->y
, result
);
705 if (result
< fuzzFactor
)
709 else if (result
> fuzzFactor
)
721 * Calculate the k component of the cross product of two vectors.
724 * p1, p2: start and end points of the first vector
725 * p3, p4: start and end points of the second vector
728 * the k component of the cross product when considering the two vectors to
729 * be in the i-j plane. The direction (sign) of the result can be useful to
730 * determine the relative orientation of the two vectors.
732 static double crossProductK(const Point
const* p1
, const Point
const* p2
,
733 const Point
const* p3
, const Point
const* p4
)
735 return ((double) p2
->x
- p1
->x
) * ((double) p4
->y
- p3
->y
) - ((double)
736 p2
->y
- p1
->y
) * ((double) p4
->x
- p3
->x
);
741 * Free a container of FactorsCHull
744 * syncState: container for synchronization data.
745 * allFactors: container of Factors
747 static void freeAllFactors(const SyncState
* const syncState
, FactorsCHull
**
752 for (i
= 0; i
< syncState
->traceNb
; i
++)
754 for (j
= 0; j
<= i
; j
++)
756 FactorsCHull
* factorsCHull
;
758 factorsCHull
= &allFactors
[i
][j
];
759 if (factorsCHull
->type
== MIDDLE
|| factorsCHull
->type
==
760 INCOMPLETE
|| factorsCHull
->type
== ABSENT
)
762 free(factorsCHull
->min
);
763 free(factorsCHull
->max
);
765 else if (factorsCHull
->type
== SCREWED
)
767 if (factorsCHull
->min
!= NULL
)
769 free(factorsCHull
->min
);
771 if (factorsCHull
->max
!= NULL
)
773 free(factorsCHull
->max
);
777 if (factorsCHull
->type
== EXACT
|| factorsCHull
->type
== MIDDLE
||
778 factorsCHull
->type
== FALLBACK
)
780 free(factorsCHull
->approx
);
790 * Analyze the convex hulls to determine the synchronization factors between
791 * each pair of trace.
794 * syncState container for synchronization data.
797 * FactorsCHull*[TraceNum][TraceNum] array. See the documentation for the
798 * member allFactors of AnalysisStatsCHull.
800 static FactorsCHull
** calculateAllFactors(SyncState
* const syncState
)
802 unsigned int traceNumA
, traceNumB
;
803 FactorsCHull
** allFactors
;
804 AnalysisDataCHull
* analysisData
;
806 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
808 // Allocate allFactors and calculate min and max
809 allFactors
= malloc(syncState
->traceNb
* sizeof(FactorsCHull
*));
810 for (traceNumA
= 0; traceNumA
< syncState
->traceNb
; traceNumA
++)
812 allFactors
[traceNumA
]= malloc((traceNumA
+ 1) * sizeof(FactorsCHull
));
814 allFactors
[traceNumA
][traceNumA
].type
= EXACT
;
815 allFactors
[traceNumA
][traceNumA
].approx
= malloc(sizeof(Factors
));
816 allFactors
[traceNumA
][traceNumA
].approx
->drift
= 1.;
817 allFactors
[traceNumA
][traceNumA
].approx
->offset
= 0.;
819 for (traceNumB
= 0; traceNumB
< traceNumA
; traceNumB
++)
826 size_t factorsOffset
;
828 {MINIMUM
, offsetof(FactorsCHull
, min
)},
829 {MAXIMUM
, offsetof(FactorsCHull
, max
)}
832 cr
= analysisData
->hullArray
[traceNumB
][traceNumA
];
833 cs
= analysisData
->hullArray
[traceNumA
][traceNumB
];
835 for (i
= 0; i
< sizeof(loopValues
) / sizeof(*loopValues
); i
++)
837 g_debug("allFactors[%u][%u].%s = calculateFactorsExact(cr= hullArray[%u][%u], cs= hullArray[%u][%u], %s)",
838 traceNumA
, traceNumB
, loopValues
[i
].factorsOffset
==
839 offsetof(FactorsCHull
, min
) ? "min" : "max", traceNumB
,
840 traceNumA
, traceNumA
, traceNumB
, loopValues
[i
].lineType
==
841 MINIMUM
? "MINIMUM" : "MAXIMUM");
842 *((Factors
**) ((void*) &allFactors
[traceNumA
][traceNumB
] +
843 loopValues
[i
].factorsOffset
))=
844 calculateFactorsExact(cr
, cs
, loopValues
[i
].lineType
);
849 // Calculate approx when possible
850 for (traceNumA
= 0; traceNumA
< syncState
->traceNb
; traceNumA
++)
852 for (traceNumB
= 0; traceNumB
< traceNumA
; traceNumB
++)
854 FactorsCHull
* factorsCHull
;
856 factorsCHull
= &allFactors
[traceNumA
][traceNumB
];
857 if (factorsCHull
->min
== NULL
&& factorsCHull
->max
== NULL
)
859 factorsCHull
->type
= FALLBACK
;
860 calculateFactorsFallback(analysisData
->hullArray
[traceNumB
][traceNumA
],
861 analysisData
->hullArray
[traceNumA
][traceNumB
],
862 &allFactors
[traceNumA
][traceNumB
]);
864 else if (factorsCHull
->min
!= NULL
&& factorsCHull
->max
!= NULL
)
866 if (factorsCHull
->min
->drift
!= -INFINITY
&&
867 factorsCHull
->max
->drift
!= INFINITY
)
869 factorsCHull
->type
= MIDDLE
;
870 calculateFactorsMiddle(factorsCHull
);
872 else if (factorsCHull
->min
->drift
!= -INFINITY
||
873 factorsCHull
->max
->drift
!= INFINITY
)
875 factorsCHull
->type
= INCOMPLETE
;
879 factorsCHull
->type
= ABSENT
;
884 //g_assert_not_reached();
885 factorsCHull
->type
= SCREWED
;
894 /* Calculate approximative factors based on minimum and maximum limits. The
895 * best approximation to make is the interior bissector of the angle formed by
896 * the minimum and maximum lines.
898 * The formulae used come from [Haddad, Yoram: Performance dans les systèmes
899 * répartis: des outils pour les mesures, Université de Paris-Sud, Centre
900 * d'Orsay, September 1988] Section 6.1 p.44
902 * The reasoning for choosing this estimator comes from [Duda, A., Harrus, G.,
903 * Haddad, Y., and Bernard, G.: Estimating global time in distributed systems,
904 * Proc. 7th Int. Conf. on Distributed Computing Systems, Berlin, volume 18,
908 * factors: contains the min and max limits, used to store the result
910 static void calculateFactorsMiddle(FactorsCHull
* factors
)
912 double amin
, amax
, bmin
, bmax
, bhat
;
914 amin
= factors
->max
->offset
;
915 amax
= factors
->min
->offset
;
916 bmin
= factors
->min
->drift
;
917 bmax
= factors
->max
->drift
;
919 g_assert_cmpfloat(bmax
, >, bmin
);
921 factors
->approx
= malloc(sizeof(Factors
));
922 bhat
= (bmax
* bmin
- 1. + sqrt(1. + pow(bmax
, 2.) * pow(bmin
, 2.) +
923 pow(bmax
, 2.) + pow(bmin
, 2.))) / (bmax
+ bmin
);
924 factors
->approx
->offset
= amax
- (amax
- amin
) / 2. * (pow(bhat
, 2.) + 1.)
925 / (1. + bhat
* bmax
);
926 factors
->approx
->drift
= bhat
;
927 factors
->accuracy
= bmax
- bmin
;
932 * Analyze the convex hulls to determine the minimum or maximum
933 * synchronization factors between one pair of trace.
935 * This implements and improves upon the algorithm in [Haddad, Yoram:
936 * Performance dans les systèmes répartis: des outils pour les mesures,
937 * Université de Paris-Sud, Centre d'Orsay, September 1988] Section 6.2 p.47
939 * Some degenerate cases are possible:
940 * 1) the result is unbounded. In that case, when searching for the maximum
941 * factors, result->drift= INFINITY; result->offset= -INFINITY. When
942 * searching for the minimum factors, it is the opposite. It is not
943 * possible to improve the situation with this data.
944 * 2) no line can be above the upper hull and below the lower hull. This is
945 * because the hulls intersect each other or are reversed. This means that
946 * an assertion was false. Most probably, the clocks are not linear. It is
947 * possible to repeat the search with another algorithm that will find a
948 * "best effort" approximation. See calculateFactorsApprox().
951 * cu: the upper half-convex hull, the line must pass above this
952 * and touch it in one point
953 * cl: the lower half-convex hull, the line must pass below this
954 * and touch it in one point
955 * lineType: search for minimum or maximum factors
958 * If a result is found, a struct Factors is allocated, filed with the
959 * result and returned
960 * NULL otherwise, degenerate case 2 is in effect
962 static Factors
* calculateFactorsExact(GQueue
* const cu
, GQueue
* const cl
, const
968 double inversionFactor
;
971 g_debug("calculateFactorsExact(cu= %p, cl= %p, %s)", cu
, cl
, lineType
==
972 MINIMUM
? "MINIMUM" : "MAXIMUM");
974 if (lineType
== MINIMUM
)
978 inversionFactor
= -1.;
990 // Check for degenerate case 1
991 if (c1
->length
== 0 || c2
->length
== 0 || ((Point
*) g_queue_peek_nth(c1
,
992 i1
))->x
>= ((Point
*) g_queue_peek_nth(c2
, i2
))->x
)
994 result
= malloc(sizeof(Factors
));
995 if (lineType
== MINIMUM
)
997 result
->drift
= -INFINITY
;
998 result
->offset
= INFINITY
;
1002 result
->drift
= INFINITY
;
1003 result
->offset
= -INFINITY
;
1015 g_queue_peek_nth(c1
, i1
),
1016 g_queue_peek_nth(c2
, i2
),
1017 g_queue_peek_nth(c1
, i1
),
1018 g_queue_peek_nth(c2
, i2
- 1)) * inversionFactor
< 0.
1021 if (((Point
*) g_queue_peek_nth(c1
, i1
))->x
1022 < ((Point
*) g_queue_peek_nth(c2
, i2
- 1))->x
)
1028 // Degenerate case 2
1034 i1
+ 1 < c1
->length
- 1
1036 g_queue_peek_nth(c1
, i1
),
1037 g_queue_peek_nth(c2
, i2
),
1038 g_queue_peek_nth(c1
, i1
+ 1),
1039 g_queue_peek_nth(c2
, i2
)) * inversionFactor
< 0.
1042 if (((Point
*) g_queue_peek_nth(c1
, i1
+ 1))->x
1043 < ((Point
*) g_queue_peek_nth(c2
, i2
))->x
)
1049 // Degenerate case 2
1057 g_queue_peek_nth(c1
, i1
),
1058 g_queue_peek_nth(c2
, i2
),
1059 g_queue_peek_nth(c1
, i1
),
1060 g_queue_peek_nth(c2
, i2
- 1)) * inversionFactor
< 0.
1063 p1
= g_queue_peek_nth(c1
, i1
);
1064 p2
= g_queue_peek_nth(c2
, i2
);
1066 g_debug("Resulting points are: c1[i1]: x= %llu y= %llu c2[i2]: x= %llu y= %llu",
1067 p1
->x
, p1
->y
, p2
->x
, p2
->y
);
1069 result
= malloc(sizeof(Factors
));
1070 result
->drift
= slope(p1
, p2
);
1071 result
->offset
= intercept(p1
, p2
);
1073 g_debug("Resulting factors are: drift= %g offset= %g", result
->drift
, result
->offset
);
1080 * Analyze the convex hulls to determine approximate synchronization factors
1081 * between one pair of trace when there is no line that can fit in the
1082 * corridor separating them.
1084 * This implements the algorithm in [Ashton, P.: Algorithms for Off-line Clock
1085 * Synchronisation, University of Canterbury, December 1995, 26] Section 4.2.2
1088 * For each point p1 in cr
1089 * For each point p2 in cs
1091 * Calculate the line paramaters
1092 * For each point p3 in each convex hull
1093 * If p3 is on the wrong side of the line
1095 * If error < errorMin
1099 * cr: the upper half-convex hull
1100 * cs: the lower half-convex hull
1101 * result: a pointer to the pre-allocated struct where the results
1104 static void calculateFactorsFallback(GQueue
* const cr
, GQueue
* const cs
,
1105 FactorsCHull
* const result
)
1107 unsigned int i
, j
, k
;
1112 approx
= malloc(sizeof(Factors
));
1114 for (i
= 0; i
< cs
->length
; i
++)
1116 for (j
= 0; j
< cr
->length
; j
++)
1123 if (((Point
*) g_queue_peek_nth(cs
, i
))->x
< ((Point
*)g_queue_peek_nth(cr
, j
))->x
)
1125 p1
= *(Point
*)g_queue_peek_nth(cs
, i
);
1126 p2
= *(Point
*)g_queue_peek_nth(cr
, j
);
1130 p1
= *(Point
*)g_queue_peek_nth(cr
, j
);
1131 p2
= *(Point
*)g_queue_peek_nth(cs
, i
);
1134 // The lower hull should be above the point
1135 for (k
= 0; k
< cs
->length
; k
++)
1137 if (jointCmp(&p1
, &p2
, g_queue_peek_nth(cs
, k
)) < 0.)
1139 error
+= verticalDistance(&p1
, &p2
, g_queue_peek_nth(cs
, k
));
1143 // The upper hull should be below the point
1144 for (k
= 0; k
< cr
->length
; k
++)
1146 if (jointCmp(&p1
, &p2
, g_queue_peek_nth(cr
, k
)) > 0.)
1148 error
+= verticalDistance(&p1
, &p2
, g_queue_peek_nth(cr
, k
));
1152 if (error
< errorMin
)
1154 g_debug("Fallback: i= %u j= %u is a better match (error= %g)", i
, j
, error
);
1155 approx
->drift
= slope(&p1
, &p2
);
1156 approx
->offset
= intercept(&p1
, &p2
);
1162 result
->approx
= approx
;
1163 result
->accuracy
= errorMin
;
1168 * Calculate the vertical distance between a line and a point
1171 * p1, p2: Two points defining the line
1175 * the vertical distance
1177 static double verticalDistance(Point
* p1
, Point
* p2
, Point
* const point
)
1179 return fabs(slope(p1
, p2
) * point
->x
+ intercept(p1
, p2
) - point
->y
);
1184 * Calculate the slope between two points
1187 * p1, p2 the two points
1192 static double slope(const Point
* const p1
, const Point
* const p2
)
1194 return ((double) p2
->y
- p1
->y
) / (p2
->x
- p1
->x
);
1198 /* Calculate the y-intercept of a line that passes by two points
1201 * p1, p2 the two points
1206 static double intercept(const Point
* const p1
, const Point
* const p2
)
1208 return ((double) p2
->y
* p1
->x
- (double) p1
->y
* p2
->x
) / ((double) p1
->x
- p2
->x
);
1213 * Calculate a resulting offset and drift for each trace.
1215 * Traces are assembled in groups. A group is an "island" of nodes/traces that
1216 * exchanged messages. A reference is determined for each group by using a
1217 * shortest path search based on the accuracy of the approximation. This also
1218 * forms a tree of the best way to relate each node's clock to the reference's
1219 * based on the accuracy. Sometimes it may be necessary or advantageous to
1220 * propagate the factors through intermediary clocks. Resulting factors for
1221 * each trace are determined based on this tree.
1223 * This part was not the focus of my research. The algorithm used here is
1224 * inexact in some ways:
1225 * 1) The reference used may not actually be the best one to use. This is
1226 * because the accuracy is not corrected based on the drift during the
1227 * shortest path search.
1228 * 2) The min and max factors are not propagated and are no longer valid.
1229 * 3) Approximations of different types (MIDDLE and FALLBACK) are compared
1230 * together. The "accuracy" parameters of these have different meanings and
1231 * are not readily comparable.
1233 * Nevertheless, the result is satisfactory. You just can't tell "how much" it
1236 * Two alternative (and subtly different) ways of propagating factors to
1237 * preserve min and max bondaries have been proposed, see:
1238 * [Duda, A., Harrus, G., Haddad, Y., and Bernard, G.: Estimating global time
1239 * in distributed systems, Proc. 7th Int. Conf. on Distributed Computing
1240 * Systems, Berlin, volume 18, 1987] p.304
1242 * [Jezequel, J.M., and Jard, C.: Building a global clock for observing
1243 * computations in distributed memory parallel computers, Concurrency:
1244 * Practice and Experience 8(1), volume 8, John Wiley & Sons, Ltd Chichester,
1245 * 1996, 32] Section 5; which is mostly the same as
1246 * [Jezequel, J.M.: Building a global time on parallel machines, Proceedings
1247 * of the 3rd International Workshop on Distributed Algorithms, LNCS, volume
1248 * 392, 136–147, 1989] Section 5
1251 * syncState: container for synchronization data.
1252 * allFactors: offset and drift between each pair of traces
1255 * Factors[traceNb] synchronization factors for each trace
1257 static GArray
* reduceFactors(SyncState
* const syncState
, FactorsCHull
** const
1262 unsigned int** predecessors
;
1263 double* distanceSums
;
1264 unsigned int* references
;
1267 // Solve the all-pairs shortest path problem using the Floyd-Warshall
1269 floydWarshall(syncState
, allFactors
, &distances
, &predecessors
);
1271 /* Find the reference for each node
1273 * First calculate, for each node, the sum of the distances to each other
1274 * node it can reach.
1276 * Then, go through each "island" of traces to find the trace that has the
1277 * lowest distance sum. Assign this trace as the reference to each trace
1280 distanceSums
= malloc(syncState
->traceNb
* sizeof(double));
1281 for (i
= 0; i
< syncState
->traceNb
; i
++)
1283 distanceSums
[i
]= 0.;
1284 for (j
= 0; j
< syncState
->traceNb
; j
++)
1286 distanceSums
[i
]+= distances
[i
][j
];
1290 references
= malloc(syncState
->traceNb
* sizeof(unsigned int));
1291 for (i
= 0; i
< syncState
->traceNb
; i
++)
1293 references
[i
]= UINT_MAX
;
1295 for (i
= 0; i
< syncState
->traceNb
; i
++)
1297 if (references
[i
] == UINT_MAX
)
1299 unsigned int reference
;
1300 double distanceSumMin
;
1302 // A node is its own reference by default
1304 distanceSumMin
= INFINITY
;
1305 for (j
= 0; j
< syncState
->traceNb
; j
++)
1307 if (distances
[i
][j
] != INFINITY
&& distanceSums
[j
] <
1311 distanceSumMin
= distanceSums
[j
];
1314 for (j
= 0; j
< syncState
->traceNb
; j
++)
1316 if (distances
[i
][j
] != INFINITY
)
1318 references
[j
]= reference
;
1324 for (i
= 0; i
< syncState
->traceNb
; i
++)
1331 /* For each trace, calculate the factors based on their corresponding
1332 * tree. The tree is rooted at the reference and the shortest path to each
1333 * other nodes are the branches.
1335 factors
= g_array_sized_new(FALSE
, FALSE
, sizeof(Factors
),
1336 syncState
->traceNb
);
1337 g_array_set_size(factors
, syncState
->traceNb
);
1338 for (i
= 0; i
< syncState
->traceNb
; i
++)
1340 getFactors(allFactors
, predecessors
, references
, i
, &g_array_index(factors
,
1344 for (i
= 0; i
< syncState
->traceNb
; i
++)
1346 free(predecessors
[i
]);
1356 * Perform an all-source shortest path search using the Floyd-Warshall
1359 * The algorithm is implemented accoding to the description here:
1360 * http://web.mit.edu/urban_or_book/www/book/chapter6/6.2.2.html
1363 * syncState: container for synchronization data.
1364 * allFactors: offset and drift between each pair of traces
1365 * distances: resulting matrix of the length of the shortest path between
1366 * two nodes. If there is no path between two nodes, the
1367 * length is INFINITY
1368 * predecessors: resulting matrix of each node's predecessor on the shortest
1369 * path between two nodes
1371 static void floydWarshall(SyncState
* const syncState
, FactorsCHull
** const
1372 allFactors
, double*** const distances
, unsigned int*** const
1375 unsigned int i
, j
, k
;
1377 // Setup initial conditions
1378 *distances
= malloc(syncState
->traceNb
* sizeof(double*));
1379 *predecessors
= malloc(syncState
->traceNb
* sizeof(unsigned int*));
1380 for (i
= 0; i
< syncState
->traceNb
; i
++)
1382 (*distances
)[i
]= malloc(syncState
->traceNb
* sizeof(double));
1383 for (j
= 0; j
< syncState
->traceNb
; j
++)
1387 g_assert(allFactors
[i
][j
].type
== EXACT
);
1389 (*distances
)[i
][j
]= 0.;
1393 unsigned int row
, col
;
1406 if (allFactors
[row
][col
].type
== MIDDLE
||
1407 allFactors
[row
][col
].type
== FALLBACK
)
1409 (*distances
)[i
][j
]= allFactors
[row
][col
].accuracy
;
1411 else if (allFactors
[row
][col
].type
== INCOMPLETE
||
1412 allFactors
[row
][col
].type
== SCREWED
||
1413 allFactors
[row
][col
].type
== ABSENT
)
1415 (*distances
)[i
][j
]= INFINITY
;
1419 g_assert_not_reached();
1424 (*predecessors
)[i
]= malloc(syncState
->traceNb
* sizeof(unsigned int));
1425 for (j
= 0; j
< syncState
->traceNb
; j
++)
1429 (*predecessors
)[i
][j
]= i
;
1433 (*predecessors
)[i
][j
]= UINT_MAX
;
1438 // Run the iterations
1439 for (k
= 0; k
< syncState
->traceNb
; k
++)
1441 for (i
= 0; i
< syncState
->traceNb
; i
++)
1443 for (j
= 0; j
< syncState
->traceNb
; j
++)
1447 distanceMin
= MIN((*distances
)[i
][j
], (*distances
)[i
][k
] +
1448 (*distances
)[k
][j
]);
1450 if (distanceMin
!= (*distances
)[i
][j
])
1452 (*predecessors
)[i
][j
]= (*predecessors
)[k
][j
];
1455 (*distances
)[i
][j
]= distanceMin
;
1463 * Cummulate the time correction factors to convert a node's time to its
1465 * This function recursively calls itself until it reaches the reference node.
1468 * allFactors: offset and drift between each pair of traces
1469 * predecessors: matrix of each node's predecessor on the shortest
1470 * path between two nodes
1471 * references: reference node for each node
1472 * traceNum: node for which to find the factors
1473 * factors: resulting factors
1475 static void getFactors(FactorsCHull
** const allFactors
, unsigned int** const
1476 predecessors
, unsigned int* const references
, const unsigned int traceNum
,
1477 Factors
* const factors
)
1479 unsigned int reference
;
1481 reference
= references
[traceNum
];
1483 if (reference
== traceNum
)
1485 factors
->offset
= 0.;
1490 Factors previousVertexFactors
;
1492 getFactors(allFactors
, predecessors
, references
,
1493 predecessors
[reference
][traceNum
], &previousVertexFactors
);
1495 // convertir de traceNum à reference
1497 // allFactors convertit de col à row
1499 if (reference
> traceNum
)
1501 factors
->offset
= previousVertexFactors
.drift
*
1502 allFactors
[reference
][traceNum
].approx
->offset
+
1503 previousVertexFactors
.offset
;
1504 factors
->drift
= previousVertexFactors
.drift
*
1505 allFactors
[reference
][traceNum
].approx
->drift
;
1509 factors
->offset
= previousVertexFactors
.drift
* (-1. *
1510 allFactors
[traceNum
][reference
].approx
->offset
/
1511 allFactors
[traceNum
][reference
].approx
->drift
) +
1512 previousVertexFactors
.offset
;
1513 factors
->drift
= previousVertexFactors
.drift
* (1. /
1514 allFactors
[traceNum
][reference
].approx
->drift
);
1521 * Write the analysis-specific graph lines in the gnuplot script.
1524 * syncState: container for synchronization data
1525 * i: first trace number
1526 * j: second trace number, garanteed to be larger than i
1528 void writeAnalysisGraphsPlotsCHull(SyncState
* const syncState
, const unsigned
1529 int i
, const unsigned int j
)
1531 AnalysisDataCHull
* analysisData
;
1532 FactorsCHull
* factorsCHull
;
1534 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
1536 fprintf(syncState
->graphsStream
,
1537 "\t\"analysis_chull-%1$03d_to_%2$03d.data\" "
1538 "title \"Lower half-hull\" with linespoints "
1539 "linecolor rgb \"#015a01\" linetype 4 pointtype 8 pointsize 0.8, \\\n"
1540 "\t\"analysis_chull-%2$03d_to_%1$03d.data\" "
1541 "title \"Upper half-hull\" with linespoints "
1542 "linecolor rgb \"#003366\" linetype 4 pointtype 10 pointsize 0.8, \\\n",
1545 factorsCHull
= &analysisData
->graphsData
->allFactors
[j
][i
];
1546 if (factorsCHull
->type
== EXACT
)
1548 fprintf(syncState
->graphsStream
,
1550 "title \"Exact conversion\" with lines "
1551 "linecolor rgb \"black\" linetype 1, \\\n",
1552 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
);
1554 else if (factorsCHull
->type
== MIDDLE
)
1556 fprintf(syncState
->graphsStream
,
1557 "\t%.2f + %.10f * x "
1558 "title \"Min conversion\" with lines "
1559 "linecolor rgb \"black\" linetype 5, \\\n",
1560 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
);
1561 fprintf(syncState
->graphsStream
,
1562 "\t%.2f + %.10f * x "
1563 "title \"Max conversion\" with lines "
1564 "linecolor rgb \"black\" linetype 8, \\\n",
1565 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
);
1566 fprintf(syncState
->graphsStream
,
1567 "\t%.2f + %.10f * x "
1568 "title \"Middle conversion\" with lines "
1569 "linecolor rgb \"gray60\" linetype 1, \\\n",
1570 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
);
1572 else if (factorsCHull
->type
== FALLBACK
)
1574 fprintf(syncState
->graphsStream
,
1575 "\t%.2f + %.10f * x "
1576 "title \"Fallback conversion\" with lines "
1577 "linecolor rgb \"gray60\" linetype 1, \\\n",
1578 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
);
1580 else if (factorsCHull
->type
== INCOMPLETE
)
1582 if (factorsCHull
->min
->drift
!= -INFINITY
)
1584 fprintf(syncState
->graphsStream
,
1585 "\t%.2f + %.10f * x "
1586 "title \"Min conversion\" with lines "
1587 "linecolor rgb \"black\" linetype 5, \\\n",
1588 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
);
1591 if (factorsCHull
->max
->drift
!= INFINITY
)
1593 fprintf(syncState
->graphsStream
,
1594 "\t%.2f + %.10f * x "
1595 "title \"Max conversion\" with lines "
1596 "linecolor rgb \"black\" linetype 8, \\\n",
1597 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
);
1600 else if (factorsCHull
->type
== SCREWED
)
1602 if (factorsCHull
->min
!= NULL
&& factorsCHull
->min
->drift
!= -INFINITY
)
1604 fprintf(syncState
->graphsStream
,
1605 "\t%.2f + %.10f * x "
1606 "title \"Min conversion\" with lines "
1607 "linecolor rgb \"black\" linetype 5, \\\n",
1608 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
);
1611 if (factorsCHull
->max
!= NULL
&& factorsCHull
->max
->drift
!= INFINITY
)
1613 fprintf(syncState
->graphsStream
,
1614 "\t%.2f + %.10f * x "
1615 "title \"Max conversion\" with lines "
1616 "linecolor rgb \"black\" linetype 8, \\\n",
1617 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
);
1620 else if (factorsCHull
->type
== ABSENT
)
1625 g_assert_not_reached();