[PATCH 4/6] [mama] Add User Stats


Ian Bell <IBell@...>
 

From 7f06542eeec6b21b9cf1fc476be15314fc3cd0c9 Mon Sep 17 00:00:00 2001

Message-Id: <7f06542eeec6b21b9cf1fc476be15314fc3cd0c9.1348674366.git.ibell@...>

In-Reply-To: <ec11f834aa5ee941affd12c0baa32480c39efabd.1348674366.git.ibell@...>

References: <ec11f834aa5ee941affd12c0baa32480c39efabd.1348674366.git.ibell@...>

From: Ian Bell <ibell@...>

Date: Wed, 26 Sep 2012 16:15:53 +0100

Subject: [PATCH 4/6] [mama] Add User Stats

 

Added ability to create a user stats collector object and attach stats

to it.  Mama then logs / publishes these stats along with its own

 

Signed-off-by: Ian Bell <ibell@...>

---

mama/c_cpp/src/c/mama.c                   |   89 ++++++++++++++++++++++++-----

mama/c_cpp/src/c/mama/mama.h              |   13 +++++

mama/c_cpp/src/c/mama/stat.h              |   29 ++++++++++

mama/c_cpp/src/c/mama/statscollector.h    |    3 +-

mama/c_cpp/src/c/stat.c                   |   65 +++++++++++++++++++++

mama/c_cpp/src/c/statscollector.c         |    9 ++-

mama/c_cpp/src/c/statscollectorinternal.h |    4 +-

7 files changed, 191 insertions(+), 21 deletions(-)

 

diff --git a/mama/c_cpp/src/c/mama.c b/mama/c_cpp/src/c/mama.c

index c7b08fb..f511994 100644

--- a/mama/c_cpp/src/c/mama.c

+++ b/mama/c_cpp/src/c/mama.c

@@ -101,16 +101,19 @@ int gLogQueueStats          = 1;

int gLogTransportStats      = 1;

int gLogGlobalStats         = 1;

int gLogLbmStats            = 1;

+int gLogUserStats           = 1;

 int gGenerateQueueStats     = 0;

int gGenerateTransportStats = 0;

int gGenerateGlobalStats    = 0;

int gGenerateLbmStats       = 0;

+int gGenerateUserStats       = 0;

 int gPublishQueueStats      = 0;

int gPublishTransportStats  = 0;

int gPublishGlobalStats     = 0;

int gPublishLbmStats        = 0;

+int gPublishUserStats       = 0;

 static mamaStatsLogger  gStatsPublisher  = NULL;

@@ -310,7 +313,8 @@ static int mamaInternal_statsPublishingEnabled (void)

     return (gPublishGlobalStats

          || gPublishTransportStats

          || gPublishQueueStats

-         || gPublishLbmStats);

+         || gPublishLbmStats

+         || gPublishUserStats);

}

 static mama_status

@@ -527,7 +531,7 @@ mamaInternal_enableStatsLogging (void)

         mamaStatsGenerator_addStatsCollector (gStatsGenerator, gGlobalStatsCollector);

     }

-    if (gLogQueueStats || gLogTransportStats || gLogGlobalStats || gLogLbmStats)

+    if (gLogQueueStats || gLogTransportStats || gLogGlobalStats || gLogLbmStats || gLogUserStats)

     {

         mamaStatsGenerator_setLogStats (gStatsGenerator, 1);

     }

@@ -796,41 +800,50 @@ mama_statsInit (void)

            generate stats (and neither log nor publish). */

         propVal           = properties_Get (gProperties, "mama.statslogging.global.logging");

-         if ( propVal != NULL)

+        if ( propVal != NULL)

             gLogGlobalStats = strtobool(propVal);

-          propVal           = properties_Get (gProperties, "mama.statslogging.global.publishing");

-         if ( propVal != NULL)

+        propVal           = properties_Get (gProperties, "mama.statslogging.global.publishing");

+        if ( propVal != NULL)

             gPublishGlobalStats = strtobool(propVal);

-         propVal           = properties_Get (gProperties, "mama.statslogging.transport.logging");

-         if ( propVal != NULL)

+        propVal           = properties_Get (gProperties, "mama.statslogging.transport.logging");

+        if ( propVal != NULL)

             gLogTransportStats = strtobool(propVal);

         propVal           = properties_Get (gProperties, "mama.statslogging.transport.publishing");

-         if ( propVal != NULL)

