Commit | Line | Data |
---|---|---|
08365995 BP |
1 | /* This file is part of the Linux Trace Toolkit viewer |
2 | * Copyright (C) 2009 Benjamin Poirier <benjamin.poirier@polymtl.ca> | |
3 | * | |
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; | |
7 | * | |
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. | |
12 | * | |
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, | |
16 | * MA 02111-1307, USA. | |
17 | */ | |
18 | #define _ISOC99_SOURCE | |
19 | ||
20 | #ifdef HAVE_CONFIG_H | |
21 | #include <config.h> | |
22 | #endif | |
23 | ||
24 | #include <errno.h> | |
25 | #include <math.h> | |
26 | #include <float.h> | |
27 | #include <stdlib.h> | |
28 | #include <stdio.h> | |
29 | #include <unistd.h> | |
30 | ||
2bd4b3e4 | 31 | #include "sync_chain.h" |
08365995 BP |
32 | |
33 | #include "event_analysis_chull.h" | |
34 | ||
35 | ||
36 | #ifndef g_info | |
37 | #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format) | |
38 | #endif | |
39 | ||
40 | ||
41 | typedef enum | |
42 | { | |
43 | LOWER, | |
44 | UPPER | |
45 | } HullType; | |
46 | ||
47 | ||
48 | typedef enum | |
49 | { | |
50 | MINIMUM, | |
51 | MAXIMUM | |
52 | } LineType; | |
53 | ||
54 | ||
55 | // Functions common to all analysis modules | |
56 | static void initAnalysisCHull(SyncState* const syncState); | |
57 | static void destroyAnalysisCHull(SyncState* const syncState); | |
58 | ||
10341d26 BP |
59 | static void analyzeMessageCHull(SyncState* const syncState, Message* const |
60 | message); | |
08365995 BP |
61 | static GArray* finalizeAnalysisCHull(SyncState* const syncState); |
62 | static void printAnalysisStatsCHull(SyncState* const syncState); | |
8d7d16dd BP |
63 | static void writeAnalysisGraphsPlotsCHull(SyncState* const syncState, const |
64 | unsigned int i, const unsigned int j); | |
08365995 BP |
65 | |
66 | // Functions specific to this module | |
67 | static void registerAnalysisCHull() __attribute__((constructor (101))); | |
68 | ||
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); | |
73 | ||
74 | static void grahamScan(GQueue* const hull, Point* const newPoint, const | |
75 | HullType type); | |
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** | |
91 | allFactors); | |
92 | static void freeAllFactors(const SyncState* const syncState, FactorsCHull** | |
93 | const allFactors); | |
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 | |
98 | predecessors); | |
99 | static void getFactors(FactorsCHull** const allFactors, unsigned int** const | |
100 | predecessors, unsigned int* const references, const unsigned int traceNum, | |
101 | Factors* const factors); | |
102 | ||
103 | static void gfPointDestroy(gpointer data, gpointer userData); | |
104 | ||
105 | ||
106 | static AnalysisModule analysisModuleCHull= { | |
107 | .name= "chull", | |
108 | .initAnalysis= &initAnalysisCHull, | |
109 | .destroyAnalysis= &destroyAnalysisCHull, | |
10341d26 | 110 | .analyzeMessage= &analyzeMessageCHull, |
08365995 BP |
111 | .finalizeAnalysis= &finalizeAnalysisCHull, |
112 | .printAnalysisStats= &printAnalysisStatsCHull, | |
467066ee BP |
113 | .graphFunctions= { |
114 | .writeTraceTracePlots= &writeAnalysisGraphsPlotsCHull, | |
115 | } | |
08365995 BP |
116 | }; |
117 | ||
118 | ||
119 | /* | |
120 | * Analysis module registering function | |
121 | */ | |
122 | static void registerAnalysisCHull() | |
123 | { | |
124 | g_queue_push_tail(&analysisModules, &analysisModuleCHull); | |
125 | } | |
126 | ||
127 | ||
128 | /* | |
129 | * Analysis init function | |
130 | * | |
131 | * This function is called at the beginning of a synchronization run for a set | |
132 | * of traces. | |
133 | * | |
134 | * Allocate some of the analysis specific data structures | |
135 | * | |
136 | * Args: | |
137 | * syncState container for synchronization data. | |
138 | * This function allocates or initializes these analysisData | |
139 | * members: | |
140 | * hullArray | |
141 | * dropped | |
142 | */ | |
143 | static void initAnalysisCHull(SyncState* const syncState) | |
144 | { | |
145 | unsigned int i, j; | |
146 | AnalysisDataCHull* analysisData; | |
147 | ||
148 | analysisData= malloc(sizeof(AnalysisDataCHull)); | |
149 | syncState->analysisData= analysisData; | |
150 | ||
151 | analysisData->hullArray= malloc(syncState->traceNb * sizeof(GQueue**)); | |
152 | for (i= 0; i < syncState->traceNb; i++) | |
153 | { | |
154 | analysisData->hullArray[i]= malloc(syncState->traceNb * sizeof(GQueue*)); | |
155 | ||
156 | for (j= 0; j < syncState->traceNb; j++) | |
157 | { | |
158 | analysisData->hullArray[i][j]= g_queue_new(); | |
159 | } | |
160 | } | |
161 | ||
162 | if (syncState->stats) | |
163 | { | |
164 | analysisData->stats= malloc(sizeof(AnalysisStatsCHull)); | |
165 | analysisData->stats->dropped= 0; | |
166 | analysisData->stats->allFactors= NULL; | |
167 | } | |
168 | ||
8d7d16dd | 169 | if (syncState->graphsStream) |
08365995 BP |
170 | { |
171 | analysisData->graphsData= malloc(sizeof(AnalysisGraphsDataCHull)); | |
172 | openGraphFiles(syncState); | |
173 | analysisData->graphsData->allFactors= NULL; | |
174 | } | |
175 | } | |
176 | ||
177 | ||
178 | /* | |
179 | * Create and open files used to store convex hull points to genereate | |
180 | * graphs. Allocate and populate array to store file pointers. | |
181 | * | |
182 | * Args: | |
183 | * syncState: container for synchronization data | |
184 | */ | |
185 | static void openGraphFiles(SyncState* const syncState) | |
186 | { | |
187 | unsigned int i, j; | |
188 | int retval; | |
189 | char* cwd; | |
190 | char name[31]; | |
191 | AnalysisDataCHull* analysisData; | |
192 | ||
193 | analysisData= (AnalysisDataCHull*) syncState->analysisData; | |
194 | ||
8d7d16dd | 195 | cwd= changeToGraphDir(syncState->graphsDir); |
08365995 BP |
196 | |
197 | analysisData->graphsData->hullPoints= malloc(syncState->traceNb * | |
198 | sizeof(FILE**)); | |
199 | for (i= 0; i < syncState->traceNb; i++) | |
200 | { | |
201 | analysisData->graphsData->hullPoints[i]= malloc(syncState->traceNb * | |
202 | sizeof(FILE*)); | |
203 | for (j= 0; j < syncState->traceNb; j++) | |
204 | { | |
205 | if (i != j) | |
206 | { | |
207 | retval= snprintf(name, sizeof(name), | |
208 | "analysis_chull-%03u_to_%03u.data", j, i); | |
209 | if (retval > sizeof(name) - 1) | |
210 | { | |
211 | name[sizeof(name) - 1]= '\0'; | |
212 | } | |
213 | if ((analysisData->graphsData->hullPoints[i][j]= fopen(name, "w")) == | |
214 | NULL) | |
215 | { | |
216 | g_error(strerror(errno)); | |
217 | } | |
218 | } | |
219 | } | |
220 | } | |
221 | ||
222 | retval= chdir(cwd); | |
223 | if (retval == -1) | |
224 | { | |
225 | g_error(strerror(errno)); | |
226 | } | |
227 | free(cwd); | |
228 | } | |
229 | ||
230 | ||
231 | /* | |
232 | * Write hull points to files to generate graphs. | |
233 | * | |
234 | * Args: | |
235 | * syncState: container for synchronization data | |
236 | */ | |
237 | static void writeGraphFiles(SyncState* const syncState) | |
238 | { | |
239 | unsigned int i, j; | |
240 | AnalysisDataCHull* analysisData; | |
241 | ||
242 | analysisData= (AnalysisDataCHull*) syncState->analysisData; | |
243 | ||
244 | for (i= 0; i < syncState->traceNb; i++) | |
245 | { | |
246 | for (j= 0; j < syncState->traceNb; j++) | |
247 | { | |
248 | if (i != j) | |
249 | { | |
250 | g_queue_foreach(analysisData->hullArray[i][j], | |
251 | &gfDumpHullToFile, | |
252 | analysisData->graphsData->hullPoints[i][j]); | |
253 | } | |
254 | } | |
255 | } | |
256 | } | |
257 | ||
258 | ||
259 | /* | |
260 | * A GFunc for g_queue_foreach. Write a hull point to a file used to generate | |
261 | * graphs | |
262 | * | |
263 | * Args: | |
264 | * data: Point*, point to write to the file | |
265 | * userData: FILE*, file pointer where to write the point | |
266 | */ | |
267 | static void gfDumpHullToFile(gpointer data, gpointer userData) | |
268 | { | |
269 | Point* point; | |
270 | ||
271 | point= (Point*) data; | |
272 | fprintf((FILE*) userData, "%20llu %20llu\n", point->x, point->y); | |
273 | } | |
274 | ||
275 | ||
276 | /* | |
277 | * Close files used to store convex hull points to generate graphs. | |
278 | * Deallocate array to store file pointers. | |
279 | * | |
280 | * Args: | |
281 | * syncState: container for synchronization data | |
282 | */ | |
283 | static void closeGraphFiles(SyncState* const syncState) | |
284 | { | |
285 | unsigned int i, j; | |
286 | AnalysisDataCHull* analysisData; | |
287 | int retval; | |
288 | ||
289 | analysisData= (AnalysisDataCHull*) syncState->analysisData; | |
290 | ||
291 | if (analysisData->graphsData->hullPoints == NULL) | |
292 | { | |
293 | return; | |
294 | } | |
295 | ||
296 | for (i= 0; i < syncState->traceNb; i++) | |
297 | { | |
298 | for (j= 0; j < syncState->traceNb; j++) | |
299 | { | |
300 | if (i != j) | |
301 | { | |
302 | retval= fclose(analysisData->graphsData->hullPoints[i][j]); | |
303 | if (retval != 0) | |
304 | { | |
305 | g_error(strerror(errno)); | |
306 | } | |
307 | } | |
308 | } | |
309 | free(analysisData->graphsData->hullPoints[i]); | |
310 | } | |
311 | free(analysisData->graphsData->hullPoints); | |
312 | analysisData->graphsData->hullPoints= NULL; | |
313 | } | |
314 | ||
315 | ||
316 | /* | |
317 | * Analysis destroy function | |
318 | * | |
319 | * Free the analysis specific data structures | |
320 | * | |
321 | * Args: | |
322 | * syncState container for synchronization data. | |
323 | * This function deallocates these analysisData members: | |
324 | * hullArray | |
325 | * stDev | |
326 | */ | |
327 | static void destroyAnalysisCHull(SyncState* const syncState) | |
328 | { | |
329 | unsigned int i, j; | |
330 | AnalysisDataCHull* analysisData; | |
331 | ||
332 | analysisData= (AnalysisDataCHull*) syncState->analysisData; | |
333 | ||
334 | if (analysisData == NULL) | |
335 | { | |
336 | return; | |
337 | } | |
338 | ||
339 | for (i= 0; i < syncState->traceNb; i++) | |
340 | { | |
341 | for (j= 0; j < syncState->traceNb; j++) | |
342 | { | |
343 | g_queue_foreach(analysisData->hullArray[i][j], gfPointDestroy, NULL); | |
344 | } | |
345 | free(analysisData->hullArray[i]); | |
346 | } | |
347 | free(analysisData->hullArray); | |
348 | ||
349 | if (syncState->stats) | |
350 | { | |
351 | if (analysisData->stats->allFactors != NULL) | |
352 | { | |
353 | freeAllFactors(syncState, analysisData->stats->allFactors); | |
354 | } | |
355 | ||
356 | free(analysisData->stats); | |
357 | } | |
358 | ||
8d7d16dd | 359 | if (syncState->graphsStream) |
08365995 BP |
360 | { |
361 | if (analysisData->graphsData->hullPoints != NULL) | |
362 | { | |
363 | closeGraphFiles(syncState); | |
364 | } | |
365 | ||
366 | if (!syncState->stats && analysisData->graphsData->allFactors != NULL) | |
367 | { | |
368 | freeAllFactors(syncState, analysisData->graphsData->allFactors); | |
369 | } | |
370 | ||
371 | free(analysisData->graphsData); | |
372 | } | |
373 | ||
374 | free(syncState->analysisData); | |
375 | syncState->analysisData= NULL; | |
376 | } | |
377 | ||
378 | ||
379 | /* | |
380 | * Perform analysis on an event pair. | |
381 | * | |
382 | * Args: | |
383 | * syncState container for synchronization data | |
10341d26 | 384 | * message structure containing the events |
08365995 | 385 | */ |
10341d26 | 386 | static void analyzeMessageCHull(SyncState* const syncState, Message* const message) |
08365995 BP |
387 | { |
388 | AnalysisDataCHull* analysisData; | |
389 | Point* newPoint; | |
390 | HullType hullType; | |
391 | GQueue* hull; | |
392 | ||
393 | analysisData= (AnalysisDataCHull*) syncState->analysisData; | |
394 | ||
395 | newPoint= malloc(sizeof(Point)); | |
10341d26 | 396 | if (message->inE->traceNum < message->outE->traceNum) |
08365995 BP |
397 | { |
398 | // CA is inE->traceNum | |
76be6fc2 BP |
399 | newPoint->x= message->inE->cpuTime; |
400 | newPoint->y= message->outE->cpuTime; | |
08365995 | 401 | hullType= UPPER; |
10341d26 BP |
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, | |
08365995 BP |
404 | newPoint->y); |
405 | } | |
406 | else | |
407 | { | |
408 | // CA is outE->traceNum | |
76be6fc2 BP |
409 | newPoint->x= message->outE->cpuTime; |
410 | newPoint->y= message->inE->cpuTime; | |
08365995 | 411 | hullType= LOWER; |
10341d26 BP |
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, | |
08365995 BP |
414 | newPoint->y); |
415 | } | |
416 | ||
417 | hull= | |
10341d26 | 418 | analysisData->hullArray[message->inE->traceNum][message->outE->traceNum]; |
08365995 BP |
419 | |
420 | if (hull->length >= 1 && newPoint->x < ((Point*) | |
421 | g_queue_peek_tail(hull))->x) | |
422 | { | |
423 | if (syncState->stats) | |
424 | { | |
425 | analysisData->stats->dropped++; | |
426 | } | |
427 | ||
428 | free(newPoint); | |
429 | } | |
430 | else | |
431 | { | |
432 | grahamScan(hull, newPoint, hullType); | |
433 | } | |
434 | } | |
435 | ||
436 | ||
437 | /* | |
438 | * Construct one half of a convex hull from abscissa-sorted points | |
439 | * | |
440 | * Args: | |
441 | * hull: the points already in the hull | |
442 | * newPoint: a new point to consider | |
443 | * type: which half of the hull to construct | |
444 | */ | |
445 | static void grahamScan(GQueue* const hull, Point* const newPoint, const | |
446 | HullType type) | |
447 | { | |
448 | int inversionFactor; | |
449 | ||
450 | g_debug("grahamScan(hull (length: %u), newPoint, %s)", hull->length, type | |
451 | == LOWER ? "LOWER" : "UPPER"); | |
452 | ||
453 | if (type == LOWER) | |
454 | { | |
455 | inversionFactor= 1; | |
456 | } | |
457 | else | |
458 | { | |
459 | inversionFactor= -1; | |
460 | } | |
461 | ||
462 | if (hull->length >= 2) | |
463 | { | |
464 | g_debug("jointCmp(hull[%u], hull[%u], newPoint) * inversionFactor = %d * %d = %d", | |
465 | hull->length - 2, | |
466 | hull->length - 1, | |
467 | jointCmp(g_queue_peek_nth(hull, hull->length - 2), | |
468 | g_queue_peek_tail(hull), newPoint), | |
469 | inversionFactor, | |
470 | jointCmp(g_queue_peek_nth(hull, hull->length - 2), | |
471 | g_queue_peek_tail(hull), newPoint) * inversionFactor); | |
472 | } | |
473 | while (hull->length >= 2 && jointCmp(g_queue_peek_nth(hull, hull->length - | |
474 | 2), g_queue_peek_tail(hull), newPoint) * inversionFactor <= 0) | |
475 | { | |
476 | g_debug("Removing hull[%u]", hull->length); | |
477 | free((Point*) g_queue_pop_tail(hull)); | |
478 | ||
479 | if (hull->length >= 2) | |
480 | { | |
481 | g_debug("jointCmp(hull[%u], hull[%u], newPoint) * inversionFactor = %d * %d = %d", | |
482 | hull->length - 2, | |
483 | hull->length - 1, | |
484 | jointCmp(g_queue_peek_nth(hull, hull->length - 2), | |
485 | g_queue_peek_tail(hull), newPoint), | |
486 | inversionFactor, | |
487 | jointCmp(g_queue_peek_nth(hull, hull->length - 2), | |
488 | g_queue_peek_tail(hull), newPoint) * inversionFactor); | |
489 | } | |
490 | } | |
491 | g_queue_push_tail(hull, newPoint); | |
492 | } | |
493 | ||
494 | ||
495 | /* | |
496 | * Finalize the factor calculations | |
497 | * | |
498 | * Args: | |
499 | * syncState container for synchronization data. | |
500 | * | |
501 | * Returns: | |
502 | * Factors[traceNb] synchronization factors for each trace | |
503 | */ | |
504 | static GArray* finalizeAnalysisCHull(SyncState* const syncState) | |
505 | { | |
506 | AnalysisDataCHull* analysisData; | |
507 | GArray* factors; | |
508 | FactorsCHull** allFactors; | |
509 | ||
510 | analysisData= (AnalysisDataCHull*) syncState->analysisData; | |
511 | ||
8d7d16dd | 512 | if (syncState->graphsStream && analysisData->graphsData->hullPoints != NULL) |
08365995 BP |
513 | { |
514 | writeGraphFiles(syncState); | |
515 | closeGraphFiles(syncState); | |
516 | } | |
517 | ||
518 | allFactors= calculateAllFactors(syncState); | |
519 | ||
520 | factors= reduceFactors(syncState, allFactors); | |
521 | ||
8d7d16dd | 522 | if (syncState->stats || syncState->graphsStream) |
08365995 BP |
523 | { |
524 | if (syncState->stats) | |
525 | { | |
526 | analysisData->stats->allFactors= allFactors; | |
527 | } | |
528 | ||
8d7d16dd | 529 | if (syncState->graphsStream) |
08365995 BP |
530 | { |
531 | analysisData->graphsData->allFactors= allFactors; | |
532 | } | |
533 | } | |
534 | else | |
535 | { | |
536 | freeAllFactors(syncState, allFactors); | |
537 | } | |
538 | ||
539 | return factors; | |
540 | } | |
541 | ||
542 | ||
543 | /* | |
544 | * Print statistics related to analysis. Must be called after | |
545 | * finalizeAnalysis. | |
546 | * | |
547 | * Args: | |
548 | * syncState container for synchronization data. | |
549 | */ | |
550 | static void printAnalysisStatsCHull(SyncState* const syncState) | |
551 | { | |
552 | AnalysisDataCHull* analysisData; | |
553 | unsigned int i, j; | |
554 | ||
555 | if (!syncState->stats) | |
556 | { | |
557 | return; | |
558 | } | |
559 | ||
560 | analysisData= (AnalysisDataCHull*) syncState->analysisData; | |
561 | ||
562 | printf("Convex hull analysis stats:\n"); | |
563 | printf("\tout of order packets dropped from analysis: %u\n", | |
564 | analysisData->stats->dropped); | |
565 | ||
566 | printf("\tNumber of points in convex hulls:\n"); | |
567 | ||
568 | for (i= 0; i < syncState->traceNb; i++) | |
569 | { | |
570 | for (j= i + 1; j < syncState->traceNb; j++) | |
571 | { | |
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); | |
575 | } | |
576 | } | |
577 | ||
578 | printf("\tIndividual synchronization factors:\n"); | |
579 | ||
580 | for (i= 0; i < syncState->traceNb; i++) | |
581 | { | |
582 | for (j= i + 1; j < syncState->traceNb; j++) | |
583 | { | |
584 | FactorsCHull* factorsCHull; | |
585 | ||
586 | factorsCHull= &analysisData->stats->allFactors[j][i]; | |
587 | printf("\t\t%3d - %-3d: ", i, j); | |
588 | ||
589 | if (factorsCHull->type == EXACT) | |
590 | { | |
591 | printf("Exact a0= % 7g a1= 1 %c %7g\n", | |
592 | factorsCHull->approx->offset, | |
593 | factorsCHull->approx->drift < 0. ? '-' : '+', | |
594 | fabs(factorsCHull->approx->drift)); | |
595 | } | |
596 | else if (factorsCHull->type == MIDDLE) | |
597 | { | |
598 | printf("Middle a0= % 7g a1= 1 %c %7g accuracy %7g\n", | |
599 | factorsCHull->approx->offset, factorsCHull->approx->drift | |
600 | - 1. < 0. ? '-' : '+', fabs(factorsCHull->approx->drift - | |
601 | 1.), factorsCHull->accuracy); | |
602 | printf("\t\t a0: % 7g to % 7g (delta= %7g)\n", | |
603 | factorsCHull->max->offset, factorsCHull->min->offset, | |
604 | factorsCHull->min->offset - factorsCHull->max->offset); | |
605 | printf("\t\t a1: 1 %+7g to %+7g (delta= %7g)\n", | |
606 | factorsCHull->min->drift - 1., factorsCHull->max->drift - | |
607 | 1., factorsCHull->max->drift - factorsCHull->min->drift); | |
608 | } | |
609 | else if (factorsCHull->type == FALLBACK) | |
610 | { | |
611 | printf("Fallback a0= % 7g a1= 1 %c %7g error= %7g\n", | |
612 | factorsCHull->approx->offset, factorsCHull->approx->drift | |
613 | - 1. < 0. ? '-' : '+', fabs(factorsCHull->approx->drift - | |
614 | 1.), factorsCHull->accuracy); | |
615 | } | |
616 | else if (factorsCHull->type == INCOMPLETE) | |
617 | { | |
618 | printf("Incomplete\n"); | |
619 | ||
620 | if (factorsCHull->min->drift != -INFINITY) | |
621 | { | |
622 | printf("\t\t min: a0: % 7g a1: 1 %c %7g\n", | |
623 | factorsCHull->min->offset, factorsCHull->min->drift - | |
624 | 1. < 0 ? '-' : '+', fabs(factorsCHull->min->drift - | |
625 | 1.)); | |
626 | } | |
627 | if (factorsCHull->max->drift != INFINITY) | |
628 | { | |
629 | printf("\t\t max: a0: % 7g a1: 1 %c %7g\n", | |
630 | factorsCHull->max->offset, factorsCHull->max->drift - | |
631 | 1. < 0 ? '-' : '+', fabs(factorsCHull->max->drift - | |
632 | 1.)); | |
633 | } | |
634 | } | |
635 | else if (factorsCHull->type == SCREWED) | |
636 | { | |
637 | printf("Screwed\n"); | |
638 | ||
639 | if (factorsCHull->min != NULL && factorsCHull->min->drift != -INFINITY) | |
640 | { | |
641 | printf("\t\t min: a0: % 7g a1: 1 %c %7g\n", | |
642 | factorsCHull->min->offset, factorsCHull->min->drift - | |
643 | 1. < 0 ? '-' : '+', fabs(factorsCHull->min->drift - | |
644 | 1.)); | |
645 | } | |
646 | if (factorsCHull->max != NULL && factorsCHull->max->drift != INFINITY) | |
647 | { | |
648 | printf("\t\t max: a0: % 7g a1: 1 %c %7g\n", | |
649 | factorsCHull->max->offset, factorsCHull->max->drift - | |
650 | 1. < 0 ? '-' : '+', fabs(factorsCHull->max->drift - | |
651 | 1.)); | |
652 | } | |
653 | } | |
654 | else if (factorsCHull->type == ABSENT) | |
655 | { | |
656 | printf("Absent\n"); | |
657 | } | |
658 | else | |
659 | { | |
660 | g_assert_not_reached(); | |
661 | } | |
662 | } | |
663 | } | |
664 | } | |
665 | ||
666 | ||
667 | /* | |
668 | * A GFunc for g_queue_foreach() | |
669 | * | |
670 | * Args: | |
671 | * data Point*, point to destroy | |
672 | * user_data NULL | |
673 | */ | |
674 | static void gfPointDestroy(gpointer data, gpointer userData) | |
675 | { | |
676 | Point* point; | |
677 | ||
678 | point= (Point*) data; | |
679 | free(point); | |
680 | } | |
681 | ||
682 | ||
683 | /* | |
684 | * Find out if a sequence of three points constitutes a "left turn" or a | |
685 | * "right turn". | |
686 | * | |
687 | * Args: | |
688 | * p1, p2, p3: The three points. | |
689 | * | |
690 | * Returns: | |
691 | * < 0 right turn | |
692 | * 0 colinear (unlikely result since this uses floating point | |
693 | * arithmetic) | |
694 | * > 0 left turn | |
695 | */ | |
696 | static int jointCmp(const Point const* p1, const Point const* p2, const | |
697 | Point const* p3) | |
698 | { | |
699 | double result; | |
700 | const double fuzzFactor= 0.; | |
701 | ||
702 | result= crossProductK(p1, p2, p1, p3); | |
703 | g_debug("crossProductK(p1= (%llu, %llu), p2= (%llu, %llu), p1= (%llu, %llu), p3= (%llu, %llu))= %g", | |
704 | p1->x, p1->y, p2->x, p2->y, p1->x, p1->y, p3->x, p3->y, result); | |
705 | if (result < fuzzFactor) | |
706 | { | |
707 | return -1; | |
708 | } | |
709 | else if (result > fuzzFactor) | |
710 | { | |
711 | return 1; | |
712 | } | |
713 | else | |
714 | { | |
715 | return 0; | |
716 | } | |
717 | } | |
718 | ||
719 | ||
720 | /* | |
721 | * Calculate the k component of the cross product of two vectors. | |
722 | * | |
723 | * Args: | |
724 | * p1, p2: start and end points of the first vector | |
725 | * p3, p4: start and end points of the second vector | |
726 | * | |
727 | * Returns: | |
728 | * the k component of the cross product when considering the two vectors to | |
729 | * be in the i-j plane. The direction (sign) of the result can be useful to | |
730 | * determine the relative orientation of the two vectors. | |
731 | */ | |
732 | static double crossProductK(const Point const* p1, const Point const* p2, | |
733 | const Point const* p3, const Point const* p4) | |
734 | { | |
735 | return ((double) p2->x - p1->x) * ((double) p4->y - p3->y) - ((double) | |
736 | p2->y - p1->y) * ((double) p4->x - p3->x); | |
737 | } | |
738 | ||
739 | ||
740 | /* | |
741 | * Free a container of FactorsCHull | |
742 | * | |
743 | * Args: | |
744 | * syncState: container for synchronization data. | |
745 | * allFactors: container of Factors | |
746 | */ | |
747 | static void freeAllFactors(const SyncState* const syncState, FactorsCHull** | |
748 | const allFactors) | |
749 | { | |
750 | unsigned int i, j; | |
751 | ||
752 | for (i= 0; i < syncState->traceNb; i++) | |
753 | { | |
754 | for (j= 0; j <= i; j++) | |
755 | { | |
756 | FactorsCHull* factorsCHull; | |
757 | ||
758 | factorsCHull= &allFactors[i][j]; | |
759 | if (factorsCHull->type == MIDDLE || factorsCHull->type == | |
760 | INCOMPLETE || factorsCHull->type == ABSENT) | |
761 | { | |
762 | free(factorsCHull->min); | |
763 | free(factorsCHull->max); | |
764 | } | |
765 | else if (factorsCHull->type == SCREWED) | |
766 | { | |
767 | if (factorsCHull->min != NULL) | |
768 | { | |
769 | free(factorsCHull->min); | |
770 | } | |
771 | if (factorsCHull->max != NULL) | |
772 | { | |
773 | free(factorsCHull->max); | |
774 | } | |
775 | } | |
776 | ||
777 | if (factorsCHull->type == EXACT || factorsCHull->type == MIDDLE || | |
778 | factorsCHull->type == FALLBACK) | |
779 | { | |
780 | free(factorsCHull->approx); | |
781 | } | |
782 | } | |
783 | free(allFactors[i]); | |
784 | } | |
785 | free(allFactors); | |
786 | } | |
787 | ||
788 | ||
789 | /* | |
790 | * Analyze the convex hulls to determine the synchronization factors between | |
791 | * each pair of trace. | |
792 | * | |
793 | * Args: | |
794 | * syncState container for synchronization data. | |
795 | * | |
796 | * Returns: | |
797 | * FactorsCHull*[TraceNum][TraceNum] array. See the documentation for the | |
798 | * member allFactors of AnalysisStatsCHull. | |
799 | */ | |
800 | static FactorsCHull** calculateAllFactors(SyncState* const syncState) | |
801 | { | |
802 | unsigned int traceNumA, traceNumB; | |
803 | FactorsCHull** allFactors; | |
804 | AnalysisDataCHull* analysisData; | |
805 | ||
806 | analysisData= (AnalysisDataCHull*) syncState->analysisData; | |
807 | ||
808 | // Allocate allFactors and calculate min and max | |
809 | allFactors= malloc(syncState->traceNb * sizeof(FactorsCHull*)); | |
810 | for (traceNumA= 0; traceNumA < syncState->traceNb; traceNumA++) | |
811 | { | |
812 | allFactors[traceNumA]= malloc((traceNumA + 1) * sizeof(FactorsCHull)); | |
813 | ||
814 | allFactors[traceNumA][traceNumA].type= EXACT; | |
815 | allFactors[traceNumA][traceNumA].approx= malloc(sizeof(Factors)); | |
816 | allFactors[traceNumA][traceNumA].approx->drift= 1.; | |
817 | allFactors[traceNumA][traceNumA].approx->offset= 0.; | |
818 | ||
819 | for (traceNumB= 0; traceNumB < traceNumA; traceNumB++) | |
820 | { | |
821 | unsigned int i; | |
822 | GQueue* cs, * cr; | |
823 | const struct | |
824 | { | |
825 | LineType lineType; | |
826 | size_t factorsOffset; | |
827 | } loopValues[]= { | |
828 | {MINIMUM, offsetof(FactorsCHull, min)}, | |
829 | {MAXIMUM, offsetof(FactorsCHull, max)} | |
830 | }; | |
831 | ||
832 | cr= analysisData->hullArray[traceNumB][traceNumA]; | |
833 | cs= analysisData->hullArray[traceNumA][traceNumB]; | |
834 | ||
835 | for (i= 0; i < sizeof(loopValues) / sizeof(*loopValues); i++) | |
836 | { | |
837 | g_debug("allFactors[%u][%u].%s = calculateFactorsExact(cr= hullArray[%u][%u], cs= hullArray[%u][%u], %s)", | |
838 | traceNumA, traceNumB, loopValues[i].factorsOffset == | |
839 | offsetof(FactorsCHull, min) ? "min" : "max", traceNumB, | |
840 | traceNumA, traceNumA, traceNumB, loopValues[i].lineType == | |
841 | MINIMUM ? "MINIMUM" : "MAXIMUM"); | |
842 | *((Factors**) ((void*) &allFactors[traceNumA][traceNumB] + | |
843 | loopValues[i].factorsOffset))= | |
844 | calculateFactorsExact(cr, cs, loopValues[i].lineType); | |
845 | } | |
846 | } | |
847 | } | |
848 | ||
849 | // Calculate approx when possible | |
850 | for (traceNumA= 0; traceNumA < syncState->traceNb; traceNumA++) | |
851 | { | |
852 | for (traceNumB= 0; traceNumB < traceNumA; traceNumB++) | |
853 | { | |
854 | FactorsCHull* factorsCHull; | |
855 | ||
856 | factorsCHull= &allFactors[traceNumA][traceNumB]; | |
857 | if (factorsCHull->min == NULL && factorsCHull->max == NULL) | |
858 | { | |
859 | factorsCHull->type= FALLBACK; | |
860 | calculateFactorsFallback(analysisData->hullArray[traceNumB][traceNumA], | |
861 | analysisData->hullArray[traceNumA][traceNumB], | |
862 | &allFactors[traceNumA][traceNumB]); | |
863 | } | |
864 | else if (factorsCHull->min != NULL && factorsCHull->max != NULL) | |
865 | { | |
866 | if (factorsCHull->min->drift != -INFINITY && | |
867 | factorsCHull->max->drift != INFINITY) | |
868 | { | |
869 | factorsCHull->type= MIDDLE; | |
870 | calculateFactorsMiddle(factorsCHull); | |
871 | } | |
872 | else if (factorsCHull->min->drift != -INFINITY || | |
873 | factorsCHull->max->drift != INFINITY) | |
874 | { | |
875 | factorsCHull->type= INCOMPLETE; | |
876 | } | |
877 | else | |
878 | { | |
879 | factorsCHull->type= ABSENT; | |
880 | } | |
881 | } | |
882 | else | |
883 | { | |
884 | //g_assert_not_reached(); | |
885 | factorsCHull->type= SCREWED; | |
886 | } | |
887 | } | |
888 | } | |
889 | ||
890 | return allFactors; | |
891 | } | |
892 | ||
893 | ||
894 | /* Calculate approximative factors based on minimum and maximum limits. The | |
895 | * best approximation to make is the interior bissector of the angle formed by | |
896 | * the minimum and maximum lines. | |
897 | * | |
898 | * The formulae used come from [Haddad, Yoram: Performance dans les systèmes | |
899 | * répartis: des outils pour les mesures, Université de Paris-Sud, Centre | |
900 | * d'Orsay, September 1988] Section 6.1 p.44 | |
901 | * | |
902 | * The reasoning for choosing this estimator comes from [Duda, A., Harrus, G., | |
903 | * Haddad, Y., and Bernard, G.: Estimating global time in distributed systems, | |
904 | * Proc. 7th Int. Conf. on Distributed Computing Systems, Berlin, volume 18, | |
905 | * 1987] p.303 | |
906 | * | |
907 | * Args: | |
908 | * factors: contains the min and max limits, used to store the result | |
909 | */ | |
910 | static void calculateFactorsMiddle(FactorsCHull* factors) | |
911 | { | |
912 | double amin, amax, bmin, bmax, bhat; | |
913 | ||
914 | amin= factors->max->offset; | |
915 | amax= factors->min->offset; | |
916 | bmin= factors->min->drift; | |
917 | bmax= factors->max->drift; | |
918 | ||
919 | g_assert_cmpfloat(bmax, >, bmin); | |
920 | ||
921 | factors->approx= malloc(sizeof(Factors)); | |
922 | bhat= (bmax * bmin - 1. + sqrt(1. + pow(bmax, 2.) * pow(bmin, 2.) + | |
923 | pow(bmax, 2.) + pow(bmin, 2.))) / (bmax + bmin); | |
924 | factors->approx->offset= amax - (amax - amin) / 2. * (pow(bhat, 2.) + 1.) | |
925 | / (1. + bhat * bmax); | |
926 | factors->approx->drift= bhat; | |
927 | factors->accuracy= bmax - bmin; | |
928 | } | |
929 | ||
930 | ||
931 | /* | |
932 | * Analyze the convex hulls to determine the minimum or maximum | |
933 | * synchronization factors between one pair of trace. | |
934 | * | |
935 | * This implements and improves upon the algorithm in [Haddad, Yoram: | |
936 | * Performance dans les systèmes répartis: des outils pour les mesures, | |
937 | * Université de Paris-Sud, Centre d'Orsay, September 1988] Section 6.2 p.47 | |
938 | * | |
939 | * Some degenerate cases are possible: | |
940 | * 1) the result is unbounded. In that case, when searching for the maximum | |
941 | * factors, result->drift= INFINITY; result->offset= -INFINITY. When | |
942 | * searching for the minimum factors, it is the opposite. It is not | |
943 | * possible to improve the situation with this data. | |
944 | * 2) no line can be above the upper hull and below the lower hull. This is | |
945 | * because the hulls intersect each other or are reversed. This means that | |
946 | * an assertion was false. Most probably, the clocks are not linear. It is | |
947 | * possible to repeat the search with another algorithm that will find a | |
948 | * "best effort" approximation. See calculateFactorsApprox(). | |
949 | * | |
950 | * Args: | |
951 | * cu: the upper half-convex hull, the line must pass above this | |
952 | * and touch it in one point | |
953 | * cl: the lower half-convex hull, the line must pass below this | |
954 | * and touch it in one point | |
955 | * lineType: search for minimum or maximum factors | |
956 | * | |
957 | * Returns: | |
958 | * If a result is found, a struct Factors is allocated, filed with the | |
959 | * result and returned | |
960 | * NULL otherwise, degenerate case 2 is in effect | |
961 | */ | |
962 | static Factors* calculateFactorsExact(GQueue* const cu, GQueue* const cl, const | |
963 | LineType lineType) | |
964 | { | |
965 | GQueue* c1, * c2; | |
966 | unsigned int i1, i2; | |
967 | Point* p1, * p2; | |
968 | double inversionFactor; | |
969 | Factors* result; | |
970 | ||
971 | g_debug("calculateFactorsExact(cu= %p, cl= %p, %s)", cu, cl, lineType == | |
972 | MINIMUM ? "MINIMUM" : "MAXIMUM"); | |
973 | ||
974 | if (lineType == MINIMUM) | |
975 | { | |
976 | c1= cl; | |
977 | c2= cu; | |
978 | inversionFactor= -1.; | |
979 | } | |
980 | else | |
981 | { | |
982 | c1= cu; | |
983 | c2= cl; | |
984 | inversionFactor= 1.; | |
985 | } | |
986 | ||
987 | i1= 0; | |
988 | i2= c2->length - 1; | |
989 | ||
990 | // Check for degenerate case 1 | |
991 | if (c1->length == 0 || c2->length == 0 || ((Point*) g_queue_peek_nth(c1, | |
992 | i1))->x >= ((Point*) g_queue_peek_nth(c2, i2))->x) | |
993 | { | |
994 | result= malloc(sizeof(Factors)); | |
995 | if (lineType == MINIMUM) | |
996 | { | |
997 | result->drift= -INFINITY; | |
998 | result->offset= INFINITY; | |
999 | } | |
1000 | else | |
1001 | { | |
1002 | result->drift= INFINITY; | |
1003 | result->offset= -INFINITY; | |
1004 | } | |
1005 | ||
1006 | return result; | |
1007 | } | |
1008 | ||
1009 | do | |
1010 | { | |
1011 | while | |
1012 | ( | |
1013 | (int) i2 - 1 > 0 | |
1014 | && crossProductK( | |
1015 | g_queue_peek_nth(c1, i1), | |
1016 | g_queue_peek_nth(c2, i2), | |
1017 | g_queue_peek_nth(c1, i1), | |
1018 | g_queue_peek_nth(c2, i2 - 1)) * inversionFactor < 0. | |
1019 | ) | |
1020 | { | |
1021 | if (((Point*) g_queue_peek_nth(c1, i1))->x | |
1022 | < ((Point*) g_queue_peek_nth(c2, i2 - 1))->x) | |
1023 | { | |
1024 | i2--; | |
1025 | } | |
1026 | else | |
1027 | { | |
1028 | // Degenerate case 2 | |
1029 | return NULL; | |
1030 | } | |
1031 | } | |
1032 | while | |
1033 | ( | |
1034 | i1 + 1 < c1->length - 1 | |
1035 | && crossProductK( | |
1036 | g_queue_peek_nth(c1, i1), | |
1037 | g_queue_peek_nth(c2, i2), | |
1038 | g_queue_peek_nth(c1, i1 + 1), | |
1039 | g_queue_peek_nth(c2, i2)) * inversionFactor < 0. | |
1040 | ) | |
1041 | { | |
1042 | if (((Point*) g_queue_peek_nth(c1, i1 + 1))->x | |
1043 | < ((Point*) g_queue_peek_nth(c2, i2))->x) | |
1044 | { | |
1045 | i1++; | |
1046 | } | |
1047 | else | |
1048 | { | |
1049 | // Degenerate case 2 | |
1050 | return NULL; | |
1051 | } | |
1052 | } | |
1053 | } while | |
1054 | ( | |
1055 | (int) i2 - 1 > 0 | |
1056 | && crossProductK( | |
1057 | g_queue_peek_nth(c1, i1), | |
1058 | g_queue_peek_nth(c2, i2), | |
1059 | g_queue_peek_nth(c1, i1), | |
1060 | g_queue_peek_nth(c2, i2 - 1)) * inversionFactor < 0. | |
1061 | ); | |
1062 | ||
1063 | p1= g_queue_peek_nth(c1, i1); | |
1064 | p2= g_queue_peek_nth(c2, i2); | |
1065 | ||
1066 | g_debug("Resulting points are: c1[i1]: x= %llu y= %llu c2[i2]: x= %llu y= %llu", | |
1067 | p1->x, p1->y, p2->x, p2->y); | |
1068 | ||
1069 | result= malloc(sizeof(Factors)); | |
1070 | result->drift= slope(p1, p2); | |
1071 | result->offset= intercept(p1, p2); | |
1072 | ||
1073 | g_debug("Resulting factors are: drift= %g offset= %g", result->drift, result->offset); | |
1074 | ||
1075 | return result; | |
1076 | } | |
1077 | ||
1078 | ||
1079 | /* | |
1080 | * Analyze the convex hulls to determine approximate synchronization factors | |
1081 | * between one pair of trace when there is no line that can fit in the | |
1082 | * corridor separating them. | |
1083 | * | |
1084 | * This implements the algorithm in [Ashton, P.: Algorithms for Off-line Clock | |
1085 | * Synchronisation, University of Canterbury, December 1995, 26] Section 4.2.2 | |
1086 | * p.7 | |
1087 | * | |
1088 | * For each point p1 in cr | |
1089 | * For each point p2 in cs | |
1090 | * errorMin= 0 | |
1091 | * Calculate the line paramaters | |
1092 | * For each point p3 in each convex hull | |
1093 | * If p3 is on the wrong side of the line | |
1094 | * error+= distance | |
1095 | * If error < errorMin | |
1096 | * Update results | |
1097 | * | |
1098 | * Args: | |
1099 | * cr: the upper half-convex hull | |
1100 | * cs: the lower half-convex hull | |
1101 | * result: a pointer to the pre-allocated struct where the results | |
1102 | * will be stored | |
1103 | */ | |
1104 | static void calculateFactorsFallback(GQueue* const cr, GQueue* const cs, | |
1105 | FactorsCHull* const result) | |
1106 | { | |
1107 | unsigned int i, j, k; | |
1108 | double errorMin; | |
1109 | Factors* approx; | |
1110 | ||
1111 | errorMin= INFINITY; | |
1112 | approx= malloc(sizeof(Factors)); | |
1113 | ||
1114 | for (i= 0; i < cs->length; i++) | |
1115 | { | |
1116 | for (j= 0; j < cr->length; j++) | |
1117 | { | |
1118 | double error; | |
1119 | Point p1, p2; | |
1120 | ||
1121 | error= 0.; | |
1122 | ||
1123 | if (((Point*) g_queue_peek_nth(cs, i))->x < ((Point*)g_queue_peek_nth(cr, j))->x) | |
1124 | { | |
1125 | p1= *(Point*)g_queue_peek_nth(cs, i); | |
1126 | p2= *(Point*)g_queue_peek_nth(cr, j); | |
1127 | } | |
1128 | else | |
1129 | { | |
1130 | p1= *(Point*)g_queue_peek_nth(cr, j); | |
1131 | p2= *(Point*)g_queue_peek_nth(cs, i); | |
1132 | } | |
1133 | ||
1134 | // The lower hull should be above the point | |
1135 | for (k= 0; k < cs->length; k++) | |
1136 | { | |
1137 | if (jointCmp(&p1, &p2, g_queue_peek_nth(cs, k)) < 0.) | |
1138 | { | |
1139 | error+= verticalDistance(&p1, &p2, g_queue_peek_nth(cs, k)); | |
1140 | } | |
1141 | } | |
1142 | ||
1143 | // The upper hull should be below the point | |
1144 | for (k= 0; k < cr->length; k++) | |
1145 | { | |
1146 | if (jointCmp(&p1, &p2, g_queue_peek_nth(cr, k)) > 0.) | |
1147 | { | |
1148 | error+= verticalDistance(&p1, &p2, g_queue_peek_nth(cr, k)); | |
1149 | } | |
1150 | } | |
1151 | ||
1152 | if (error < errorMin) | |
1153 | { | |
1154 | g_debug("Fallback: i= %u j= %u is a better match (error= %g)", i, j, error); | |
1155 | approx->drift= slope(&p1, &p2); | |
1156 | approx->offset= intercept(&p1, &p2); | |
1157 | errorMin= error; | |
1158 | } | |
1159 | } | |
1160 | } | |
1161 | ||
1162 | result->approx= approx; | |
1163 | result->accuracy= errorMin; | |
1164 | } | |
1165 | ||
1166 | ||
1167 | /* | |
1168 | * Calculate the vertical distance between a line and a point | |
1169 | * | |
1170 | * Args: | |
1171 | * p1, p2: Two points defining the line | |
1172 | * point: a point | |
1173 | * | |
1174 | * Return: | |
1175 | * the vertical distance | |
1176 | */ | |
1177 | static double verticalDistance(Point* p1, Point* p2, Point* const point) | |
1178 | { | |
1179 | return fabs(slope(p1, p2) * point->x + intercept(p1, p2) - point->y); | |
1180 | } | |
1181 | ||
1182 | ||
1183 | /* | |
1184 | * Calculate the slope between two points | |
1185 | * | |
1186 | * Args: | |
1187 | * p1, p2 the two points | |
1188 | * | |
1189 | * Returns: | |
1190 | * the slope | |
1191 | */ | |
1192 | static double slope(const Point* const p1, const Point* const p2) | |
1193 | { | |
1194 | return ((double) p2->y - p1->y) / (p2->x - p1->x); | |
1195 | } | |
1196 | ||
1197 | ||
1198 | /* Calculate the y-intercept of a line that passes by two points | |
1199 | * | |
1200 | * Args: | |
1201 | * p1, p2 the two points | |
1202 | * | |
1203 | * Returns: | |
1204 | * the y-intercept | |
1205 | */ | |
1206 | static double intercept(const Point* const p1, const Point* const p2) | |
1207 | { | |
1208 | return ((double) p2->y * p1->x - (double) p1->y * p2->x) / ((double) p1->x - p2->x); | |
1209 | } | |
1210 | ||
1211 | ||
1212 | /* | |
1213 | * Calculate a resulting offset and drift for each trace. | |
1214 | * | |
1215 | * Traces are assembled in groups. A group is an "island" of nodes/traces that | |
1216 | * exchanged messages. A reference is determined for each group by using a | |
1217 | * shortest path search based on the accuracy of the approximation. This also | |
1218 | * forms a tree of the best way to relate each node's clock to the reference's | |
1219 | * based on the accuracy. Sometimes it may be necessary or advantageous to | |
1220 | * propagate the factors through intermediary clocks. Resulting factors for | |
1221 | * each trace are determined based on this tree. | |
1222 | * | |
1223 | * This part was not the focus of my research. The algorithm used here is | |
1224 | * inexact in some ways: | |
1225 | * 1) The reference used may not actually be the best one to use. This is | |
1226 | * because the accuracy is not corrected based on the drift during the | |
1227 | * shortest path search. | |
1228 | * 2) The min and max factors are not propagated and are no longer valid. | |
1229 | * 3) Approximations of different types (MIDDLE and FALLBACK) are compared | |
1230 | * together. The "accuracy" parameters of these have different meanings and | |
1231 | * are not readily comparable. | |
1232 | * | |
1233 | * Nevertheless, the result is satisfactory. You just can't tell "how much" it | |
1234 | * is. | |
1235 | * | |
1236 | * Two alternative (and subtly different) ways of propagating factors to | |
1237 | * preserve min and max bondaries have been proposed, see: | |
1238 | * [Duda, A., Harrus, G., Haddad, Y., and Bernard, G.: Estimating global time | |
1239 | * in distributed systems, Proc. 7th Int. Conf. on Distributed Computing | |
1240 | * Systems, Berlin, volume 18, 1987] p.304 | |
1241 | * | |
1242 | * [Jezequel, J.M., and Jard, C.: Building a global clock for observing | |
1243 | * computations in distributed memory parallel computers, Concurrency: | |
1244 | * Practice and Experience 8(1), volume 8, John Wiley & Sons, Ltd Chichester, | |
1245 | * 1996, 32] Section 5; which is mostly the same as | |
1246 | * [Jezequel, J.M.: Building a global time on parallel machines, Proceedings | |
1247 | * of the 3rd International Workshop on Distributed Algorithms, LNCS, volume | |
1248 | * 392, 136–147, 1989] Section 5 | |
1249 | * | |
1250 | * Args: | |
1251 | * syncState: container for synchronization data. | |
1252 | * allFactors: offset and drift between each pair of traces | |
1253 | * | |
1254 | * Returns: | |
1255 | * Factors[traceNb] synchronization factors for each trace | |
1256 | */ | |
1257 | static GArray* reduceFactors(SyncState* const syncState, FactorsCHull** const | |
1258 | allFactors) | |
1259 | { | |
1260 | GArray* factors; | |
1261 | double** distances; | |
1262 | unsigned int** predecessors; | |
1263 | double* distanceSums; | |
1264 | unsigned int* references; | |
1265 | unsigned int i, j; | |
1266 | ||
1267 | // Solve the all-pairs shortest path problem using the Floyd-Warshall | |
1268 | // algorithm | |
1269 | floydWarshall(syncState, allFactors, &distances, &predecessors); | |
1270 | ||
1271 | /* Find the reference for each node | |
1272 | * | |
1273 | * First calculate, for each node, the sum of the distances to each other | |
1274 | * node it can reach. | |
1275 | * | |
1276 | * Then, go through each "island" of traces to find the trace that has the | |
1277 | * lowest distance sum. Assign this trace as the reference to each trace | |
1278 | * of the island. | |
1279 | */ | |
1280 | distanceSums= malloc(syncState->traceNb * sizeof(double)); | |
1281 | for (i= 0; i < syncState->traceNb; i++) | |
1282 | { | |
1283 | distanceSums[i]= 0.; | |
1284 | for (j= 0; j < syncState->traceNb; j++) | |
1285 | { | |
1286 | distanceSums[i]+= distances[i][j]; | |
1287 | } | |
1288 | } | |
1289 | ||
1290 | references= malloc(syncState->traceNb * sizeof(unsigned int)); | |
1291 | for (i= 0; i < syncState->traceNb; i++) | |
1292 | { | |
1293 | references[i]= UINT_MAX; | |
1294 | } | |
1295 | for (i= 0; i < syncState->traceNb; i++) | |
1296 | { | |
1297 | if (references[i] == UINT_MAX) | |
1298 | { | |
1299 | unsigned int reference; | |
1300 | double distanceSumMin; | |
1301 | ||
1302 | // A node is its own reference by default | |
1303 | reference= i; | |
1304 | distanceSumMin= INFINITY; | |
1305 | for (j= 0; j < syncState->traceNb; j++) | |
1306 | { | |
1307 | if (distances[i][j] != INFINITY && distanceSums[j] < | |
1308 | distanceSumMin) | |
1309 | { | |
1310 | reference= j; | |
1311 | distanceSumMin= distanceSums[j]; | |
1312 | } | |
1313 | } | |
1314 | for (j= 0; j < syncState->traceNb; j++) | |
1315 | { | |
1316 | if (distances[i][j] != INFINITY) | |
1317 | { | |
1318 | references[j]= reference; | |
1319 | } | |
1320 | } | |
1321 | } | |
1322 | } | |
1323 | ||
1324 | for (i= 0; i < syncState->traceNb; i++) | |
1325 | { | |
1326 | free(distances[i]); | |
1327 | } | |
1328 | free(distances); | |
1329 | free(distanceSums); | |
1330 | ||
1331 | /* For each trace, calculate the factors based on their corresponding | |
1332 | * tree. The tree is rooted at the reference and the shortest path to each | |
1333 | * other nodes are the branches. | |
1334 | */ | |
1335 | factors= g_array_sized_new(FALSE, FALSE, sizeof(Factors), | |
1336 | syncState->traceNb); | |
1337 | g_array_set_size(factors, syncState->traceNb); | |
1338 | for (i= 0; i < syncState->traceNb; i++) | |
1339 | { | |
1340 | getFactors(allFactors, predecessors, references, i, &g_array_index(factors, | |
1341 | Factors, i)); | |
1342 | } | |
1343 | ||
1344 | for (i= 0; i < syncState->traceNb; i++) | |
1345 | { | |
1346 | free(predecessors[i]); | |
1347 | } | |
1348 | free(predecessors); | |
1349 | free(references); | |
1350 | ||
1351 | return factors; | |
1352 | } | |
1353 | ||
1354 | ||
1355 | /* | |
1356 | * Perform an all-source shortest path search using the Floyd-Warshall | |
1357 | * algorithm. | |
1358 | * | |
1359 | * The algorithm is implemented accoding to the description here: | |
1360 | * http://web.mit.edu/urban_or_book/www/book/chapter6/6.2.2.html | |
1361 | * | |
1362 | * Args: | |
1363 | * syncState: container for synchronization data. | |
1364 | * allFactors: offset and drift between each pair of traces | |
1365 | * distances: resulting matrix of the length of the shortest path between | |
1366 | * two nodes. If there is no path between two nodes, the | |
1367 | * length is INFINITY | |
1368 | * predecessors: resulting matrix of each node's predecessor on the shortest | |
1369 | * path between two nodes | |
1370 | */ | |
1371 | static void floydWarshall(SyncState* const syncState, FactorsCHull** const | |
1372 | allFactors, double*** const distances, unsigned int*** const | |
1373 | predecessors) | |
1374 | { | |
1375 | unsigned int i, j, k; | |
1376 | ||
1377 | // Setup initial conditions | |
1378 | *distances= malloc(syncState->traceNb * sizeof(double*)); | |
1379 | *predecessors= malloc(syncState->traceNb * sizeof(unsigned int*)); | |
1380 | for (i= 0; i < syncState->traceNb; i++) | |
1381 | { | |
1382 | (*distances)[i]= malloc(syncState->traceNb * sizeof(double)); | |
1383 | for (j= 0; j < syncState->traceNb; j++) | |
1384 | { | |
1385 | if (i == j) | |
1386 | { | |
1387 | g_assert(allFactors[i][j].type == EXACT); | |
1388 | ||
1389 | (*distances)[i][j]= 0.; | |
1390 | } | |
1391 | else | |
1392 | { | |
1393 | unsigned int row, col; | |
1394 | ||
1395 | if (i > j) | |
1396 | { | |
1397 | row= i; | |
1398 | col= j; | |
1399 | } | |
1400 | else if (i < j) | |
1401 | { | |
1402 | row= j; | |
1403 | col= i; | |
1404 | } | |
1405 | ||
1406 | if (allFactors[row][col].type == MIDDLE || | |
1407 | allFactors[row][col].type == FALLBACK) | |
1408 | { | |
1409 | (*distances)[i][j]= allFactors[row][col].accuracy; | |
1410 | } | |
1411 | else if (allFactors[row][col].type == INCOMPLETE || | |
1412 | allFactors[row][col].type == SCREWED || | |
1413 | allFactors[row][col].type == ABSENT) | |
1414 | { | |
1415 | (*distances)[i][j]= INFINITY; | |
1416 | } | |
1417 | else | |
1418 | { | |
1419 | g_assert_not_reached(); | |
1420 | } | |
1421 | } | |
1422 | } | |
1423 | ||
1424 | (*predecessors)[i]= malloc(syncState->traceNb * sizeof(unsigned int)); | |
1425 | for (j= 0; j < syncState->traceNb; j++) | |
1426 | { | |
1427 | if (i != j) | |
1428 | { | |
1429 | (*predecessors)[i][j]= i; | |
1430 | } | |
1431 | else | |
1432 | { | |
1433 | (*predecessors)[i][j]= UINT_MAX; | |
1434 | } | |
1435 | } | |
1436 | } | |
1437 | ||
1438 | // Run the iterations | |
1439 | for (k= 0; k < syncState->traceNb; k++) | |
1440 | { | |
1441 | for (i= 0; i < syncState->traceNb; i++) | |
1442 | { | |
1443 | for (j= 0; j < syncState->traceNb; j++) | |
1444 | { | |
1445 | double distanceMin; | |
1446 | ||
1447 | distanceMin= MIN((*distances)[i][j], (*distances)[i][k] + | |
1448 | (*distances)[k][j]); | |
1449 | ||
1450 | if (distanceMin != (*distances)[i][j]) | |
1451 | { | |
1452 | (*predecessors)[i][j]= (*predecessors)[k][j]; | |
1453 | } | |
1454 | ||
1455 | (*distances)[i][j]= distanceMin; | |
1456 | } | |
1457 | } | |
1458 | } | |
1459 | } | |
1460 | ||
1461 | ||
1462 | /* | |
1463 | * Cummulate the time correction factors to convert a node's time to its | |
1464 | * reference's time. | |
1465 | * This function recursively calls itself until it reaches the reference node. | |
1466 | * | |
1467 | * Args: | |
1468 | * allFactors: offset and drift between each pair of traces | |
1469 | * predecessors: matrix of each node's predecessor on the shortest | |
1470 | * path between two nodes | |
1471 | * references: reference node for each node | |
1472 | * traceNum: node for which to find the factors | |
1473 | * factors: resulting factors | |
1474 | */ | |
1475 | static void getFactors(FactorsCHull** const allFactors, unsigned int** const | |
1476 | predecessors, unsigned int* const references, const unsigned int traceNum, | |
1477 | Factors* const factors) | |
1478 | { | |
1479 | unsigned int reference; | |
1480 | ||
1481 | reference= references[traceNum]; | |
1482 | ||
1483 | if (reference == traceNum) | |
1484 | { | |
1485 | factors->offset= 0.; | |
1486 | factors->drift= 1.; | |
1487 | } | |
1488 | else | |
1489 | { | |
1490 | Factors previousVertexFactors; | |
1491 | ||
1492 | getFactors(allFactors, predecessors, references, | |
1493 | predecessors[reference][traceNum], &previousVertexFactors); | |
1494 | ||
1495 | // convertir de traceNum à reference | |
1496 | ||
1497 | // allFactors convertit de col à row | |
1498 | ||
1499 | if (reference > traceNum) | |
1500 | { | |
1501 | factors->offset= previousVertexFactors.drift * | |
1502 | allFactors[reference][traceNum].approx->offset + | |
1503 | previousVertexFactors.offset; | |
1504 | factors->drift= previousVertexFactors.drift * | |
1505 | allFactors[reference][traceNum].approx->drift; | |
1506 | } | |
1507 | else | |
1508 | { | |
1509 | factors->offset= previousVertexFactors.drift * (-1. * | |
1510 | allFactors[traceNum][reference].approx->offset / | |
1511 | allFactors[traceNum][reference].approx->drift) + | |
1512 | previousVertexFactors.offset; | |
1513 | factors->drift= previousVertexFactors.drift * (1. / | |
1514 | allFactors[traceNum][reference].approx->drift); | |
1515 | } | |
1516 | } | |
1517 | } | |
1518 | ||
1519 | ||
1520 | /* | |
1521 | * Write the analysis-specific graph lines in the gnuplot script. | |
1522 | * | |
1523 | * Args: | |
08365995 BP |
1524 | * syncState: container for synchronization data |
1525 | * i: first trace number | |
1526 | * j: second trace number, garanteed to be larger than i | |
1527 | */ | |
8d7d16dd BP |
1528 | void writeAnalysisGraphsPlotsCHull(SyncState* const syncState, const unsigned |
1529 | int i, const unsigned int j) | |
08365995 BP |
1530 | { |
1531 | AnalysisDataCHull* analysisData; | |
1532 | FactorsCHull* factorsCHull; | |
1533 | ||
1534 | analysisData= (AnalysisDataCHull*) syncState->analysisData; | |
1535 | ||
8d7d16dd | 1536 | fprintf(syncState->graphsStream, |
08365995 BP |
1537 | "\t\"analysis_chull-%1$03d_to_%2$03d.data\" " |
1538 | "title \"Lower half-hull\" with linespoints " | |
1539 | "linecolor rgb \"#015a01\" linetype 4 pointtype 8 pointsize 0.8, \\\n" | |
1540 | "\t\"analysis_chull-%2$03d_to_%1$03d.data\" " | |
1541 | "title \"Upper half-hull\" with linespoints " | |
1542 | "linecolor rgb \"#003366\" linetype 4 pointtype 10 pointsize 0.8, \\\n", | |
1543 | i, j); | |
1544 | ||
1545 | factorsCHull= &analysisData->graphsData->allFactors[j][i]; | |
1546 | if (factorsCHull->type == EXACT) | |
1547 | { | |
8d7d16dd | 1548 | fprintf(syncState->graphsStream, |
08365995 BP |
1549 | "\t%7g + %7g * x " |
1550 | "title \"Exact conversion\" with lines " | |
1551 | "linecolor rgb \"black\" linetype 1, \\\n", | |
1552 | factorsCHull->approx->offset, factorsCHull->approx->drift); | |
1553 | } | |
1554 | else if (factorsCHull->type == MIDDLE) | |
1555 | { | |
8d7d16dd | 1556 | fprintf(syncState->graphsStream, |
08365995 BP |
1557 | "\t%.2f + %.10f * x " |
1558 | "title \"Min conversion\" with lines " | |
1559 | "linecolor rgb \"black\" linetype 5, \\\n", | |
1560 | factorsCHull->min->offset, factorsCHull->min->drift); | |
8d7d16dd | 1561 | fprintf(syncState->graphsStream, |
08365995 BP |
1562 | "\t%.2f + %.10f * x " |
1563 | "title \"Max conversion\" with lines " | |
1564 | "linecolor rgb \"black\" linetype 8, \\\n", | |
1565 | factorsCHull->max->offset, factorsCHull->max->drift); | |
8d7d16dd | 1566 | fprintf(syncState->graphsStream, |
08365995 BP |
1567 | "\t%.2f + %.10f * x " |
1568 | "title \"Middle conversion\" with lines " | |
1569 | "linecolor rgb \"gray60\" linetype 1, \\\n", | |
1570 | factorsCHull->approx->offset, factorsCHull->approx->drift); | |
1571 | } | |
1572 | else if (factorsCHull->type == FALLBACK) | |
1573 | { | |
8d7d16dd | 1574 | fprintf(syncState->graphsStream, |
08365995 BP |
1575 | "\t%.2f + %.10f * x " |
1576 | "title \"Fallback conversion\" with lines " | |
1577 | "linecolor rgb \"gray60\" linetype 1, \\\n", | |
1578 | factorsCHull->approx->offset, factorsCHull->approx->drift); | |
1579 | } | |
1580 | else if (factorsCHull->type == INCOMPLETE) | |
1581 | { | |
1582 | if (factorsCHull->min->drift != -INFINITY) | |
1583 | { | |
8d7d16dd | 1584 | fprintf(syncState->graphsStream, |
08365995 BP |
1585 | "\t%.2f + %.10f * x " |
1586 | "title \"Min conversion\" with lines " | |
1587 | "linecolor rgb \"black\" linetype 5, \\\n", | |
1588 | factorsCHull->min->offset, factorsCHull->min->drift); | |
1589 | } | |
1590 | ||
1591 | if (factorsCHull->max->drift != INFINITY) | |
1592 | { | |
8d7d16dd | 1593 | fprintf(syncState->graphsStream, |
08365995 BP |
1594 | "\t%.2f + %.10f * x " |
1595 | "title \"Max conversion\" with lines " | |
1596 | "linecolor rgb \"black\" linetype 8, \\\n", | |
1597 | factorsCHull->max->offset, factorsCHull->max->drift); | |
1598 | } | |
1599 | } | |
1600 | else if (factorsCHull->type == SCREWED) | |
1601 | { | |
1602 | if (factorsCHull->min != NULL && factorsCHull->min->drift != -INFINITY) | |
1603 | { | |
8d7d16dd | 1604 | fprintf(syncState->graphsStream, |
08365995 BP |
1605 | "\t%.2f + %.10f * x " |
1606 | "title \"Min conversion\" with lines " | |
1607 | "linecolor rgb \"black\" linetype 5, \\\n", | |
1608 | factorsCHull->min->offset, factorsCHull->min->drift); | |
1609 | } | |
1610 | ||
1611 | if (factorsCHull->max != NULL && factorsCHull->max->drift != INFINITY) | |
1612 | { | |
8d7d16dd | 1613 | fprintf(syncState->graphsStream, |
08365995 BP |
1614 | "\t%.2f + %.10f * x " |
1615 | "title \"Max conversion\" with lines " | |
1616 | "linecolor rgb \"black\" linetype 8, \\\n", | |
1617 | factorsCHull->max->offset, factorsCHull->max->drift); | |
1618 | } | |
1619 | } | |
1620 | else if (factorsCHull->type == ABSENT) | |
1621 | { | |
1622 | } | |
1623 | else | |
1624 | { | |
1625 | g_assert_not_reached(); | |
1626 | } | |
1627 | } |