Change synchronization code license to LGPLv2.1
[lttv.git] / lttv / lttv / sync / event_analysis_chull.c
index 41fdbca4c7764a45d19ed463232f8199c7bab709..8c7c54ddb82adab9025e29a72354e2cf0cd54d90 100644 (file)
@@ -1,19 +1,18 @@
 /* This file is part of the Linux Trace Toolkit viewer
- * Copyright (C) 2009 Benjamin Poirier <benjamin.poirier@polymtl.ca>
+ * Copyright (C) 2009, 2010 Benjamin Poirier <benjamin.poirier@polymtl.ca>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License Version 2 as
- * published by the Free Software Foundation;
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 2.1 of the License, or (at
+ * your option) any later version.
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ * License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
- * MA 02111-1307, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 #define _ISOC99_SOURCE
 
 #endif
 
 #include <errno.h>
+#include <inttypes.h>
 #include <math.h>
 #include <float.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 #include <unistd.h>
 
 #include "sync_chain.h"
 #include "event_analysis_chull.h"
 
 
-#ifndef g_info
-#define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
-#endif
-
-
 typedef enum
 {
        LOWER,
@@ -60,12 +56,10 @@ static void analyzeMessageCHull(SyncState* const syncState, Message* const
        message);
 static GArray* finalizeAnalysisCHull(SyncState* const syncState);
 static void printAnalysisStatsCHull(SyncState* const syncState);
-static void writeAnalysisGraphsPlotsCHull(FILE* stream, SyncState* const
-       syncState, const unsigned int i, const unsigned int j);
+static void writeAnalysisGraphsPlotsCHull(SyncState* const syncState, const
+       unsigned int i, const unsigned int j);
 
 // Functions specific to this module
-static void registerAnalysisCHull() __attribute__((constructor (101)));
-
 static void openGraphFiles(SyncState* const syncState);
 static void closeGraphFiles(SyncState* const syncState);
 static void writeGraphFiles(SyncState* const syncState);
@@ -77,10 +71,8 @@ static int jointCmp(const Point* const p1, const Point* const p2, const Point*
        const p3) __attribute__((pure));
 static double crossProductK(const Point const* p1, const Point const* p2,
        const Point const* p3, const Point const* p4) __attribute__((pure));
-static FactorsCHull** calculateAllFactors(SyncState* const syncState);
 static Factors* calculateFactorsExact(GQueue* const cu, GQueue* const cl, const
        LineType lineType) __attribute__((pure));
-static void calculateFactorsMiddle(FactorsCHull* factors);
 static void calculateFactorsFallback(GQueue* const cr, GQueue* const cs,
        FactorsCHull* const result);
 static double slope(const Point* const p1, const Point* const p2)
@@ -89,8 +81,6 @@ static double intercept(const Point* const p1, const Point* const p2)
        __attribute__((pure));
 static GArray* reduceFactors(SyncState* const syncState, FactorsCHull**
        allFactors);
-static void freeAllFactors(const SyncState* const syncState, FactorsCHull**
-       const allFactors);
 static double verticalDistance(Point* p1, Point* p2, Point* const point)
        __attribute__((pure));
 static void floydWarshall(SyncState* const syncState, FactorsCHull** const
@@ -108,19 +98,27 @@ static AnalysisModule analysisModuleCHull= {
        .initAnalysis= &initAnalysisCHull,
        .destroyAnalysis= &destroyAnalysisCHull,
        .analyzeMessage= &analyzeMessageCHull,
-       .analyzeExchange= NULL,
-       .analyzeBroadcast= NULL,
        .finalizeAnalysis= &finalizeAnalysisCHull,
        .printAnalysisStats= &printAnalysisStatsCHull,
-       .writeAnalysisGraphsPlots= &writeAnalysisGraphsPlotsCHull,
-       .writeAnalysisGraphsOptions= NULL,
+       .graphFunctions= {
+               .writeTraceTraceForePlots= &writeAnalysisGraphsPlotsCHull,
+       }
+};
+
+const char* const approxNames[]= {
+       [EXACT]= "Exact",
+       [MIDDLE]= "Middle",
+       [FALLBACK]= "Fallback",
+       [INCOMPLETE]= "Incomplete",
+       [ABSENT]= "Absent",
+       [SCREWED]= "Screwed",
 };
 
 
 /*
  * Analysis module registering function
  */
