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"
50 // Functions common to all analysis modules
51 static void initAnalysisCHull(SyncState
* const syncState
);
52 static void destroyAnalysisCHull(SyncState
* const syncState
);
54 static void analyzeMessageCHull(SyncState
* const syncState
, Message
* const
56 static GArray
* finalizeAnalysisCHull(SyncState
* const syncState
);
57 static void printAnalysisStatsCHull(SyncState
* const syncState
);
58 static void writeAnalysisGraphsPlotsCHull(SyncState
* const syncState
, const
59 unsigned int i
, const unsigned int j
);
61 // Functions specific to this module
62 static void registerAnalysisCHull() __attribute__((constructor (101)));
64 static void openGraphFiles(SyncState
* const syncState
);
65 static void closeGraphFiles(SyncState
* const syncState
);
66 static void writeGraphFiles(SyncState
* const syncState
);
67 static void gfDumpHullToFile(gpointer data
, gpointer userData
);
69 static void grahamScan(GQueue
* const hull
, Point
* const newPoint
, const
71 static int jointCmp(const Point
* const p1
, const Point
* const p2
, const Point
*
72 const p3
) __attribute__((pure
));
73 static double crossProductK(const Point
const* p1
, const Point
const* p2
,
74 const Point
const* p3
, const Point
const* p4
) __attribute__((pure
));
75 static Factors
* calculateFactorsExact(GQueue
* const cu
, GQueue
* const cl
, const
76 LineType lineType
) __attribute__((pure
));
77 static void calculateFactorsFallback(GQueue
* const cr
, GQueue
* const cs
,
78 FactorsCHull
* const result
);
79 static double slope(const Point
* const p1
, const Point
* const p2
)
80 __attribute__((pure
));
81 static double intercept(const Point
* const p1
, const Point
* const p2
)
82 __attribute__((pure
));
83 static GArray
* reduceFactors(SyncState
* const syncState
, FactorsCHull
**
85 static double verticalDistance(Point
* p1
, Point
* p2
, Point
* const point
)
86 __attribute__((pure
));
87 static void floydWarshall(SyncState
* const syncState
, FactorsCHull
** const
88 allFactors
, double*** const distances
, unsigned int*** const
90 static void getFactors(FactorsCHull
** const allFactors
, unsigned int** const
91 predecessors
, unsigned int* const references
, const unsigned int traceNum
,
92 Factors
* const factors
);
94 static void gfPointDestroy(gpointer data
, gpointer userData
);
97 static AnalysisModule analysisModuleCHull
= {
99 .initAnalysis
= &initAnalysisCHull
,
100 .destroyAnalysis
= &destroyAnalysisCHull
,
101 .analyzeMessage
= &analyzeMessageCHull
,
102 .finalizeAnalysis
= &finalizeAnalysisCHull
,
103 .printAnalysisStats
= &printAnalysisStatsCHull
,
105 .writeTraceTraceForePlots
= &writeAnalysisGraphsPlotsCHull
,
109 const char* const approxNames
[]= {
112 [FALLBACK
]= "Fallback",
113 [INCOMPLETE
]= "Incomplete",
115 [SCREWED
]= "Screwed",
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
= changeToGraphsDir(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
->traceNb
, 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
->traceNb
, 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
->traceNb
, 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: %s", i
, j
,
588 approxNames
[factorsCHull
->type
]);
590 if (factorsCHull
->type
== EXACT
)
592 printf(" a0= % 7g a1= 1 %c %7g\n",
593 factorsCHull
->approx
->offset
,
594 factorsCHull
->approx
->drift
< 0. ? '-' : '+',
595 fabs(factorsCHull
->approx
->drift
));
597 else if (factorsCHull
->type
== MIDDLE
)
599 printf(" a0= % 7g a1= 1 %c %7g accuracy %7g\n",
600 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
601 - 1. < 0. ? '-' : '+', fabs(factorsCHull
->approx
->drift
-
602 1.), factorsCHull
->accuracy
);
603 printf("\t\t a0: % 7g to % 7g (delta= %7g)\n",
604 factorsCHull
->max
->offset
, factorsCHull
->min
->offset
,
605 factorsCHull
->min
->offset
- factorsCHull
->max
->offset
);
606 printf("\t\t a1: 1 %+7g to %+7g (delta= %7g)\n",
607 factorsCHull
->min
->drift
- 1., factorsCHull
->max
->drift
-
608 1., factorsCHull
->max
->drift
- factorsCHull
->min
->drift
);
610 else if (factorsCHull
->type
== FALLBACK
)
612 printf(" a0= % 7g a1= 1 %c %7g error= %7g\n",
613 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
614 - 1. < 0. ? '-' : '+', fabs(factorsCHull
->approx
->drift
-
615 1.), factorsCHull
->accuracy
);
617 else if (factorsCHull
->type
== INCOMPLETE
)
621 if (factorsCHull
->min
->drift
!= -INFINITY
)
623 printf("\t\t min: a0: % 7g a1: 1 %c %7g\n",
624 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
-
625 1. < 0 ? '-' : '+', fabs(factorsCHull
->min
->drift
-
628 if (factorsCHull
->max
->drift
!= INFINITY
)
630 printf("\t\t max: a0: % 7g a1: 1 %c %7g\n",
631 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
-
632 1. < 0 ? '-' : '+', fabs(factorsCHull
->max
->drift
-
636 else if (factorsCHull
->type
== SCREWED
)
640 if (factorsCHull
->min
!= NULL
&& factorsCHull
->min
->drift
!= -INFINITY
)
642 printf("\t\t min: a0: % 7g a1: 1 %c %7g\n",
643 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
-
644 1. < 0 ? '-' : '+', fabs(factorsCHull
->min
->drift
-
647 if (factorsCHull
->max
!= NULL
&& factorsCHull
->max
->drift
!= INFINITY
)
649 printf("\t\t max: a0: % 7g a1: 1 %c %7g\n",
650 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
-
651 1. < 0 ? '-' : '+', fabs(factorsCHull
->max
->drift
-
655 else if (factorsCHull
->type
== ABSENT
)
661 g_assert_not_reached();
669 * A GFunc for g_queue_foreach()
672 * data Point*, point to destroy
675 static void gfPointDestroy(gpointer data
, gpointer userData
)
679 point
= (Point
*) data
;
685 * Find out if a sequence of three points constitutes a "left turn" or a
689 * p1, p2, p3: The three points.
693 * 0 colinear (unlikely result since this uses floating point
697 static int jointCmp(const Point
const* p1
, const Point
const* p2
, const
701 const double fuzzFactor
= 0.;
703 result
= crossProductK(p1
, p2
, p1
, p3
);
704 g_debug("crossProductK(p1= (%llu, %llu), p2= (%llu, %llu), p1= (%llu, %llu), p3= (%llu, %llu))= %g",
705 p1
->x
, p1
->y
, p2
->x
, p2
->y
, p1
->x
, p1
->y
, p3
->x
, p3
->y
, result
);
706 if (result
< fuzzFactor
)
710 else if (result
> fuzzFactor
)
722 * Calculate the k component of the cross product of two vectors.
725 * p1, p2: start and end points of the first vector
726 * p3, p4: start and end points of the second vector
729 * the k component of the cross product when considering the two vectors to
730 * be in the i-j plane. The direction (sign) of the result can be useful to
731 * determine the relative orientation of the two vectors.
733 static double crossProductK(const Point
const* p1
, const Point
const* p2
,
734 const Point
const* p3
, const Point
const* p4
)
736 return ((double) p2
->x
- p1
->x
) * ((double) p4
->y
- p3
->y
) - ((double)
737 p2
->y
- p1
->y
) * ((double) p4
->x
- p3
->x
);
742 * Free a container of FactorsCHull
745 * traceNb: number of traces
746 * allFactors: container of FactorsCHull
748 void freeAllFactors(const unsigned int traceNb
, FactorsCHull
** const
753 for (i
= 0; i
< traceNb
; i
++)
755 for (j
= 0; j
<= i
; j
++)
757 destroyFactorsCHull(&allFactors
[i
][j
]);
766 * Free a FactorsCHull
769 * factorsCHull: container of Factors
771 void destroyFactorsCHull(FactorsCHull
* factorsCHull
)
773 if (factorsCHull
->type
== MIDDLE
|| factorsCHull
->type
==
774 INCOMPLETE
|| factorsCHull
->type
== ABSENT
)
776 free(factorsCHull
->min
);
777 free(factorsCHull
->max
);
779 else if (factorsCHull
->type
== SCREWED
)
781 if (factorsCHull
->min
!= NULL
)
783 free(factorsCHull
->min
);
785 if (factorsCHull
->max
!= NULL
)
787 free(factorsCHull
->max
);
791 if (factorsCHull
->type
== EXACT
|| factorsCHull
->type
== MIDDLE
||
792 factorsCHull
->type
== FALLBACK
)
794 free(factorsCHull
->approx
);
800 * Analyze the convex hulls to determine the synchronization factors between
801 * each pair of trace.
804 * syncState container for synchronization data.
807 * FactorsCHull*[TraceNum][TraceNum] array. See the documentation for the
808 * member allFactors of AnalysisStatsCHull.
810 FactorsCHull
** calculateAllFactors(SyncState
* const syncState
)
812 unsigned int traceNumA
, traceNumB
;
813 FactorsCHull
** allFactors
;
814 AnalysisDataCHull
* analysisData
;
816 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
818 // Allocate allFactors and calculate min and max
819 allFactors
= malloc(syncState
->traceNb
* sizeof(FactorsCHull
*));
820 for (traceNumA
= 0; traceNumA
< syncState
->traceNb
; traceNumA
++)
822 allFactors
[traceNumA
]= malloc((traceNumA
+ 1) * sizeof(FactorsCHull
));
824 allFactors
[traceNumA
][traceNumA
].type
= EXACT
;
825 allFactors
[traceNumA
][traceNumA
].approx
= malloc(sizeof(Factors
));
826 allFactors
[traceNumA
][traceNumA
].approx
->drift
= 1.;
827 allFactors
[traceNumA
][traceNumA
].approx
->offset
= 0.;
829 for (traceNumB
= 0; traceNumB
< traceNumA
; traceNumB
++)
836 size_t factorsOffset
;
838 {MINIMUM
, offsetof(FactorsCHull
, min
)},
839 {MAXIMUM
, offsetof(FactorsCHull
, max
)}
842 cr
= analysisData
->hullArray
[traceNumB
][traceNumA
];
843 cs
= analysisData
->hullArray
[traceNumA
][traceNumB
];
845 for (i
= 0; i
< sizeof(loopValues
) / sizeof(*loopValues
); i
++)
847 g_debug("allFactors[%u][%u].%s = calculateFactorsExact(cr= hullArray[%u][%u], cs= hullArray[%u][%u], %s)",
848 traceNumA
, traceNumB
, loopValues
[i
].factorsOffset
==
849 offsetof(FactorsCHull
, min
) ? "min" : "max", traceNumB
,
850 traceNumA
, traceNumA
, traceNumB
, loopValues
[i
].lineType
==
851 MINIMUM
? "MINIMUM" : "MAXIMUM");
852 *((Factors
**) ((void*) &allFactors
[traceNumA
][traceNumB
] +
853 loopValues
[i
].factorsOffset
))=
854 calculateFactorsExact(cr
, cs
, loopValues
[i
].lineType
);
859 // Calculate approx when possible
860 for (traceNumA
= 0; traceNumA
< syncState
->traceNb
; traceNumA
++)
862 for (traceNumB
= 0; traceNumB
< traceNumA
; traceNumB
++)
864 FactorsCHull
* factorsCHull
;
866 factorsCHull
= &allFactors
[traceNumA
][traceNumB
];
867 if (factorsCHull
->min
== NULL
&& factorsCHull
->max
== NULL
)
869 factorsCHull
->type
= FALLBACK
;
870 calculateFactorsFallback(analysisData
->hullArray
[traceNumB
][traceNumA
],
871 analysisData
->hullArray
[traceNumA
][traceNumB
],
872 &allFactors
[traceNumA
][traceNumB
]);
874 else if (factorsCHull
->min
!= NULL
&& factorsCHull
->max
!= NULL
)
876 if (factorsCHull
->min
->drift
!= -INFINITY
&&
877 factorsCHull
->max
->drift
!= INFINITY
)
879 factorsCHull
->type
= MIDDLE
;
880 calculateFactorsMiddle(factorsCHull
);
882 else if (factorsCHull
->min
->drift
!= -INFINITY
||
883 factorsCHull
->max
->drift
!= INFINITY
)
885 factorsCHull
->type
= INCOMPLETE
;
889 factorsCHull
->type
= ABSENT
;
894 //g_assert_not_reached();
895 factorsCHull
->type
= SCREWED
;
904 /* Calculate approximative factors based on minimum and maximum limits. The
905 * best approximation to make is the interior bissector of the angle formed by
906 * the minimum and maximum lines.
908 * The formulae used come from [Haddad, Yoram: Performance dans les systèmes
909 * répartis: des outils pour les mesures, Université de Paris-Sud, Centre
910 * d'Orsay, September 1988] Section 6.1 p.44
912 * The reasoning for choosing this estimator comes from [Duda, A., Harrus, G.,
913 * Haddad, Y., and Bernard, G.: Estimating global time in distributed systems,
914 * Proc. 7th Int. Conf. on Distributed Computing Systems, Berlin, volume 18,
918 * factors: contains the min and max limits, used to store the result
920 void calculateFactorsMiddle(FactorsCHull
* const factors
)
922 double amin
, amax
, bmin
, bmax
, bhat
;
924 amin
= factors
->max
->offset
;
925 amax
= factors
->min
->offset
;
926 bmin
= factors
->min
->drift
;
927 bmax
= factors
->max
->drift
;
929 g_assert_cmpfloat(bmax
, >, bmin
);
931 factors
->approx
= malloc(sizeof(Factors
));
932 bhat
= (bmax
* bmin
- 1. + sqrt(1. + pow(bmax
, 2.) * pow(bmin
, 2.) +
933 pow(bmax
, 2.) + pow(bmin
, 2.))) / (bmax
+ bmin
);
934 factors
->approx
->offset
= amax
- (amax
- amin
) / 2. * (pow(bhat
, 2.) + 1.)
935 / (1. + bhat
* bmax
);
936 factors
->approx
->drift
= bhat
;
937 factors
->accuracy
= bmax
- bmin
;
942 * Analyze the convex hulls to determine the minimum or maximum
943 * synchronization factors between one pair of trace.
945 * This implements and improves upon the algorithm in [Haddad, Yoram:
946 * Performance dans les systèmes répartis: des outils pour les mesures,
947 * Université de Paris-Sud, Centre d'Orsay, September 1988] Section 6.2 p.47
949 * Some degenerate cases are possible:
950 * 1) the result is unbounded. In that case, when searching for the maximum
951 * factors, result->drift= INFINITY; result->offset= -INFINITY. When
952 * searching for the minimum factors, it is the opposite. It is not
953 * possible to improve the situation with this data.
954 * 2) no line can be above the upper hull and below the lower hull. This is
955 * because the hulls intersect each other or are reversed. This means that
956 * an assertion was false. Most probably, the clocks are not linear. It is
957 * possible to repeat the search with another algorithm that will find a
958 * "best effort" approximation. See calculateFactorsApprox().
961 * cu: the upper half-convex hull, the line must pass above this
962 * and touch it in one point
963 * cl: the lower half-convex hull, the line must pass below this
964 * and touch it in one point
965 * lineType: search for minimum or maximum factors
968 * If a result is found, a struct Factors is allocated, filed with the
969 * result and returned
970 * NULL otherwise, degenerate case 2 is in effect
972 static Factors
* calculateFactorsExact(GQueue
* const cu
, GQueue
* const cl
, const
978 double inversionFactor
;
981 g_debug("calculateFactorsExact(cu= %p, cl= %p, %s)", cu
, cl
, lineType
==
982 MINIMUM
? "MINIMUM" : "MAXIMUM");
984 if (lineType
== MINIMUM
)
988 inversionFactor
= -1.;
1000 // Check for degenerate case 1
1001 if (c1
->length
== 0 || c2
->length
== 0 || ((Point
*) g_queue_peek_nth(c1
,
1002 i1
))->x
>= ((Point
*) g_queue_peek_nth(c2
, i2
))->x
)
1004 result
= malloc(sizeof(Factors
));
1005 if (lineType
== MINIMUM
)
1007 result
->drift
= -INFINITY
;
1008 result
->offset
= INFINITY
;
1012 result
->drift
= INFINITY
;
1013 result
->offset
= -INFINITY
;
1025 g_queue_peek_nth(c1
, i1
),
1026 g_queue_peek_nth(c2
, i2
),
1027 g_queue_peek_nth(c1
, i1
),
1028 g_queue_peek_nth(c2
, i2
- 1)) * inversionFactor
< 0.
1031 if (((Point
*) g_queue_peek_nth(c1
, i1
))->x
1032 < ((Point
*) g_queue_peek_nth(c2
, i2
- 1))->x
)
1038 // Degenerate case 2
1044 i1
+ 1 < c1
->length
- 1
1046 g_queue_peek_nth(c1
, i1
),
1047 g_queue_peek_nth(c2
, i2
),
1048 g_queue_peek_nth(c1
, i1
+ 1),
1049 g_queue_peek_nth(c2
, i2
)) * inversionFactor
< 0.
1052 if (((Point
*) g_queue_peek_nth(c1
, i1
+ 1))->x
1053 < ((Point
*) g_queue_peek_nth(c2
, i2
))->x
)
1059 // Degenerate case 2
1067 g_queue_peek_nth(c1
, i1
),
1068 g_queue_peek_nth(c2
, i2
),
1069 g_queue_peek_nth(c1
, i1
),
1070 g_queue_peek_nth(c2
, i2
- 1)) * inversionFactor
< 0.
1073 p1
= g_queue_peek_nth(c1
, i1
);
1074 p2
= g_queue_peek_nth(c2
, i2
);
1076 g_debug("Resulting points are: c1[i1]: x= %llu y= %llu c2[i2]: x= %llu y= %llu",
1077 p1
->x
, p1
->y
, p2
->x
, p2
->y
);
1079 result
= malloc(sizeof(Factors
));
1080 result
->drift
= slope(p1
, p2
);
1081 result
->offset
= intercept(p1
, p2
);
1083 g_debug("Resulting factors are: drift= %g offset= %g", result
->drift
, result
->offset
);
1090 * Analyze the convex hulls to determine approximate synchronization factors
1091 * between one pair of trace when there is no line that can fit in the
1092 * corridor separating them.
1094 * This implements the algorithm in [Ashton, P.: Algorithms for Off-line Clock
1095 * Synchronisation, University of Canterbury, December 1995, 26] Section 4.2.2
1098 * For each point p1 in cr
1099 * For each point p2 in cs
1101 * Calculate the line paramaters
1102 * For each point p3 in each convex hull
1103 * If p3 is on the wrong side of the line
1105 * If error < errorMin
1109 * cr: the upper half-convex hull
1110 * cs: the lower half-convex hull
1111 * result: a pointer to the pre-allocated struct where the results
1114 static void calculateFactorsFallback(GQueue
* const cr
, GQueue
* const cs
,
1115 FactorsCHull
* const result
)
1117 unsigned int i
, j
, k
;
1122 approx
= malloc(sizeof(Factors
));
1124 for (i
= 0; i
< cs
->length
; i
++)
1126 for (j
= 0; j
< cr
->length
; j
++)
1133 if (((Point
*) g_queue_peek_nth(cs
, i
))->x
< ((Point
*)g_queue_peek_nth(cr
, j
))->x
)
1135 p1
= *(Point
*)g_queue_peek_nth(cs
, i
);
1136 p2
= *(Point
*)g_queue_peek_nth(cr
, j
);
1140 p1
= *(Point
*)g_queue_peek_nth(cr
, j
);
1141 p2
= *(Point
*)g_queue_peek_nth(cs
, i
);
1144 // The lower hull should be above the point
1145 for (k
= 0; k
< cs
->length
; k
++)
1147 if (jointCmp(&p1
, &p2
, g_queue_peek_nth(cs
, k
)) < 0.)
1149 error
+= verticalDistance(&p1
, &p2
, g_queue_peek_nth(cs
, k
));
1153 // The upper hull should be below the point
1154 for (k
= 0; k
< cr
->length
; k
++)
1156 if (jointCmp(&p1
, &p2
, g_queue_peek_nth(cr
, k
)) > 0.)
1158 error
+= verticalDistance(&p1
, &p2
, g_queue_peek_nth(cr
, k
));
1162 if (error
< errorMin
)
1164 g_debug("Fallback: i= %u j= %u is a better match (error= %g)", i
, j
, error
);
1165 approx
->drift
= slope(&p1
, &p2
);
1166 approx
->offset
= intercept(&p1
, &p2
);
1172 result
->approx
= approx
;
1173 result
->accuracy
= errorMin
;
1178 * Calculate the vertical distance between a line and a point
1181 * p1, p2: Two points defining the line
1185 * the vertical distance
1187 static double verticalDistance(Point
* p1
, Point
* p2
, Point
* const point
)
1189 return fabs(slope(p1
, p2
) * point
->x
+ intercept(p1
, p2
) - point
->y
);
1194 * Calculate the slope between two points
1197 * p1, p2 the two points
1202 static double slope(const Point
* const p1
, const Point
* const p2
)
1204 return ((double) p2
->y
- p1
->y
) / (p2
->x
- p1
->x
);
1208 /* Calculate the y-intercept of a line that passes by two points
1211 * p1, p2 the two points
1216 static double intercept(const Point
* const p1
, const Point
* const p2
)
1218 return ((double) p2
->y
* p1
->x
- (double) p1
->y
* p2
->x
) / ((double) p1
->x
- p2
->x
);
1223 * Calculate a resulting offset and drift for each trace.
1225 * Traces are assembled in groups. A group is an "island" of nodes/traces that
1226 * exchanged messages. A reference is determined for each group by using a
1227 * shortest path search based on the accuracy of the approximation. This also
1228 * forms a tree of the best way to relate each node's clock to the reference's
1229 * based on the accuracy. Sometimes it may be necessary or advantageous to
1230 * propagate the factors through intermediary clocks. Resulting factors for
1231 * each trace are determined based on this tree.
1233 * This part was not the focus of my research. The algorithm used here is
1234 * inexact in some ways:
1235 * 1) The reference used may not actually be the best one to use. This is
1236 * because the accuracy is not corrected based on the drift during the
1237 * shortest path search.
1238 * 2) The min and max factors are not propagated and are no longer valid.
1239 * 3) Approximations of different types (MIDDLE and FALLBACK) are compared
1240 * together. The "accuracy" parameters of these have different meanings and
1241 * are not readily comparable.
1243 * Nevertheless, the result is satisfactory. You just can't tell "how much" it
1246 * Two alternative (and subtly different) ways of propagating factors to
1247 * preserve min and max bondaries have been proposed, see:
1248 * [Duda, A., Harrus, G., Haddad, Y., and Bernard, G.: Estimating global time
1249 * in distributed systems, Proc. 7th Int. Conf. on Distributed Computing
1250 * Systems, Berlin, volume 18, 1987] p.304
1252 * [Jezequel, J.M., and Jard, C.: Building a global clock for observing
1253 * computations in distributed memory parallel computers, Concurrency:
1254 * Practice and Experience 8(1), volume 8, John Wiley & Sons, Ltd Chichester,
1255 * 1996, 32] Section 5; which is mostly the same as
1256 * [Jezequel, J.M.: Building a global time on parallel machines, Proceedings
1257 * of the 3rd International Workshop on Distributed Algorithms, LNCS, volume
1258 * 392, 136–147, 1989] Section 5
1261 * syncState: container for synchronization data.
1262 * allFactors: offset and drift between each pair of traces
1265 * Factors[traceNb] synchronization factors for each trace
1267 static GArray
* reduceFactors(SyncState
* const syncState
, FactorsCHull
** const
1272 unsigned int** predecessors
;
1273 double* distanceSums
;
1274 unsigned int* references
;
1277 // Solve the all-pairs shortest path problem using the Floyd-Warshall
1279 floydWarshall(syncState
, allFactors
, &distances
, &predecessors
);
1281 /* Find the reference for each node
1283 * First calculate, for each node, the sum of the distances to each other
1284 * node it can reach.
1286 * Then, go through each "island" of traces to find the trace that has the
1287 * lowest distance sum. Assign this trace as the reference to each trace
1290 distanceSums
= malloc(syncState
->traceNb
* sizeof(double));
1291 for (i
= 0; i
< syncState
->traceNb
; i
++)
1293 distanceSums
[i
]= 0.;
1294 for (j
= 0; j
< syncState
->traceNb
; j
++)
1296 distanceSums
[i
]+= distances
[i
][j
];
1300 references
= malloc(syncState
->traceNb
* sizeof(unsigned int));
1301 for (i
= 0; i
< syncState
->traceNb
; i
++)
1303 references
[i
]= UINT_MAX
;
1305 for (i
= 0; i
< syncState
->traceNb
; i
++)
1307 if (references
[i
] == UINT_MAX
)
1309 unsigned int reference
;
1310 double distanceSumMin
;
1312 // A node is its own reference by default
1314 distanceSumMin
= INFINITY
;
1315 for (j
= 0; j
< syncState
->traceNb
; j
++)
1317 if (distances
[i
][j
] != INFINITY
&& distanceSums
[j
] <
1321 distanceSumMin
= distanceSums
[j
];
1324 for (j
= 0; j
< syncState
->traceNb
; j
++)
1326 if (distances
[i
][j
] != INFINITY
)
1328 references
[j
]= reference
;
1334 for (i
= 0; i
< syncState
->traceNb
; i
++)
1341 /* For each trace, calculate the factors based on their corresponding
1342 * tree. The tree is rooted at the reference and the shortest path to each
1343 * other nodes are the branches.
1345 factors
= g_array_sized_new(FALSE
, FALSE
, sizeof(Factors
),
1346 syncState
->traceNb
);
1347 g_array_set_size(factors
, syncState
->traceNb
);
1348 for (i
= 0; i
< syncState
->traceNb
; i
++)
1350 getFactors(allFactors
, predecessors
, references
, i
, &g_array_index(factors
,
1354 for (i
= 0; i
< syncState
->traceNb
; i
++)
1356 free(predecessors
[i
]);
1366 * Perform an all-source shortest path search using the Floyd-Warshall
1369 * The algorithm is implemented accoding to the description here:
1370 * http://web.mit.edu/urban_or_book/www/book/chapter6/6.2.2.html
1373 * syncState: container for synchronization data.
1374 * allFactors: offset and drift between each pair of traces
1375 * distances: resulting matrix of the length of the shortest path between
1376 * two nodes. If there is no path between two nodes, the
1377 * length is INFINITY
1378 * predecessors: resulting matrix of each node's predecessor on the shortest
1379 * path between two nodes
1381 static void floydWarshall(SyncState
* const syncState
, FactorsCHull
** const
1382 allFactors
, double*** const distances
, unsigned int*** const
1385 unsigned int i
, j
, k
;
1387 // Setup initial conditions
1388 *distances
= malloc(syncState
->traceNb
* sizeof(double*));
1389 *predecessors
= malloc(syncState
->traceNb
* sizeof(unsigned int*));
1390 for (i
= 0; i
< syncState
->traceNb
; i
++)
1392 (*distances
)[i
]= malloc(syncState
->traceNb
* sizeof(double));
1393 for (j
= 0; j
< syncState
->traceNb
; j
++)
1397 g_assert(allFactors
[i
][j
].type
== EXACT
);
1399 (*distances
)[i
][j
]= 0.;
1403 unsigned int row
, col
;
1416 if (allFactors
[row
][col
].type
== MIDDLE
||
1417 allFactors
[row
][col
].type
== FALLBACK
)
1419 (*distances
)[i
][j
]= allFactors
[row
][col
].accuracy
;
1421 else if (allFactors
[row
][col
].type
== INCOMPLETE
||
1422 allFactors
[row
][col
].type
== SCREWED
||
1423 allFactors
[row
][col
].type
== ABSENT
)
1425 (*distances
)[i
][j
]= INFINITY
;
1429 g_assert_not_reached();
1434 (*predecessors
)[i
]= malloc(syncState
->traceNb
* sizeof(unsigned int));
1435 for (j
= 0; j
< syncState
->traceNb
; j
++)
1439 (*predecessors
)[i
][j
]= i
;
1443 (*predecessors
)[i
][j
]= UINT_MAX
;
1448 // Run the iterations
1449 for (k
= 0; k
< syncState
->traceNb
; k
++)
1451 for (i
= 0; i
< syncState
->traceNb
; i
++)
1453 for (j
= 0; j
< syncState
->traceNb
; j
++)
1457 distanceMin
= MIN((*distances
)[i
][j
], (*distances
)[i
][k
] +
1458 (*distances
)[k
][j
]);
1460 if (distanceMin
!= (*distances
)[i
][j
])
1462 (*predecessors
)[i
][j
]= (*predecessors
)[k
][j
];
1465 (*distances
)[i
][j
]= distanceMin
;
1473 * Cummulate the time correction factors to convert a node's time to its
1475 * This function recursively calls itself until it reaches the reference node.
1478 * allFactors: offset and drift between each pair of traces
1479 * predecessors: matrix of each node's predecessor on the shortest
1480 * path between two nodes
1481 * references: reference node for each node
1482 * traceNum: node for which to find the factors
1483 * factors: resulting factors
1485 static void getFactors(FactorsCHull
** const allFactors
, unsigned int** const
1486 predecessors
, unsigned int* const references
, const unsigned int traceNum
,
1487 Factors
* const factors
)
1489 unsigned int reference
;
1491 reference
= references
[traceNum
];
1493 if (reference
== traceNum
)
1495 factors
->offset
= 0.;
1500 Factors previousVertexFactors
;
1502 getFactors(allFactors
, predecessors
, references
,
1503 predecessors
[reference
][traceNum
], &previousVertexFactors
);
1505 // convertir de traceNum à reference
1507 // allFactors convertit de col à row
1509 if (reference
> traceNum
)
1511 factors
->offset
= previousVertexFactors
.drift
*
1512 allFactors
[reference
][traceNum
].approx
->offset
+
1513 previousVertexFactors
.offset
;
1514 factors
->drift
= previousVertexFactors
.drift
*
1515 allFactors
[reference
][traceNum
].approx
->drift
;
1519 factors
->offset
= previousVertexFactors
.drift
* (-1. *
1520 allFactors
[traceNum
][reference
].approx
->offset
/
1521 allFactors
[traceNum
][reference
].approx
->drift
) +
1522 previousVertexFactors
.offset
;
1523 factors
->drift
= previousVertexFactors
.drift
* (1. /
1524 allFactors
[traceNum
][reference
].approx
->drift
);
1531 * Write the analysis-specific graph lines in the gnuplot script.
1534 * syncState: container for synchronization data
1535 * i: first trace number
1536 * j: second trace number, garanteed to be larger than i
1538 void writeAnalysisGraphsPlotsCHull(SyncState
* const syncState
, const unsigned
1539 int i
, const unsigned int j
)
1541 AnalysisDataCHull
* analysisData
;
1542 FactorsCHull
* factorsCHull
;
1544 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
1546 fprintf(syncState
->graphsStream
,
1547 "\t\"analysis_chull-%1$03d_to_%2$03d.data\" "
1548 "title \"Lower half-hull\" with linespoints "
1549 "linecolor rgb \"#015a01\" linetype 4 pointtype 8 pointsize 0.8, \\\n"
1550 "\t\"analysis_chull-%2$03d_to_%1$03d.data\" "
1551 "title \"Upper half-hull\" with linespoints "
1552 "linecolor rgb \"#003366\" linetype 4 pointtype 10 pointsize 0.8, \\\n",
1555 factorsCHull
= &analysisData
->graphsData
->allFactors
[j
][i
];
1556 if (factorsCHull
->type
== EXACT
)
1558 fprintf(syncState
->graphsStream
,
1560 "title \"Exact conversion\" with lines "
1561 "linecolor rgb \"black\" linetype 1, \\\n",
1562 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
);
1564 else if (factorsCHull
->type
== MIDDLE
)
1566 fprintf(syncState
->graphsStream
,
1567 "\t%.2f + %.10f * x "
1568 "title \"Min conversion\" with lines "
1569 "linecolor rgb \"black\" linetype 5, \\\n",
1570 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
);
1571 fprintf(syncState
->graphsStream
,
1572 "\t%.2f + %.10f * x "
1573 "title \"Max conversion\" with lines "
1574 "linecolor rgb \"black\" linetype 8, \\\n",
1575 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
);
1576 fprintf(syncState
->graphsStream
,
1577 "\t%.2f + %.10f * x "
1578 "title \"Middle conversion\" with lines "
1579 "linecolor rgb \"black\" linetype 1, \\\n",
1580 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
);
1582 else if (factorsCHull
->type
== FALLBACK
)
1584 fprintf(syncState
->graphsStream
,
1585 "\t%.2f + %.10f * x "
1586 "title \"Fallback conversion\" with lines "
1587 "linecolor rgb \"gray60\" linetype 1, \\\n",
1588 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
);
1590 else if (factorsCHull
->type
== INCOMPLETE
)
1592 if (factorsCHull
->min
->drift
!= -INFINITY
)
1594 fprintf(syncState
->graphsStream
,
1595 "\t%.2f + %.10f * x "
1596 "title \"Min conversion\" with lines "
1597 "linecolor rgb \"black\" linetype 5, \\\n",
1598 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
);
1601 if (factorsCHull
->max
->drift
!= INFINITY
)
1603 fprintf(syncState
->graphsStream
,
1604 "\t%.2f + %.10f * x "
1605 "title \"Max conversion\" with lines "
1606 "linecolor rgb \"black\" linetype 8, \\\n",
1607 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
);
1610 else if (factorsCHull
->type
== SCREWED
)
1612 if (factorsCHull
->min
!= NULL
&& factorsCHull
->min
->drift
!= -INFINITY
)
1614 fprintf(syncState
->graphsStream
,
1615 "\t%.2f + %.10f * x "
1616 "title \"Min conversion\" with lines "
1617 "linecolor rgb \"black\" linetype 5, \\\n",
1618 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
);
1621 if (factorsCHull
->max
!= NULL
&& factorsCHull
->max
->drift
!= INFINITY
)
1623 fprintf(syncState
->graphsStream
,
1624 "\t%.2f + %.10f * x "
1625 "title \"Max conversion\" with lines "
1626 "linecolor rgb \"black\" linetype 8, \\\n",
1627 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
);
1630 else if (factorsCHull
->type
== ABSENT
)
1635 g_assert_not_reached();