xref: /PHP-7.2/ext/mysqlnd/mysqlnd_statistics.h (revision 7a7ec01a)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 2006-2018 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: Andrey Hristov <andrey@php.net>                             |
16   |          Ulf Wendel <uw@php.net>                                     |
17   +----------------------------------------------------------------------+
18 */
19 
20 #ifndef MYSQLND_STATISTICS_H
21 #define MYSQLND_STATISTICS_H
22 
23 #ifdef ZTS
24 #define MYSQLND_STATS_LOCK(stats) tsrm_mutex_lock((stats)->LOCK_access)
25 #define MYSQLND_STATS_UNLOCK(stats) tsrm_mutex_unlock((stats)->LOCK_access)
26 #else
27 #define MYSQLND_STATS_LOCK(stats)
28 #define MYSQLND_STATS_UNLOCK(stats)
29 #endif
30 
31 #ifndef MYSQLND_CORE_STATISTICS_TRIGGERS_DISABLED
32 #define MYSQLND_STAT_CALL_TRIGGER(s_array, statistic, val) \
33 			if ((s_array)->triggers[(statistic)] && (s_array)->in_trigger == FALSE) { \
34 				(s_array)->in_trigger = TRUE; \
35 				MYSQLND_STATS_UNLOCK((s_array)); \
36 																						\
37 				(s_array)->triggers[(statistic)]((s_array), (statistic), (val)); \
38 																						\
39 				MYSQLND_STATS_LOCK((s_array)); \
40 				(s_array)->in_trigger = FALSE; \
41 			}
42 #else
43 #define MYSQLND_STAT_CALL_TRIGGER(s_array, statistic, val)
44 #endif /* MYSQLND_CORE_STATISTICS_TRIGGERS_DISABLED */
45 
46 #define MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(stats, statistic, value) \
47 	{ \
48 			MYSQLND_STATS_LOCK(stats); \
49 			(stats)->values[(statistic)] += (value); \
50 			MYSQLND_STAT_CALL_TRIGGER((stats), (statistic), (value)); \
51 			MYSQLND_STATS_UNLOCK(_p_s); \
52 	}
53 
54 #define MYSQLND_DEC_STATISTIC(enabler, stats, statistic) \
55  { \
56 	enum_mysqlnd_collected_stats _s = (statistic);\
57 	MYSQLND_STATS * _p_s = (MYSQLND_STATS *) (stats); \
58 	if ((enabler) && _p_s && _s != _p_s->count) { \
59 		MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s, -1); \
60 	}\
61  }
62 
63 #define MYSQLND_INC_STATISTIC(enabler, stats, statistic) \
64  { \
65 	enum_mysqlnd_collected_stats _s = (statistic);\
66 	MYSQLND_STATS * _p_s = (MYSQLND_STATS *) (stats); \
67 	if ((enabler) && _p_s && _s != _p_s->count) { \
68 		MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s, 1); \
69 	}\
70  }
71 
72 #define MYSQLND_INC_STATISTIC_W_VALUE(enabler, stats, statistic, value) \
73  { \
74 	enum_mysqlnd_collected_stats _s = (statistic);\
75 	MYSQLND_STATS * _p_s = (MYSQLND_STATS *) (stats); \
76 	if ((enabler) && _p_s && _s != _p_s->count) { \
77 		uint64_t v = (uint64_t) (value); \
78 		MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s, v); \
79 	}\
80  }
81 
82 #define MYSQLND_INC_STATISTIC_W_VALUE2(enabler, stats, statistic1, value1, statistic2, value2) \
83  { \
84 	MYSQLND_STATS * _p_s = (MYSQLND_STATS *) (stats); \
85 	if ((enabler) && _p_s) { \
86 		uint64_t v1 = (uint64_t) (value1); \
87 		uint64_t v2 = (uint64_t) (value2); \
88 		enum_mysqlnd_collected_stats _s1 = (statistic1);\
89 		enum_mysqlnd_collected_stats _s2 = (statistic2);\
90 		if (_s1 != _p_s->count) MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s1, v1); \
91 		if (_s2 != _p_s->count) MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s2, v2); \
92 	}\
93  }
94 
95 #define MYSQLND_INC_STATISTIC_W_VALUE3(enabler, stats, statistic1, value1, statistic2, value2, statistic3, value3) \
96  { \
97 	MYSQLND_STATS * _p_s = (MYSQLND_STATS *) (stats); \
98 	if ((enabler) && _p_s) { \
99 		uint64_t v1 = (uint64_t) (value1); \
100 		uint64_t v2 = (uint64_t) (value2); \
101 		uint64_t v3 = (uint64_t) (value3); \
102 		enum_mysqlnd_collected_stats _s1 = (statistic1);\
103 		enum_mysqlnd_collected_stats _s2 = (statistic2);\
104 		enum_mysqlnd_collected_stats _s3 = (statistic3);\
105 		if (_s1 != _p_s->count) MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s1, v1); \
106 		if (_s2 != _p_s->count) MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s2, v2); \
107 		if (_s3 != _p_s->count) MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s3, v3); \
108 	}\
109  }
110 
111 
112 
113 PHPAPI void mysqlnd_stats_init(MYSQLND_STATS ** stats, const size_t statistic_count, const zend_bool persistent);
114 PHPAPI void mysqlnd_stats_end(MYSQLND_STATS * stats, const zend_bool persistent);
115 
116 PHPAPI void mysqlnd_fill_stats_hash(const MYSQLND_STATS * const stats, const MYSQLND_STRING * names, zval *return_value ZEND_FILE_LINE_DC);
117 
118 PHPAPI mysqlnd_stat_trigger mysqlnd_stats_set_trigger(MYSQLND_STATS * const stats, enum_mysqlnd_collected_stats stat, mysqlnd_stat_trigger trigger);
119 PHPAPI mysqlnd_stat_trigger mysqlnd_stats_reset_triggers(MYSQLND_STATS * const stats);
120 
121 #endif	/* MYSQLND_STATISTICS_H */
122 
123 /*
124  * Local variables:
125  * tab-width: 4
126  * c-basic-offset: 4
127  * End:
128  * vim600: noet sw=4 ts=4 fdm=marker
129  * vim<600: noet sw=4 ts=4
130  */
131