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 .writeTraceTracePlots
= &writeAnalysisGraphsPlotsCHull
,
114 const char* const approxNames
[]= {
117 [FALLBACK
]= "Fallback",
118 [INCOMPLETE
]= "Incomplete",
120 [SCREWED
]= "Screwed",
124 * Analysis module registering function
126 static void registerAnalysisCHull()
128 g_queue_push_tail(&analysisModules
, &analysisModuleCHull
);
133 * Analysis init function
135 * This function is called at the beginning of a synchronization run for a set
138 * Allocate some of the analysis specific data structures
141 * syncState container for synchronization data.
142 * This function allocates or initializes these analysisData
147 static void initAnalysisCHull(SyncState
* const syncState
)
150 AnalysisDataCHull
* analysisData
;
152 analysisData
= malloc(sizeof(AnalysisDataCHull
));
153 syncState
->analysisData
= analysisData
;
155 analysisData
->hullArray
= malloc(syncState
->traceNb
* sizeof(GQueue
**));
156 for (i
= 0; i
< syncState
->traceNb
; i
++)
158 analysisData
->hullArray
[i
]= malloc(syncState
->traceNb
* sizeof(GQueue
*));
160 for (j
= 0; j
< syncState
->traceNb
; j
++)
162 analysisData
->hullArray
[i
][j
]= g_queue_new();
166 if (syncState
->stats
)
168 analysisData
->stats
= malloc(sizeof(AnalysisStatsCHull
));
169 analysisData
->stats
->dropped
= 0;
170 analysisData
->stats
->allFactors
= NULL
;
173 if (syncState
->graphsStream
)
175 analysisData
->graphsData
= malloc(sizeof(AnalysisGraphsDataCHull
));
176 openGraphFiles(syncState
);
177 analysisData
->graphsData
->allFactors
= NULL
;
183 * Create and open files used to store convex hull points to genereate
184 * graphs. Allocate and populate array to store file pointers.
187 * syncState: container for synchronization data
189 static void openGraphFiles(SyncState
* const syncState
)
195 AnalysisDataCHull
* analysisData
;
197 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
199 cwd
= changeToGraphDir(syncState
->graphsDir
);
201 analysisData
->graphsData
->hullPoints
= malloc(syncState
->traceNb
*
203 for (i
= 0; i
< syncState
->traceNb
; i
++)
205 analysisData
->graphsData
->hullPoints
[i
]= malloc(syncState
->traceNb
*
207 for (j
= 0; j
< syncState
->traceNb
; j
++)
211 retval
= snprintf(name
, sizeof(name
),
212 "analysis_chull-%03u_to_%03u.data", j
, i
);
213 if (retval
> sizeof(name
) - 1)
215 name
[sizeof(name
) - 1]= '\0';
217 if ((analysisData
->graphsData
->hullPoints
[i
][j
]= fopen(name
, "w")) ==
220 g_error(strerror(errno
));
229 g_error(strerror(errno
));
236 * Write hull points to files to generate graphs.
239 * syncState: container for synchronization data
241 static void writeGraphFiles(SyncState
* const syncState
)
244 AnalysisDataCHull
* analysisData
;
246 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
248 for (i
= 0; i
< syncState
->traceNb
; i
++)
250 for (j
= 0; j
< syncState
->traceNb
; j
++)
254 g_queue_foreach(analysisData
->hullArray
[i
][j
],
256 analysisData
->graphsData
->hullPoints
[i
][j
]);
264 * A GFunc for g_queue_foreach. Write a hull point to a file used to generate
268 * data: Point*, point to write to the file
269 * userData: FILE*, file pointer where to write the point
271 static void gfDumpHullToFile(gpointer data
, gpointer userData
)
275 point
= (Point
*) data
;
276 fprintf((FILE*) userData
, "%20llu %20llu\n", point
->x
, point
->y
);
281 * Close files used to store convex hull points to generate graphs.
282 * Deallocate array to store file pointers.
285 * syncState: container for synchronization data
287 static void closeGraphFiles(SyncState
* const syncState
)
290 AnalysisDataCHull
* analysisData
;
293 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
295 if (analysisData
->graphsData
->hullPoints
== NULL
)
300 for (i
= 0; i
< syncState
->traceNb
; i
++)
302 for (j
= 0; j
< syncState
->traceNb
; j
++)
306 retval
= fclose(analysisData
->graphsData
->hullPoints
[i
][j
]);
309 g_error(strerror(errno
));
313 free(analysisData
->graphsData
->hullPoints
[i
]);
315 free(analysisData
->graphsData
->hullPoints
);
316 analysisData
->graphsData
->hullPoints
= NULL
;
321 * Analysis destroy function
323 * Free the analysis specific data structures
326 * syncState container for synchronization data.
327 * This function deallocates these analysisData members:
331 static void destroyAnalysisCHull(SyncState
* const syncState
)
334 AnalysisDataCHull
* analysisData
;
336 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
338 if (analysisData
== NULL
)
343 for (i
= 0; i
< syncState
->traceNb
; i
++)
345 for (j
= 0; j
< syncState
->traceNb
; j
++)
347 g_queue_foreach(analysisData
->hullArray
[i
][j
], gfPointDestroy
, NULL
);
349 free(analysisData
->hullArray
[i
]);
351 free(analysisData
->hullArray
);
353 if (syncState
->stats
)
355 if (analysisData
->stats
->allFactors
!= NULL
)
357 freeAllFactors(syncState
->traceNb
, analysisData
->stats
->allFactors
);
360 free(analysisData
->stats
);
363 if (syncState
->graphsStream
)
365 if (analysisData
->graphsData
->hullPoints
!= NULL
)
367 closeGraphFiles(syncState
);
370 if (!syncState
->stats
&& analysisData
->graphsData
->allFactors
!= NULL
)
372 freeAllFactors(syncState
->traceNb
, analysisData
->graphsData
->allFactors
);
375 free(analysisData
->graphsData
);
378 free(syncState
->analysisData
);
379 syncState
->analysisData
= NULL
;
384 * Perform analysis on an event pair.
387 * syncState container for synchronization data
388 * message structure containing the events
390 static void analyzeMessageCHull(SyncState
* const syncState
, Message
* const message
)
392 AnalysisDataCHull
* analysisData
;
397 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
399 newPoint
= malloc(sizeof(Point
));
400 if (message
->inE
->traceNum
< message
->outE
->traceNum
)
402 // CA is inE->traceNum
403 newPoint
->x
= message
->inE
->cpuTime
;
404 newPoint
->y
= message
->outE
->cpuTime
;
406 g_debug("Reception point hullArray[%lu][%lu] x= inE->time= %llu y= outE->time= %llu",
407 message
->inE
->traceNum
, message
->outE
->traceNum
, newPoint
->x
,
412 // CA is outE->traceNum
413 newPoint
->x
= message
->outE
->cpuTime
;
414 newPoint
->y
= message
->inE
->cpuTime
;
416 g_debug("Send point hullArray[%lu][%lu] x= inE->time= %llu y= outE->time= %llu",
417 message
->inE
->traceNum
, message
->outE
->traceNum
, newPoint
->x
,
422 analysisData
->hullArray
[message
->inE
->traceNum
][message
->outE
->traceNum
];
424 if (hull
->length
>= 1 && newPoint
->x
< ((Point
*)
425 g_queue_peek_tail(hull
))->x
)
427 if (syncState
->stats
)
429 analysisData
->stats
->dropped
++;
436 grahamScan(hull
, newPoint
, hullType
);
442 * Construct one half of a convex hull from abscissa-sorted points
445 * hull: the points already in the hull
446 * newPoint: a new point to consider
447 * type: which half of the hull to construct
449 static void grahamScan(GQueue
* const hull
, Point
* const newPoint
, const
454 g_debug("grahamScan(hull (length: %u), newPoint, %s)", hull
->length
, type
455 == LOWER
? "LOWER" : "UPPER");
466 if (hull
->length
>= 2)
468 g_debug("jointCmp(hull[%u], hull[%u], newPoint) * inversionFactor = %d * %d = %d",
471 jointCmp(g_queue_peek_nth(hull
, hull
->length
- 2),
472 g_queue_peek_tail(hull
), newPoint
),
474 jointCmp(g_queue_peek_nth(hull
, hull
->length
- 2),
475 g_queue_peek_tail(hull
), newPoint
) * inversionFactor
);
477 while (hull
->length
>= 2 && jointCmp(g_queue_peek_nth(hull
, hull
->length
-
478 2), g_queue_peek_tail(hull
), newPoint
) * inversionFactor
<= 0)
480 g_debug("Removing hull[%u]", hull
->length
);
481 free((Point
*) g_queue_pop_tail(hull
));
483 if (hull
->length
>= 2)
485 g_debug("jointCmp(hull[%u], hull[%u], newPoint) * inversionFactor = %d * %d = %d",
488 jointCmp(g_queue_peek_nth(hull
, hull
->length
- 2),
489 g_queue_peek_tail(hull
), newPoint
),
491 jointCmp(g_queue_peek_nth(hull
, hull
->length
- 2),
492 g_queue_peek_tail(hull
), newPoint
) * inversionFactor
);
495 g_queue_push_tail(hull
, newPoint
);
500 * Finalize the factor calculations
503 * syncState container for synchronization data.
506 * Factors[traceNb] synchronization factors for each trace
508 static GArray
* finalizeAnalysisCHull(SyncState
* const syncState
)
510 AnalysisDataCHull
* analysisData
;
512 FactorsCHull
** allFactors
;
514 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
516 if (syncState
->graphsStream
&& analysisData
->graphsData
->hullPoints
!= NULL
)
518 writeGraphFiles(syncState
);
519 closeGraphFiles(syncState
);
522 allFactors
= calculateAllFactors(syncState
);
524 factors
= reduceFactors(syncState
, allFactors
);
526 if (syncState
->stats
|| syncState
->graphsStream
)
528 if (syncState
->stats
)
530 analysisData
->stats
->allFactors
= allFactors
;
533 if (syncState
->graphsStream
)
535 analysisData
->graphsData
->allFactors
= allFactors
;
540 freeAllFactors(syncState
->traceNb
, allFactors
);
548 * Print statistics related to analysis. Must be called after
552 * syncState container for synchronization data.
554 static void printAnalysisStatsCHull(SyncState
* const syncState
)
556 AnalysisDataCHull
* analysisData
;
559 if (!syncState
->stats
)
564 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
566 printf("Convex hull analysis stats:\n");
567 printf("\tout of order packets dropped from analysis: %u\n",
568 analysisData
->stats
->dropped
);
570 printf("\tNumber of points in convex hulls:\n");
572 for (i
= 0; i
< syncState
->traceNb
; i
++)
574 for (j
= i
+ 1; j
< syncState
->traceNb
; j
++)
576 printf("\t\t%3d - %-3d: lower half-hull %-5u upper half-hull %-5u\n",
577 i
, j
, analysisData
->hullArray
[j
][i
]->length
,
578 analysisData
->hullArray
[i
][j
]->length
);
582 printf("\tIndividual synchronization factors:\n");
584 for (i
= 0; i
< syncState
->traceNb
; i
++)
586 for (j
= i
+ 1; j
< syncState
->traceNb
; j
++)
588 FactorsCHull
* factorsCHull
;
590 factorsCHull
= &analysisData
->stats
->allFactors
[j
][i
];
591 printf("\t\t%3d - %-3d: ", i
, j
);
593 if (factorsCHull
->type
== EXACT
)
595 printf("Exact a0= % 7g a1= 1 %c %7g\n",
596 factorsCHull
->approx
->offset
,
597 factorsCHull
->approx
->drift
< 0. ? '-' : '+',
598 fabs(factorsCHull
->approx
->drift
));
600 else if (factorsCHull
->type
== MIDDLE
)
602 printf("Middle a0= % 7g a1= 1 %c %7g accuracy %7g\n",
603 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
604 - 1. < 0. ? '-' : '+', fabs(factorsCHull
->approx
->drift
-
605 1.), factorsCHull
->accuracy
);
606 printf("\t\t a0: % 7g to % 7g (delta= %7g)\n",
607 factorsCHull
->max
->offset
, factorsCHull
->min
->offset
,
608 factorsCHull
->min
->offset
- factorsCHull
->max
->offset
);
609 printf("\t\t a1: 1 %+7g to %+7g (delta= %7g)\n",
610 factorsCHull
->min
->drift
- 1., factorsCHull
->max
->drift
-
611 1., factorsCHull
->max
->drift
- factorsCHull
->min
->drift
);
613 else if (factorsCHull
->type
== FALLBACK
)
615 printf("Fallback a0= % 7g a1= 1 %c %7g error= %7g\n",
616 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
617 - 1. < 0. ? '-' : '+', fabs(factorsCHull
->approx
->drift
-
618 1.), factorsCHull
->accuracy
);
620 else if (factorsCHull
->type
== INCOMPLETE
)
622 printf("Incomplete\n");
624 if (factorsCHull
->min
->drift
!= -INFINITY
)
626 printf("\t\t min: a0: % 7g a1: 1 %c %7g\n",
627 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
-
628 1. < 0 ? '-' : '+', fabs(factorsCHull
->min
->drift
-
631 if (factorsCHull
->max
->drift
!= INFINITY
)
633 printf("\t\t max: a0: % 7g a1: 1 %c %7g\n",
634 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
-
635 1. < 0 ? '-' : '+', fabs(factorsCHull
->max
->drift
-
639 else if (factorsCHull
->type
== SCREWED
)
643 if (factorsCHull
->min
!= NULL
&& factorsCHull
->min
->drift
!= -INFINITY
)
645 printf("\t\t min: a0: % 7g a1: 1 %c %7g\n",
646 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
-
647 1. < 0 ? '-' : '+', fabs(factorsCHull
->min
->drift
-
650 if (factorsCHull
->max
!= NULL
&& factorsCHull
->max
->drift
!= INFINITY
)
652 printf("\t\t max: a0: % 7g a1: 1 %c %7g\n",
653 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
-
654 1. < 0 ? '-' : '+', fabs(factorsCHull
->max
->drift
-
658 else if (factorsCHull
->type
== ABSENT
)
664 g_assert_not_reached();
672 * A GFunc for g_queue_foreach()
675 * data Point*, point to destroy
678 static void gfPointDestroy(gpointer data
, gpointer userData
)
682 point
= (Point
*) data
;
688 * Find out if a sequence of three points constitutes a "left turn" or a
692 * p1, p2, p3: The three points.
696 * 0 colinear (unlikely result since this uses floating point
700 static int jointCmp(const Point
const* p1
, const Point
const* p2
, const
704 const double fuzzFactor
= 0.;
706 result
= crossProductK(p1
, p2
, p1
, p3
);
707 g_debug("crossProductK(p1= (%llu, %llu), p2= (%llu, %llu), p1= (%llu, %llu), p3= (%llu, %llu))= %g",
708 p1
->x
, p1
->y
, p2
->x
, p2
->y
, p1
->x
, p1
->y
, p3
->x
, p3
->y
, result
);
709 if (result
< fuzzFactor
)
713 else if (result
> fuzzFactor
)
725 * Calculate the k component of the cross product of two vectors.
728 * p1, p2: start and end points of the first vector
729 * p3, p4: start and end points of the second vector
732 * the k component of the cross product when considering the two vectors to
733 * be in the i-j plane. The direction (sign) of the result can be useful to
734 * determine the relative orientation of the two vectors.
736 static double crossProductK(const Point
const* p1
, const Point
const* p2
,
737 const Point
const* p3
, const Point
const* p4
)
739 return ((double) p2
->x
- p1
->x
) * ((double) p4
->y
- p3
->y
) - ((double)
740 p2
->y
- p1
->y
) * ((double) p4
->x
- p3
->x
);
745 * Free a container of FactorsCHull
748 * traceNb: number of traces
749 * allFactors: container of FactorsCHull
751 void freeAllFactors(const unsigned int traceNb
, FactorsCHull
** const
756 for (i
= 0; i
< traceNb
; i
++)
758 for (j
= 0; j
<= i
; j
++)
760 destroyFactorsCHull(&allFactors
[i
][j
]);
769 * Free a FactorsCHull
772 * factorsCHull: container of Factors
774 void destroyFactorsCHull(FactorsCHull
* factorsCHull
)
776 if (factorsCHull
->type
== MIDDLE
|| factorsCHull
->type
==
777 INCOMPLETE
|| factorsCHull
->type
== ABSENT
)
779 free(factorsCHull
->min
);
780 free(factorsCHull
->max
);
782 else if (factorsCHull
->type
== SCREWED
)
784 if (factorsCHull
->min
!= NULL
)
786 free(factorsCHull
->min
);
788 if (factorsCHull
->max
!= NULL
)
790 free(factorsCHull
->max
);
794 if (factorsCHull
->type
== EXACT
|| factorsCHull
->type
== MIDDLE
||
795 factorsCHull
->type
== FALLBACK
)
797 free(factorsCHull
->approx
);
803 * Analyze the convex hulls to determine the synchronization factors between
804 * each pair of trace.
807 * syncState container for synchronization data.
810 * FactorsCHull*[TraceNum][TraceNum] array. See the documentation for the
811 * member allFactors of AnalysisStatsCHull.
813 FactorsCHull
** calculateAllFactors(SyncState
* const syncState
)
815 unsigned int traceNumA
, traceNumB
;
816 FactorsCHull
** allFactors
;
817 AnalysisDataCHull
* analysisData
;
819 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
821 // Allocate allFactors and calculate min and max
822 allFactors
= malloc(syncState
->traceNb
* sizeof(FactorsCHull
*));
823 for (traceNumA
= 0; traceNumA
< syncState
->traceNb
; traceNumA
++)
825 allFactors
[traceNumA
]= malloc((traceNumA
+ 1) * sizeof(FactorsCHull
));
827 allFactors
[traceNumA
][traceNumA
].type
= EXACT
;
828 allFactors
[traceNumA
][traceNumA
].approx
= malloc(sizeof(Factors
));
829 allFactors
[traceNumA
][traceNumA
].approx
->drift
= 1.;
830 allFactors
[traceNumA
][traceNumA
].approx
->offset
= 0.;
832 for (traceNumB
= 0; traceNumB
< traceNumA
; traceNumB
++)
839 size_t factorsOffset
;
841 {MINIMUM
, offsetof(FactorsCHull
, min
)},
842 {MAXIMUM
, offsetof(FactorsCHull
, max
)}
845 cr
= analysisData
->hullArray
[traceNumB
][traceNumA
];
846 cs
= analysisData
->hullArray
[traceNumA
][traceNumB
];
848 for (i
= 0; i
< sizeof(loopValues
) / sizeof(*loopValues
); i
++)
850 g_debug("allFactors[%u][%u].%s = calculateFactorsExact(cr= hullArray[%u][%u], cs= hullArray[%u][%u], %s)",
851 traceNumA
, traceNumB
, loopValues
[i
].factorsOffset
==
852 offsetof(FactorsCHull
, min
) ? "min" : "max", traceNumB
,
853 traceNumA
, traceNumA
, traceNumB
, loopValues
[i
].lineType
==
854 MINIMUM
? "MINIMUM" : "MAXIMUM");
855 *((Factors
**) ((void*) &allFactors
[traceNumA
][traceNumB
] +
856 loopValues
[i
].factorsOffset
))=
857 calculateFactorsExact(cr
, cs
, loopValues
[i
].lineType
);
862 // Calculate approx when possible
863 for (traceNumA
= 0; traceNumA
< syncState
->traceNb
; traceNumA
++)
865 for (traceNumB
= 0; traceNumB
< traceNumA
; traceNumB
++)
867 FactorsCHull
* factorsCHull
;
869 factorsCHull
= &allFactors
[traceNumA
][traceNumB
];
870 if (factorsCHull
->min
== NULL
&& factorsCHull
->max
== NULL
)
872 factorsCHull
->type
= FALLBACK
;
873 calculateFactorsFallback(analysisData
->hullArray
[traceNumB
][traceNumA
],
874 analysisData
->hullArray
[traceNumA
][traceNumB
],
875 &allFactors
[traceNumA
][traceNumB
]);
877 else if (factorsCHull
->min
!= NULL
&& factorsCHull
->max
!= NULL
)
879 if (factorsCHull
->min
->drift
!= -INFINITY
&&
880 factorsCHull
->max
->drift
!= INFINITY
)
882 factorsCHull
->type
= MIDDLE
;
883 calculateFactorsMiddle(factorsCHull
);
885 else if (factorsCHull
->min
->drift
!= -INFINITY
||
886 factorsCHull
->max
->drift
!= INFINITY
)
888 factorsCHull
->type
= INCOMPLETE
;
892 factorsCHull
->type
= ABSENT
;
897 //g_assert_not_reached();
898 factorsCHull
->type
= SCREWED
;
907 /* Calculate approximative factors based on minimum and maximum limits. The
908 * best approximation to make is the interior bissector of the angle formed by
909 * the minimum and maximum lines.
911 * The formulae used come from [Haddad, Yoram: Performance dans les systèmes
912 * répartis: des outils pour les mesures, Université de Paris-Sud, Centre
913 * d'Orsay, September 1988] Section 6.1 p.44
915 * The reasoning for choosing this estimator comes from [Duda, A., Harrus, G.,
916 * Haddad, Y., and Bernard, G.: Estimating global time in distributed systems,
917 * Proc. 7th Int. Conf. on Distributed Computing Systems, Berlin, volume 18,
921 * factors: contains the min and max limits, used to store the result
923 void calculateFactorsMiddle(FactorsCHull
* const factors
)
925 double amin
, amax
, bmin
, bmax
, bhat
;
927 amin
= factors
->max
->offset
;
928 amax
= factors
->min
->offset
;
929 bmin
= factors
->min
->drift
;
930 bmax
= factors
->max
->drift
;
932 g_assert_cmpfloat(bmax
, >, bmin
);
934 factors
->approx
= malloc(sizeof(Factors
));
935 bhat
= (bmax
* bmin
- 1. + sqrt(1. + pow(bmax
, 2.) * pow(bmin
, 2.) +
936 pow(bmax
, 2.) + pow(bmin
, 2.))) / (bmax
+ bmin
);
937 factors
->approx
->offset
= amax
- (amax
- amin
) / 2. * (pow(bhat
, 2.) + 1.)
938 / (1. + bhat
* bmax
);
939 factors
->approx
->drift
= bhat
;
940 factors
->accuracy
= bmax
- bmin
;
945 * Analyze the convex hulls to determine the minimum or maximum
946 * synchronization factors between one pair of trace.
948 * This implements and improves upon the algorithm in [Haddad, Yoram:
949 * Performance dans les systèmes répartis: des outils pour les mesures,
950 * Université de Paris-Sud, Centre d'Orsay, September 1988] Section 6.2 p.47
952 * Some degenerate cases are possible:
953 * 1) the result is unbounded. In that case, when searching for the maximum
954 * factors, result->drift= INFINITY; result->offset= -INFINITY. When
955 * searching for the minimum factors, it is the opposite. It is not
956 * possible to improve the situation with this data.
957 * 2) no line can be above the upper hull and below the lower hull. This is
958 * because the hulls intersect each other or are reversed. This means that
959 * an assertion was false. Most probably, the clocks are not linear. It is
960 * possible to repeat the search with another algorithm that will find a
961 * "best effort" approximation. See calculateFactorsApprox().
964 * cu: the upper half-convex hull, the line must pass above this
965 * and touch it in one point
966 * cl: the lower half-convex hull, the line must pass below this
967 * and touch it in one point
968 * lineType: search for minimum or maximum factors
971 * If a result is found, a struct Factors is allocated, filed with the
972 * result and returned
973 * NULL otherwise, degenerate case 2 is in effect
975 static Factors
* calculateFactorsExact(GQueue
* const cu
, GQueue
* const cl
, const
981 double inversionFactor
;
984 g_debug("calculateFactorsExact(cu= %p, cl= %p, %s)", cu
, cl
, lineType
==
985 MINIMUM
? "MINIMUM" : "MAXIMUM");
987 if (lineType
== MINIMUM
)
991 inversionFactor
= -1.;
1003 // Check for degenerate case 1
1004 if (c1
->length
== 0 || c2
->length
== 0 || ((Point
*) g_queue_peek_nth(c1
,
1005 i1
))->x
>= ((Point
*) g_queue_peek_nth(c2
, i2
))->x
)
1007 result
= malloc(sizeof(Factors
));
1008 if (lineType
== MINIMUM
)
1010 result
->drift
= -INFINITY
;
1011 result
->offset
= INFINITY
;
1015 result
->drift
= INFINITY
;
1016 result
->offset
= -INFINITY
;
1028 g_queue_peek_nth(c1
, i1
),
1029 g_queue_peek_nth(c2
, i2
),
1030 g_queue_peek_nth(c1
, i1
),
1031 g_queue_peek_nth(c2
, i2
- 1)) * inversionFactor
< 0.
1034 if (((Point
*) g_queue_peek_nth(c1
, i1
))->x
1035 < ((Point
*) g_queue_peek_nth(c2
, i2
- 1))->x
)
1041 // Degenerate case 2
1047 i1
+ 1 < c1
->length
- 1
1049 g_queue_peek_nth(c1
, i1
),
1050 g_queue_peek_nth(c2
, i2
),
1051 g_queue_peek_nth(c1
, i1
+ 1),
1052 g_queue_peek_nth(c2
, i2
)) * inversionFactor
< 0.
1055 if (((Point
*) g_queue_peek_nth(c1
, i1
+ 1))->x
1056 < ((Point
*) g_queue_peek_nth(c2
, i2
))->x
)
1062 // Degenerate case 2
1070 g_queue_peek_nth(c1
, i1
),
1071 g_queue_peek_nth(c2
, i2
),
1072 g_queue_peek_nth(c1
, i1
),
1073 g_queue_peek_nth(c2
, i2
- 1)) * inversionFactor
< 0.
1076 p1
= g_queue_peek_nth(c1
, i1
);
1077 p2
= g_queue_peek_nth(c2
, i2
);
1079 g_debug("Resulting points are: c1[i1]: x= %llu y= %llu c2[i2]: x= %llu y= %llu",
1080 p1
->x
, p1
->y
, p2
->x
, p2
->y
);
1082 result
= malloc(sizeof(Factors
));
1083 result
->drift
= slope(p1
, p2
);
1084 result
->offset
= intercept(p1
, p2
);
1086 g_debug("Resulting factors are: drift= %g offset= %g", result
->drift
, result
->offset
);
1093 * Analyze the convex hulls to determine approximate synchronization factors
1094 * between one pair of trace when there is no line that can fit in the
1095 * corridor separating them.
1097 * This implements the algorithm in [Ashton, P.: Algorithms for Off-line Clock
1098 * Synchronisation, University of Canterbury, December 1995, 26] Section 4.2.2
1101 * For each point p1 in cr
1102 * For each point p2 in cs
1104 * Calculate the line paramaters
1105 * For each point p3 in each convex hull
1106 * If p3 is on the wrong side of the line
1108 * If error < errorMin
1112 * cr: the upper half-convex hull
1113 * cs: the lower half-convex hull
1114 * result: a pointer to the pre-allocated struct where the results
1117 static void calculateFactorsFallback(GQueue
* const cr
, GQueue
* const cs
,
1118 FactorsCHull
* const result
)
1120 unsigned int i
, j
, k
;
1125 approx
= malloc(sizeof(Factors
));
1127 for (i
= 0; i
< cs
->length
; i
++)
1129 for (j
= 0; j
< cr
->length
; j
++)
1136 if (((Point
*) g_queue_peek_nth(cs
, i
))->x
< ((Point
*)g_queue_peek_nth(cr
, j
))->x
)
1138 p1
= *(Point
*)g_queue_peek_nth(cs
, i
);
1139 p2
= *(Point
*)g_queue_peek_nth(cr
, j
);
1143 p1
= *(Point
*)g_queue_peek_nth(cr
, j
);
1144 p2
= *(Point
*)g_queue_peek_nth(cs
, i
);
1147 // The lower hull should be above the point
1148 for (k
= 0; k
< cs
->length
; k
++)
1150 if (jointCmp(&p1
, &p2
, g_queue_peek_nth(cs
, k
)) < 0.)
1152 error
+= verticalDistance(&p1
, &p2
, g_queue_peek_nth(cs
, k
));
1156 // The upper hull should be below the point
1157 for (k
= 0; k
< cr
->length
; k
++)
1159 if (jointCmp(&p1
, &p2
, g_queue_peek_nth(cr
, k
)) > 0.)
1161 error
+= verticalDistance(&p1
, &p2
, g_queue_peek_nth(cr
, k
));
1165 if (error
< errorMin
)
1167 g_debug("Fallback: i= %u j= %u is a better match (error= %g)", i
, j
, error
);
1168 approx
->drift
= slope(&p1
, &p2
);
1169 approx
->offset
= intercept(&p1
, &p2
);
1175 result
->approx
= approx
;
1176 result
->accuracy
= errorMin
;
1181 * Calculate the vertical distance between a line and a point
1184 * p1, p2: Two points defining the line
1188 * the vertical distance
1190 static double verticalDistance(Point
* p1
, Point
* p2
, Point
* const point
)
1192 return fabs(slope(p1
, p2
) * point
->x
+ intercept(p1
, p2
) - point
->y
);
1197 * Calculate the slope between two points
1200 * p1, p2 the two points
1205 static double slope(const Point
* const p1
, const Point
* const p2
)
1207 return ((double) p2
->y
- p1
->y
) / (p2
->x
- p1
->x
);
1211 /* Calculate the y-intercept of a line that passes by two points
1214 * p1, p2 the two points
1219 static double intercept(const Point
* const p1
, const Point
* const p2
)
1221 return ((double) p2
->y
* p1
->x
- (double) p1
->y
* p2
->x
) / ((double) p1
->x
- p2
->x
);
1226 * Calculate a resulting offset and drift for each trace.
1228 * Traces are assembled in groups. A group is an "island" of nodes/traces that
1229 * exchanged messages. A reference is determined for each group by using a
1230 * shortest path search based on the accuracy of the approximation. This also
1231 * forms a tree of the best way to relate each node's clock to the reference's
1232 * based on the accuracy. Sometimes it may be necessary or advantageous to
1233 * propagate the factors through intermediary clocks. Resulting factors for
1234 * each trace are determined based on this tree.
1236 * This part was not the focus of my research. The algorithm used here is
1237 * inexact in some ways:
1238 * 1) The reference used may not actually be the best one to use. This is
1239 * because the accuracy is not corrected based on the drift during the
1240 * shortest path search.
1241 * 2) The min and max factors are not propagated and are no longer valid.
1242 * 3) Approximations of different types (MIDDLE and FALLBACK) are compared
1243 * together. The "accuracy" parameters of these have different meanings and
1244 * are not readily comparable.
1246 * Nevertheless, the result is satisfactory. You just can't tell "how much" it
1249 * Two alternative (and subtly different) ways of propagating factors to
1250 * preserve min and max bondaries have been proposed, see:
1251 * [Duda, A., Harrus, G., Haddad, Y., and Bernard, G.: Estimating global time
1252 * in distributed systems, Proc. 7th Int. Conf. on Distributed Computing
1253 * Systems, Berlin, volume 18, 1987] p.304
1255 * [Jezequel, J.M., and Jard, C.: Building a global clock for observing
1256 * computations in distributed memory parallel computers, Concurrency:
1257 * Practice and Experience 8(1), volume 8, John Wiley & Sons, Ltd Chichester,
1258 * 1996, 32] Section 5; which is mostly the same as
1259 * [Jezequel, J.M.: Building a global time on parallel machines, Proceedings
1260 * of the 3rd International Workshop on Distributed Algorithms, LNCS, volume
1261 * 392, 136–147, 1989] Section 5
1264 * syncState: container for synchronization data.
1265 * allFactors: offset and drift between each pair of traces
1268 * Factors[traceNb] synchronization factors for each trace
1270 static GArray
* reduceFactors(SyncState
* const syncState
, FactorsCHull
** const
1275 unsigned int** predecessors
;
1276 double* distanceSums
;
1277 unsigned int* references
;
1280 // Solve the all-pairs shortest path problem using the Floyd-Warshall
1282 floydWarshall(syncState
, allFactors
, &distances
, &predecessors
);
1284 /* Find the reference for each node
1286 * First calculate, for each node, the sum of the distances to each other
1287 * node it can reach.
1289 * Then, go through each "island" of traces to find the trace that has the
1290 * lowest distance sum. Assign this trace as the reference to each trace
1293 distanceSums
= malloc(syncState
->traceNb
* sizeof(double));
1294 for (i
= 0; i
< syncState
->traceNb
; i
++)
1296 distanceSums
[i
]= 0.;
1297 for (j
= 0; j
< syncState
->traceNb
; j
++)
1299 distanceSums
[i
]+= distances
[i
][j
];
1303 references
= malloc(syncState
->traceNb
* sizeof(unsigned int));
1304 for (i
= 0; i
< syncState
->traceNb
; i
++)
1306 references
[i
]= UINT_MAX
;
1308 for (i
= 0; i
< syncState
->traceNb
; i
++)
1310 if (references
[i
] == UINT_MAX
)
1312 unsigned int reference
;
1313 double distanceSumMin
;
1315 // A node is its own reference by default
1317 distanceSumMin
= INFINITY
;
1318 for (j
= 0; j
< syncState
->traceNb
; j
++)
1320 if (distances
[i
][j
] != INFINITY
&& distanceSums
[j
] <
1324 distanceSumMin
= distanceSums
[j
];
1327 for (j
= 0; j
< syncState
->traceNb
; j
++)
1329 if (distances
[i
][j
] != INFINITY
)
1331 references
[j
]= reference
;
1337 for (i
= 0; i
< syncState
->traceNb
; i
++)
1344 /* For each trace, calculate the factors based on their corresponding
1345 * tree. The tree is rooted at the reference and the shortest path to each
1346 * other nodes are the branches.
1348 factors
= g_array_sized_new(FALSE
, FALSE
, sizeof(Factors
),
1349 syncState
->traceNb
);
1350 g_array_set_size(factors
, syncState
->traceNb
);
1351 for (i
= 0; i
< syncState
->traceNb
; i
++)
1353 getFactors(allFactors
, predecessors
, references
, i
, &g_array_index(factors
,
1357 for (i
= 0; i
< syncState
->traceNb
; i
++)
1359 free(predecessors
[i
]);
1369 * Perform an all-source shortest path search using the Floyd-Warshall
1372 * The algorithm is implemented accoding to the description here:
1373 * http://web.mit.edu/urban_or_book/www/book/chapter6/6.2.2.html
1376 * syncState: container for synchronization data.
1377 * allFactors: offset and drift between each pair of traces
1378 * distances: resulting matrix of the length of the shortest path between
1379 * two nodes. If there is no path between two nodes, the
1380 * length is INFINITY
1381 * predecessors: resulting matrix of each node's predecessor on the shortest
1382 * path between two nodes
1384 static void floydWarshall(SyncState
* const syncState
, FactorsCHull
** const
1385 allFactors
, double*** const distances
, unsigned int*** const
1388 unsigned int i
, j
, k
;
1390 // Setup initial conditions
1391 *distances
= malloc(syncState
->traceNb
* sizeof(double*));
1392 *predecessors
= malloc(syncState
->traceNb
* sizeof(unsigned int*));
1393 for (i
= 0; i
< syncState
->traceNb
; i
++)
1395 (*distances
)[i
]= malloc(syncState
->traceNb
* sizeof(double));
1396 for (j
= 0; j
< syncState
->traceNb
; j
++)
1400 g_assert(allFactors
[i
][j
].type
== EXACT
);
1402 (*distances
)[i
][j
]= 0.;
1406 unsigned int row
, col
;
1419 if (allFactors
[row
][col
].type
== MIDDLE
||
1420 allFactors
[row
][col
].type
== FALLBACK
)
1422 (*distances
)[i
][j
]= allFactors
[row
][col
].accuracy
;
1424 else if (allFactors
[row
][col
].type
== INCOMPLETE
||
1425 allFactors
[row
][col
].type
== SCREWED
||
1426 allFactors
[row
][col
].type
== ABSENT
)
1428 (*distances
)[i
][j
]= INFINITY
;
1432 g_assert_not_reached();
1437 (*predecessors
)[i
]= malloc(syncState
->traceNb
* sizeof(unsigned int));
1438 for (j
= 0; j
< syncState
->traceNb
; j
++)
1442 (*predecessors
)[i
][j
]= i
;
1446 (*predecessors
)[i
][j
]= UINT_MAX
;
1451 // Run the iterations
1452 for (k
= 0; k
< syncState
->traceNb
; k
++)
1454 for (i
= 0; i
< syncState
->traceNb
; i
++)
1456 for (j
= 0; j
< syncState
->traceNb
; j
++)
1460 distanceMin
= MIN((*distances
)[i
][j
], (*distances
)[i
][k
] +
1461 (*distances
)[k
][j
]);
1463 if (distanceMin
!= (*distances
)[i
][j
])
1465 (*predecessors
)[i
][j
]= (*predecessors
)[k
][j
];
1468 (*distances
)[i
][j
]= distanceMin
;
1476 * Cummulate the time correction factors to convert a node's time to its
1478 * This function recursively calls itself until it reaches the reference node.
1481 * allFactors: offset and drift between each pair of traces
1482 * predecessors: matrix of each node's predecessor on the shortest
1483 * path between two nodes
1484 * references: reference node for each node
1485 * traceNum: node for which to find the factors
1486 * factors: resulting factors
1488 static void getFactors(FactorsCHull
** const allFactors
, unsigned int** const
1489 predecessors
, unsigned int* const references
, const unsigned int traceNum
,
1490 Factors
* const factors
)
1492 unsigned int reference
;
1494 reference
= references
[traceNum
];
1496 if (reference
== traceNum
)
1498 factors
->offset
= 0.;
1503 Factors previousVertexFactors
;
1505 getFactors(allFactors
, predecessors
, references
,
1506 predecessors
[reference
][traceNum
], &previousVertexFactors
);
1508 // convertir de traceNum à reference
1510 // allFactors convertit de col à row
1512 if (reference
> traceNum
)
1514 factors
->offset
= previousVertexFactors
.drift
*
1515 allFactors
[reference
][traceNum
].approx
->offset
+
1516 previousVertexFactors
.offset
;
1517 factors
->drift
= previousVertexFactors
.drift
*
1518 allFactors
[reference
][traceNum
].approx
->drift
;
1522 factors
->offset
= previousVertexFactors
.drift
* (-1. *
1523 allFactors
[traceNum
][reference
].approx
->offset
/
1524 allFactors
[traceNum
][reference
].approx
->drift
) +
1525 previousVertexFactors
.offset
;
1526 factors
->drift
= previousVertexFactors
.drift
* (1. /
1527 allFactors
[traceNum
][reference
].approx
->drift
);
1534 * Write the analysis-specific graph lines in the gnuplot script.
1537 * syncState: container for synchronization data
1538 * i: first trace number
1539 * j: second trace number, garanteed to be larger than i
1541 void writeAnalysisGraphsPlotsCHull(SyncState
* const syncState
, const unsigned
1542 int i
, const unsigned int j
)
1544 AnalysisDataCHull
* analysisData
;
1545 FactorsCHull
* factorsCHull
;
1547 analysisData
= (AnalysisDataCHull
*) syncState
->analysisData
;
1549 fprintf(syncState
->graphsStream
,
1550 "\t\"analysis_chull-%1$03d_to_%2$03d.data\" "
1551 "title \"Lower half-hull\" with linespoints "
1552 "linecolor rgb \"#015a01\" linetype 4 pointtype 8 pointsize 0.8, \\\n"
1553 "\t\"analysis_chull-%2$03d_to_%1$03d.data\" "
1554 "title \"Upper half-hull\" with linespoints "
1555 "linecolor rgb \"#003366\" linetype 4 pointtype 10 pointsize 0.8, \\\n",
1558 factorsCHull
= &analysisData
->graphsData
->allFactors
[j
][i
];
1559 if (factorsCHull
->type
== EXACT
)
1561 fprintf(syncState
->graphsStream
,
1563 "title \"Exact conversion\" with lines "
1564 "linecolor rgb \"black\" linetype 1, \\\n",
1565 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
);
1567 else if (factorsCHull
->type
== MIDDLE
)
1569 fprintf(syncState
->graphsStream
,
1570 "\t%.2f + %.10f * x "
1571 "title \"Min conversion\" with lines "
1572 "linecolor rgb \"black\" linetype 5, \\\n",
1573 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
);
1574 fprintf(syncState
->graphsStream
,
1575 "\t%.2f + %.10f * x "
1576 "title \"Max conversion\" with lines "
1577 "linecolor rgb \"black\" linetype 8, \\\n",
1578 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
);
1579 fprintf(syncState
->graphsStream
,
1580 "\t%.2f + %.10f * x "
1581 "title \"Middle conversion\" with lines "
1582 "linecolor rgb \"black\" linetype 1, \\\n",
1583 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
);
1585 else if (factorsCHull
->type
== FALLBACK
)
1587 fprintf(syncState
->graphsStream
,
1588 "\t%.2f + %.10f * x "
1589 "title \"Fallback conversion\" with lines "
1590 "linecolor rgb \"gray60\" linetype 1, \\\n",
1591 factorsCHull
->approx
->offset
, factorsCHull
->approx
->drift
);
1593 else if (factorsCHull
->type
== INCOMPLETE
)
1595 if (factorsCHull
->min
->drift
!= -INFINITY
)
1597 fprintf(syncState
->graphsStream
,
1598 "\t%.2f + %.10f * x "
1599 "title \"Min conversion\" with lines "
1600 "linecolor rgb \"black\" linetype 5, \\\n",
1601 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
);
1604 if (factorsCHull
->max
->drift
!= INFINITY
)
1606 fprintf(syncState
->graphsStream
,
1607 "\t%.2f + %.10f * x "
1608 "title \"Max conversion\" with lines "
1609 "linecolor rgb \"black\" linetype 8, \\\n",
1610 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
);
1613 else if (factorsCHull
->type
== SCREWED
)
1615 if (factorsCHull
->min
!= NULL
&& factorsCHull
->min
->drift
!= -INFINITY
)
1617 fprintf(syncState
->graphsStream
,
1618 "\t%.2f + %.10f * x "
1619 "title \"Min conversion\" with lines "
1620 "linecolor rgb \"black\" linetype 5, \\\n",
1621 factorsCHull
->min
->offset
, factorsCHull
->min
->drift
);
1624 if (factorsCHull
->max
!= NULL
&& factorsCHull
->max
->drift
!= INFINITY
)
1626 fprintf(syncState
->graphsStream
,
1627 "\t%.2f + %.10f * x "
1628 "title \"Max conversion\" with lines "
1629 "linecolor rgb \"black\" linetype 8, \\\n",
1630 factorsCHull
->max
->offset
, factorsCHull
->max
->drift
);
1633 else if (factorsCHull
->type
== ABSENT
)
1638 g_assert_not_reached();