]> granicus.if.org Git - php/commitdiff
Rework the statistics macros to be reusable by external entities.
authorAndrey Hristov <andrey@php.net>
Mon, 11 Jan 2010 14:27:35 +0000 (14:27 +0000)
committerAndrey Hristov <andrey@php.net>
Mon, 11 Jan 2010 14:27:35 +0000 (14:27 +0000)
Rename handlers to triggers. Dynamically allocate space for the
statistics thus allow reusability.

ext/mysqlnd/mysqlnd.c
ext/mysqlnd/mysqlnd_statistics.c
ext/mysqlnd/mysqlnd_statistics.h
ext/mysqlnd/mysqlnd_structs.h

index 55c147b36677527e1da63d7c3d92971b61d2f17f..b3994e26c21b3009cb1eded641c2a262003b0112 100644 (file)
@@ -2115,7 +2115,7 @@ MYSQLND_METHOD(mysqlnd_conn, init)(MYSQLND * conn TSRMLS_DC)
        DBG_ENTER("mysqlnd_conn::init");
        conn->net = mysqlnd_net_init(conn->persistent TSRMLS_CC);
        conn->protocol = mysqlnd_protocol_init(conn->persistent TSRMLS_CC);
-       mysqlnd_stats_init(&conn->stats);
+       mysqlnd_stats_init(&conn->stats, STAT_LAST);
 
        SET_ERROR_AFF_ROWS(conn);
 
@@ -2153,7 +2153,7 @@ void mysqlnd_library_init(TSRMLS_D)
                mysqlnd_conn_methods = &MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_conn);
                _mysqlnd_init_ps_subsystem();
                /* Should be calloc, as mnd_calloc will reference LOCK_access*/
-               mysqlnd_stats_init(&mysqlnd_global_stats);
+               mysqlnd_stats_init(&mysqlnd_global_stats, STAT_LAST);
        }
 }
 /* }}} */