-static void registerAnalysisCHull()
+void registerAnalysisCHull()
 {
        g_queue_push_tail(&analysisModules, &analysisModuleCHull);
 }
@@ -167,7 +165,7 @@ static void initAnalysisCHull(SyncState* const syncState)
                analysisData->stats->allFactors= NULL;
        }
 
-       if (syncState->graphs)
+       if (syncState->graphsStream)
        {
                analysisData->graphsData= malloc(sizeof(AnalysisGraphsDataCHull));
                openGraphFiles(syncState);
@@ -193,7 +191,7 @@ static void openGraphFiles(SyncState* const syncState)
 
        analysisData= (AnalysisDataCHull*) syncState->analysisData;
 
-       cwd= changeToGraphDir(syncState->graphs);
+       cwd= changeToGraphsDir(syncState->graphsDir);
 
        analysisData->graphsData->hullPoints= malloc(syncState->traceNb *
                sizeof(FILE**));
@@ -270,7 +268,7 @@ static void gfDumpHullToFile(gpointer data, gpointer userData)
        Point* point;
 
        point= (Point*) data;
-       fprintf((FILE*) userData, "%20llu %20llu\n", point->x, point->y);
+       fprintf((FILE*) userData, "%20" PRIu64 " %20" PRIu64 "\n", point->x, point->y);
 }
 
 
@@ -342,6 +340,7 @@ static void destroyAnalysisCHull(SyncState* const syncState)
                for (j= 0; j < syncState->traceNb; j++)
                {
                        g_queue_foreach(analysisData->hullArray[i][j], gfPointDestroy, NULL);
+                       g_queue_free(analysisData->hullArray[i][j]);
                }
                free(analysisData->hullArray[i]);
        }
@@ -351,13 +350,13 @@ static void destroyAnalysisCHull(SyncState* const syncState)
        {
                if (analysisData->stats->allFactors != NULL)
                {
-                       freeAllFactors(syncState, analysisData->stats->allFactors);
+                       freeAllFactors(syncState->traceNb, analysisData->stats->allFactors);
                }
 
                free(analysisData->stats);
        }
 