+        if ( propVal != NULL)

             gPublishTransportStats = strtobool(propVal);

-          propVal           = properties_Get (gProperties, "mama.statslogging.queue.logging");

-         if ( propVal != NULL)

+        propVal           = properties_Get (gProperties, "mama.statslogging.queue.logging");

+        if ( propVal != NULL)

             gLogQueueStats = strtobool(propVal);

-          propVal           = properties_Get (gProperties, "mama.statslogging.queue.publishing");

-         if ( propVal != NULL)

+        propVal           = properties_Get (gProperties, "mama.statslogging.queue.publishing");

+        if ( propVal != NULL)

             gPublishQueueStats = strtobool(propVal);

         propVal           = properties_Get (gProperties, "mama.statslogging.lbm.logging");

-         if ( propVal != NULL)

+        if ( propVal != NULL)

             gLogLbmStats = strtobool(propVal);

-          propVal           = properties_Get (gProperties, "mama.statslogging.lbm.publishing");

-         if ( propVal != NULL)

+        propVal           = properties_Get (gProperties, "mama.statslogging.lbm.publishing");

+        if ( propVal != NULL)

             gPublishLbmStats = strtobool(propVal);

+        propVal           = properties_Get (gProperties, "mama.statslogging.user.logging");

+        if ( propVal != NULL)

+            gLogUserStats = strtobool(propVal);

+

+        propVal           = properties_Get (gProperties, "mama.statslogging.user.publishing");

+        if ( propVal != NULL)

+            gPublishUserStats = strtobool(propVal);

+       

         if (gLogGlobalStats || gPublishGlobalStats) gGenerateGlobalStats=1;

         if (gLogTransportStats || gPublishTransportStats) gGenerateTransportStats=1;

         if (gLogQueueStats || gPublishQueueStats) gGenerateQueueStats=1;

         if (gLogLbmStats || gPublishLbmStats) gGenerateLbmStats=1;

+        if (gLogUserStats || gPublishUserStats) gGenerateUserStats=1;

          

         mama_setupStatsGenerator();

@@ -2071,3 +2084,49 @@ mama_setBridgeInfoCallback (mamaBridge bridgeImpl, bridgeInfoCallback callback)

     return MAMA_STATUS_OK;

}

+

+mama_status

+mama_addStatsCollector (mamaStatsCollector statsCollector)

+{

+    mama_status status = MAMA_STATUS_NOT_FOUND;

+

+    if (!gStatsGenerator)

+        mama_statsInit();

+

+    if (MAMA_STATUS_OK != (

+            status = mamaStatsGenerator_addStatsCollector (

+                        gStatsGenerator,

+                        statsCollector)))

+    {

+        mama_log (MAMA_LOG_LEVEL_ERROR, "mama_addStatsCollector (): "

+                    "Could not add User stats collector.");

+        return status;

+    }

+

+    return status;

+}

+

+mama_status

+mama_removeStatsCollector (mamaStatsCollector statsCollector)

+{

+    mama_status status = MAMA_STATUS_NOT_FOUND;

+

+    if (gStatsGenerator)

+    {

+        if (MAMA_STATUS_OK != (

+                status = mamaStatsGenerator_removeStatsCollector (

+                            mamaInternal_getStatsGenerator(),

+                            statsCollector)))

+        {

+            mama_log (MAMA_LOG_LEVEL_ERROR, "mama_removeStatsCollector (): "

+                     "Could not remove User stats collector.");

+            return status;

+        }

+    }

+    else

+    {

+        mama_log (MAMA_LOG_LEVEL_ERROR, "mamaInternal_getStatsGenerator (): "

+                  "Could not find stats generator.");

+    }

+    return status;

+}

diff --git a/mama/c_cpp/src/c/mama/mama.h b/mama/c_cpp/src/c/mama/mama.h

index 597164f..a574f36 100644

--- a/mama/c_cpp/src/c/mama/mama.h

+++ b/mama/c_cpp/src/c/mama/mama.h

@@ -574,6 +574,19 @@ extern "C"

     extern mama_status

     mama_setBridgeInfoCallback (mamaBridge         bridgeImpl,

                                 bridgeInfoCallback callback);

+    /**

+     * Add a user stats collector

+     */

+    MAMAExpDLL

+    extern mama_status

+    mama_addStatsCollector (mamaStatsCollector  statsCollector);

+

+    /**

+     * Remove a user stats collector

+     */

+    MAMAExpDLL

