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,
20 #define _ISOC99_SOURCE
26 #include <arpa/inet.h>
29 #include <netinet/in.h>
34 #include <sys/socket.h>
38 #include "sync_chain.h"
39 #include "event_analysis_chull.h"
41 #include "event_analysis_eval.h"
44 struct WriteHistogramInfo
55 GArray
* iArray
, * jArray
, * aArray
;
59 // Functions common to all analysis modules
60 static void initAnalysisEval(SyncState
* const syncState
);
61 static void destroyAnalysisEval(SyncState
* const syncState
);
63 static void analyzeMessageEval(SyncState
* const syncState
, Message
* const
65 static void analyzeExchangeEval(SyncState
* const syncState
, Exchange
* const
67 static void analyzeBroadcastEval(SyncState
* const syncState
, Broadcast
* const
69 static GArray
* finalizeAnalysisEval(SyncState
* const syncState
);
70 static void printAnalysisStatsEval(SyncState
* const syncState
);
71 static void writeAnalysisTraceTimePlotsEval(SyncState
* const syncState
, const
72 unsigned int i
, const unsigned int j
);
73 static void writeAnalysisTraceTracePlotsEval(SyncState
* const syncState
, const
74 unsigned int i
, const unsigned int j
);
76 // Functions specific to this module
77 static void registerAnalysisEval() __attribute__((constructor (102)));
78 static guint
ghfRttKeyHash(gconstpointer key
);
79 static gboolean
gefRttKeyEqual(gconstpointer a
, gconstpointer b
);
80 static void gdnDestroyRttKey(gpointer data
);
81 static void gdnDestroyDouble(gpointer data
);
82 static void readRttInfo(GHashTable
* rttInfo
, FILE* rttFile
);
83 static void positionStream(FILE* stream
);
85 static void gfSum(gpointer data
, gpointer userData
);
86 static void gfSumSquares(gpointer data
, gpointer userData
);
87 static void ghfPrintExchangeRtt(gpointer key
, gpointer value
, gpointer
90 static void hitBin(struct Bins
* const bins
, const double value
);
91 static unsigned int binNum(const double value
) __attribute__((pure
));
92 static double binStart(const unsigned int binNum
) __attribute__((pure
));
93 static double binEnd(const unsigned int binNum
) __attribute__((pure
));
94 static uint32_t normalTotal(struct Bins
* const bins
) __attribute__((const));
96 static AnalysisHistogramEval
* constructAnalysisHistogramEval(const char* const
97 graphsDir
, const struct RttKey
* const rttKey
);
98 static void destroyAnalysisHistogramEval(AnalysisHistogramEval
* const
100 static void gdnDestroyAnalysisHistogramEval(gpointer data
);
101 static void ghfWriteHistogram(gpointer key
, gpointer value
, gpointer
103 static void dumpBinToFile(const struct Bins
* const bins
, FILE* const file
);
104 static void writeHistogram(FILE* graphsStream
, const struct RttKey
* rttKey
,
105 double* minRtt
, AnalysisHistogramEval
* const histogram
);
107 static void updateBounds(Bounds
** const bounds
, Event
* const e1
, Event
* const e2
);
109 // The next group of functions is only needed when computing synchronization
112 static glp_prob
* lpCreateProblem(GQueue
* const lowerHull
, GQueue
* const upperHull
);
113 static void gfLPAddRow(gpointer data
, gpointer user_data
);
114 static Factors
* calculateFactors(glp_prob
* const lp
, const int direction
);
115 static void calculateCompleteFactors(glp_prob
* const lp
, FactorsCHull
* factors
);
116 static FactorsCHull
** createAllFactors(const unsigned int traceNb
);
117 static inline void finalizeAnalysisEvalLP(SyncState
* const syncState
);
119 static void finalizeAnalysisEvalLP(SyncState
* const syncState
);
123 // initialized in registerAnalysisEval()
126 static AnalysisModule analysisModuleEval
= {
128 .initAnalysis
= &initAnalysisEval
,
129 .destroyAnalysis
= &destroyAnalysisEval
,
130 .analyzeMessage
= &analyzeMessageEval
,
131 .analyzeExchange
= &analyzeExchangeEval
,
132 .analyzeBroadcast
= &analyzeBroadcastEval
,
133 .finalizeAnalysis
= &finalizeAnalysisEval
,
134 .printAnalysisStats
= &printAnalysisStatsEval
,
136 .writeTraceTimePlots
= &writeAnalysisTraceTimePlotsEval
,
137 .writeTraceTracePlots
= &writeAnalysisTraceTracePlotsEval
,
141 static ModuleOption optionEvalRttFile
= {
142 .longName
= "eval-rtt-file",
143 .hasArg
= REQUIRED_ARG
,
145 .optionHelp
= "specify the file containing RTT information",
151 * Analysis module registering function
153 static void registerAnalysisEval()
155 binBase
= exp10(6. / (BIN_NB
- 3));
157 g_queue_push_tail(&analysisModules
, &analysisModuleEval
);
158 g_queue_push_tail(&moduleOptions
, &optionEvalRttFile
);
163 * Analysis init function
165 * This function is called at the beginning of a synchronization run for a set
169 * syncState container for synchronization data.
171 static void initAnalysisEval(SyncState
* const syncState
)
173 AnalysisDataEval
* analysisData
;
176 analysisData
= malloc(sizeof(AnalysisDataEval
));
177 syncState
->analysisData
= analysisData
;
179 analysisData
->rttInfo
= g_hash_table_new_full(&ghfRttKeyHash
,
180 &gefRttKeyEqual
, &gdnDestroyRttKey
, &gdnDestroyDouble
);
181 if (optionEvalRttFile
.arg
)
186 rttStream
= fopen(optionEvalRttFile
.arg
, "r");
187 if (rttStream
== NULL
)
189 g_error(strerror(errno
));
192 readRttInfo(analysisData
->rttInfo
, rttStream
);
194 retval
= fclose(rttStream
);
197 g_error(strerror(errno
));
201 if (syncState
->stats
)
203 analysisData
->stats
= calloc(1, sizeof(AnalysisStatsEval
));
204 analysisData
->stats
->broadcastDiffSum
= 0.;
206 analysisData
->stats
->messageStats
= malloc(syncState
->traceNb
*
207 sizeof(MessageStats
*));
208 for (i
= 0; i
< syncState
->traceNb
; i
++)
210 analysisData
->stats
->messageStats
[i
]= calloc(syncState
->traceNb
,
211 sizeof(MessageStats
));
214 analysisData
->stats
->exchangeRtt
=
215 g_hash_table_new_full(&ghfRttKeyHash
, &gefRttKeyEqual
,
216 &gdnDestroyRttKey
, &gdnDestroyDouble
);
219 analysisData
->stats
->chFactorsArray
= NULL
;
220 analysisData
->stats
->lpFactorsArray
= NULL
;
224 if (syncState
->graphsStream
)
226 AnalysisGraphsEval
* graphs
= malloc(sizeof(AnalysisGraphsEval
));
228 analysisData
->graphs
= graphs
;
230 graphs
->histograms
= g_hash_table_new_full(&ghfRttKeyHash
,
231 &gefRttKeyEqual
, &gdnDestroyRttKey
,
232 &gdnDestroyAnalysisHistogramEval
);
234 graphs
->bounds
= malloc(syncState
->traceNb
* sizeof(Bounds
*));
235 for (i
= 0; i
< syncState
->traceNb
; i
++)
237 graphs
->bounds
[i
]= malloc(i
* sizeof(Bounds
));
238 for (j
= 0; j
< i
; j
++)
240 graphs
->bounds
[i
][j
].min
= UINT64_MAX
;
241 graphs
->bounds
[i
][j
].max
= 0;
247 graphs
->lpFactorsArray
= NULL
;
251 if (syncState
->stats
|| syncState
->graphsStream
)
255 analysisData
->chullSS
= malloc(sizeof(SyncState
));
256 memcpy(analysisData
->chullSS
, syncState
, sizeof(SyncState
));
257 analysisData
->chullSS
->stats
= false;
258 analysisData
->chullSS
->analysisData
= NULL
;
259 result
= g_queue_find_custom(&analysisModules
, "chull",
260 &gcfCompareAnalysis
);
261 analysisData
->chullSS
->analysisModule
= (AnalysisModule
*) result
->data
;
262 analysisData
->chullSS
->analysisModule
->initAnalysis(analysisData
->chullSS
);
268 * Create and open files used to store histogram points to generate graphs.
269 * Create data structures to store histogram points during analysis.
272 * graphsDir: folder where to write files
273 * rttKey: host pair, make sure saddr < daddr
275 static AnalysisHistogramEval
* constructAnalysisHistogramEval(const char* const
276 graphsDir
, const struct RttKey
* const rttKey
)
281 char name
[60], saddr
[16], daddr
[16];
282 AnalysisHistogramEval
* histogram
= calloc(1, sizeof(*histogram
));
285 const char* fileName
;
286 const char* host1
, *host2
;
288 {offsetof(AnalysisHistogramEval
, ttSendPoints
),
289 "analysis_eval_tt-%s_to_%s.data", saddr
, daddr
},
290 {offsetof(AnalysisHistogramEval
, ttRecvPoints
),
291 "analysis_eval_tt-%s_to_%s.data", daddr
, saddr
},
292 {offsetof(AnalysisHistogramEval
, hrttPoints
),
293 "analysis_eval_hrtt-%s_and_%s.data", saddr
, daddr
},
296 histogram
->ttSendBins
.min
= BIN_NB
- 1;
297 histogram
->ttRecvBins
.min
= BIN_NB
- 1;
298 histogram
->hrttBins
.min
= BIN_NB
- 1;
300 convertIP(saddr
, rttKey
->saddr
);
301 convertIP(daddr
, rttKey
->daddr
);
303 cwd
= changeToGraphDir(graphsDir
);
305 for (i
= 0; i
< sizeof(loopValues
) / sizeof(*loopValues
); i
++)
307 retval
= snprintf(name
, sizeof(name
), loopValues
[i
].fileName
,
308 loopValues
[i
].host1
, loopValues
[i
].host2
);
309 if (retval
> sizeof(name
) - 1)
311 name
[sizeof(name
) - 1]= '\0';
313 if ((*(FILE**)((void*) histogram
+ loopValues
[i
].pointsOffset
)=
314 fopen(name
, "w")) == NULL
)
316 g_error(strerror(errno
));
323 g_error(strerror(errno
));
332 * Close files used to store histogram points to generate graphs.
335 * graphsDir: folder where to write files
336 * rttKey: host pair, make sure saddr < daddr
338 static void destroyAnalysisHistogramEval(AnalysisHistogramEval
* const
346 {offsetof(AnalysisHistogramEval
, ttSendPoints
)},
347 {offsetof(AnalysisHistogramEval
, ttRecvPoints
)},
348 {offsetof(AnalysisHistogramEval
, hrttPoints
)},
351 for (i
= 0; i
< sizeof(loopValues
) / sizeof(*loopValues
); i
++)
353 retval
= fclose(*(FILE**)((void*) histogram
+ loopValues
[i
].pointsOffset
));
356 g_error(strerror(errno
));
365 * A GDestroyNotify function for g_hash_table_new_full()
368 * data: AnalysisHistogramEval*
370 static void gdnDestroyAnalysisHistogramEval(gpointer data
)
372 destroyAnalysisHistogramEval(data
);
377 * A GHFunc for g_hash_table_foreach()
380 * key: RttKey* where saddr < daddr
381 * value: AnalysisHistogramEval*
382 * user_data struct WriteHistogramInfo*
384 static void ghfWriteHistogram(gpointer key
, gpointer value
, gpointer user_data
)
386 double* rtt1
, * rtt2
;
387 struct RttKey
* rttKey
= key
;
388 struct RttKey oppositeRttKey
= {.saddr
= rttKey
->daddr
, .daddr
=
390 AnalysisHistogramEval
* histogram
= value
;
391 struct WriteHistogramInfo
* info
= user_data
;
393 rtt1
= g_hash_table_lookup(info
->rttInfo
, rttKey
);
394 rtt2
= g_hash_table_lookup(info
->rttInfo
, &oppositeRttKey
);
400 else if (rtt2
!= NULL
)
402 rtt1
= MIN(rtt1
, rtt2
);
405 dumpBinToFile(&histogram
->ttSendBins
, histogram
->ttSendPoints
);
406 dumpBinToFile(&histogram
->ttRecvBins
, histogram
->ttRecvPoints
);
407 dumpBinToFile(&histogram
->hrttBins
, histogram
->hrttPoints
);
408 writeHistogram(info
->graphsStream
, rttKey
, rtt1
, histogram
);
413 * Write the content of one bin in a histogram point file
416 * bin: array of values that make up a histogram
417 * file: FILE*, write to this file
419 static void dumpBinToFile(const struct Bins
* const bins
, FILE* const file
)
423 // The first and last bins are skipped, see struct Bins
424 for (i
= 1; i
< BIN_NB
- 1; i
++)
426 if (bins
->bin
[i
] > 0)
428 fprintf(file
, "%20.9f %20.9f %20.9f\n", (binStart(i
) + binEnd(i
))
429 / 2., (double) bins
->bin
[i
] / ((binEnd(i
) - binStart(i
)) *
430 bins
->total
), binEnd(i
) - binStart(i
));
437 * Write the analysis-specific plot in the gnuplot script.
440 * graphsStream: write to this file
441 * rttKey: must be sorted such that saddr < daddr
442 * minRtt: if available, else NULL
443 * histogram: struct that contains the bins for the pair of traces
444 * identified by rttKey
446 static void writeHistogram(FILE* graphsStream
, const struct RttKey
* rttKey
,
447 double* minRtt
, AnalysisHistogramEval
* const histogram
)
449 char saddr
[16], daddr
[16];
451 convertIP(saddr
, rttKey
->saddr
);
452 convertIP(daddr
, rttKey
->daddr
);
454 fprintf(graphsStream
,
456 "set output \"histogram-%s-%s.eps\"\n"
458 "set xlabel \"Message Latency (s)\"\n"
459 "set ylabel \"Proportion of messages per second\"\n", saddr
, daddr
);
463 fprintf(graphsStream
,
464 "set arrow from %.9f, 0 rto 0, graph 1 "
465 "nohead linetype 3 linewidth 3 linecolor rgb \"black\"\n", *minRtt
469 if (normalTotal(&histogram
->ttSendBins
) ||
470 normalTotal(&histogram
->ttRecvBins
) ||
471 normalTotal(&histogram
->hrttBins
))
473 fprintf(graphsStream
, "plot \\\n");
475 if (normalTotal(&histogram
->hrttBins
))
477 fprintf(graphsStream
,
478 "\t\"analysis_eval_hrtt-%s_and_%s.data\" "
479 "title \"RTT/2\" with linespoints linetype 1 linewidth 2 "
480 "linecolor rgb \"black\" pointtype 6 pointsize 1,\\\n",
484 if (normalTotal(&histogram
->ttSendBins
))
486 fprintf(graphsStream
,
487 "\t\"analysis_eval_tt-%1$s_to_%2$s.data\" "
488 "title \"%1$s to %2$s\" with linespoints linetype 4 linewidth 2 "
489 "linecolor rgb \"gray60\" pointtype 6 pointsize 1,\\\n",
493 if (normalTotal(&histogram
->ttRecvBins
))
495 fprintf(graphsStream
,
496 "\t\"analysis_eval_tt-%1$s_to_%2$s.data\" "
497 "title \"%1$s to %2$s\" with linespoints linetype 4 linewidth 2 "
498 "linecolor rgb \"gray30\" pointtype 6 pointsize 1,\\\n",
502 // Remove the ",\\\n" from the last graph plot line
503 if (ftruncate(fileno(graphsStream
), ftell(graphsStream
) - 3) == -1)
505 g_error(strerror(errno
));
507 if (fseek(graphsStream
, 0, SEEK_END
) == -1)
509 g_error(strerror(errno
));
511 fprintf(graphsStream
, "\n");
517 * Analysis destroy function
519 * Free the analysis specific data structures
522 * syncState container for synchronization data.
524 static void destroyAnalysisEval(SyncState
* const syncState
)
527 AnalysisDataEval
* analysisData
;
529 analysisData
= (AnalysisDataEval
*) syncState
->analysisData
;
531 if (analysisData
== NULL
)
536 g_hash_table_destroy(analysisData
->rttInfo
);
538 if (syncState
->stats
)
540 AnalysisStatsEval
* stats
= analysisData
->stats
;
542 for (i
= 0; i
< syncState
->traceNb
; i
++)
544 free(stats
->messageStats
[i
]);
546 free(stats
->messageStats
);
548 g_hash_table_destroy(stats
->exchangeRtt
);
551 freeAllFactors(syncState
->traceNb
, stats
->chFactorsArray
);
552 freeAllFactors(syncState
->traceNb
, stats
->lpFactorsArray
);
558 if (syncState
->graphsStream
)
560 AnalysisGraphsEval
* graphs
= analysisData
->graphs
;
562 if (graphs
->histograms
)
564 g_hash_table_destroy(graphs
->histograms
);
567 for (i
= 0; i
< syncState
->traceNb
; i
++)
569 free(graphs
->bounds
[i
]);
571 free(graphs
->bounds
);
574 for (i
= 0; i
< syncState
->traceNb
; i
++)
576 for (j
= 0; j
< i
; j
++)
578 // There seems to be a memory leak in glpk, valgrind reports a
579 // loss even if the problem is deleted
580 glp_delete_prob(graphs
->lps
[i
][j
]);
582 free(graphs
->lps
[i
]);
586 if (!syncState
->stats
)
588 freeAllFactors(syncState
->traceNb
, graphs
->lpFactorsArray
);
595 if (syncState
->stats
|| syncState
->graphsStream
)
597 analysisData
->chullSS
->analysisModule
->destroyAnalysis(analysisData
->chullSS
);
598 free(analysisData
->chullSS
);
601 free(syncState
->analysisData
);
602 syncState
->analysisData
= NULL
;
607 * Perform analysis on an event pair.
609 * Check if there is message inversion or messages that are too fast.
612 * syncState container for synchronization data
613 * message structure containing the events
615 static void analyzeMessageEval(SyncState
* const syncState
, Message
* const
618 AnalysisDataEval
* analysisData
= syncState
->analysisData
;
619 MessageStats
* messageStats
=
620 &analysisData
->stats
->messageStats
[message
->outE
->traceNum
][message
->inE
->traceNum
];
623 struct RttKey rttKey
;
625 g_assert(message
->inE
->type
== TCP
);
627 if (syncState
->stats
)
629 messageStats
->total
++;
632 tt
= wallTimeSub(&message
->inE
->wallTime
, &message
->outE
->wallTime
);
635 if (syncState
->stats
)
637 messageStats
->inversionNb
++;
640 else if (syncState
->graphsStream
)
642 struct RttKey rttKey
= {
643 .saddr
=MIN(message
->inE
->event
.tcpEvent
->segmentKey
->connectionKey
.saddr
,
644 message
->inE
->event
.tcpEvent
->segmentKey
->connectionKey
.daddr
),
645 .daddr
=MAX(message
->inE
->event
.tcpEvent
->segmentKey
->connectionKey
.saddr
,
646 message
->inE
->event
.tcpEvent
->segmentKey
->connectionKey
.daddr
),
648 AnalysisHistogramEval
* histogram
=
649 g_hash_table_lookup(analysisData
->graphs
->histograms
, &rttKey
);
651 if (histogram
== NULL
)
653 struct RttKey
* tableKey
= malloc(sizeof(*tableKey
));
655 histogram
= constructAnalysisHistogramEval(syncState
->graphsDir
, &rttKey
);
656 memcpy(tableKey
, &rttKey
, sizeof(*tableKey
));
657 g_hash_table_insert(analysisData
->graphs
->histograms
, tableKey
, histogram
);
660 if (message
->inE
->event
.udpEvent
->datagramKey
->saddr
<
661 message
->inE
->event
.udpEvent
->datagramKey
->daddr
)
663 hitBin(&histogram
->ttSendBins
, tt
);
667 hitBin(&histogram
->ttRecvBins
, tt
);
671 if (syncState
->stats
)
674 message
->inE
->event
.tcpEvent
->segmentKey
->connectionKey
.saddr
;
676 message
->inE
->event
.tcpEvent
->segmentKey
->connectionKey
.daddr
;
677 rtt
= g_hash_table_lookup(analysisData
->rttInfo
, &rttKey
);
678 g_debug("rttInfo, looking up (%u, %u)->(%f)", rttKey
.saddr
,
679 rttKey
.daddr
, rtt
? *rtt
: NAN
);
683 g_debug("rttInfo, tt: %f rtt / 2: %f", tt
, *rtt
/ 2.);
686 messageStats
->tooFastNb
++;
691 messageStats
->noRTTInfoNb
++;
695 if (syncState
->graphsStream
)
697 updateBounds(analysisData
->graphs
->bounds
, message
->inE
,
701 if (syncState
->stats
|| syncState
->graphsStream
)
703 analysisData
->chullSS
->analysisModule
->analyzeMessage(analysisData
->chullSS
,
710 * Perform analysis on multiple messages
715 * syncState container for synchronization data
716 * exchange structure containing the messages
718 static void analyzeExchangeEval(SyncState
* const syncState
, Exchange
* const
721 AnalysisDataEval
* analysisData
= syncState
->analysisData
;
722 Message
* m1
= g_queue_peek_tail(exchange
->acks
);
723 Message
* m2
= exchange
->message
;
724 struct RttKey
* rttKey
;
725 double* rtt
, * exchangeRtt
;
727 g_assert(m1
->inE
->type
== TCP
);
729 // (T2 - T1) - (T3 - T4)
730 rtt
= malloc(sizeof(double));
731 *rtt
= wallTimeSub(&m1
->inE
->wallTime
, &m1
->outE
->wallTime
) -
732 wallTimeSub(&m2
->outE
->wallTime
, &m2
->inE
->wallTime
);
734 rttKey
= malloc(sizeof(struct RttKey
));
736 MIN(m1
->inE
->event
.tcpEvent
->segmentKey
->connectionKey
.saddr
,
737 m1
->inE
->event
.tcpEvent
->segmentKey
->connectionKey
.daddr
);
739 MAX(m1
->inE
->event
.tcpEvent
->segmentKey
->connectionKey
.saddr
,
740 m1
->inE
->event
.tcpEvent
->segmentKey
->connectionKey
.daddr
);
742 if (syncState
->graphsStream
)
744 AnalysisHistogramEval
* histogram
=
745 g_hash_table_lookup(analysisData
->graphs
->histograms
, rttKey
);
747 if (histogram
== NULL
)
749 struct RttKey
* tableKey
= malloc(sizeof(*tableKey
));
751 histogram
= constructAnalysisHistogramEval(syncState
->graphsDir
,
753 memcpy(tableKey
, rttKey
, sizeof(*tableKey
));
754 g_hash_table_insert(analysisData
->graphs
->histograms
, tableKey
,
758 hitBin(&histogram
->hrttBins
, *rtt
/ 2);
761 if (syncState
->stats
)
763 exchangeRtt
= g_hash_table_lookup(analysisData
->stats
->exchangeRtt
,
768 if (*rtt
< *exchangeRtt
)
770 g_hash_table_replace(analysisData
->stats
->exchangeRtt
, rttKey
, rtt
);
780 g_hash_table_insert(analysisData
->stats
->exchangeRtt
, rttKey
, rtt
);
792 * Perform analysis on muliple events
794 * Sum the broadcast differential delays
797 * syncState container for synchronization data
798 * broadcast structure containing the events
800 static void analyzeBroadcastEval(SyncState
* const syncState
, Broadcast
* const
803 AnalysisDataEval
* analysisData
= syncState
->analysisData
;
805 if (syncState
->stats
)
807 double sum
= 0, squaresSum
= 0;
810 g_queue_foreach(broadcast
->events
, &gfSum
, &sum
);
811 g_queue_foreach(broadcast
->events
, &gfSumSquares
, &squaresSum
);
813 analysisData
->stats
->broadcastNb
++;
814 // Because of numerical errors, this can at times be < 0
815 y
= squaresSum
/ g_queue_get_length(broadcast
->events
) - pow(sum
/
816 g_queue_get_length(broadcast
->events
), 2.);
819 analysisData
->stats
->broadcastDiffSum
+= sqrt(y
);
823 if (syncState
->graphsStream
)
827 unsigned int eventNb
= broadcast
->events
->length
;
829 events
= g_array_sized_new(FALSE
, FALSE
, sizeof(Event
*), eventNb
);
830 g_queue_foreach(broadcast
->events
, &gfAddEventToArray
, events
);
832 for (i
= 0; i
< eventNb
; i
++)
834 for (j
= 0; j
< eventNb
; j
++)
836 Event
* eventI
= g_array_index(events
, Event
*, i
), * eventJ
=
837 g_array_index(events
, Event
*, j
);
839 if (eventI
->traceNum
< eventJ
->traceNum
)
841 updateBounds(analysisData
->graphs
->bounds
, eventI
, eventJ
);
846 g_array_free(events
, TRUE
);
852 * Finalize the factor calculations. Since this module does not really
853 * calculate factors, identity factors are returned. Instead, histograms are
854 * written out and histogram structures are freed.
857 * syncState container for synchronization data.
860 * Factors[traceNb] identity factors for each trace
862 static GArray
* finalizeAnalysisEval(SyncState
* const syncState
)
866 AnalysisDataEval
* analysisData
= syncState
->analysisData
;
868 if (syncState
->graphsStream
&& analysisData
->graphs
->histograms
)
870 g_hash_table_foreach(analysisData
->graphs
->histograms
,
871 &ghfWriteHistogram
, &(struct WriteHistogramInfo
) {.rttInfo
=
872 analysisData
->rttInfo
, .graphsStream
= syncState
->graphsStream
});
873 g_hash_table_destroy(analysisData
->graphs
->histograms
);
874 analysisData
->graphs
->histograms
= NULL
;
877 finalizeAnalysisEvalLP(syncState
);
879 factors
= g_array_sized_new(FALSE
, FALSE
, sizeof(Factors
),
881 g_array_set_size(factors
, syncState
->traceNb
);
882 for (i
= 0; i
< syncState
->traceNb
; i
++)
886 e
= &g_array_index(factors
, Factors
, i
);
896 * Print statistics related to analysis. Must be called after
900 * syncState container for synchronization data.
902 static void printAnalysisStatsEval(SyncState
* const syncState
)
904 AnalysisDataEval
* analysisData
;
905 unsigned int i
, j
, k
;
906 unsigned int totInversion
= 0, totTooFast
= 0, totNoInfo
= 0, totTotal
= 0;
909 if (!syncState
->stats
)
914 analysisData
= (AnalysisDataEval
*) syncState
->analysisData
;
916 printf("Synchronization evaluation analysis stats:\n");
917 if (analysisData
->stats
->broadcastNb
)
919 printf("\tsum of broadcast differential delays: %g\n",
920 analysisData
->stats
->broadcastDiffSum
);
921 printf("\taverage broadcast differential delay: %g\n",
922 analysisData
->stats
->broadcastDiffSum
/
923 analysisData
->stats
->broadcastNb
);
926 printf("\tIndividual evaluation:\n"
927 "\t\tTrace pair Inversions Too fast No RTT info Total\n");
929 for (i
= 0; i
< syncState
->traceNb
; i
++)
931 for (j
= i
+ 1; j
< syncState
->traceNb
; j
++)
933 MessageStats
* messageStats
;
941 for (k
= 0; k
< sizeof(loopValues
) / sizeof(*loopValues
); k
++)
944 &analysisData
->stats
->messageStats
[loopValues
[k
].t1
][loopValues
[k
].t2
];
946 printf("\t\t%3d - %-3d ", loopValues
[k
].t1
, loopValues
[k
].t2
);
947 printf("%u (%u%%)%n", messageStats
->inversionNb
, (unsigned
948 int) ceil((double) messageStats
->inversionNb
/
949 messageStats
->total
* 100), &charNb
);
950 printf("%*s", 17 - charNb
> 0 ? 17 - charNb
+ 1: 1, " ");
951 printf("%u (%u%%)%n", messageStats
->tooFastNb
, (unsigned int)
952 ceil((double) messageStats
->tooFastNb
/
953 messageStats
->total
* 100), &charNb
);
954 printf("%*s%-10u %u\n", 17 - charNb
> 0 ? 17 - charNb
+ 1:
955 1, " ", messageStats
->noRTTInfoNb
, messageStats
->total
);
957 totInversion
+= messageStats
->inversionNb
;
958 totTooFast
+= messageStats
->tooFastNb
;
959 totNoInfo
+= messageStats
->noRTTInfoNb
;
960 totTotal
+= messageStats
->total
;
965 printf("\t\t total ");
966 printf("%u (%u%%)%n", totInversion
, (unsigned int) ceil((double)
967 totInversion
/ totTotal
* 100), &charNb
);
968 printf("%*s", 17 - charNb
> 0 ? 17 - charNb
+ 1: 1, " ");
969 printf("%u (%u%%)%n", totTooFast
, (unsigned int) ceil((double) totTooFast
970 / totTotal
* 100), &charNb
);
971 printf("%*s%-10u %u\n", 17 - charNb
> 0 ? 17 - charNb
+ 1: 1, " ",
972 totNoInfo
, totTotal
);
974 printf("\tRound-trip times:\n"
975 "\t\tHost pair RTT from exchanges RTTs from file (ms)\n");
976 g_hash_table_foreach(analysisData
->stats
->exchangeRtt
,
977 &ghfPrintExchangeRtt
, analysisData
->rttInfo
);
979 printf("\tConvex hull factors comparisons:\n"
980 "\t\tTrace pair Factors type Differences (lp - chull)\n"
982 "\t\t Min Max Min Max\n");
984 for (i
= 0; i
< syncState
->traceNb
; i
++)
986 for (j
= 0; j
< i
; j
++)
988 FactorsCHull
* chFactors
= &analysisData
->stats
->chFactorsArray
[i
][j
];
989 FactorsCHull
* lpFactors
= &analysisData
->stats
->lpFactorsArray
[i
][j
];
991 printf("\t\t%3d - %-3d ", i
, j
);
992 if (lpFactors
->type
== chFactors
->type
)
994 if (lpFactors
->type
== MIDDLE
)
996 printf("%-13s %-10.4g %-10.4g %-10.4g %.4g\n",
997 approxNames
[lpFactors
->type
],
998 lpFactors
->min
->offset
- chFactors
->min
->offset
,
999 lpFactors
->max
->offset
- chFactors
->max
->offset
,
1000 lpFactors
->min
->drift
- chFactors
->min
->drift
,
1001 lpFactors
->max
->drift
- chFactors
->max
->drift
);
1003 else if (lpFactors
->type
== ABSENT
)
1005 printf("%s\n", approxNames
[lpFactors
->type
]);
1010 printf("Different! %s and %s\n", approxNames
[lpFactors
->type
],
1011 approxNames
[chFactors
->type
]);
1019 * A GHFunc for g_hash_table_foreach()
1022 * key: RttKey* where saddr < daddr
1023 * value: double*, RTT estimated from exchanges
1024 * user_data GHashTable* rttInfo
1026 static void ghfPrintExchangeRtt(gpointer key
, gpointer value
, gpointer
1029 char addr1
[16], addr2
[16];
1030 struct RttKey
* rttKey1
= key
;
1031 struct RttKey rttKey2
= {rttKey1
->daddr
, rttKey1
->saddr
};
1032 double* fileRtt1
, *fileRtt2
;
1033 GHashTable
* rttInfo
= user_data
;
1035 convertIP(addr1
, rttKey1
->saddr
);
1036 convertIP(addr2
, rttKey1
->daddr
);
1038 fileRtt1
= g_hash_table_lookup(rttInfo
, rttKey1
);
1039 fileRtt2
= g_hash_table_lookup(rttInfo
, &rttKey2
);
1041 printf("\t\t(%15s, %-15s) %-18.3f ", addr1
, addr2
, *(double*) value
* 1e3
);
1043 if (fileRtt1
|| fileRtt2
)
1047 printf("%.3f", *fileRtt1
* 1e3
);
1049 if (fileRtt1
&& fileRtt2
)
1055 printf("%.3f", *fileRtt2
* 1e3
);
1067 * A GHashFunc for g_hash_table_new()
1070 * key struct RttKey*
1072 static guint
ghfRttKeyHash(gconstpointer key
)
1074 struct RttKey
* rttKey
;
1077 rttKey
= (struct RttKey
*) key
;
1089 * A GDestroyNotify function for g_hash_table_new_full()
1092 * data: struct RttKey*
1094 static void gdnDestroyRttKey(gpointer data
)
1101 * A GDestroyNotify function for g_hash_table_new_full()
1106 static void gdnDestroyDouble(gpointer data
)
1113 * A GEqualFunc for g_hash_table_new()
1119 * TRUE if both values are equal
1121 static gboolean
gefRttKeyEqual(gconstpointer a
, gconstpointer b
)
1123 const struct RttKey
* rkA
, * rkB
;
1125 rkA
= (struct RttKey
*) a
;
1126 rkB
= (struct RttKey
*) b
;
1128 if (rkA
->saddr
== rkB
->saddr
&& rkA
->daddr
== rkB
->daddr
)
1140 * Read a file contain minimum round trip time values and fill an array with
1141 * them. The file is formatted as such:
1142 * <host1 IP> <host2 IP> <RTT in milliseconds>
1143 * ip's should be in dotted quad format
1146 * rttInfo: double* rttInfo[RttKey], empty table, will be filled
1147 * rttStream: stream from which to read
1149 static void readRttInfo(GHashTable
* rttInfo
, FILE* rttStream
)
1155 positionStream(rttStream
);
1156 retval
= getline(&line
, &len
, rttStream
);
1157 while(!feof(rttStream
))
1159 struct RttKey
* rttKey
;
1160 char saddrDQ
[20], daddrDQ
[20];
1163 struct in_addr addr
;
1169 {saddrDQ
, offsetof(struct RttKey
, saddr
)},
1170 {daddrDQ
, offsetof(struct RttKey
, daddr
)}
1173 if (retval
== -1 && !feof(rttStream
))
1175 g_error(strerror(errno
));
1178 if (line
[retval
- 1] == '\n')
1180 line
[retval
- 1]= '\0';
1183 rtt
= malloc(sizeof(double));
1184 retval
= sscanf(line
, " %19s %19s %lf %c", saddrDQ
, daddrDQ
, rtt
,
1188 g_error(strerror(errno
));
1190 else if (retval
!= 3)
1192 g_error("Error parsing RTT file, line was '%s'", line
);
1195 rttKey
= malloc(sizeof(struct RttKey
));
1196 for (i
= 0; i
< sizeof(loopValues
) / sizeof(*loopValues
); i
++)
1198 retval
= inet_aton(loopValues
[i
].dq
, &addr
);
1201 g_error("Error converting address '%s'", loopValues
[i
].dq
);
1203 *(uint32_t*) ((void*) rttKey
+ loopValues
[i
].offset
)=
1208 g_debug("rttInfo, Inserting (%u, %u)->(%f)", rttKey
->saddr
,
1209 rttKey
->daddr
, *rtt
);
1210 g_hash_table_insert(rttInfo
, rttKey
, rtt
);
1212 positionStream(rttStream
);
1213 retval
= getline(&line
, &len
, rttStream
);
1224 * Advance stream over empty space, empty lines and lines that begin with '#'
1227 * stream: stream, at exit, will be over the first non-empty character
1228 * of a line of be at EOF
1230 static void positionStream(FILE* stream
)
1239 firstChar
= fgetc(stream
);
1240 if (firstChar
== (int) '#')
1242 retval
= getline(&line
, &len
, stream
);
1251 g_error(strerror(errno
));
1255 else if (firstChar
== (int) '\n' || firstChar
== (int) ' ' ||
1256 firstChar
== (int) '\t')
1258 else if (firstChar
== EOF
)
1267 retval
= ungetc(firstChar
, stream
);
1270 g_error("Error: ungetc()");
1282 * A GFunc for g_queue_foreach()
1285 * data Event*, a UDP broadcast event
1286 * user_data double*, the running sum
1289 * Adds the time of the event to the sum
1291 static void gfSum(gpointer data
, gpointer userData
)
1293 Event
* event
= (Event
*) data
;
1295 *(double*) userData
+= event
->wallTime
.seconds
+ event
->wallTime
.nanosec
/
1301 * A GFunc for g_queue_foreach()
1304 * data Event*, a UDP broadcast event
1305 * user_data double*, the running sum
1308 * Adds the square of the time of the event to the sum
1310 static void gfSumSquares(gpointer data
, gpointer userData
)
1312 Event
* event
= (Event
*) data
;
1314 *(double*) userData
+= pow(event
->wallTime
.seconds
+ event
->wallTime
.nanosec
1320 * Update a struct Bins according to a new value
1323 * bins: the structure containing bins to build a histrogram
1324 * value: the new value
1326 static void hitBin(struct Bins
* const bins
, const double value
)
1328 unsigned int binN
= binNum(value
);
1330 if (binN
< bins
->min
)
1334 else if (binN
> bins
->max
)
1346 * Figure out the bin in a histogram to which a value belongs.
1348 * This uses exponentially sized bins that go from 0 to infinity.
1351 * value: in the range -INFINITY to INFINITY
1354 * The number of the bin in a struct Bins.bin
1356 static unsigned int binNum(const double value
)
1362 else if (value
< binEnd(1))
1366 else if (value
>= binStart(BIN_NB
- 1))
1372 return floor(log(value
) / log(binBase
)) + BIN_NB
+ 1;
1378 * Figure out the start of the interval of a bin in a histogram. See struct
1381 * This uses exponentially sized bins that go from 0 to infinity.
1384 * binNum: bin number
1387 * The start of the interval, this value is included in the interval (except
1388 * for -INFINITY, naturally)
1390 static double binStart(const unsigned int binNum
)
1392 g_assert_cmpuint(binNum
, <, BIN_NB
);
1398 else if (binNum
== 1)
1404 return pow(binBase
, (double) binNum
- BIN_NB
+ 1);
1410 * Figure out the end of the interval of a bin in a histogram. See struct
1413 * This uses exponentially sized bins that go from 0 to infinity.
1416 * binNum: bin number
1419 * The end of the interval, this value is not included in the interval
1421 static double binEnd(const unsigned int binNum
)
1423 g_assert_cmpuint(binNum
, <, BIN_NB
);
1429 else if (binNum
< BIN_NB
- 1)
1431 return pow(binBase
, (double) binNum
- BIN_NB
+ 2);
1441 * Return the total number of elements in the "normal" bins (not underflow or
1445 * bins: the structure containing bins to build a histrogram
1447 static uint32_t normalTotal(struct Bins
* const bins
)
1449 return bins
->total
- bins
->bin
[0] - bins
->bin
[BIN_NB
- 1];
1453 /* Update the bounds between two traces
1456 * bounds: the array containing all the trace-pair bounds
1457 * e1, e2: the two related events
1459 static void updateBounds(Bounds
** const bounds
, Event
* const e1
, Event
* const e2
)
1461 unsigned int traceI
, traceJ
;
1462 uint64_t messageTime
;
1465 if (e1
->traceNum
< e2
->traceNum
)
1467 traceI
= e2
->traceNum
;
1468 traceJ
= e1
->traceNum
;
1469 messageTime
= e1
->cpuTime
;
1473 traceI
= e1
->traceNum
;
1474 traceJ
= e2
->traceNum
;
1475 messageTime
= e2
->cpuTime
;
1477 tpBounds
= &bounds
[traceI
][traceJ
];
1479 if (messageTime
< tpBounds
->min
)
1481 tpBounds
->min
= messageTime
;
1483 if (messageTime
> tpBounds
->max
)
1485 tpBounds
->max
= messageTime
;
1492 * Create the linear programming problem containing the constraints defined by
1493 * two half-hulls. The objective function and optimization directions are not
1497 * syncState: container for synchronization data
1498 * i: first trace number
1499 * j: second trace number, garanteed to be larger than i
1501 * A new glp_prob*, this problem must be freed by the caller with
1504 static glp_prob
* lpCreateProblem(GQueue
* const lowerHull
, GQueue
* const upperHull
)
1508 const double zeroD
= 0.;
1509 glp_prob
* lp
= glp_create_prob();
1510 unsigned int hullPointNb
= g_queue_get_length(lowerHull
) +
1511 g_queue_get_length(upperHull
);
1512 GArray
* iArray
= g_array_sized_new(FALSE
, FALSE
, sizeof(int), hullPointNb
+
1514 GArray
* jArray
= g_array_sized_new(FALSE
, FALSE
, sizeof(int), hullPointNb
+
1516 GArray
* aArray
= g_array_sized_new(FALSE
, FALSE
, sizeof(double),
1520 struct LPAddRowInfo rowInfo
;
1522 {lowerHull
, {lp
, GLP_UP
, iArray
, jArray
, aArray
}},
1523 {upperHull
, {lp
, GLP_LO
, iArray
, jArray
, aArray
}},
1526 // Create the LP problem
1527 glp_term_out(GLP_OFF
);
1528 glp_add_rows(lp
, hullPointNb
);
1529 glp_add_cols(lp
, 2);
1531 glp_set_col_name(lp
, 1, "a0");
1532 glp_set_col_bnds(lp
, 1, GLP_FR
, 0., 0.);
1533 glp_set_col_name(lp
, 2, "a1");
1534 glp_set_col_bnds(lp
, 2, GLP_LO
, 0., 0.);
1536 // Add row constraints
1537 g_array_append_val(iArray
, zero
);
1538 g_array_append_val(jArray
, zero
);
1539 g_array_append_val(aArray
, zeroD
);
1541 for (it
= 0; it
< sizeof(loopValues
) / sizeof(*loopValues
); it
++)
1543 g_queue_foreach(loopValues
[it
].hull
, &gfLPAddRow
,
1544 &loopValues
[it
].rowInfo
);
1547 g_assert_cmpuint(iArray
->len
, ==, jArray
->len
);
1548 g_assert_cmpuint(jArray
->len
, ==, aArray
->len
);
1549 g_assert_cmpuint(aArray
->len
- 1, ==, hullPointNb
* 2);
1551 glp_load_matrix(lp
, aArray
->len
- 1, &g_array_index(iArray
, int, 0),
1552 &g_array_index(jArray
, int, 0), &g_array_index(aArray
, double, 0));
1554 glp_scale_prob(lp
, GLP_SF_AUTO
);
1556 g_array_free(iArray
, TRUE
);
1557 g_array_free(jArray
, TRUE
);
1558 g_array_free(aArray
, TRUE
);
1565 * A GFunc for g_queue_foreach(). Add constraints and bounds for one row.
1568 * data Point*, synchronization point for which to add an LP row
1570 * user_data LPAddRowInfo*
1572 static void gfLPAddRow(gpointer data
, gpointer user_data
)
1575 struct LPAddRowInfo
* rowInfo
= user_data
;
1577 double constraints
[2];
1579 indexes
[0]= g_array_index(rowInfo
->iArray
, int, rowInfo
->iArray
->len
- 1) + 1;
1580 indexes
[1]= indexes
[0];
1582 if (rowInfo
->boundType
== GLP_UP
)
1584 glp_set_row_bnds(rowInfo
->lp
, indexes
[0], GLP_UP
, 0., p
->y
);
1586 else if (rowInfo
->boundType
== GLP_LO
)
1588 glp_set_row_bnds(rowInfo
->lp
, indexes
[0], GLP_LO
, p
->y
, 0.);
1592 g_assert_not_reached();
1595 g_array_append_vals(rowInfo
->iArray
, indexes
, 2);
1598 g_array_append_vals(rowInfo
->jArray
, indexes
, 2);
1600 constraints
[1]= p
->x
;
1601 g_array_append_vals(rowInfo
->aArray
, constraints
, 2);
1606 * Calculate min or max correction factors (as possible) using an LP problem.
1609 * lp: A linear programming problem with constraints and bounds
1611 * direction: The type of factors desired. Use GLP_MAX for max
1612 * approximation factors (a1, the drift or slope is the
1613 * largest) and GLP_MIN in the other case.
1616 * If the calculation was successful, a new Factors struct. Otherwise, NULL.
1617 * The calculation will fail if the hull assumptions are not respected.
1619 static Factors
* calculateFactors(glp_prob
* const lp
, const int direction
)
1624 glp_set_obj_coef(lp
, 1, 0.);
1625 glp_set_obj_coef(lp
, 2, 1.);
1627 glp_set_obj_dir(lp
, direction
);
1628 retval
= glp_simplex(lp
, NULL
);
1629 status
= glp_get_status(lp
);
1631 if (retval
== 0 && status
== GLP_OPT
)
1633 factors
= malloc(sizeof(Factors
));
1634 factors
->offset
= glp_get_col_prim(lp
, 1);
1635 factors
->drift
= glp_get_col_prim(lp
, 2);
1647 * Calculate min, max and approx correction factors (as possible) using an LP
1651 * lp: A linear programming problem with constraints and bounds
1655 * Please note that the approximation type may be MIDDLE, INCOMPLETE or
1656 * ABSENT. Unlike in analysis_chull, ABSENT is also used when the hulls do
1657 * not respect assumptions.
1659 static void calculateCompleteFactors(glp_prob
* const lp
, FactorsCHull
* factors
)
1661 factors
->min
= calculateFactors(lp
, GLP_MIN
);
1662 factors
->max
= calculateFactors(lp
, GLP_MAX
);
1664 if (factors
->min
&& factors
->max
)
1666 factors
->type
= MIDDLE
;
1667 calculateFactorsMiddle(factors
);
1669 else if (factors
->min
|| factors
->max
)
1671 factors
->type
= INCOMPLETE
;
1672 factors
->approx
= NULL
;
1676 factors
->type
= ABSENT
;
1677 factors
->approx
= NULL
;
1683 * Create and initialize an array like AnalysisStatsCHull.allFactors
1686 * traceNb: number of traces
1689 * A new array, which can be freed with freeAllFactors()
1691 static FactorsCHull
** createAllFactors(const unsigned int traceNb
)
1693 FactorsCHull
** factorsArray
;
1696 factorsArray
= malloc(traceNb
* sizeof(FactorsCHull
*));
1697 for (i
= 0; i
< traceNb
; i
++)
1699 factorsArray
[i
]= calloc((i
+ 1), sizeof(FactorsCHull
));
1701 factorsArray
[i
][i
].type
= EXACT
;
1702 factorsArray
[i
][i
].approx
= malloc(sizeof(Factors
));
1703 factorsArray
[i
][i
].approx
->drift
= 1.;
1704 factorsArray
[i
][i
].approx
->offset
= 0.;
1707 return factorsArray
;
1713 * Compute synchronization factors using a linear programming approach.
1714 * Compute the factors using analysis_chull. Compare the two.
1716 * There are two definitions of this function. The empty one is used when the
1717 * solver library, glpk, is not available at build time. In that case, nothing
1718 * is actually produced.
1721 * syncState: container for synchronization data
1723 #ifndef HAVE_LIBGLPK
1724 static inline void finalizeAnalysisEvalLP(SyncState
* const syncState
)
1728 static void finalizeAnalysisEvalLP(SyncState
* const syncState
)
1731 AnalysisDataEval
* analysisData
= syncState
->analysisData
;
1732 AnalysisDataCHull
* chAnalysisData
= analysisData
->chullSS
->analysisData
;
1733 FactorsCHull
** lpFactorsArray
= createAllFactors(syncState
->traceNb
);
1734 FactorsCHull
* lpFactors
;
1736 if (!syncState
->stats
&& !syncState
->graphsStream
)
1741 if ((syncState
->graphsStream
&& analysisData
->graphs
->lps
!= NULL
) ||
1742 (syncState
->stats
&& analysisData
->stats
->chFactorsArray
!= NULL
))
1747 if (syncState
->stats
)
1749 analysisData
->stats
->chFactorsArray
=
1750 calculateAllFactors(analysisData
->chullSS
);
1751 analysisData
->stats
->lpFactorsArray
= lpFactorsArray
;
1754 if (syncState
->graphsStream
)
1756 analysisData
->graphs
->lps
= malloc(syncState
->traceNb
*
1757 sizeof(glp_prob
**));
1758 for (i
= 0; i
< syncState
->traceNb
; i
++)
1760 analysisData
->graphs
->lps
[i
]= malloc(i
* sizeof(glp_prob
*));
1762 analysisData
->graphs
->lpFactorsArray
= lpFactorsArray
;
1765 for (i
= 0; i
< syncState
->traceNb
; i
++)
1767 for (j
= 0; j
< i
; j
++)
1771 // Create the LP problem
1772 lp
= lpCreateProblem(chAnalysisData
->hullArray
[i
][j
],
1773 chAnalysisData
->hullArray
[j
][i
]);
1775 // Use the LP problem to find the correction factors for this pair of
1777 calculateCompleteFactors(lp
, &lpFactorsArray
[i
][j
]);
1779 if (syncState
->graphsStream
)
1781 analysisData
->graphs
->lps
[i
][j
]= lp
;
1785 glp_delete_prob(lp
);
1786 destroyFactorsCHull(lpFactors
);
1791 g_array_free(analysisData
->chullSS
->analysisModule
->finalizeAnalysis(analysisData
->chullSS
),
1798 * Compute synchronization accuracy information using a linear programming
1799 * approach. Write the neccessary data files and plot lines in the gnuplot
1802 * There are two definitions of this function. The empty one is used when the
1803 * solver library, glpk, is not available at build time. In that case, nothing
1804 * is actually produced.
1807 * syncState: container for synchronization data
1808 * i: first trace number
1809 * j: second trace number, garanteed to be larger than i
1811 #ifndef HAVE_LIBGLPK
1812 static inline void writeAccuracyGraphs(SyncState
* const syncState
, const unsigned int
1813 i
, const unsigned int j
)
1817 static void writeAccuracyGraphs(SyncState
* const syncState
, const unsigned int
1818 i
, const unsigned int j
)
1821 AnalysisDataEval
* analysisData
= syncState
->analysisData
;
1822 AnalysisGraphsEval
* graphs
= analysisData
->graphs
;
1823 GQueue
*** hullArray
= ((AnalysisDataCHull
*)
1824 analysisData
->chullSS
->analysisData
)->hullArray
;
1825 FactorsCHull
* lpFactors
= &graphs
->lpFactorsArray
[j
][i
];
1826 glp_prob
* lp
= graphs
->lps
[j
][i
];
1828 if (lpFactors
->type
== MIDDLE
)
1835 unsigned int xBegin
, xEnd
;
1837 const unsigned int graphPointNb
= 1000;
1839 // Open the data file
1840 snprintf(fileName
, 40, "analysis_eval_accuracy-%03u_and_%03u.data", i
, j
);
1841 fileName
[sizeof(fileName
) - 1]= '\0';
1843 cwd
= changeToGraphDir(syncState
->graphsDir
);
1845 if ((fp
= fopen(fileName
, "w")) == NULL
)
1847 g_error(strerror(errno
));
1849 fprintf(fp
, "#%-24s %-25s %-25s %-25s\n", "x", "middle", "min", "max");
1854 g_error(strerror(errno
));
1858 // Build the list of absisca values for the points in the accuracy graph
1859 g_assert_cmpuint(graphPointNb
, >=, 4);
1860 xValues
= malloc(graphPointNb
* sizeof(double));
1861 xValues
[0]= graphs
->bounds
[j
][i
].min
;
1862 xValues
[graphPointNb
- 1]= graphs
->bounds
[j
][i
].max
;
1863 xValues
[1]= MIN(((Point
*) g_queue_peek_head(hullArray
[i
][j
]))->x
,
1864 ((Point
*) g_queue_peek_head(hullArray
[j
][i
]))->x
);
1865 xValues
[graphPointNb
- 2]= MAX(((Point
*)
1866 g_queue_peek_tail(hullArray
[i
][j
]))->x
, ((Point
*)
1867 g_queue_peek_tail(hullArray
[j
][i
]))->x
);
1869 if (xValues
[0] == xValues
[1])
1877 if (xValues
[graphPointNb
- 2] == xValues
[graphPointNb
- 1])
1879 xEnd
= graphPointNb
- 1;
1883 xEnd
= graphPointNb
- 2;
1885 interval
= (xValues
[xEnd
] - xValues
[xBegin
]) / (graphPointNb
- 1);
1887 for (it
= xBegin
; it
<= xEnd
; it
++)
1889 xValues
[it
]= xValues
[xBegin
] + interval
* (it
- xBegin
);
1892 /* For each absisca value and each optimisation direction, solve the LP
1893 * and write a line in the data file */
1894 for (it
= 0; it
< graphPointNb
; it
++)
1897 int directions
[]= {GLP_MIN
, GLP_MAX
};
1899 glp_set_obj_coef(lp
, 1, 1.);
1900 glp_set_obj_coef(lp
, 2, xValues
[it
]);
1902 fprintf(fp
, "%25.9f %25.9f", xValues
[it
], lpFactors
->approx
->offset
1903 + lpFactors
->approx
->drift
* xValues
[it
]);
1904 for (it2
= 0; it2
< sizeof(directions
) / sizeof(*directions
); it2
++)
1908 glp_set_obj_dir(lp
, directions
[it2
]);
1909 retval
= glp_simplex(lp
, NULL
);
1910 status
= glp_get_status(lp
);
1912 g_assert(retval
== 0 && status
== GLP_OPT
);
1913 fprintf(fp
, " %25.9f", glp_get_obj_val(lp
));
1921 fprintf(syncState
->graphsStream
,
1922 "\t\"analysis_eval_accuracy-%1$03u_and_%2$03u.data\" "
1923 "using 1:(($3 - $2) / clock_freq_%2$u):(($4 - $2) / clock_freq_%2$u) "
1924 "title \"Synchronization accuracy\" "
1925 "with filledcurves linewidth 2 linetype 1 "
1926 "linecolor rgb \"black\" fill solid 0.25 noborder, \\\n", i
,
1934 * Write the analysis-specific graph lines in the gnuplot script.
1937 * syncState: container for synchronization data
1938 * i: first trace number
1939 * j: second trace number, garanteed to be larger than i
1941 static void writeAnalysisTraceTimePlotsEval(SyncState
* const syncState
, const
1942 unsigned int i
, const unsigned int j
)
1944 AnalysisDataEval
* analysisData
= syncState
->analysisData
;
1945 AnalysisGraphsEval
* graphs
= analysisData
->graphs
;
1946 GQueue
*** hullArray
= ((AnalysisDataCHull
*)
1947 analysisData
->chullSS
->analysisData
)->hullArray
;
1949 printf("Between %u and %u:\n", i
, j
);
1950 printf("\tbounds min %llu max %llu\n", graphs
->bounds
[j
][i
].min
,
1951 graphs
->bounds
[j
][i
].max
);
1952 printf("\tnumber of points in lower half-hull %u upper half-hull %u\n",
1953 g_queue_get_length(hullArray
[j
][i
]),
1954 g_queue_get_length(hullArray
[i
][j
]));
1956 writeAccuracyGraphs(syncState
, i
, j
);
1960 static void writeAnalysisTraceTracePlotsEval(SyncState
* const syncState
, const
1961 unsigned int i
, const unsigned int j
)
1963 AnalysisDataEval
* analysisData
= syncState
->analysisData
;
1966 fprintf(syncState
->graphsStream
,
1967 "\t\"analysis_eval_accuracy-%1$03u_and_%2$03u.data\" "
1969 "title \"Synchronization accuracy\" "
1970 "with filledcurves linewidth 2 linetype 1 "
1971 "linecolor rgb \"black\" fill solid 0.25 noborder, \\\n", i
, j
);
1974 analysisData
->chullSS
->analysisModule
->graphFunctions
.writeTraceTracePlots(analysisData
->chullSS
,