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 Factors
* calculateFactorsExact(GQueue
* const cu
, GQueue
* const cl
, const
81 LineType lineType
) __attribute__((pure
));
82 static void calculateFactorsFallback(GQueue
* const cr
, GQueue
* const cs
,
83 FactorsCHull
* const result
);
84 static double slope(const Point
* const p1
, const Point
* const p2
)
85 __attribute__((pure
));
86 static double intercept(const Point
* const p1
, const Point
* const p2
)
87 __attribute__((pure
));
88 static GArray
* reduceFactors(SyncState
* const syncState
, FactorsCHull
**
90 static double verticalDistance(Point
* p1
, Point
* p2
, Point
* const point
)
91 __attribute__((pure
));
92 static void floydWarshall(SyncState
* const syncState
, FactorsCHull
** const
93 allFactors
, double*** const distances
, unsigned int*** const
95 static void getFactors(FactorsCHull
** const allFactors
, unsigned int** const
96 predecessors
, unsigned int* const references
, const unsigned int traceNum
,
97 Factors
* const factors
);
99 static void gfPointDestroy(gpointer data
, gpointer userData
);
102 static AnalysisModule analysisModuleCHull
= {
104 .initAnalysis
= &initAnalysisCHull
,
105 .destroyAnalysis
= &destroyAnalysisCHull
,
106 .analyzeMessage
= &analyzeMessageCHull
,
107 .finalizeAnalysis
= &finalizeAnalysisCHull
,
108 .printAnalysisStats
= &printAnalysisStatsCHull
,
110 .writeTraceTraceForePlots
= &writeAnalysisGraphsPlotsCHull
,
114 const char* const approxNames
[]= {
117 [FALLBACK
]= "Fallback",
118 [INCOMPLETE
]= "Incomplete",
120 [SCREWED
]= "Screwed",
125 * Analysis module registering function
127 static void registerAnalysisCHull()
129 g_queue_push_tail(&analysisModules
, &analysisModuleCHull
);
134 * Analysis init function
136 * This function is called at the beginning of a synchronization run for a set
139 * Allocate some of the analysis specific data structures
142 * syncState container for synchronization data.
143 * This function allocates or initializes these analysisData
148 static void initAnalysisCHull(SyncState
* const syncState
)
151 AnalysisDataCHull
* analysisData
;
153 analysisData
= malloc(sizeof(AnalysisDataCHull
));
154 syncState
->analysisData
= analysisData
;
156 analysisData
->hullArray
= malloc(syncState
->traceNb
* sizeof(GQueue
**));
157 for (i
= 0; i
< syncState
->traceNb
; i
++)
159 analysisData
->hullArray
[i
]= malloc(syncState
->traceNb
* sizeof(GQueue
*));
161 for (j
= 0; j
< syncState
->traceNb
; j
++)
163 analysisData
->hullArray
[i
][j
]= g_queue_new();
167 if (syncState
->stats
)
169 analysisData
->stats
= malloc(sizeof(AnalysisStatsCHull
));
170 analysisData
->stats
->dropped
= 0;
171 analysisData
->stats
->allFactors
= NULL
;
174 if (syncState
->graphsStream
)
176 analysisData
->graphsData
= malloc(sizeof(AnalysisGraphsDataCHull
));
177 openGraphFiles(syncState
);
178 analysisData
->graphsData
->allFactors
= NULL
;
184 * Create and open files used to store convex hull points to genereate
185 * graphs. Allocate and populate array to store file pointers.
188 * syncState: container for synchronization data
190 static void openGraphFiles(SyncState
* const syncState
)
196 AnalysisDataCHull
* analysisData
;
198 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
200 cwd
= changeToGraphDir(syncState
->graphsDir
);
202 analysisData
->graphsData
->hullPoints
= malloc(syncState
->traceNb
*
204 for (i
= 0; i
< syncState
->traceNb
; i
++)
206 analysisData
->graphsData
->hullPoints
[i
]= malloc(syncState
->traceNb
*
208 for (j
= 0; j
< syncState
->traceNb
; j
++)
212 retval
= snprintf(name
, sizeof(name
),
213 "analysis_chull-%03u_to_%03u.data", j
, i
);
214 if (retval
> sizeof(name
) - 1)
216 name
[sizeof(name
) - 1]= '\0';
218 if ((analysisData
->graphsData
->hullPoints
[i
][j
]= fopen(name
, "w")) ==
221 g_error(strerror(errno
));
230 g_error(strerror(errno
));
237 * Write hull points to files to generate graphs.
240 * syncState: container for synchronization data
242 static void writeGraphFiles(SyncState
* const syncState
)
245 AnalysisDataCHull
* analysisData
;
247 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
249 for (i
= 0; i
< syncState
->traceNb
; i
++)
251 for (j
= 0; j
< syncState
->traceNb
; j
++)
255 g_queue_foreach(analysisData
->hullArray
[i
][j
],
257 analysisData
->graphsData
->hullPoints
[i
][j
]);
265 * A GFunc for g_queue_foreach. Write a hull point to a file used to generate
269 * data: Point*, point to write to the file
270 * userData: FILE*, file pointer where to write the point
272 static void gfDumpHullToFile(gpointer data
, gpointer userData
)
276 point
= (Point
*) data
;
277 fprintf((FILE*) userData
, "%20llu %20llu\n", point
->x
, point
->y
);
282 * Close files used to store convex hull points to generate graphs.
283 * Deallocate array to store file pointers.
286 * syncState: container for synchronization data
288 static void closeGraphFiles(SyncState
* const syncState
)
291 AnalysisDataCHull
* analysisData
;
294 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
296 if (analysisData
->graphsData
->hullPoints
== NULL
)
301 for (i
= 0; i
< syncState
->traceNb
; i
++)
303 for (j
= 0; j
< syncState
->traceNb
; j
++)
307 retval
= fclose(analysisData
->graphsData
->hullPoints
[i
][j
]);
310 g_error(strerror(errno
));
314 free(analysisData
->graphsData
->hullPoints
[i
]);
316 free(analysisData
->graphsData
->hullPoints
);
317 analysisData
->graphsData
->hullPoints
= NULL
;
322 * Analysis destroy function
324 * Free the analysis specific data structures
327 * syncState container for synchronization data.
328 * This function deallocates these analysisData members:
332 static void destroyAnalysisCHull(SyncState
* const syncState
)
335 AnalysisDataCHull
* analysisData
;
337 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
339 if (analysisData
== NULL
)
344 for (i
= 0; i
< syncState
->traceNb
; i
++)
346 for (j
= 0; j
< syncState
->traceNb
; j
++)
348 g_queue_foreach(analysisData
->hullArray
[i
][j
], gfPointDestroy
, NULL
);
350 free(analysisData
->hullArray
[i
]);
352 free(analysisData
->hullArray
);
354 if (syncState
->stats
)
356 if (analysisData
->stats
->allFactors
!= NULL
)
358 freeAllFactors(syncState
->traceNb
, analysisData
->stats
->allFactors
);
361 free(analysisData
->stats
);
364 if (syncState
->graphsStream
)
366 if (analysisData
->graphsData
->hullPoints
!= NULL
)
368 closeGraphFiles(syncState
);
371 if (!syncState
->stats
&& analysisData
->graphsData
->allFactors
!= NULL
)
373 freeAllFactors(syncState
->traceNb
, analysisData
->graphsData
->allFactors
);
376 free(analysisData
->graphsData
);
379 free(syncState
->analysisData
);
380 syncState
->analysisData
= NULL
;
385 * Perform analysis on an event pair.
388 * syncState container for synchronization data
389 * message structure containing the events
391 static void analyzeMessageCHull(SyncState
* const syncState
, Message
* const message
)
393 AnalysisDataCHull
* analysisData
;
398 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
400 newPoint
= malloc(sizeof(Point
));
401 if (message
->inE
->traceNum
< message
->outE
->traceNum
)
403 // CA is inE->traceNum
404 newPoint
->x
= message
->inE
->cpuTime
;
405 newPoint
->y
= message
->outE
->cpuTime
;
407 g_debug("Reception point hullArray[%lu][%lu] x= inE->time= %llu y= outE->time= %llu",
408 message
->inE
->traceNum
, message
->outE
->traceNum
, newPoint
->x
,
413 // CA is outE->traceNum
414 newPoint
->x
= message
->outE
->cpuTime
;
415 newPoint
->y
= message
->inE
->cpuTime
;
417 g_debug("Send point hullArray[%lu][%lu] x= inE->time= %llu y= outE->time= %llu",
418 message
->inE
->traceNum
, message
->outE
->traceNum
, newPoint
->x
,
423 analysisData
->hullArray
[message
->inE
->traceNum
][message
->outE
->traceNum
];
425 if (hull
->length
>= 1 && newPoint
->x
< ((Point
*)
426 g_queue_peek_tail(hull
))->x
)
428 if (syncState
->stats
)
430 analysisData
->stats
->dropped
++;
437 grahamScan(hull
, newPoint
, hullType
);
443 * Construct one half of a convex hull from abscissa-sorted points
446 * hull: the points already in the hull
447 * newPoint: a new point to consider
448 * type: which half of the hull to construct
450 static void grahamScan(GQueue
* const hull
, Point
* const newPoint
, const
455 g_debug("grahamScan(hull (length: %u), newPoint, %s)", hull
->length
, type
456 == LOWER
? "LOWER" : "UPPER");
467 if (hull
->length
>= 2)
469 g_debug("jointCmp(hull[%u], hull[%u], newPoint) * inversionFactor = %d * %d = %d",
472 jointCmp(g_queue_peek_nth(hull
, hull
->length
- 2),
473 g_queue_peek_tail(hull
), newPoint
),
475 jointCmp(g_queue_peek_nth(hull
, hull
->length
- 2),
476 g_queue_peek_tail(hull
), newPoint
) * inversionFactor
);
478 while (hull
->length
>= 2 && jointCmp(g_queue_peek_nth(hull
, hull
->length
-
479 2), g_queue_peek_tail(hull
), newPoint
) * inversionFactor
<= 0)
481 g_debug("Removing hull[%u]", hull
->length
);
482 free((Point
*) g_queue_pop_tail(hull
));
484 if (hull
->length
>= 2)
486 g_debug("jointCmp(hull[%u], hull[%u], newPoint) * inversionFactor = %d * %d = %d",
489 jointCmp(g_queue_peek_nth(hull
, hull
->length
- 2),
490 g_queue_peek_tail(hull
), newPoint
),
492 jointCmp(g_queue_peek_nth(hull
, hull
->length
- 2),
493 g_queue_peek_tail(hull
), newPoint
) * inversionFactor
);
496 g_queue_push_tail(hull
, newPoint
);
501 * Finalize the factor calculations
504 * syncState container for synchronization data.
507 * Factors[traceNb] synchronization factors for each trace
509 static GArray
* finalizeAnalysisCHull(SyncState
* const syncState
)
511 AnalysisDataCHull
* analysisData
;
513 FactorsCHull
** allFactors
;
515 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
517 if (syncState
->graphsStream
&& analysisData
->graphsData
->hullPoints
!= NULL
)
519 writeGraphFiles(syncState
);
520 closeGraphFiles(syncState
);
523 allFactors
= calculateAllFactors(syncState
);
525 factors
= reduceFactors(syncState
, allFactors
);
527 if (syncState
->stats
|| syncState
->graphsStream
)
529 if (syncState
->stats
)
531 analysisData
->stats
->allFactors
= allFactors
;
534 if (syncState
->graphsStream
)
536 analysisData
->graphsData
->allFactors
= allFactors
;
541 freeAllFactors(syncState
->traceNb
, allFactors
);
549 * Print statistics related to analysis. Must be called after
553 * syncState container for synchronization data.
555 static void printAnalysisStatsCHull(SyncState
* const syncState
)
557 AnalysisDataCHull
* analysisData
;
560 if (!syncState
->stats
)
565 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
567 printf("Convex hull analysis stats:\n");
568 printf("\tout of order packets dropped from analysis: %u\n",
569 analysisData
->stats
->dropped
);
571 printf("\tNumber of points in convex hulls:\n");
573 for (i
= 0; i
< syncState
->traceNb
; i
++)
575 for (j
= i
+ 1; j
< syncState
->traceNb
; j
++)
577 printf("\t\t%3d - %-3d: lower half-hull %-5u upper half-hull %-5u\n",
578 i
, j
, analysisData
->hullArray
[j
][i
]->length
,
579 analysisData
->hullArray
[i
][j
]->length
);
583 printf("\tIndividual synchronization factors:\n");
585 for (i
= 0; i
< syncState
->traceNb
; i
++)
587 for (j
= i
+ 1; j
< syncState
->traceNb
; j
++)
589 FactorsCHull
* factorsCHull
;
591 factorsCHull
= &analysisData
->stats
->allFactors
[j
][i
];
592 printf("\t\t%3d - %-3d: %s", i
, j
,
593 approxNames
[factorsCHull
->type
]);
595 if (factorsCHull
->type
== EXACT
)
597 printf(" a0= % 7g a1= 1 %c %7g\n",
598 factorsCHull
->approx
->offset
,
599 factorsCHull
->approx
->drift
< 0. ? '-' : '+',
600 fabs(factorsCHull
->approx
->drift
));
602 else if (factorsCHull
->type
== MIDDLE
)
604 printf(" a0= % 7g a1= 1 %c %7g accuracy %7g\n",
605 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
606 - 1. < 0. ? '-' : '+', fabs(factorsCHull
->approx
->drift
-
607 1.), factorsCHull
->accuracy
);
608 printf("\t\t a0: % 7g to % 7g (delta= %7g)\n",
609 factorsCHull
->max
->offset
, factorsCHull
->min
->offset
,
610 factorsCHull
->min
->offset
- factorsCHull
->max
->offset
);
611 printf("\t\t a1: 1 %+7g to %+7g (delta= %7g)\n",
612 factorsCHull
->min
->drift
- 1., factorsCHull
->max
->drift
-
613 1., factorsCHull
->max
->drift
- factorsCHull
->min
->drift
);
615 else if (factorsCHull
->type
== FALLBACK
)
617 printf(" a0= % 7g a1= 1 %c %7g error= %7g\n",
618 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
619 - 1. < 0. ? '-' : '+', fabs(factorsCHull
->approx
->drift
-
620 1.), factorsCHull
->accuracy
);
622 else if (factorsCHull
->type
== INCOMPLETE
)
626 if (factorsCHull
->min
->drift
!= -INFINITY
)
628 printf("\t\t min: a0: % 7g a1: 1 %c %7g\n",
629 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
-
630 1. < 0 ? '-' : '+', fabs(factorsCHull
->min
->drift
-
633 if (factorsCHull
->max
->drift
!= INFINITY
)
635 printf("\t\t max: a0: % 7g a1: 1 %c %7g\n",
636 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
-
637 1. < 0 ? '-' : '+', fabs(factorsCHull
->max
->drift
-
641 else if (factorsCHull
->type
== SCREWED
)
645 if (factorsCHull
->min
!= NULL
&& factorsCHull
->min
->drift
!= -INFINITY
)
647 printf("\t\t min: a0: % 7g a1: 1 %c %7g\n",
648 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
-
649 1. < 0 ? '-' : '+', fabs(factorsCHull
->min
->drift
-
652 if (factorsCHull
->max
!= NULL
&& factorsCHull
->max
->drift
!= INFINITY
)
654 printf("\t\t max: a0: % 7g a1: 1 %c %7g\n",
655 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
-
656 1. < 0 ? '-' : '+', fabs(factorsCHull
->max
->drift
-
660 else if (factorsCHull
->type
== ABSENT
)
666 g_assert_not_reached();
674 * A GFunc for g_queue_foreach()
677 * data Point*, point to destroy
680 static void gfPointDestroy(gpointer data
, gpointer userData
)
684 point
= (Point
*) data
;
690 * Find out if a sequence of three points constitutes a "left turn" or a
694 * p1, p2, p3: The three points.
698 * 0 colinear (unlikely result since this uses floating point
702 static int jointCmp(const Point
const* p1
, const Point
const* p2
, const
706 const double fuzzFactor
= 0.;
708 result
= crossProductK(p1
, p2
, p1
, p3
);
709 g_debug("crossProductK(p1= (%llu, %llu), p2= (%llu, %llu), p1= (%llu, %llu), p3= (%llu, %llu))= %g",
710 p1
->x
, p1
->y
, p2
->x
, p2
->y
, p1
->x
, p1
->y
, p3
->x
, p3
->y
, result
);
711 if (result
< fuzzFactor
)
715 else if (result
> fuzzFactor
)
727 * Calculate the k component of the cross product of two vectors.
730 * p1, p2: start and end points of the first vector
731 * p3, p4: start and end points of the second vector
734 * the k component of the cross product when considering the two vectors to
735 * be in the i-j plane. The direction (sign) of the result can be useful to
736 * determine the relative orientation of the two vectors.
738 static double crossProductK(const Point
const* p1
, const Point
const* p2
,
739 const Point
const* p3
, const Point
const* p4
)
741 return ((double) p2
->x
- p1
->x
) * ((double) p4
->y
- p3
->y
) - ((double)
742 p2
->y
- p1
->y
) * ((double) p4
->x
- p3
->x
);
747 * Free a container of FactorsCHull
750 * traceNb: number of traces
751 * allFactors: container of FactorsCHull
753 void freeAllFactors(const unsigned int traceNb
, FactorsCHull
** const
758 for (i
= 0; i
< traceNb
; i
++)
760 for (j
= 0; j
<= i
; j
++)
762 destroyFactorsCHull(&allFactors
[i
][j
]);
771 * Free a FactorsCHull
774 * factorsCHull: container of Factors
776 void destroyFactorsCHull(FactorsCHull
* factorsCHull
)
778 if (factorsCHull
->type
== MIDDLE
|| factorsCHull
->type
==
779 INCOMPLETE
|| factorsCHull
->type
== ABSENT
)
781 free(factorsCHull
->min
);
782 free(factorsCHull
->max
);
784 else if (factorsCHull
->type
== SCREWED
)
786 if (factorsCHull
->min
!= NULL
)
788 free(factorsCHull
->min
);
790 if (factorsCHull
->max
!= NULL
)
792 free(factorsCHull
->max
);
796 if (factorsCHull
->type
== EXACT
|| factorsCHull
->type
== MIDDLE
||
797 factorsCHull
->type
== FALLBACK
)
799 free(factorsCHull
->approx
);
805 * Analyze the convex hulls to determine the synchronization factors between
806 * each pair of trace.
809 * syncState container for synchronization data.
812 * FactorsCHull*[TraceNum][TraceNum] array. See the documentation for the
813 * member allFactors of AnalysisStatsCHull.
815 FactorsCHull
** calculateAllFactors(SyncState
* const syncState
)
817 unsigned int traceNumA
, traceNumB
;
818 FactorsCHull
** allFactors
;
819 AnalysisDataCHull
* analysisData
;
821 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
823 // Allocate allFactors and calculate min and max
824 allFactors
= malloc(syncState
->traceNb
* sizeof(FactorsCHull
*));
825 for (traceNumA
= 0; traceNumA
< syncState
->traceNb
; traceNumA
++)
827 allFactors
[traceNumA
]= malloc((traceNumA
+ 1) * sizeof(FactorsCHull
));
829 allFactors
[traceNumA
][traceNumA
].type
= EXACT
;
830 allFactors
[traceNumA
][traceNumA
].approx
= malloc(sizeof(Factors
));
831 allFactors
[traceNumA
][traceNumA
].approx
->drift
= 1.;
832 allFactors
[traceNumA
][traceNumA
].approx
->offset
= 0.;
834 for (traceNumB
= 0; traceNumB
< traceNumA
; traceNumB
++)
841 size_t factorsOffset
;
843 {MINIMUM
, offsetof(FactorsCHull
, min
)},
844 {MAXIMUM
, offsetof(FactorsCHull
, max
)}
847 cr
= analysisData
->hullArray
[traceNumB
][traceNumA
];
848 cs
= analysisData
->hullArray
[traceNumA
][traceNumB
];
850 for (i
= 0; i
< sizeof(loopValues
) / sizeof(*loopValues
); i
++)
852 g_debug("allFactors[%u][%u].%s = calculateFactorsExact(cr= hullArray[%u][%u], cs= hullArray[%u][%u], %s)",
853 traceNumA
, traceNumB
, loopValues
[i
].factorsOffset
==
854 offsetof(FactorsCHull
, min
) ? "min" : "max", traceNumB
,
855 traceNumA
, traceNumA
, traceNumB
, loopValues
[i
].lineType
==
856 MINIMUM
? "MINIMUM" : "MAXIMUM");
857 *((Factors
**) ((void*) &allFactors
[traceNumA
][traceNumB
] +
858 loopValues
[i
].factorsOffset
))=
859 calculateFactorsExact(cr
, cs
, loopValues
[i
].lineType
);
864 // Calculate approx when possible
865 for (traceNumA
= 0; traceNumA
< syncState
->traceNb
; traceNumA
++)
867 for (traceNumB
= 0; traceNumB
< traceNumA
; traceNumB
++)
869 FactorsCHull
* factorsCHull
;
871 factorsCHull
= &allFactors
[traceNumA
][traceNumB
];
872 if (factorsCHull
->min
== NULL
&& factorsCHull
->max
== NULL
)
874 factorsCHull
->type
= FALLBACK
;
875 calculateFactorsFallback(analysisData
->hullArray
[traceNumB
][traceNumA
],
876 analysisData
->hullArray
[traceNumA
][traceNumB
],
877 &allFactors
[traceNumA
][traceNumB
]);
879 else if (factorsCHull
->min
!= NULL
&& factorsCHull
->max
!= NULL
)
881 if (factorsCHull
->min
->drift
!= -INFINITY
&&
882 factorsCHull
->max
->drift
!= INFINITY
)
884 factorsCHull
->type
= MIDDLE
;
885 calculateFactorsMiddle(factorsCHull
);
887 else if (factorsCHull
->min
->drift
!= -INFINITY
||
888 factorsCHull
->max
->drift
!= INFINITY
)
890 factorsCHull
->type
= INCOMPLETE
;
894 factorsCHull
->type
= ABSENT
;
899 //g_assert_not_reached();
900 factorsCHull
->type
= SCREWED
;
909 /* Calculate approximative factors based on minimum and maximum limits. The
910 * best approximation to make is the interior bissector of the angle formed by
911 * the minimum and maximum lines.
913 * The formulae used come from [Haddad, Yoram: Performance dans les systèmes
914 * répartis: des outils pour les mesures, Université de Paris-Sud, Centre
915 * d'Orsay, September 1988] Section 6.1 p.44
917 * The reasoning for choosing this estimator comes from [Duda, A., Harrus, G.,
918 * Haddad, Y., and Bernard, G.: Estimating global time in distributed systems,
919 * Proc. 7th Int. Conf. on Distributed Computing Systems, Berlin, volume 18,
923 * factors: contains the min and max limits, used to store the result
925 void calculateFactorsMiddle(FactorsCHull
* const factors
)
927 double amin
, amax
, bmin
, bmax
, bhat
;
929 amin
= factors
->max
->offset
;
930 amax
= factors
->min
->offset
;
931 bmin
= factors
->min
->drift
;
932 bmax
= factors
->max
->drift
;
934 g_assert_cmpfloat(bmax
, >, bmin
);
936 factors
->approx
= malloc(sizeof(Factors
));
937 bhat
= (bmax
* bmin
- 1. + sqrt(1. + pow(bmax
, 2.) * pow(bmin
, 2.) +
938 pow(bmax
, 2.) + pow(bmin
, 2.))) / (bmax
+ bmin
);
939 factors
->approx
->offset
= amax
- (amax
- amin
) / 2. * (pow(bhat
, 2.) + 1.)
940 / (1. + bhat
* bmax
);
941 factors
->approx
->drift
= bhat
;
942 factors
->accuracy
= bmax
- bmin
;
947 * Analyze the convex hulls to determine the minimum or maximum
948 * synchronization factors between one pair of trace.
950 * This implements and improves upon the algorithm in [Haddad, Yoram:
951 * Performance dans les systèmes répartis: des outils pour les mesures,
952 * Université de Paris-Sud, Centre d'Orsay, September 1988] Section 6.2 p.47
954 * Some degenerate cases are possible:
955 * 1) the result is unbounded. In that case, when searching for the maximum
956 * factors, result->drift= INFINITY; result->offset= -INFINITY. When
957 * searching for the minimum factors, it is the opposite. It is not
958 * possible to improve the situation with this data.
959 * 2) no line can be above the upper hull and below the lower hull. This is
960 * because the hulls intersect each other or are reversed. This means that
961 * an assertion was false. Most probably, the clocks are not linear. It is
962 * possible to repeat the search with another algorithm that will find a
963 * "best effort" approximation. See calculateFactorsApprox().
966 * cu: the upper half-convex hull, the line must pass above this
967 * and touch it in one point
968 * cl: the lower half-convex hull, the line must pass below this
969 * and touch it in one point
970 * lineType: search for minimum or maximum factors
973 * If a result is found, a struct Factors is allocated, filed with the
974 * result and returned
975 * NULL otherwise, degenerate case 2 is in effect
977 static Factors
* calculateFactorsExact(GQueue
* const cu
, GQueue
* const cl
, const
983 double inversionFactor
;
986 g_debug("calculateFactorsExact(cu= %p, cl= %p, %s)", cu
, cl
, lineType
==
987 MINIMUM
? "MINIMUM" : "MAXIMUM");
989 if (lineType
== MINIMUM
)
993 inversionFactor
= -1.;
1005 // Check for degenerate case 1
1006 if (c1
->length
== 0 || c2
->length
== 0 || ((Point
*) g_queue_peek_nth(c1
,
1007 i1
))->x
>= ((Point
*) g_queue_peek_nth(c2
, i2
))->x
)
1009 result
= malloc(sizeof(Factors
));
1010 if (lineType
== MINIMUM
)
1012 result
->drift
= -INFINITY
;
1013 result
->offset
= INFINITY
;
1017 result
->drift
= INFINITY
;
1018 result
->offset
= -INFINITY
;
1030 g_queue_peek_nth(c1
, i1
),
1031 g_queue_peek_nth(c2
, i2
),
1032 g_queue_peek_nth(c1
, i1
),
1033 g_queue_peek_nth(c2
, i2
- 1)) * inversionFactor
< 0.
1036 if (((Point
*) g_queue_peek_nth(c1
, i1
))->x
1037 < ((Point
*) g_queue_peek_nth(c2
, i2
- 1))->x
)
1043 // Degenerate case 2
1049 i1
+ 1 < c1
->length
- 1
1051 g_queue_peek_nth(c1
, i1
),
1052 g_queue_peek_nth(c2
, i2
),
1053 g_queue_peek_nth(c1
, i1
+ 1),
1054 g_queue_peek_nth(c2
, i2
)) * inversionFactor
< 0.
1057 if (((Point
*) g_queue_peek_nth(c1
, i1
+ 1))->x
1058 < ((Point
*) g_queue_peek_nth(c2
, i2
))->x
)
1064 // Degenerate case 2
1072 g_queue_peek_nth(c1
, i1
),
1073 g_queue_peek_nth(c2
, i2
),
1074 g_queue_peek_nth(c1
, i1
),
1075 g_queue_peek_nth(c2
, i2
- 1)) * inversionFactor
< 0.
1078 p1
= g_queue_peek_nth(c1
, i1
);
1079 p2
= g_queue_peek_nth(c2
, i2
);
1081 g_debug("Resulting points are: c1[i1]: x= %llu y= %llu c2[i2]: x= %llu y= %llu",
1082 p1
->x
, p1
->y
, p2
->x
, p2
->y
);
1084 result
= malloc(sizeof(Factors
));
1085 result
->drift
= slope(p1
, p2
);
1086 result
->offset
= intercept(p1
, p2
);
1088 g_debug("Resulting factors are: drift= %g offset= %g", result
->drift
, result
->offset
);
1095 * Analyze the convex hulls to determine approximate synchronization factors
1096 * between one pair of trace when there is no line that can fit in the
1097 * corridor separating them.
1099 * This implements the algorithm in [Ashton, P.: Algorithms for Off-line Clock
1100 * Synchronisation, University of Canterbury, December 1995, 26] Section 4.2.2
1103 * For each point p1 in cr
1104 * For each point p2 in cs
1106 * Calculate the line paramaters
1107 * For each point p3 in each convex hull
1108 * If p3 is on the wrong side of the line
1110 * If error < errorMin
1114 * cr: the upper half-convex hull
1115 * cs: the lower half-convex hull
1116 * result: a pointer to the pre-allocated struct where the results
1119 static void calculateFactorsFallback(GQueue
* const cr
, GQueue
* const cs
,
1120 FactorsCHull
* const result
)
1122 unsigned int i
, j
, k
;
1127 approx
= malloc(sizeof(Factors
));
1129 for (i
= 0; i
< cs
->length
; i
++)
1131 for (j
= 0; j
< cr
->length
; j
++)
1138 if (((Point
*) g_queue_peek_nth(cs
, i
))->x
< ((Point
*)g_queue_peek_nth(cr
, j
))->x
)
1140 p1
= *(Point
*)g_queue_peek_nth(cs
, i
);
1141 p2
= *(Point
*)g_queue_peek_nth(cr
, j
);
1145 p1
= *(Point
*)g_queue_peek_nth(cr
, j
);
1146 p2
= *(Point
*)g_queue_peek_nth(cs
, i
);
1149 // The lower hull should be above the point
1150 for (k
= 0; k
< cs
->length
; k
++)
1152 if (jointCmp(&p1
, &p2
, g_queue_peek_nth(cs
, k
)) < 0.)
1154 error
+= verticalDistance(&p1
, &p2
, g_queue_peek_nth(cs
, k
));
1158 // The upper hull should be below the point
1159 for (k
= 0; k
< cr
->length
; k
++)
1161 if (jointCmp(&p1
, &p2
, g_queue_peek_nth(cr
, k
)) > 0.)
1163 error
+= verticalDistance(&p1
, &p2
, g_queue_peek_nth(cr
, k
));
1167 if (error
< errorMin
)
1169 g_debug("Fallback: i= %u j= %u is a better match (error= %g)", i
, j
, error
);
1170 approx
->drift
= slope(&p1
, &p2
);
1171 approx
->offset
= intercept(&p1
, &p2
);
1177 result
->approx
= approx
;
1178 result
->accuracy
= errorMin
;
1183 * Calculate the vertical distance between a line and a point
1186 * p1, p2: Two points defining the line
1190 * the vertical distance
1192 static double verticalDistance(Point
* p1
, Point
* p2
, Point
* const point
)
1194 return fabs(slope(p1
, p2
) * point
->x
+ intercept(p1
, p2
) - point
->y
);
1199 * Calculate the slope between two points
1202 * p1, p2 the two points
1207 static double slope(const Point
* const p1
, const Point
* const p2
)
1209 return ((double) p2
->y
- p1
->y
) / (p2
->x
- p1
->x
);
1213 /* Calculate the y-intercept of a line that passes by two points
1216 * p1, p2 the two points
1221 static double intercept(const Point
* const p1
, const Point
* const p2
)
1223 return ((double) p2
->y
* p1
->x
- (double) p1
->y
* p2
->x
) / ((double) p1
->x
- p2
->x
);
1228 * Calculate a resulting offset and drift for each trace.
1230 * Traces are assembled in groups. A group is an "island" of nodes/traces that
1231 * exchanged messages. A reference is determined for each group by using a
1232 * shortest path search based on the accuracy of the approximation. This also
1233 * forms a tree of the best way to relate each node's clock to the reference's
1234 * based on the accuracy. Sometimes it may be necessary or advantageous to
1235 * propagate the factors through intermediary clocks. Resulting factors for
1236 * each trace are determined based on this tree.
1238 * This part was not the focus of my research. The algorithm used here is
1239 * inexact in some ways:
1240 * 1) The reference used may not actually be the best one to use. This is
1241 * because the accuracy is not corrected based on the drift during the
1242 * shortest path search.
1243 * 2) The min and max factors are not propagated and are no longer valid.
1244 * 3) Approximations of different types (MIDDLE and FALLBACK) are compared
1245 * together. The "accuracy" parameters of these have different meanings and
1246 * are not readily comparable.
1248 * Nevertheless, the result is satisfactory. You just can't tell "how much" it
1251 * Two alternative (and subtly different) ways of propagating factors to
1252 * preserve min and max bondaries have been proposed, see:
1253 * [Duda, A., Harrus, G., Haddad, Y., and Bernard, G.: Estimating global time
1254 * in distributed systems, Proc. 7th Int. Conf. on Distributed Computing
1255 * Systems, Berlin, volume 18, 1987] p.304
1257 * [Jezequel, J.M., and Jard, C.: Building a global clock for observing
1258 * computations in distributed memory parallel computers, Concurrency:
1259 * Practice and Experience 8(1), volume 8, John Wiley & Sons, Ltd Chichester,
1260 * 1996, 32] Section 5; which is mostly the same as
1261 * [Jezequel, J.M.: Building a global time on parallel machines, Proceedings
1262 * of the 3rd International Workshop on Distributed Algorithms, LNCS, volume
1263 * 392, 136–147, 1989] Section 5
1266 * syncState: container for synchronization data.
1267 * allFactors: offset and drift between each pair of traces
1270 * Factors[traceNb] synchronization factors for each trace
1272 static GArray
* reduceFactors(SyncState
* const syncState
, FactorsCHull
** const
1277 unsigned int** predecessors
;
1278 double* distanceSums
;
1279 unsigned int* references
;
1282 // Solve the all-pairs shortest path problem using the Floyd-Warshall
1284 floydWarshall(syncState
, allFactors
, &distances
, &predecessors
);
1286 /* Find the reference for each node
1288 * First calculate, for each node, the sum of the distances to each other
1289 * node it can reach.
1291 * Then, go through each "island" of traces to find the trace that has the
1292 * lowest distance sum. Assign this trace as the reference to each trace
1295 distanceSums
= malloc(syncState
->traceNb
* sizeof(double));
1296 for (i
= 0; i
< syncState
->traceNb
; i
++)
1298 distanceSums
[i
]= 0.;
1299 for (j
= 0; j
< syncState
->traceNb
; j
++)
1301 distanceSums
[i
]+= distances
[i
][j
];
1305 references
= malloc(syncState
->traceNb
* sizeof(unsigned int));
1306 for (i
= 0; i
< syncState
->traceNb
; i
++)
1308 references
[i
]= UINT_MAX
;
1310 for (i
= 0; i
< syncState
->traceNb
; i
++)
1312 if (references
[i
] == UINT_MAX
)
1314 unsigned int reference
;
1315 double distanceSumMin
;
1317 // A node is its own reference by default
1319 distanceSumMin
= INFINITY
;
1320 for (j
= 0; j
< syncState
->traceNb
; j
++)
1322 if (distances
[i
][j
] != INFINITY
&& distanceSums
[j
] <
1326 distanceSumMin
= distanceSums
[j
];
1329 for (j
= 0; j
< syncState
->traceNb
; j
++)
1331 if (distances
[i
][j
] != INFINITY
)
1333 references
[j
]= reference
;
1339 for (i
= 0; i
< syncState
->traceNb
; i
++)
1346 /* For each trace, calculate the factors based on their corresponding
1347 * tree. The tree is rooted at the reference and the shortest path to each
1348 * other nodes are the branches.
1350 factors
= g_array_sized_new(FALSE
, FALSE
, sizeof(Factors
),
1351 syncState
->traceNb
);
1352 g_array_set_size(factors
, syncState
->traceNb
);
1353 for (i
= 0; i
< syncState
->traceNb
; i
++)
1355 getFactors(allFactors
, predecessors
, references
, i
, &g_array_index(factors
,
1359 for (i
= 0; i
< syncState
->traceNb
; i
++)
1361 free(predecessors
[i
]);
1371 * Perform an all-source shortest path search using the Floyd-Warshall
1374 * The algorithm is implemented accoding to the description here:
1375 * http://web.mit.edu/urban_or_book/www/book/chapter6/6.2.2.html
1378 * syncState: container for synchronization data.
1379 * allFactors: offset and drift between each pair of traces
1380 * distances: resulting matrix of the length of the shortest path between
1381 * two nodes. If there is no path between two nodes, the
1382 * length is INFINITY
1383 * predecessors: resulting matrix of each node's predecessor on the shortest
1384 * path between two nodes
1386 static void floydWarshall(SyncState
* const syncState
, FactorsCHull
** const
1387 allFactors
, double*** const distances
, unsigned int*** const
1390 unsigned int i
, j
, k
;
1392 // Setup initial conditions
1393 *distances
= malloc(syncState
->traceNb
* sizeof(double*));
1394 *predecessors
= malloc(syncState
->traceNb
* sizeof(unsigned int*));
1395 for (i
= 0; i
< syncState
->traceNb
; i
++)
1397 (*distances
)[i
]= malloc(syncState
->traceNb
* sizeof(double));
1398 for (j
= 0; j
< syncState
->traceNb
; j
++)
1402 g_assert(allFactors
[i
][j
].type
== EXACT
);
1404 (*distances
)[i
][j
]= 0.;
1408 unsigned int row
, col
;
1421 if (allFactors
[row
][col
].type
== MIDDLE
||
1422 allFactors
[row
][col
].type
== FALLBACK
)
1424 (*distances
)[i
][j
]= allFactors
[row
][col
].accuracy
;
1426 else if (allFactors
[row
][col
].type
== INCOMPLETE
||
1427 allFactors
[row
][col
].type
== SCREWED
||
1428 allFactors
[row
][col
].type
== ABSENT
)
1430 (*distances
)[i
][j
]= INFINITY
;
1434 g_assert_not_reached();
1439 (*predecessors
)[i
]= malloc(syncState
->traceNb
* sizeof(unsigned int));
1440 for (j
= 0; j
< syncState
->traceNb
; j
++)
1444 (*predecessors
)[i
][j
]= i
;
1448 (*predecessors
)[i
][j
]= UINT_MAX
;
1453 // Run the iterations
1454 for (k
= 0; k
< syncState
->traceNb
; k
++)
1456 for (i
= 0; i
< syncState
->traceNb
; i
++)
1458 for (j
= 0; j
< syncState
->traceNb
; j
++)
1462 distanceMin
= MIN((*distances
)[i
][j
], (*distances
)[i
][k
] +
1463 (*distances
)[k
][j
]);
1465 if (distanceMin
!= (*distances
)[i
][j
])
1467 (*predecessors
)[i
][j
]= (*predecessors
)[k
][j
];
1470 (*distances
)[i
][j
]= distanceMin
;
1478 * Cummulate the time correction factors to convert a node's time to its
1480 * This function recursively calls itself until it reaches the reference node.
1483 * allFactors: offset and drift between each pair of traces
1484 * predecessors: matrix of each node's predecessor on the shortest
1485 * path between two nodes
1486 * references: reference node for each node
1487 * traceNum: node for which to find the factors
1488 * factors: resulting factors
1490 static void getFactors(FactorsCHull
** const allFactors
, unsigned int** const
1491 predecessors
, unsigned int* const references
, const unsigned int traceNum
,
1492 Factors
* const factors
)
1494 unsigned int reference
;
1496 reference
= references
[traceNum
];
1498 if (reference
== traceNum
)
1500 factors
->offset
= 0.;
1505 Factors previousVertexFactors
;
1507 getFactors(allFactors
, predecessors
, references
,
1508 predecessors
[reference
][traceNum
], &previousVertexFactors
);
1510 // convertir de traceNum à reference
1512 // allFactors convertit de col à row
1514 if (reference
> traceNum
)
1516 factors
->offset
= previousVertexFactors
.drift
*
1517 allFactors
[reference
][traceNum
].approx
->offset
+
1518 previousVertexFactors
.offset
;
1519 factors
->drift
= previousVertexFactors
.drift
*
1520 allFactors
[reference
][traceNum
].approx
->drift
;
1524 factors
->offset
= previousVertexFactors
.drift
* (-1. *
1525 allFactors
[traceNum
][reference
].approx
->offset
/
1526 allFactors
[traceNum
][reference
].approx
->drift
) +
1527 previousVertexFactors
.offset
;
1528 factors
->drift
= previousVertexFactors
.drift
* (1. /
1529 allFactors
[traceNum
][reference
].approx
->drift
);
1536 * Write the analysis-specific graph lines in the gnuplot script.
1539 * syncState: container for synchronization data
1540 * i: first trace number
1541 * j: second trace number, garanteed to be larger than i
1543 void writeAnalysisGraphsPlotsCHull(SyncState
* const syncState
, const unsigned
1544 int i
, const unsigned int j
)
1546 AnalysisDataCHull
* analysisData
;
1547 FactorsCHull
* factorsCHull
;
1549 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
1551 fprintf(syncState
->graphsStream
,
1552 "\t\"analysis_chull-%1$03d_to_%2$03d.data\" "
1553 "title \"Lower half-hull\" with linespoints "
1554 "linecolor rgb \"#015a01\" linetype 4 pointtype 8 pointsize 0.8, \\\n"
1555 "\t\"analysis_chull-%2$03d_to_%1$03d.data\" "
1556 "title \"Upper half-hull\" with linespoints "
1557 "linecolor rgb \"#003366\" linetype 4 pointtype 10 pointsize 0.8, \\\n",
1560 factorsCHull
= &analysisData
->graphsData
->allFactors
[j
][i
];
1561 if (factorsCHull
->type
== EXACT
)
1563 fprintf(syncState
->graphsStream
,
1565 "title \"Exact conversion\" with lines "
1566 "linecolor rgb \"black\" linetype 1, \\\n",
1567 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
);
1569 else if (factorsCHull
->type
== MIDDLE
)
1571 fprintf(syncState
->graphsStream
,
1572 "\t%.2f + %.10f * x "
1573 "title \"Min conversion\" with lines "
1574 "linecolor rgb \"black\" linetype 5, \\\n",
1575 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
);
1576 fprintf(syncState
->graphsStream
,
1577 "\t%.2f + %.10f * x "
1578 "title \"Max conversion\" with lines "
1579 "linecolor rgb \"black\" linetype 8, \\\n",
1580 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
);
1581 fprintf(syncState
->graphsStream
,
1582 "\t%.2f + %.10f * x "
1583 "title \"Middle conversion\" with lines "
1584 "linecolor rgb \"black\" linetype 1, \\\n",
1585 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
);
1587 else if (factorsCHull
->type
== FALLBACK
)
1589 fprintf(syncState
->graphsStream
,
1590 "\t%.2f + %.10f * x "
1591 "title \"Fallback conversion\" with lines "
1592 "linecolor rgb \"gray60\" linetype 1, \\\n",
1593 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
);
1595 else if (factorsCHull
->type
== INCOMPLETE
)
1597 if (factorsCHull
->min
->drift
!= -INFINITY
)
1599 fprintf(syncState
->graphsStream
,
1600 "\t%.2f + %.10f * x "
1601 "title \"Min conversion\" with lines "
1602 "linecolor rgb \"black\" linetype 5, \\\n",
1603 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
);
1606 if (factorsCHull
->max
->drift
!= INFINITY
)
1608 fprintf(syncState
->graphsStream
,
1609 "\t%.2f + %.10f * x "
1610 "title \"Max conversion\" with lines "
1611 "linecolor rgb \"black\" linetype 8, \\\n",
1612 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
);
1615 else if (factorsCHull
->type
== SCREWED
)
1617 if (factorsCHull
->min
!= NULL
&& factorsCHull
->min
->drift
!= -INFINITY
)
1619 fprintf(syncState
->graphsStream
,
1620 "\t%.2f + %.10f * x "
1621 "title \"Min conversion\" with lines "
1622 "linecolor rgb \"black\" linetype 5, \\\n",
1623 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
);
1626 if (factorsCHull
->max
!= NULL
&& factorsCHull
->max
->drift
!= INFINITY
)
1628 fprintf(syncState
->graphsStream
,
1629 "\t%.2f + %.10f * x "
1630 "title \"Max conversion\" with lines "
1631 "linecolor rgb \"black\" linetype 8, \\\n",
1632 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
);
1635 else if (factorsCHull
->type
== ABSENT
)
1640 g_assert_not_reached();