+    extern mama_status

+    mama_removeStatsCollector (mamaStatsCollector  statsCollector);

 #if defined(__cplusplus)

}

diff --git a/mama/c_cpp/src/c/mama/stat.h b/mama/c_cpp/src/c/mama/stat.h

index 0d60c27..55b171c 100644

--- a/mama/c_cpp/src/c/mama/stat.h

+++ b/mama/c_cpp/src/c/mama/stat.h

@@ -103,6 +103,35 @@ mamaStat_decrement (mamaStat stat);

MAMAExpDLL

extern mama_status

mamaStat_reset (mamaStat stat);

+/**

+ * Adds the stats object counter with the value

+ *

+ * @param stat  The stats object to added

+ * @param value The value to be added to be stat

+ */

+MAMAExpDLL

+extern mama_status

+mamaStat_add (mamaStat stat, int value);

+

+/**

+ * Subtract the stats object counter with the value

+ *

+ * @param stat  The stats object to subtracted

+ * @param value the value to be subtracted from stat

+ */

+MAMAExpDLL

+extern mama_status

+mamaStat_subtract (mamaStat stat, int value);

+

+/**

+ * set the Interval value

+ *

+ * @param stat  The stats object to be changed

+ * @param value the value to be used

+ */

+MAMAExpDLL

+extern mama_status

+mamaStat_setIntervalValue (mamaStat stat, int value);

 /**

  * Get the FID used when publishing the

diff --git a/mama/c_cpp/src/c/mama/statscollector.h b/mama/c_cpp/src/c/mama/statscollector.h

index 930161c..a50405d 100644

--- a/mama/c_cpp/src/c/mama/statscollector.h

+++ b/mama/c_cpp/src/c/mama/statscollector.h

@@ -32,7 +32,8 @@ typedef enum mamaStatsCollectorType

{

     MAMA_STATS_COLLECTOR_TYPE_QUEUE     = 0,

     MAMA_STATS_COLLECTOR_TYPE_TRANSPORT = 1,

-    MAMA_STATS_COLLECTOR_TYPE_GLOBAL    = 2

+    MAMA_STATS_COLLECTOR_TYPE_USER      = 2,

+    MAMA_STATS_COLLECTOR_TYPE_GLOBAL    = 3

} mamaStatsCollectorType;

 /**

diff --git a/mama/c_cpp/src/c/stat.c b/mama/c_cpp/src/c/stat.c

index 75e41fd..a4297a2 100644

--- a/mama/c_cpp/src/c/stat.c

+++ b/mama/c_cpp/src/c/stat.c

@@ -154,6 +154,71 @@ mamaStat_reset (mamaStat stat)

     return MAMA_STATUS_OK;

}

+mama_status

+mamaStat_add (mamaStat stat, int value)

+{

+    mamaStatImpl* impl = (mamaStatImpl*)stat;

+    if (!impl) return MAMA_STATUS_NULL_ARG;

+

+    if (impl->mLockable)

+        wthread_mutex_lock (&impl->mUpdateMutex);

+

+    impl->mIntervalValue += value;

+    if (impl->mIntervalValue > impl->mMaxValue)

+    {

+        impl->mMaxValue = impl->mIntervalValue;

+    }

+    impl->mTotalValue += value;

+

+    if (impl->mLockable)

+        wthread_mutex_unlock (&impl->mUpdateMutex);

+

+    return MAMA_STATUS_OK;

+}

+

+mama_status

+mamaStat_subtract (mamaStat stat, int value)

+{

+    mamaStatImpl* impl = (mamaStatImpl*)stat;

+    if (!impl) return MAMA_STATUS_NULL_ARG;

+

+    if (impl->mLockable)

+    {

+        wthread_mutex_lock (&impl->mUpdateMutex);

+    }

+

+    impl->mIntervalValue -= value;

+    impl->mTotalValue -= value;

+

+    if (impl->mLockable)

+    {

+        wthread_mutex_unlock (&impl->mUpdateMutex);

+    }

+

+    return MAMA_STATUS_OK;

+}

+

+

+mama_status

+mamaStat_setIntervalValue (mamaStat stat, int value)

+{

+      mamaStatImpl* impl = (mamaStatImpl*)stat;

+    if (!impl) return MAMA_STATUS_NULL_ARG;

+

+    if (impl->mLockable)

+    {

+        wthread_mutex_lock (&impl->mUpdateMutex);

+    }

+

+    impl->mIntervalValue = value;

+

+    if (impl->mLockable)

+    {

+        wthread_mutex_unlock (&impl->mUpdateMutex);

+    }

+

+    return MAMA_STATUS_OK;

+}

mama_fid_t

mamaStat_getFid (mamaStat stat)

{

diff --git a/mama/c_cpp/src/c/statscollector.c b/mama/c_cpp/src/c/statscollector.c

index 739d6a1..83ddff5 100644

--- a/mama/c_cpp/src/c/statscollector.c

+++ b/mama/c_cpp/src/c/statscollector.c

@@ -42,6 +42,7 @@ mamaStatsCollector_create (mamaStatsCollector* statsCollector, mamaStatsCollecto

     impl->mPollClosure  = NULL;

     /* Default behaviour is to log, but not publish, stats */

     impl->mPublishStats = 0;