-       if (syncState->graphs)
+       if (syncState->graphsStream)
        {
                if (analysisData->graphsData->hullPoints != NULL)
                {
@@ -366,7 +365,7 @@ static void destroyAnalysisCHull(SyncState* const syncState)
 
                if (!syncState->stats && analysisData->graphsData->allFactors != NULL)
                {
-                       freeAllFactors(syncState, analysisData->graphsData->allFactors);
+                       freeAllFactors(syncState->traceNb, analysisData->graphsData->allFactors);
                }
 
                free(analysisData->graphsData);
@@ -400,7 +399,8 @@ static void analyzeMessageCHull(SyncState* const syncState, Message* const messa
                newPoint->x= message->inE->cpuTime;
                newPoint->y= message->outE->cpuTime;
                hullType= UPPER;
-               g_debug("Reception point hullArray[%lu][%lu] x= inE->time= %llu y= outE->time= %llu",
+               g_debug("Reception point hullArray[%lu][%lu] "
+                       "x= inE->time= %" PRIu64 " y= outE->time= %" PRIu64,
                        message->inE->traceNum, message->outE->traceNum, newPoint->x,
                        newPoint->y);
        }
@@ -410,7 +410,8 @@ static void analyzeMessageCHull(SyncState* const syncState, Message* const messa
                newPoint->x= message->outE->cpuTime;
                newPoint->y= message->inE->cpuTime;
                hullType= LOWER;
-               g_debug("Send point hullArray[%lu][%lu] x= inE->time= %llu y= outE->time= %llu",
+               g_debug("Send point hullArray[%lu][%lu] "
+                       "x= inE->time= %" PRIu64 " y= outE->time= %" PRIu64,
                        message->inE->traceNum, message->outE->traceNum, newPoint->x,
                        newPoint->y);
        }
@@ -510,7 +511,7 @@ static GArray* finalizeAnalysisCHull(SyncState* const syncState)
 
        analysisData= (AnalysisDataCHull*) syncState->analysisData;
 
-       if (syncState->graphs && analysisData->graphsData->hullPoints != NULL)
+       if (syncState->graphsStream && analysisData->graphsData->hullPoints != NULL)
        {
                writeGraphFiles(syncState);
                closeGraphFiles(syncState);
@@ -520,21 +521,21 @@ static GArray* finalizeAnalysisCHull(SyncState* const syncState)
 
        factors= reduceFactors(syncState, allFactors);
 
-       if (syncState->stats || syncState->graphs)
+       if (syncState->stats || syncState->graphsStream)
        {
                if (syncState->stats)
                {
                        analysisData->stats->allFactors= allFactors;
                }
 
-               if (syncState->graphs)
+               if (syncState->graphsStream)
                {
                        analysisData->graphsData->allFactors= allFactors;
                }
        }
        else
        {
-               freeAllFactors(syncState, allFactors);
+               freeAllFactors(syncState->traceNb, allFactors);
        }
 
        return factors;
@@ -585,18 +586,19 @@ static void printAnalysisStatsCHull(SyncState* const syncState)
                        FactorsCHull* factorsCHull;
 
                        factorsCHull= &analysisData->stats->allFactors[j][i];
-                       printf("\t\t%3d - %-3d: ", i, j);
+                       printf("\t\t%3d - %-3d: %s", i, j,
+                               approxNames[factorsCHull->type]);
 
                        if (factorsCHull->type == EXACT)
                        {
-                               printf("Exact      a0= % 7g a1= 1 %c %7g\n",
+                               printf("      a0= % 7g a1= 1 %c %7g\n",
                                        factorsCHull->approx->offset,
                                        factorsCHull->approx->drift < 0. ? '-' : '+',
                                        fabs(factorsCHull->approx->drift));
                        }
                        else if (factorsCHull->type == MIDDLE)
                        {
-                               printf("Middle     a0= % 7g a1= 1 %c %7g accuracy %7g\n",
+                               printf("     a0= % 7g a1= 1 %c %7g accuracy %7g\n",
                                        factorsCHull->approx->offset, factorsCHull->approx->drift
                                        - 1. < 0. ? '-' : '+', fabs(factorsCHull->approx->drift -
                                                1.), factorsCHull->accuracy);
@@ -609,14 +611,14 @@ static void printAnalysisStatsCHull(SyncState* const syncState)
                        }
                        else if (factorsCHull->type == FALLBACK)
                        {
-                               printf("Fallback   a0= % 7g a1= 1 %c %7g error= %7g\n",
+                               printf("   a0= % 7g a1= 1 %c %7g error= %7g\n",
                                        factorsCHull->approx->offset, factorsCHull->approx->drift
                                        - 1. < 0. ? '-' : '+', fabs(factorsCHull->approx->drift -
                                                1.), factorsCHull->accuracy);
                        }
                        else if (factorsCHull->type == INCOMPLETE)
                        {
-                               printf("Incomplete\n");
+                               printf("\n");
 
                                if (factorsCHull->min->drift != -INFINITY)
                                {
@@ -635,7 +637,7 @@ static void printAnalysisStatsCHull(SyncState* const syncState)
                        }
                        else if (factorsCHull->type == SCREWED)
                        {
-                               printf("Screwed\n");
+                               printf("\n");
 
                                if (factorsCHull->min != NULL && factorsCHull->min->drift != -INFINITY)
                                {
@@ -654,7 +656,7 @@ static void printAnalysisStatsCHull(SyncState* const syncState)
                        }
                        else if (factorsCHull->type == ABSENT)
                        {
-                               printf("Absent\n");
+                               printf("\n");
                        }
                        else
                        {
@@ -701,7 +703,9 @@ static int jointCmp(const Point const* p1, const Point const* p2, const
        const double fuzzFactor= 0.;
 
        result= crossProductK(p1, p2, p1, p3);
-       g_debug("crossProductK(p1= (%llu, %llu), p2= (%llu, %llu), p1= (%llu, %llu), p3= (%llu, %llu))= %g",
+       g_debug("crossProductK(p1= (%" PRIu64 ", %" PRIu64 "), "
+               "p2= (%" PRIu64 ", %" PRIu64 "), p1= (%" PRIu64 ", %" PRIu64 "), "
+               "p3= (%" PRIu64 ", %" PRIu64 "))= %g",
                p1->x, p1->y, p2->x, p2->y, p1->x, p1->y, p3->x, p3->y, result);
        if (result < fuzzFactor)
        {
@@ -742,44 +746,19 @@ static double crossProductK(const Point const* p1, const Point const* p2,
  * Free a container of FactorsCHull
  *
  * Args:
- *   syncState:     container for synchronization data.
- *   allFactors:   container of Factors
+ *   traceNb:      number of traces
+ *   allFactors:   container of FactorsCHull
  */
-static void freeAllFactors(const SyncState* const syncState, FactorsCHull**
-       const allFactors)
+void freeAllFactors(const unsigned int traceNb, FactorsCHull** const
+       allFactors)
 {
        unsigned int i, j;
 
-       for (i= 0; i < syncState->traceNb; i++)
+       for (i= 0; i < traceNb; i++)
        {
                for (j= 0; j <= i; j++)
                {
-                       FactorsCHull* factorsCHull;
-
-                       factorsCHull= &allFactors[i][j];
-                       if (factorsCHull->type == MIDDLE || factorsCHull->type ==
-                               INCOMPLETE || factorsCHull->type == ABSENT)
-                       {
-                               free(factorsCHull->min);
-                               free(factorsCHull->max);
-                       }
-                       else if (factorsCHull->type == SCREWED)
-                       {
-                               if (factorsCHull->min != NULL)
-                               {
-                                       free(factorsCHull->min);
-                               }
-                               if (factorsCHull->max != NULL)
-                               {
-                                       free(factorsCHull->max);
-                               }
-                       }
-
-                       if (factorsCHull->type == EXACT || factorsCHull->type == MIDDLE ||
-                               factorsCHull->type == FALLBACK)
-                       {
-                               free(factorsCHull->approx);
-                       }
+                       destroyFactorsCHull(&allFactors[i][j]);
                }
                free(allFactors[i]);
        }
@@ -787,6 +766,40 @@ static void freeAllFactors(const SyncState* const syncState, FactorsCHull**
 }
 
 
+/*
+ * Free a FactorsCHull
+ *
+ * Args:
+ *   factorsCHull: container of Factors
+ */
+void destroyFactorsCHull(FactorsCHull* factorsCHull)
+{
+       if (factorsCHull->type == MIDDLE || factorsCHull->type ==
+               INCOMPLETE || factorsCHull->type == ABSENT)
+       {
+               free(factorsCHull->min);
+               free(factorsCHull->max);
+       }
+       else if (factorsCHull->type == SCREWED)
+       {
+               if (factorsCHull->min != NULL)
+               {
+                       free(factorsCHull->min);
+               }
+               if (factorsCHull->max != NULL)
+               {
+                       free(factorsCHull->max);
+               }
+       }
+
+       if (factorsCHull->type == EXACT || factorsCHull->type == MIDDLE ||
+               factorsCHull->type == FALLBACK)
+       {
+               free(factorsCHull->approx);
+       }
+}
+
+
 /*
  * Analyze the convex hulls to determine the synchronization factors between
  * each pair of trace.
@@ -798,7 +811,7 @@ static void freeAllFactors(const SyncState* const syncState, FactorsCHull**
  *   FactorsCHull*[TraceNum][TraceNum] array. See the documentation for the
  *   member allFactors of AnalysisStatsCHull.
  */
-static FactorsCHull** calculateAllFactors(SyncState* const syncState)
+FactorsCHull** calculateAllFactors(SyncState* const syncState)
 {
        unsigned int traceNumA, traceNumB;
        FactorsCHull** allFactors;
@@ -908,7 +921,7 @@ static FactorsCHull** calculateAllFactors(SyncState* const syncState)
  * Args:
  *   factors:      contains the min and max limits, used to store the result
  */
-static void calculateFactorsMiddle(FactorsCHull* factors)
+void calculateFactorsMiddle(FactorsCHull* const factors)
 {
        double amin, amax, bmin, bmax, bhat;
 
@@ -917,7 +930,7 @@ static void calculateFactorsMiddle(FactorsCHull* factors)
        bmin= factors->min->drift;
        bmax= factors->max->drift;
 
-       g_assert_cmpfloat(bmax, >, bmin);
+       g_assert_cmpfloat(bmax, >=, bmin);
 
        factors->approx= malloc(sizeof(Factors));
        bhat= (bmax * bmin - 1. + sqrt(1. + pow(bmax, 2.) * pow(bmin, 2.) +
@@ -1064,14 +1077,15 @@ static Factors* calculateFactorsExact(GQueue* const cu, GQueue* const cl, const
        p1= g_queue_peek_nth(c1, i1);
        p2= g_queue_peek_nth(c2, i2);
 
-       g_debug("Resulting points are: c1[i1]: x= %llu y= %llu c2[i2]: x= %llu y= %llu",
-               p1->x, p1->y, p2->x, p2->y);
+       g_debug("Resulting points are: c1[i1]: x= %" PRIu64 " y= %" PRIu64
+               " c2[i2]: x= %" PRIu64 " y= %" PRIu64 "", p1->x, p1->y, p2->x, p2->y);
 
        result= malloc(sizeof(Factors));
        result->drift= slope(p1, p2);
        result->offset= intercept(p1, p2);
 
-       g_debug("Resulting factors are: drift= %g offset= %g", result->drift, result->offset);
+       g_debug("Resulting factors are: drift= %g offset= %g", result->drift,
+               result->offset);
 
        return result;
 }
@@ -1522,20 +1536,19 @@ static void getFactors(FactorsCHull** const allFactors, unsigned int** const
  * Write the analysis-specific graph lines in the gnuplot script.
  *
  * Args:
- *   stream:       stream where to write the data
  *   syncState:    container for synchronization data
  *   i:            first trace number
  *   j:            second trace number, garanteed to be larger than i
  */
-void writeAnalysisGraphsPlotsCHull(FILE* stream, SyncState* const syncState,
-       const unsigned int i, const unsigned int j)
+void writeAnalysisGraphsPlotsCHull(SyncState* const syncState, const unsigned
+       int i, const unsigned int j)
 {
        AnalysisDataCHull* analysisData;
        FactorsCHull* factorsCHull;
 
        analysisData= (AnalysisDataCHull*) syncState->analysisData;
 
-       fprintf(stream,
+       fprintf(syncState->graphsStream,
                "\t\"analysis_chull-%1$03d_to_%2$03d.data\" "
                        "title \"Lower half-hull\" with linespoints "
                        "linecolor rgb \"#015a01\" linetype 4 pointtype 8 pointsize 0.8, \\\n"
@@ -1547,7 +1560,7 @@ void writeAnalysisGraphsPlotsCHull(FILE* stream, SyncState* const syncState,
        factorsCHull= &analysisData->graphsData->allFactors[j][i];
        if (factorsCHull->type == EXACT)
        {
-               fprintf(stream,
+               fprintf(syncState->graphsStream,
                        "\t%7g + %7g * x "
                                "title \"Exact conversion\" with lines "
                                "linecolor rgb \"black\" linetype 1, \\\n",
@@ -1555,25 +1568,25 @@ void writeAnalysisGraphsPlotsCHull(FILE* stream, SyncState* const syncState,
        }
        else if (factorsCHull->type == MIDDLE)
        {
-               fprintf(stream,
+               fprintf(syncState->graphsStream,
                        "\t%.2f + %.10f * x "
                                "title \"Min conversion\" with lines "
                                "linecolor rgb \"black\" linetype 5, \\\n",
                                factorsCHull->min->offset, factorsCHull->min->drift);
-               fprintf(stream,
+               fprintf(syncState->graphsStream,
                        "\t%.2f + %.10f * x "
                                "title \"Max conversion\" with lines "
                                "linecolor rgb \"black\" linetype 8, \\\n",
                                factorsCHull->max->offset, factorsCHull->max->drift);
-               fprintf(stream,
+               fprintf(syncState->graphsStream,
                        "\t%.2f + %.10f * x "
                                "title \"Middle conversion\" with lines "
-                               "linecolor rgb \"gray60\" linetype 1, \\\n",
+                               "linecolor rgb \"black\" linetype 1, \\\n",
                                factorsCHull->approx->offset, factorsCHull->approx->drift);
        }
        else if (factorsCHull->type == FALLBACK)
        {
-               fprintf(stream,
+               fprintf(syncState->graphsStream,
                        "\t%.2f + %.10f * x "
                                "title \"Fallback conversion\" with lines "
                                "linecolor rgb \"gray60\" linetype 1, \\\n",
@@ -1583,7 +1596,7 @@ void writeAnalysisGraphsPlotsCHull(FILE* stream, SyncState* const syncState,
        {
                if (factorsCHull->min->drift != -INFINITY)
                {
-                       fprintf(stream,
+                       fprintf(syncState->graphsStream,
                                "\t%.2f + %.10f * x "
                                        "title \"Min conversion\" with lines "
                                        "linecolor rgb \"black\" linetype 5, \\\n",
@@ -1592,7 +1605,7 @@ void writeAnalysisGraphsPlotsCHull(FILE* stream, SyncState* const syncState,
 
                if (factorsCHull->max->drift != INFINITY)
                {
-                       fprintf(stream,
+                       fprintf(syncState->graphsStream,
                                "\t%.2f + %.10f * x "
                                        "title \"Max conversion\" with lines "
                                        "linecolor rgb \"black\" linetype 8, \\\n",
@@ -1603,7 +1616,7 @@ void writeAnalysisGraphsPlotsCHull(FILE* stream, SyncState* const syncState,
        {
                if (factorsCHull->min != NULL && factorsCHull->min->drift != -INFINITY)
                {
-                       fprintf(stream,
+                       fprintf(syncState->graphsStream,
                                "\t%.2f + %.10f * x "
                                        "title \"Min conversion\" with lines "
                                        "linecolor rgb \"black\" linetype 5, \\\n",
@@ -1612,7 +1625,7 @@ void writeAnalysisGraphsPlotsCHull(FILE* stream, SyncState* const syncState,
 
                if (factorsCHull->max != NULL && factorsCHull->max->drift != INFINITY)
                {
-                       fprintf(stream,
+                       fprintf(syncState->graphsStream,
                                "\t%.2f + %.10f * x "
                                        "title \"Max conversion\" with lines "
                                        "linecolor rgb \"black\" linetype 8, \\\n",
This page took 0.03332 seconds and 4 git commands to generate.