index 50eba8cfcdb1abaa0303d1c934b090675ec8b60d..756d4ce2281b66a1e42e8099ed1c54d323da7f9d 100644 (file)
@@ -237,11 +237,13 @@ PHPAPI void _mysqlnd_get_client_stats(zval *return_value TSRMLS_DC ZEND_FILE_LIN
 
 /* {{{ mysqlnd_stats_init */
 PHPAPI void
-mysqlnd_stats_init(MYSQLND_STATS ** stats)
+mysqlnd_stats_init(MYSQLND_STATS ** stats, size_t statistic_count)
 {
        *stats = calloc(1, sizeof(MYSQLND_STATS));
-       (*stats)->triggers = calloc(STAT_LAST, sizeof(mysqlnd_stat_trigger));
+       (*stats)->values = calloc(statistic_count, sizeof(uint64_t));
+       (*stats)->triggers = calloc(statistic_count, sizeof(mysqlnd_stat_trigger));
        (*stats)->in_trigger = FALSE;
+       (*stats)->count = statistic_count;
 #ifdef ZTS
        (*stats)->LOCK_access = tsrm_mutex_alloc();
 #endif
@@ -258,6 +260,7 @@ mysqlnd_stats_end(MYSQLND_STATS * stats)
        tsrm_mutex_free(stats->LOCK_access);
 #endif
        free(stats->triggers);
+       free(stats->values);
        /* mnd_free will reference LOCK_access and crash...*/
        free(stats);
 }
index 65891f54f113a8eb22fa433df8f0ca95cc6e5cdb..fce783803fcff4f994c232fe55f356ac4d35c05f 100644 (file)
@@ -43,184 +43,131 @@ extern const MYSQLND_STRING mysqlnd_stats_values_names[];
 #endif
 
 #define MYSQLND_CHECK_AND_CALL_HANDLER(stats, statistic, value) \
+       { \
                        if ((stats)->triggers[(statistic)] && (stats)->in_trigger == FALSE) { \
                                (stats)->in_trigger = TRUE; \
                                (stats)->triggers[(statistic)]((stats), (statistic), (value) TSRMLS_CC); \
                                (stats)->in_trigger = FALSE; \
                        } \
+       } \
+       
+#define MYSQLND_DEC_STATISTIC(enabler, stats, statistic) \
+ { \
+       enum_mysqlnd_collected_stats _s = (statistic);\
+       MYSQLND_STATS * _p_s = (MYSQLND_STATS *) (stats); \
+       if ((enabler) && _p_s && _s != _p_s->count) { \
+               MYSQLND_STATS_LOCK(_p_s); \
+               MYSQLND_CHECK_AND_CALL_HANDLER(_p_s, _s, -1); \
+               _p_s->values[_s]--; \
+               MYSQLND_STATS_UNLOCK(_p_s); \
+       }\
+ }
 
-
-
-#define MYSQLND_INC_GLOBAL_STATISTIC(statistic) \
+#define MYSQLND_INC_STATISTIC(enabler, stats, statistic) \
  { \
-       if (MYSQLND_G(collect_statistics) && (statistic) != STAT_LAST) { \
-               DBG_INF_FMT("Global stat increase [%s]", mysqlnd_stats_values_names[(statistic)].s); \
-                       \
-               MYSQLND_STATS_LOCK(mysqlnd_global_stats); \
-               MYSQLND_CHECK_AND_CALL_HANDLER(mysqlnd_global_stats, (statistic), 1); \
-               mysqlnd_global_stats->values[(statistic)]++; \
-               MYSQLND_STATS_UNLOCK(mysqlnd_global_stats); \
+       enum_mysqlnd_collected_stats _s = (statistic);\
+       MYSQLND_STATS * _p_s = (MYSQLND_STATS *) (stats); \
+       if ((enabler) && _p_s && _s != _p_s->count) { \
+               MYSQLND_STATS_LOCK(_p_s); \
+               MYSQLND_CHECK_AND_CALL_HANDLER(_p_s, _s, 1); \
+               _p_s->values[_s]++; \
+               MYSQLND_STATS_UNLOCK(_p_s); \
        }\
  }
 
-#define MYSQLND_DEC_CONN_STATISTIC(conn_stats, statistic) \
+#define MYSQLND_INC_STATISTIC_W_VALUE(enabler, stats, statistic, value) \
  { \
-       if (MYSQLND_G(collect_statistics) && (statistic) != STAT_LAST) { \
-               DBG_INF_FMT("Global&conn stat decrease [%s]", mysqlnd_stats_values_names[(statistic)].s); \
-                       \
-               MYSQLND_STATS_LOCK(mysqlnd_global_stats); \
-               MYSQLND_CHECK_AND_CALL_HANDLER(mysqlnd_global_stats, (statistic), -1); \
-               mysqlnd_global_stats->values[(statistic)]--; \
-               MYSQLND_STATS_UNLOCK(mysqlnd_global_stats); \
-               if ((conn_stats)) {  \
-                       MYSQLND_CHECK_AND_CALL_HANDLER((conn_stats), (statistic), -1); \
-               } \
+       enum_mysqlnd_collected_stats _s = (statistic);\
+       MYSQLND_STATS * _p_s = (MYSQLND_STATS *) (stats); \
+       if ((enabler) && _p_s && _s != _p_s->count) { \
+               uint64_t v = (uint64_t) (value); \
+               MYSQLND_STATS_LOCK(_p_s); \
+               MYSQLND_CHECK_AND_CALL_HANDLER(_p_s, _s, v); \
+               _p_s->values[_s] += v; \
+               MYSQLND_STATS_UNLOCK(_p_s); \
        }\
  }
 
-#define MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(statistic1, value1, statistic2, value2) \
+#define MYSQLND_INC_STATISTIC_W_VALUE2(enabler, stats, statistic1, value1, statistic2, value2) \
  { \
-       if (MYSQLND_G(collect_statistics)) { \
+       MYSQLND_STATS * _p_s = (MYSQLND_STATS *) (stats); \
+       if ((enabler) && _p_s) { \
                uint64_t v1 = (uint64_t) (value1); \
                uint64_t v2 = (uint64_t) (value2); \
                enum_mysqlnd_collected_stats _s1 = (statistic1);\
                enum_mysqlnd_collected_stats _s2 = (statistic2);\
-                                                                \
-               if (_s1 != STAT_LAST) DBG_INF_FMT("Global stat increase1 [%s]", mysqlnd_stats_values_names[_s1].s); \
-               if (_s2 != STAT_LAST) DBG_INF_FMT("Global stat increase2 [%s]", mysqlnd_stats_values_names[_s2].s); \
-                                                                               \
-               MYSQLND_STATS_LOCK(mysqlnd_global_stats); \
-               if (_s1 != STAT_LAST) { \
-                       MYSQLND_CHECK_AND_CALL_HANDLER(mysqlnd_global_stats, _s1, v1); \
-                       mysqlnd_global_stats->values[_s1]+= v1; \
-               } \
-               if (_s2 != STAT_LAST) { \
-                       MYSQLND_CHECK_AND_CALL_HANDLER(mysqlnd_global_stats, _s2, v2); \
-                       mysqlnd_global_stats->values[_s2]+= v2; \
-               } \
-               MYSQLND_STATS_UNLOCK(mysqlnd_global_stats); \
-       }\
- }
-
-#define MYSQLND_INC_CONN_STATISTIC(conn_stats, statistic) \
- { \
-       if (MYSQLND_G(collect_statistics) && (statistic) != STAT_LAST) { \
-               DBG_INF_FMT("Global&Conn stat increase [%s]", mysqlnd_stats_values_names[(statistic)].s); \
-                       \
-               MYSQLND_STATS_LOCK(mysqlnd_global_stats); \
-               MYSQLND_CHECK_AND_CALL_HANDLER(mysqlnd_global_stats, (statistic), 1); \
-               mysqlnd_global_stats->values[(statistic)]++; \
-               MYSQLND_STATS_UNLOCK(mysqlnd_global_stats); \
-               if (conn_stats) { \
-                       MYSQLND_CHECK_AND_CALL_HANDLER((conn_stats), (statistic), 1); \
-                       (conn_stats)->values[(statistic)]++; \
+               MYSQLND_STATS_LOCK(_p_s); \
+               if (_s1 != _p_s->count) { \
+                       MYSQLND_CHECK_AND_CALL_HANDLER(_p_s, _s1, v1); \
+                       _p_s->values[_s1]+= v1; \
                } \
-       }\
- }
-
-#define MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn_stats, statistic, value) \
- { \
-       if (MYSQLND_G(collect_statistics) && (statistic) != STAT_LAST) { \
-               uint64_t v = (uint64_t) (value); \
-               DBG_INF_FMT("Global&Conn stat increase w value [%s]", mysqlnd_stats_values_names[(statistic)].s); \
-                       \
-               MYSQLND_STATS_LOCK(mysqlnd_global_stats); \
-               MYSQLND_CHECK_AND_CALL_HANDLER(mysqlnd_global_stats, (statistic), v); \
-               mysqlnd_global_stats->values[(statistic)] += v; \
-               MYSQLND_STATS_UNLOCK(mysqlnd_global_stats); \
-               if (conn_stats) { \
-                       MYSQLND_CHECK_AND_CALL_HANDLER((conn_stats), (statistic), v); \
-                       (conn_stats)->values[(statistic)]+= v; \
+               if (_s2 != _p_s->count) { \
+                       MYSQLND_CHECK_AND_CALL_HANDLER(_p_s, _s2, v2); \
+                       _p_s->values[_s2]+= v2; \
                } \
+               MYSQLND_STATS_UNLOCK(_p_s); \
        }\
  }
 
-#define MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn_stats, statistic1, value1, statistic2, value2) \
+#define MYSQLND_INC_STATISTIC_W_VALUE3(enabler, stats, statistic1, value1, statistic2, value2, statistic3, value3) \
  { \
-       if (MYSQLND_G(collect_statistics)) { \
+       MYSQLND_STATS * _p_s = (MYSQLND_STATS *) (stats); \
+       if ((enabler) && _p_s) { \
                uint64_t v1 = (uint64_t) (value1); \
                uint64_t v2 = (uint64_t) (value2); \
+               uint64_t v3 = (uint64_t) (value3); \
                enum_mysqlnd_collected_stats _s1 = (statistic1);\
                enum_mysqlnd_collected_stats _s2 = (statistic2);\
-                                                                \
-               if (_s1 != STAT_LAST) DBG_INF_FMT("Global stat increase1 [%s]", mysqlnd_stats_values_names[_s1].s); \
-               if (_s2 != STAT_LAST) DBG_INF_FMT("Global stat increase2 [%s]", mysqlnd_stats_values_names[_s2].s); \
-                                       \
-               MYSQLND_STATS_LOCK(mysqlnd_global_stats); \
-               if (_s1 != STAT_LAST) { \
-                       MYSQLND_CHECK_AND_CALL_HANDLER(mysqlnd_global_stats, _s1, v1); \
-                       mysqlnd_global_stats->values[_s1]+= v1; \
+               enum_mysqlnd_collected_stats _s3 = (statistic3);\
+               MYSQLND_STATS_LOCK(_p_s); \
+               if (_s1 != _p_s->count) { \
+                       MYSQLND_CHECK_AND_CALL_HANDLER(_p_s, _s1, v1); \
+                       _p_s->values[_s1]+= v1; \
                } \
-               if (_s2 != STAT_LAST) { \
-                       MYSQLND_CHECK_AND_CALL_HANDLER(mysqlnd_global_stats, _s2, v2); \
-                       mysqlnd_global_stats->values[_s2]+= v2; \
+               if (_s2 != _p_s->count) { \
+                       MYSQLND_CHECK_AND_CALL_HANDLER(_p_s, _s2, v2); \
+                       _p_s->values[_s2]+= v2; \
                } \
-               MYSQLND_STATS_UNLOCK(mysqlnd_global_stats); \
-               if (conn_stats) { \
-                       if (_s1 != STAT_LAST) { \
-                               MYSQLND_CHECK_AND_CALL_HANDLER((conn_stats), _s1, v1); \
-                               (conn_stats)->values[_s1]+= v1; \
-                       } \
-                       if (_s2 != STAT_LAST) { \
-                               MYSQLND_CHECK_AND_CALL_HANDLER((conn_stats), _s2, v2); \
-                               (conn_stats)->values[_s2]+= v2; \
-                       } \
+               if (_s3 != _p_s->count) { \
+                       MYSQLND_CHECK_AND_CALL_HANDLER(_p_s, _s3, v3); \
+                       _p_s->values[_s3]+= v3; \
                } \
-       } \
+               MYSQLND_STATS_UNLOCK(_p_s); \
+       }\
  }
 
 
+#define MYSQLND_INC_GLOBAL_STATISTIC(statistic) \
+        MYSQLND_INC_STATISTIC(MYSQLND_G(collect_statistics), mysqlnd_global_stats, (statistic))
+
+#define MYSQLND_DEC_CONN_STATISTIC(conn_stats, statistic) \
+        MYSQLND_DEC_STATISTIC(MYSQLND_G(collect_statistics), mysqlnd_global_stats, (statistic))
+
+#define MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(statistic1, value1, statistic2, value2) \
+       MYSQLND_INC_STATISTIC_W_VALUE2(MYSQLND_G(collect_statistics), mysqlnd_global_stats, (statistic1), (value1), (statistic2), (value2))
+
+#define MYSQLND_INC_CONN_STATISTIC(conn_stats, statistic) \
+        MYSQLND_INC_STATISTIC(MYSQLND_G(collect_statistics), mysqlnd_global_stats, (statistic)); \
+        MYSQLND_INC_STATISTIC(MYSQLND_G(collect_statistics), (conn_stats), (statistic));
+
+#define MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn_stats, statistic, value) \
+        MYSQLND_INC_STATISTIC_W_VALUE(MYSQLND_G(collect_statistics), mysqlnd_global_stats, (statistic), (value)); \
+        MYSQLND_INC_STATISTIC_W_VALUE(MYSQLND_G(collect_statistics), (conn_stats), (statistic), (value));
+
+#define MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn_stats, statistic1, value1, statistic2, value2) \
+        MYSQLND_INC_STATISTIC_W_VALUE2(MYSQLND_G(collect_statistics), mysqlnd_global_stats, (statistic1), (value1), (statistic2), (value2)); \
+        MYSQLND_INC_STATISTIC_W_VALUE2(MYSQLND_G(collect_statistics), (conn_stats), (statistic1), (value1), (statistic2), (value2));
+
 #define MYSQLND_INC_CONN_STATISTIC_W_VALUE3(conn_stats, statistic1, value1, statistic2, value2, statistic3, value3) \
- { \
-       if (MYSQLND_G(collect_statistics)) { \
-               uint64_t v1 = (uint64_t) (value1); \
-               uint64_t v2 = (uint64_t) (value2); \
-               uint64_t v3 = (uint64_t) (value3); \
-               enum_mysqlnd_collected_stats _s1 = (statistic1); \
-               enum_mysqlnd_collected_stats _s2 = (statistic2); \
-               enum_mysqlnd_collected_stats _s3 = (statistic3); \
-                                                                \
-               if (_s1 != STAT_LAST) DBG_INF_FMT("Global stat increase1 [%s]", mysqlnd_stats_values_names[_s1].s); \
-               if (_s2 != STAT_LAST) DBG_INF_FMT("Global stat increase2 [%s]", mysqlnd_stats_values_names[_s2].s); \
-               if (_s3 != STAT_LAST) DBG_INF_FMT("Global stat increase3 [%s]", mysqlnd_stats_values_names[_s3].s); \
-                                                                               \
-               MYSQLND_STATS_LOCK(mysqlnd_global_stats); \
-               if (_s1 != STAT_LAST) { \
-                       MYSQLND_CHECK_AND_CALL_HANDLER(mysqlnd_global_stats, _s1, v1); \
-                       mysqlnd_global_stats->values[_s1]+= v1; \
-               } \
-               if (_s2 != STAT_LAST) { \
-                       MYSQLND_CHECK_AND_CALL_HANDLER(mysqlnd_global_stats, _s2, v2); \
-                       mysqlnd_global_stats->values[_s2]+= v2; \
-               } \
-               if (_s3 != STAT_LAST) { \
-                       MYSQLND_CHECK_AND_CALL_HANDLER(mysqlnd_global_stats, _s3, v3); \
-                       mysqlnd_global_stats->values[_s3]+= v3; \
-               } \
-               MYSQLND_STATS_UNLOCK(mysqlnd_global_stats); \
-               if (conn_stats) { \
-                       if (_s1 != STAT_LAST) { \
-                               MYSQLND_CHECK_AND_CALL_HANDLER((conn_stats), _s1, v1); \
-                               (conn_stats)->values[_s1]+= v1; \
-                       } \
-                       if (_s2 != STAT_LAST) { \
-                               MYSQLND_CHECK_AND_CALL_HANDLER((conn_stats), _s2, v2); \
-                               (conn_stats)->values[_s2]+= v2; \
-                       } \
-                       if (_s3 != STAT_LAST) { \
-                               MYSQLND_CHECK_AND_CALL_HANDLER((conn_stats), _s3, v3); \
-                               (conn_stats)->values[_s3]+= v3; \
-                       } \
-               } \
-       } \
- }
+        MYSQLND_INC_STATISTIC_W_VALUE3(MYSQLND_G(collect_statistics), mysqlnd_global_stats, (statistic1), (value1), (statistic2), (value2), (statistic3), (value3)); \
+        MYSQLND_INC_STATISTIC_W_VALUE3(MYSQLND_G(collect_statistics), (conn_stats), (statistic1), (value1), (statistic2), (value2), (statistic3), (value3));
 
 
 void mysqlnd_fill_stats_hash(const MYSQLND_STATS * const stats, zval *return_value
                                                         TSRMLS_DC ZEND_FILE_LINE_DC);
 
 
-PHPAPI void mysqlnd_stats_init(MYSQLND_STATS ** stats);
+PHPAPI void mysqlnd_stats_init(MYSQLND_STATS ** stats, size_t statistic_count);
 PHPAPI void mysqlnd_stats_end(MYSQLND_STATS * stats);
 
 PHPAPI mysqlnd_stat_trigger mysqlnd_stats_set_trigger(MYSQLND_STATS * const stats, enum_mysqlnd_collected_stats stat, mysqlnd_stat_trigger trigger TSRMLS_DC);
index 963a87dfd30f71dd62b6eae9746264742a84b776..65611cce585f1376c90656cb9e0f127ef5f44fad 100644 (file)
@@ -214,8 +214,9 @@ typedef void (*mysqlnd_stat_trigger)(MYSQLND_STATS * stats, enum_mysqlnd_collect
 
 struct st_mysqlnd_stats
 {
-       uint64_t                                values[STAT_LAST];
+       uint64_t                                *values;
        mysqlnd_stat_trigger    *triggers;
+       size_t                                  count;
        zend_bool                               in_trigger;
 #ifdef ZTS
        MUTEX_T LOCK_access;