+    impl->mOffset = 0;

     impl->mLogStats     = 1;

     memset (impl->mMamaStats, 0, MAMA_STAT_MAX_STATS*sizeof(mamaStat));

@@ -74,7 +75,8 @@ mamaStatsCollector_addStat (mamaStatsCollector statsCollector, mamaStat stat)

     mamaStatsCollectorImpl* impl = (mamaStatsCollectorImpl*)statsCollector;

     if (impl == NULL) return MAMA_STATUS_NULL_ARG;

-    index = mamaStat_getFid (stat) - MAMA_STAT_ARRAY_OFFSET;

+    if (impl->mOffset==0) impl->mOffset=mamaStat_getFid (stat);

+    index = mamaStat_getFid (stat) - impl->mOffset;

     impl->mMamaStats[index] = stat;

     return MAMA_STATUS_OK;

@@ -87,7 +89,7 @@ mamaStatsCollector_incrementStat (mamaStatsCollector statsCollector, mama_fid_t

     mamaStatsCollectorImpl* impl = (mamaStatsCollectorImpl*)statsCollector;

     if (impl == NULL) return MAMA_STATUS_NULL_ARG;

-    index = identifier - MAMA_STAT_ARRAY_OFFSET;

+    index = identifier - impl->mOffset;

     mamaStat_increment (impl->mMamaStats[index]);

     return MAMA_STATUS_OK;

@@ -100,7 +102,7 @@ mamaStatsCollector_setStatIntervalValueFromTotal (mamaStatsCollector statsCollec

     mamaStatsCollectorImpl* impl = (mamaStatsCollectorImpl*)statsCollector;

     if (impl == NULL) return MAMA_STATUS_NULL_ARG;

-    index = identifer - MAMA_STAT_ARRAY_OFFSET;

+    index = identifer - impl->mOffset;

     mamaStat_setIntervalValueFromTotal (impl->mMamaStats[index], value);

     return MAMA_STATUS_OK;

@@ -251,6 +253,7 @@ mamaStatsCollectorType_stringForType (mamaStatsCollectorType type)

     {

         case MAMA_STATS_COLLECTOR_TYPE_QUEUE     : return "Queue";

         case MAMA_STATS_COLLECTOR_TYPE_TRANSPORT : return "Transport";

+        case MAMA_STATS_COLLECTOR_TYPE_USER      : return "User";

         case MAMA_STATS_COLLECTOR_TYPE_GLOBAL    : return "Global";

         default : return "Unknown";

     }

diff --git a/mama/c_cpp/src/c/statscollectorinternal.h b/mama/c_cpp/src/c/statscollectorinternal.h

index b368f60..5ee0118 100644

--- a/mama/c_cpp/src/c/statscollectorinternal.h

+++ b/mama/c_cpp/src/c/statscollectorinternal.h

@@ -31,8 +31,7 @@ extern "C" {

typedef void (

*collectorPollStatCb) (mamaStatsCollector statsCollector, void* closure);

-#define MAMA_STAT_ARRAY_OFFSET                                105

-#define MAMA_STAT_MAX_STATS                      30

+#define MAMA_STAT_MAX_STATS                     35

 typedef struct mamaStatsCollectorImpl__

{

@@ -44,6 +43,7 @@ typedef struct mamaStatsCollectorImpl__

     void*                                                mPollClosure;

     int                                                      mPublishStats;

     int                                                      mLogStats;

+     int                                                   mOffset;

     void*                                                mHandle;

} mamaStatsCollectorImpl;

--

1.7.9.5

 




Please consider the environment before printing this e-mail.

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy.

Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.