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(FILE* stream
, SyncState
* const
64 syncState
, const 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 .analyzeExchange
= NULL
,
112 .analyzeBroadcast
= NULL
,
113 .finalizeAnalysis
= &finalizeAnalysisCHull
,
114 .printAnalysisStats
= &printAnalysisStatsCHull
,
115 .writeAnalysisGraphsPlots
= &writeAnalysisGraphsPlotsCHull
,
116 .writeAnalysisGraphsOptions
= NULL
,
121 * Analysis module registering function
123 static void registerAnalysisCHull()
125 g_queue_push_tail(&analysisModules
, &analysisModuleCHull
);
130 * Analysis init function
132 * This function is called at the beginning of a synchronization run for a set
135 * Allocate some of the analysis specific data structures
138 * syncState container for synchronization data.
139 * This function allocates or initializes these analysisData
144 static void initAnalysisCHull(SyncState
* const syncState
)
147 AnalysisDataCHull
* analysisData
;
149 analysisData
= malloc(sizeof(AnalysisDataCHull
));
150 syncState
->analysisData
= analysisData
;
152 analysisData
->hullArray
= malloc(syncState
->traceNb
* sizeof(GQueue
**));
153 for (i
= 0; i
< syncState
->traceNb
; i
++)
155 analysisData
->hullArray
[i
]= malloc(syncState
->traceNb
* sizeof(GQueue
*));
157 for (j
= 0; j
< syncState
->traceNb
; j
++)
159 analysisData
->hullArray
[i
][j
]= g_queue_new();
163 if (syncState
->stats
)
165 analysisData
->stats
= malloc(sizeof(AnalysisStatsCHull
));
166 analysisData
->stats
->dropped
= 0;
167 analysisData
->stats
->allFactors
= NULL
;
170 if (syncState
->graphs
)
172 analysisData
->graphsData
= malloc(sizeof(AnalysisGraphsDataCHull
));
173 openGraphFiles(syncState
);
174 analysisData
->graphsData
->allFactors
= NULL
;
180 * Create and open files used to store convex hull points to genereate
181 * graphs. Allocate and populate array to store file pointers.
184 * syncState: container for synchronization data
186 static void openGraphFiles(SyncState
* const syncState
)
192 AnalysisDataCHull
* analysisData
;
194 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
196 cwd
= changeToGraphDir(syncState
->graphs
);
198 analysisData
->graphsData
->hullPoints
= malloc(syncState
->traceNb
*
200 for (i
= 0; i
< syncState
->traceNb
; i
++)
202 analysisData
->graphsData
->hullPoints
[i
]= malloc(syncState
->traceNb
*
204 for (j
= 0; j
< syncState
->traceNb
; j
++)
208 retval
= snprintf(name
, sizeof(name
),
209 "analysis_chull-%03u_to_%03u.data", j
, i
);
210 if (retval
> sizeof(name
) - 1)
212 name
[sizeof(name
) - 1]= '\0';
214 if ((analysisData
->graphsData
->hullPoints
[i
][j
]= fopen(name
, "w")) ==
217 g_error(strerror(errno
));
226 g_error(strerror(errno
));
233 * Write hull points to files to generate graphs.
236 * syncState: container for synchronization data
238 static void writeGraphFiles(SyncState
* const syncState
)
241 AnalysisDataCHull
* analysisData
;
243 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
245 for (i
= 0; i
< syncState
->traceNb
; i
++)
247 for (j
= 0; j
< syncState
->traceNb
; j
++)
251 g_queue_foreach(analysisData
->hullArray
[i
][j
],
253 analysisData
->graphsData
->hullPoints
[i
][j
]);
261 * A GFunc for g_queue_foreach. Write a hull point to a file used to generate
265 * data: Point*, point to write to the file
266 * userData: FILE*, file pointer where to write the point
268 static void gfDumpHullToFile(gpointer data
, gpointer userData
)
272 point
= (Point
*) data
;
273 fprintf((FILE*) userData
, "%20llu %20llu\n", point
->x
, point
->y
);
278 * Close files used to store convex hull points to generate graphs.
279 * Deallocate array to store file pointers.
282 * syncState: container for synchronization data
284 static void closeGraphFiles(SyncState
* const syncState
)
287 AnalysisDataCHull
* analysisData
;
290 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
292 if (analysisData
->graphsData
->hullPoints
== NULL
)
297 for (i
= 0; i
< syncState
->traceNb
; i
++)
299 for (j
= 0; j
< syncState
->traceNb
; j
++)
303 retval
= fclose(analysisData
->graphsData
->hullPoints
[i
][j
]);
306 g_error(strerror(errno
));
310 free(analysisData
->graphsData
->hullPoints
[i
]);
312 free(analysisData
->graphsData
->hullPoints
);
313 analysisData
->graphsData
->hullPoints
= NULL
;
318 * Analysis destroy function
320 * Free the analysis specific data structures
323 * syncState container for synchronization data.
324 * This function deallocates these analysisData members:
328 static void destroyAnalysisCHull(SyncState
* const syncState
)
331 AnalysisDataCHull
* analysisData
;
333 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
335 if (analysisData
== NULL
)
340 for (i
= 0; i
< syncState
->traceNb
; i
++)
342 for (j
= 0; j
< syncState
->traceNb
; j
++)
344 g_queue_foreach(analysisData
->hullArray
[i
][j
], gfPointDestroy
, NULL
);
346 free(analysisData
->hullArray
[i
]);
348 free(analysisData
->hullArray
);
350 if (syncState
->stats
)
352 if (analysisData
->stats
->allFactors
!= NULL
)
354 freeAllFactors(syncState
, analysisData
->stats
->allFactors
);
357 free(analysisData
->stats
);
360 if (syncState
->graphs
)
362 if (analysisData
->graphsData
->hullPoints
!= NULL
)
364 closeGraphFiles(syncState
);
367 if (!syncState
->stats
&& analysisData
->graphsData
->allFactors
!= NULL
)
369 freeAllFactors(syncState
, analysisData
->graphsData
->allFactors
);
372 free(analysisData
->graphsData
);
375 free(syncState
->analysisData
);
376 syncState
->analysisData
= NULL
;
381 * Perform analysis on an event pair.
384 * syncState container for synchronization data
385 * message structure containing the events
387 static void analyzeMessageCHull(SyncState
* const syncState
, Message
* const message
)
389 AnalysisDataCHull
* analysisData
;
394 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
396 newPoint
= malloc(sizeof(Point
));
397 if (message
->inE
->traceNum
< message
->outE
->traceNum
)
399 // CA is inE->traceNum
400 newPoint
->x
= message
->inE
->time
;
401 newPoint
->y
= message
->outE
->time
;
403 g_debug("Reception point hullArray[%lu][%lu] x= inE->time= %llu y= outE->time= %llu",
404 message
->inE
->traceNum
, message
->outE
->traceNum
, newPoint
->x
,
409 // CA is outE->traceNum
410 newPoint
->x
= message
->outE
->time
;
411 newPoint
->y
= message
->inE
->time
;
413 g_debug("Send point hullArray[%lu][%lu] x= inE->time= %llu y= outE->time= %llu",
414 message
->inE
->traceNum
, message
->outE
->traceNum
, newPoint
->x
,
419 analysisData
->hullArray
[message
->inE
->traceNum
][message
->outE
->traceNum
];
421 if (hull
->length
>= 1 && newPoint
->x
< ((Point
*)
422 g_queue_peek_tail(hull
))->x
)
424 if (syncState
->stats
)
426 analysisData
->stats
->dropped
++;
433 grahamScan(hull
, newPoint
, hullType
);
439 * Construct one half of a convex hull from abscissa-sorted points
442 * hull: the points already in the hull
443 * newPoint: a new point to consider
444 * type: which half of the hull to construct
446 static void grahamScan(GQueue
* const hull
, Point
* const newPoint
, const
451 g_debug("grahamScan(hull (length: %u), newPoint, %s)", hull
->length
, type
452 == LOWER
? "LOWER" : "UPPER");
463 if (hull
->length
>= 2)
465 g_debug("jointCmp(hull[%u], hull[%u], newPoint) * inversionFactor = %d * %d = %d",
468 jointCmp(g_queue_peek_nth(hull
, hull
->length
- 2),
469 g_queue_peek_tail(hull
), newPoint
),
471 jointCmp(g_queue_peek_nth(hull
, hull
->length
- 2),
472 g_queue_peek_tail(hull
), newPoint
) * inversionFactor
);
474 while (hull
->length
>= 2 && jointCmp(g_queue_peek_nth(hull
, hull
->length
-
475 2), g_queue_peek_tail(hull
), newPoint
) * inversionFactor
<= 0)
477 g_debug("Removing hull[%u]", hull
->length
);
478 free((Point
*) g_queue_pop_tail(hull
));
480 if (hull
->length
>= 2)
482 g_debug("jointCmp(hull[%u], hull[%u], newPoint) * inversionFactor = %d * %d = %d",
485 jointCmp(g_queue_peek_nth(hull
, hull
->length
- 2),
486 g_queue_peek_tail(hull
), newPoint
),
488 jointCmp(g_queue_peek_nth(hull
, hull
->length
- 2),
489 g_queue_peek_tail(hull
), newPoint
) * inversionFactor
);
492 g_queue_push_tail(hull
, newPoint
);
497 * Finalize the factor calculations
500 * syncState container for synchronization data.
503 * Factors[traceNb] synchronization factors for each trace
505 static GArray
* finalizeAnalysisCHull(SyncState
* const syncState
)
507 AnalysisDataCHull
* analysisData
;
509 FactorsCHull
** allFactors
;
511 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
513 if (syncState
->graphs
)
515 writeGraphFiles(syncState
);
516 closeGraphFiles(syncState
);
519 allFactors
= calculateAllFactors(syncState
);
521 factors
= reduceFactors(syncState
, allFactors
);
523 if (syncState
->stats
|| syncState
->graphs
)
525 if (syncState
->stats
)
527 analysisData
->stats
->allFactors
= allFactors
;
530 if (syncState
->graphs
)
532 analysisData
->graphsData
->allFactors
= allFactors
;
537 freeAllFactors(syncState
, allFactors
);
545 * Print statistics related to analysis. Must be called after
549 * syncState container for synchronization data.
551 static void printAnalysisStatsCHull(SyncState
* const syncState
)
553 AnalysisDataCHull
* analysisData
;
556 if (!syncState
->stats
)
561 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
563 printf("Convex hull analysis stats:\n");
564 printf("\tout of order packets dropped from analysis: %u\n",
565 analysisData
->stats
->dropped
);
567 printf("\tNumber of points in convex hulls:\n");
569 for (i
= 0; i
< syncState
->traceNb
; i
++)
571 for (j
= i
+ 1; j
< syncState
->traceNb
; j
++)
573 printf("\t\t%3d - %-3d: lower half-hull %-5u upper half-hull %-5u\n",
574 i
, j
, analysisData
->hullArray
[j
][i
]->length
,
575 analysisData
->hullArray
[i
][j
]->length
);
579 printf("\tIndividual synchronization factors:\n");
581 for (i
= 0; i
< syncState
->traceNb
; i
++)
583 for (j
= i
+ 1; j
< syncState
->traceNb
; j
++)
585 FactorsCHull
* factorsCHull
;
587 factorsCHull
= &analysisData
->stats
->allFactors
[j
][i
];
588 printf("\t\t%3d - %-3d: ", i
, j
);
590 if (factorsCHull
->type
== EXACT
)
592 printf("Exact 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("Middle 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("Fallback 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
)
619 printf("Incomplete\n");
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 * syncState: container for synchronization data.
746 * allFactors: container of Factors
748 static void freeAllFactors(const SyncState
* const syncState
, FactorsCHull
**
753 for (i
= 0; i
< syncState
->traceNb
; i
++)
755 for (j
= 0; j
<= i
; j
++)
757 FactorsCHull
* factorsCHull
;
759 factorsCHull
= &allFactors
[i
][j
];
760 if (factorsCHull
->type
== MIDDLE
|| factorsCHull
->type
==
761 INCOMPLETE
|| factorsCHull
->type
== ABSENT
)
763 free(factorsCHull
->min
);
764 free(factorsCHull
->max
);
766 else if (factorsCHull
->type
== SCREWED
)
768 if (factorsCHull
->min
!= NULL
)
770 free(factorsCHull
->min
);
772 if (factorsCHull
->max
!= NULL
)
774 free(factorsCHull
->max
);
778 if (factorsCHull
->type
== EXACT
|| factorsCHull
->type
== MIDDLE
||
779 factorsCHull
->type
== FALLBACK
)
781 free(factorsCHull
->approx
);
791 * Analyze the convex hulls to determine the synchronization factors between
792 * each pair of trace.
795 * syncState container for synchronization data.
798 * FactorsCHull*[TraceNum][TraceNum] array. See the documentation for the
799 * member allFactors of AnalysisStatsCHull.
801 static FactorsCHull
** calculateAllFactors(SyncState
* const syncState
)
803 unsigned int traceNumA
, traceNumB
;
804 FactorsCHull
** allFactors
;
805 AnalysisDataCHull
* analysisData
;
807 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
809 // Allocate allFactors and calculate min and max
810 allFactors
= malloc(syncState
->traceNb
* sizeof(FactorsCHull
*));
811 for (traceNumA
= 0; traceNumA
< syncState
->traceNb
; traceNumA
++)
813 allFactors
[traceNumA
]= malloc((traceNumA
+ 1) * sizeof(FactorsCHull
));
815 allFactors
[traceNumA
][traceNumA
].type
= EXACT
;
816 allFactors
[traceNumA
][traceNumA
].approx
= malloc(sizeof(Factors
));
817 allFactors
[traceNumA
][traceNumA
].approx
->drift
= 1.;
818 allFactors
[traceNumA
][traceNumA
].approx
->offset
= 0.;
820 for (traceNumB
= 0; traceNumB
< traceNumA
; traceNumB
++)
827 size_t factorsOffset
;
829 {MINIMUM
, offsetof(FactorsCHull
, min
)},
830 {MAXIMUM
, offsetof(FactorsCHull
, max
)}
833 cr
= analysisData
->hullArray
[traceNumB
][traceNumA
];
834 cs
= analysisData
->hullArray
[traceNumA
][traceNumB
];
836 for (i
= 0; i
< sizeof(loopValues
) / sizeof(*loopValues
); i
++)
838 g_debug("allFactors[%u][%u].%s = calculateFactorsExact(cr= hullArray[%u][%u], cs= hullArray[%u][%u], %s)",
839 traceNumA
, traceNumB
, loopValues
[i
].factorsOffset
==
840 offsetof(FactorsCHull
, min
) ? "min" : "max", traceNumB
,
841 traceNumA
, traceNumA
, traceNumB
, loopValues
[i
].lineType
==
842 MINIMUM
? "MINIMUM" : "MAXIMUM");
843 *((Factors
**) ((void*) &allFactors
[traceNumA
][traceNumB
] +
844 loopValues
[i
].factorsOffset
))=
845 calculateFactorsExact(cr
, cs
, loopValues
[i
].lineType
);
850 // Calculate approx when possible
851 for (traceNumA
= 0; traceNumA
< syncState
->traceNb
; traceNumA
++)
853 for (traceNumB
= 0; traceNumB
< traceNumA
; traceNumB
++)
855 FactorsCHull
* factorsCHull
;
857 factorsCHull
= &allFactors
[traceNumA
][traceNumB
];
858 if (factorsCHull
->min
== NULL
&& factorsCHull
->max
== NULL
)
860 factorsCHull
->type
= FALLBACK
;
861 calculateFactorsFallback(analysisData
->hullArray
[traceNumB
][traceNumA
],
862 analysisData
->hullArray
[traceNumA
][traceNumB
],
863 &allFactors
[traceNumA
][traceNumB
]);
865 else if (factorsCHull
->min
!= NULL
&& factorsCHull
->max
!= NULL
)
867 if (factorsCHull
->min
->drift
!= -INFINITY
&&
868 factorsCHull
->max
->drift
!= INFINITY
)
870 factorsCHull
->type
= MIDDLE
;
871 calculateFactorsMiddle(factorsCHull
);
873 else if (factorsCHull
->min
->drift
!= -INFINITY
||
874 factorsCHull
->max
->drift
!= INFINITY
)
876 factorsCHull
->type
= INCOMPLETE
;
880 factorsCHull
->type
= ABSENT
;
885 //g_assert_not_reached();
886 factorsCHull
->type
= SCREWED
;
895 /* Calculate approximative factors based on minimum and maximum limits. The
896 * best approximation to make is the interior bissector of the angle formed by
897 * the minimum and maximum lines.
899 * The formulae used come from [Haddad, Yoram: Performance dans les systèmes
900 * répartis: des outils pour les mesures, Université de Paris-Sud, Centre
901 * d'Orsay, September 1988] Section 6.1 p.44
903 * The reasoning for choosing this estimator comes from [Duda, A., Harrus, G.,
904 * Haddad, Y., and Bernard, G.: Estimating global time in distributed systems,
905 * Proc. 7th Int. Conf. on Distributed Computing Systems, Berlin, volume 18,
909 * factors: contains the min and max limits, used to store the result
911 static void calculateFactorsMiddle(FactorsCHull
* factors
)
913 double amin
, amax
, bmin
, bmax
, bhat
;
915 amin
= factors
->max
->offset
;
916 amax
= factors
->min
->offset
;
917 bmin
= factors
->min
->drift
;
918 bmax
= factors
->max
->drift
;
920 g_assert_cmpfloat(bmax
, >, bmin
);
922 factors
->approx
= malloc(sizeof(Factors
));
923 bhat
= (bmax
* bmin
- 1. + sqrt(1. + pow(bmax
, 2.) * pow(bmin
, 2.) +
924 pow(bmax
, 2.) + pow(bmin
, 2.))) / (bmax
+ bmin
);
925 factors
->approx
->offset
= amax
- (amax
- amin
) / 2. * (pow(bhat
, 2.) + 1.)
926 / (1. + bhat
* bmax
);
927 factors
->approx
->drift
= bhat
;
928 factors
->accuracy
= bmax
- bmin
;
933 * Analyze the convex hulls to determine the minimum or maximum
934 * synchronization factors between one pair of trace.
936 * This implements and improves upon the algorithm in [Haddad, Yoram:
937 * Performance dans les systèmes répartis: des outils pour les mesures,
938 * Université de Paris-Sud, Centre d'Orsay, September 1988] Section 6.2 p.47
940 * Some degenerate cases are possible:
941 * 1) the result is unbounded. In that case, when searching for the maximum
942 * factors, result->drift= INFINITY; result->offset= -INFINITY. When
943 * searching for the minimum factors, it is the opposite. It is not
944 * possible to improve the situation with this data.
945 * 2) no line can be above the upper hull and below the lower hull. This is
946 * because the hulls intersect each other or are reversed. This means that
947 * an assertion was false. Most probably, the clocks are not linear. It is
948 * possible to repeat the search with another algorithm that will find a
949 * "best effort" approximation. See calculateFactorsApprox().
952 * cu: the upper half-convex hull, the line must pass above this
953 * and touch it in one point
954 * cl: the lower half-convex hull, the line must pass below this
955 * and touch it in one point
956 * lineType: search for minimum or maximum factors
959 * If a result is found, a struct Factors is allocated, filed with the
960 * result and returned
961 * NULL otherwise, degenerate case 2 is in effect
963 static Factors
* calculateFactorsExact(GQueue
* const cu
, GQueue
* const cl
, const
969 double inversionFactor
;
972 g_debug("calculateFactorsExact(cu= %p, cl= %p, %s)", cu
, cl
, lineType
==
973 MINIMUM
? "MINIMUM" : "MAXIMUM");
975 if (lineType
== MINIMUM
)
979 inversionFactor
= -1.;
991 // Check for degenerate case 1
992 if (c1
->length
== 0 || c2
->length
== 0 || ((Point
*) g_queue_peek_nth(c1
,
993 i1
))->x
>= ((Point
*) g_queue_peek_nth(c2
, i2
))->x
)
995 result
= malloc(sizeof(Factors
));
996 if (lineType
== MINIMUM
)
998 result
->drift
= -INFINITY
;
999 result
->offset
= INFINITY
;
1003 result
->drift
= INFINITY
;
1004 result
->offset
= -INFINITY
;
1016 g_queue_peek_nth(c1
, i1
),
1017 g_queue_peek_nth(c2
, i2
),
1018 g_queue_peek_nth(c1
, i1
),
1019 g_queue_peek_nth(c2
, i2
- 1)) * inversionFactor
< 0.
1022 if (((Point
*) g_queue_peek_nth(c1
, i1
))->x
1023 < ((Point
*) g_queue_peek_nth(c2
, i2
- 1))->x
)
1029 // Degenerate case 2
1035 i1
+ 1 < c1
->length
- 1
1037 g_queue_peek_nth(c1
, i1
),
1038 g_queue_peek_nth(c2
, i2
),
1039 g_queue_peek_nth(c1
, i1
+ 1),
1040 g_queue_peek_nth(c2
, i2
)) * inversionFactor
< 0.
1043 if (((Point
*) g_queue_peek_nth(c1
, i1
+ 1))->x
1044 < ((Point
*) g_queue_peek_nth(c2
, i2
))->x
)
1050 // Degenerate case 2
1058 g_queue_peek_nth(c1
, i1
),
1059 g_queue_peek_nth(c2
, i2
),
1060 g_queue_peek_nth(c1
, i1
),
1061 g_queue_peek_nth(c2
, i2
- 1)) * inversionFactor
< 0.
1064 p1
= g_queue_peek_nth(c1
, i1
);
1065 p2
= g_queue_peek_nth(c2
, i2
);
1067 g_debug("Resulting points are: c1[i1]: x= %llu y= %llu c2[i2]: x= %llu y= %llu",
1068 p1
->x
, p1
->y
, p2
->x
, p2
->y
);
1070 result
= malloc(sizeof(Factors
));
1071 result
->drift
= slope(p1
, p2
);
1072 result
->offset
= intercept(p1
, p2
);
1074 g_debug("Resulting factors are: drift= %g offset= %g", result
->drift
, result
->offset
);
1081 * Analyze the convex hulls to determine approximate synchronization factors
1082 * between one pair of trace when there is no line that can fit in the
1083 * corridor separating them.
1085 * This implements the algorithm in [Ashton, P.: Algorithms for Off-line Clock
1086 * Synchronisation, University of Canterbury, December 1995, 26] Section 4.2.2
1089 * For each point p1 in cr
1090 * For each point p2 in cs
1092 * Calculate the line paramaters
1093 * For each point p3 in each convex hull
1094 * If p3 is on the wrong side of the line
1096 * If error < errorMin
1100 * cr: the upper half-convex hull
1101 * cs: the lower half-convex hull
1102 * result: a pointer to the pre-allocated struct where the results
1105 static void calculateFactorsFallback(GQueue
* const cr
, GQueue
* const cs
,
1106 FactorsCHull
* const result
)
1108 unsigned int i
, j
, k
;
1113 approx
= malloc(sizeof(Factors
));
1115 for (i
= 0; i
< cs
->length
; i
++)
1117 for (j
= 0; j
< cr
->length
; j
++)
1124 if (((Point
*) g_queue_peek_nth(cs
, i
))->x
< ((Point
*)g_queue_peek_nth(cr
, j
))->x
)
1126 p1
= *(Point
*)g_queue_peek_nth(cs
, i
);
1127 p2
= *(Point
*)g_queue_peek_nth(cr
, j
);
1131 p1
= *(Point
*)g_queue_peek_nth(cr
, j
);
1132 p2
= *(Point
*)g_queue_peek_nth(cs
, i
);
1135 // The lower hull should be above the point
1136 for (k
= 0; k
< cs
->length
; k
++)
1138 if (jointCmp(&p1
, &p2
, g_queue_peek_nth(cs
, k
)) < 0.)
1140 error
+= verticalDistance(&p1
, &p2
, g_queue_peek_nth(cs
, k
));
1144 // The upper hull should be below the point
1145 for (k
= 0; k
< cr
->length
; k
++)
1147 if (jointCmp(&p1
, &p2
, g_queue_peek_nth(cr
, k
)) > 0.)
1149 error
+= verticalDistance(&p1
, &p2
, g_queue_peek_nth(cr
, k
));
1153 if (error
< errorMin
)
1155 g_debug("Fallback: i= %u j= %u is a better match (error= %g)", i
, j
, error
);
1156 approx
->drift
= slope(&p1
, &p2
);
1157 approx
->offset
= intercept(&p1
, &p2
);
1163 result
->approx
= approx
;
1164 result
->accuracy
= errorMin
;
1169 * Calculate the vertical distance between a line and a point
1172 * p1, p2: Two points defining the line
1176 * the vertical distance
1178 static double verticalDistance(Point
* p1
, Point
* p2
, Point
* const point
)
1180 return fabs(slope(p1
, p2
) * point
->x
+ intercept(p1
, p2
) - point
->y
);
1185 * Calculate the slope between two points
1188 * p1, p2 the two points
1193 static double slope(const Point
* const p1
, const Point
* const p2
)
1195 return ((double) p2
->y
- p1
->y
) / (p2
->x
- p1
->x
);
1199 /* Calculate the y-intercept of a line that passes by two points
1202 * p1, p2 the two points
1207 static double intercept(const Point
* const p1
, const Point
* const p2
)
1209 return ((double) p2
->y
* p1
->x
- (double) p1
->y
* p2
->x
) / ((double) p1
->x
- p2
->x
);
1214 * Calculate a resulting offset and drift for each trace.
1216 * Traces are assembled in groups. A group is an "island" of nodes/traces that
1217 * exchanged messages. A reference is determined for each group by using a
1218 * shortest path search based on the accuracy of the approximation. This also
1219 * forms a tree of the best way to relate each node's clock to the reference's
1220 * based on the accuracy. Sometimes it may be necessary or advantageous to
1221 * propagate the factors through intermediary clocks. Resulting factors for
1222 * each trace are determined based on this tree.
1224 * This part was not the focus of my research. The algorithm used here is
1225 * inexact in some ways:
1226 * 1) The reference used may not actually be the best one to use. This is
1227 * because the accuracy is not corrected based on the drift during the
1228 * shortest path search.
1229 * 2) The min and max factors are not propagated and are no longer valid.
1230 * 3) Approximations of different types (MIDDLE and FALLBACK) are compared
1231 * together. The "accuracy" parameters of these have different meanings and
1232 * are not readily comparable.
1234 * Nevertheless, the result is satisfactory. You just can't tell "how much" it
1237 * Two alternative (and subtly different) ways of propagating factors to
1238 * preserve min and max bondaries have been proposed, see:
1239 * [Duda, A., Harrus, G., Haddad, Y., and Bernard, G.: Estimating global time
1240 * in distributed systems, Proc. 7th Int. Conf. on Distributed Computing
1241 * Systems, Berlin, volume 18, 1987] p.304
1243 * [Jezequel, J.M., and Jard, C.: Building a global clock for observing
1244 * computations in distributed memory parallel computers, Concurrency:
1245 * Practice and Experience 8(1), volume 8, John Wiley & Sons, Ltd Chichester,
1246 * 1996, 32] Section 5; which is mostly the same as
1247 * [Jezequel, J.M.: Building a global time on parallel machines, Proceedings
1248 * of the 3rd International Workshop on Distributed Algorithms, LNCS, volume
1249 * 392, 136–147, 1989] Section 5
1252 * syncState: container for synchronization data.
1253 * allFactors: offset and drift between each pair of traces
1256 * Factors[traceNb] synchronization factors for each trace
1258 static GArray
* reduceFactors(SyncState
* const syncState
, FactorsCHull
** const
1263 unsigned int** predecessors
;
1264 double* distanceSums
;
1265 unsigned int* references
;
1268 // Solve the all-pairs shortest path problem using the Floyd-Warshall
1270 floydWarshall(syncState
, allFactors
, &distances
, &predecessors
);
1272 /* Find the reference for each node
1274 * First calculate, for each node, the sum of the distances to each other
1275 * node it can reach.
1277 * Then, go through each "island" of traces to find the trace that has the
1278 * lowest distance sum. Assign this trace as the reference to each trace
1281 distanceSums
= malloc(syncState
->traceNb
* sizeof(double));
1282 for (i
= 0; i
< syncState
->traceNb
; i
++)
1284 distanceSums
[i
]= 0.;
1285 for (j
= 0; j
< syncState
->traceNb
; j
++)
1287 distanceSums
[i
]+= distances
[i
][j
];
1291 references
= malloc(syncState
->traceNb
* sizeof(unsigned int));
1292 for (i
= 0; i
< syncState
->traceNb
; i
++)
1294 references
[i
]= UINT_MAX
;
1296 for (i
= 0; i
< syncState
->traceNb
; i
++)
1298 if (references
[i
] == UINT_MAX
)
1300 unsigned int reference
;
1301 double distanceSumMin
;
1303 // A node is its own reference by default
1305 distanceSumMin
= INFINITY
;
1306 for (j
= 0; j
< syncState
->traceNb
; j
++)
1308 if (distances
[i
][j
] != INFINITY
&& distanceSums
[j
] <
1312 distanceSumMin
= distanceSums
[j
];
1315 for (j
= 0; j
< syncState
->traceNb
; j
++)
1317 if (distances
[i
][j
] != INFINITY
)
1319 references
[j
]= reference
;
1325 for (i
= 0; i
< syncState
->traceNb
; i
++)
1332 /* For each trace, calculate the factors based on their corresponding
1333 * tree. The tree is rooted at the reference and the shortest path to each
1334 * other nodes are the branches.
1336 factors
= g_array_sized_new(FALSE
, FALSE
, sizeof(Factors
),
1337 syncState
->traceNb
);
1338 g_array_set_size(factors
, syncState
->traceNb
);
1339 for (i
= 0; i
< syncState
->traceNb
; i
++)
1341 getFactors(allFactors
, predecessors
, references
, i
, &g_array_index(factors
,
1345 for (i
= 0; i
< syncState
->traceNb
; i
++)
1347 free(predecessors
[i
]);
1357 * Perform an all-source shortest path search using the Floyd-Warshall
1360 * The algorithm is implemented accoding to the description here:
1361 * http://web.mit.edu/urban_or_book/www/book/chapter6/6.2.2.html
1364 * syncState: container for synchronization data.
1365 * allFactors: offset and drift between each pair of traces
1366 * distances: resulting matrix of the length of the shortest path between
1367 * two nodes. If there is no path between two nodes, the
1368 * length is INFINITY
1369 * predecessors: resulting matrix of each node's predecessor on the shortest
1370 * path between two nodes
1372 static void floydWarshall(SyncState
* const syncState
, FactorsCHull
** const
1373 allFactors
, double*** const distances
, unsigned int*** const
1376 unsigned int i
, j
, k
;
1378 // Setup initial conditions
1379 *distances
= malloc(syncState
->traceNb
* sizeof(double*));
1380 *predecessors
= malloc(syncState
->traceNb
* sizeof(unsigned int*));
1381 for (i
= 0; i
< syncState
->traceNb
; i
++)
1383 (*distances
)[i
]= malloc(syncState
->traceNb
* sizeof(double));
1384 for (j
= 0; j
< syncState
->traceNb
; j
++)
1388 g_assert(allFactors
[i
][j
].type
== EXACT
);
1390 (*distances
)[i
][j
]= 0.;
1394 unsigned int row
, col
;
1407 if (allFactors
[row
][col
].type
== MIDDLE
||
1408 allFactors
[row
][col
].type
== FALLBACK
)
1410 (*distances
)[i
][j
]= allFactors
[row
][col
].accuracy
;
1412 else if (allFactors
[row
][col
].type
== INCOMPLETE
||
1413 allFactors
[row
][col
].type
== SCREWED
||
1414 allFactors
[row
][col
].type
== ABSENT
)
1416 (*distances
)[i
][j
]= INFINITY
;
1420 g_assert_not_reached();
1425 (*predecessors
)[i
]= malloc(syncState
->traceNb
* sizeof(unsigned int));
1426 for (j
= 0; j
< syncState
->traceNb
; j
++)
1430 (*predecessors
)[i
][j
]= i
;
1434 (*predecessors
)[i
][j
]= UINT_MAX
;
1439 // Run the iterations
1440 for (k
= 0; k
< syncState
->traceNb
; k
++)
1442 for (i
= 0; i
< syncState
->traceNb
; i
++)
1444 for (j
= 0; j
< syncState
->traceNb
; j
++)
1448 distanceMin
= MIN((*distances
)[i
][j
], (*distances
)[i
][k
] +
1449 (*distances
)[k
][j
]);
1451 if (distanceMin
!= (*distances
)[i
][j
])
1453 (*predecessors
)[i
][j
]= (*predecessors
)[k
][j
];
1456 (*distances
)[i
][j
]= distanceMin
;
1464 * Cummulate the time correction factors to convert a node's time to its
1466 * This function recursively calls itself until it reaches the reference node.
1469 * allFactors: offset and drift between each pair of traces
1470 * predecessors: matrix of each node's predecessor on the shortest
1471 * path between two nodes
1472 * references: reference node for each node
1473 * traceNum: node for which to find the factors
1474 * factors: resulting factors
1476 static void getFactors(FactorsCHull
** const allFactors
, unsigned int** const
1477 predecessors
, unsigned int* const references
, const unsigned int traceNum
,
1478 Factors
* const factors
)
1480 unsigned int reference
;
1482 reference
= references
[traceNum
];
1484 if (reference
== traceNum
)
1486 factors
->offset
= 0.;
1491 Factors previousVertexFactors
;
1493 getFactors(allFactors
, predecessors
, references
,
1494 predecessors
[reference
][traceNum
], &previousVertexFactors
);
1496 // convertir de traceNum à reference
1498 // allFactors convertit de col à row
1500 if (reference
> traceNum
)
1502 factors
->offset
= previousVertexFactors
.drift
*
1503 allFactors
[reference
][traceNum
].approx
->offset
+
1504 previousVertexFactors
.offset
;
1505 factors
->drift
= previousVertexFactors
.drift
*
1506 allFactors
[reference
][traceNum
].approx
->drift
;
1510 factors
->offset
= previousVertexFactors
.drift
* (-1. *
1511 allFactors
[traceNum
][reference
].approx
->offset
/
1512 allFactors
[traceNum
][reference
].approx
->drift
) +
1513 previousVertexFactors
.offset
;
1514 factors
->drift
= previousVertexFactors
.drift
* (1. /
1515 allFactors
[traceNum
][reference
].approx
->drift
);
1522 * Write the analysis-specific graph lines in the gnuplot script.
1525 * stream: stream where to write the data
1526 * syncState: container for synchronization data
1527 * i: first trace number
1528 * j: second trace number, garanteed to be larger than i
1530 void writeAnalysisGraphsPlotsCHull(FILE* stream
, SyncState
* const syncState
,
1531 const unsigned int i
, const unsigned int j
)
1533 AnalysisDataCHull
* analysisData
;
1534 FactorsCHull
* factorsCHull
;
1536 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
1539 "\t\"analysis_chull-%1$03d_to_%2$03d.data\" "
1540 "title \"Lower half-hull\" with linespoints "
1541 "linecolor rgb \"#015a01\" linetype 4 pointtype 8 pointsize 0.8, \\\n"
1542 "\t\"analysis_chull-%2$03d_to_%1$03d.data\" "
1543 "title \"Upper half-hull\" with linespoints "
1544 "linecolor rgb \"#003366\" linetype 4 pointtype 10 pointsize 0.8, \\\n",
1547 factorsCHull
= &analysisData
->graphsData
->allFactors
[j
][i
];
1548 if (factorsCHull
->type
== EXACT
)
1552 "title \"Exact conversion\" with lines "
1553 "linecolor rgb \"black\" linetype 1, \\\n",
1554 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
);
1556 else if (factorsCHull
->type
== MIDDLE
)
1559 "\t%.2f + %.10f * x "
1560 "title \"Min conversion\" with lines "
1561 "linecolor rgb \"black\" linetype 5, \\\n",
1562 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
);
1564 "\t%.2f + %.10f * x "
1565 "title \"Max conversion\" with lines "
1566 "linecolor rgb \"black\" linetype 8, \\\n",
1567 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
);
1569 "\t%.2f + %.10f * x "
1570 "title \"Middle conversion\" with lines "
1571 "linecolor rgb \"gray60\" linetype 1, \\\n",
1572 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
);
1574 else if (factorsCHull
->type
== FALLBACK
)
1577 "\t%.2f + %.10f * x "
1578 "title \"Fallback conversion\" with lines "
1579 "linecolor rgb \"gray60\" linetype 1, \\\n",
1580 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
);
1582 else if (factorsCHull
->type
== INCOMPLETE
)
1584 if (factorsCHull
->min
->drift
!= -INFINITY
)
1587 "\t%.2f + %.10f * x "
1588 "title \"Min conversion\" with lines "
1589 "linecolor rgb \"black\" linetype 5, \\\n",
1590 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
);
1593 if (factorsCHull
->max
->drift
!= INFINITY
)
1596 "\t%.2f + %.10f * x "
1597 "title \"Max conversion\" with lines "
1598 "linecolor rgb \"black\" linetype 8, \\\n",
1599 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
);
1602 else if (factorsCHull
->type
== SCREWED
)
1604 if (factorsCHull
->min
!= NULL
&& factorsCHull
->min
->drift
!= -INFINITY
)
1607 "\t%.2f + %.10f * x "
1608 "title \"Min conversion\" with lines "
1609 "linecolor rgb \"black\" linetype 5, \\\n",
1610 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
);
1613 if (factorsCHull
->max
!= NULL
&& factorsCHull
->max
->drift
!= INFINITY
)
1616 "\t%.2f + %.10f * x "
1617 "title \"Max conversion\" with lines "
1618 "linecolor rgb \"black\" linetype 8, \\\n",
1619 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
);
1622 else if (factorsCHull
->type
== ABSENT
)
1627 g_assert_not_reached();