xref: /PHP-7.0/ext/mysqlnd/mysqlnd_statistics.h (revision 478f119a)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 2006-2017 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   |          Georg Richter <georg@php.net>                               |
18   +----------------------------------------------------------------------+
19 */
20 
21 #ifndef MYSQLND_STATISTICS_H
22 #define MYSQLND_STATISTICS_H
23 
24 
25 PHPAPI extern MYSQLND_STATS * mysqlnd_global_stats;
26 
27 extern const MYSQLND_STRING mysqlnd_stats_values_names[];
28 
29 #ifdef ZTS
30 #define MYSQLND_STATS_LOCK(stats) tsrm_mutex_lock((stats)->LOCK_access)
31 #define MYSQLND_STATS_UNLOCK(stats) tsrm_mutex_unlock((stats)->LOCK_access)
32 #else
33 #define MYSQLND_STATS_LOCK(stats)
34 #define MYSQLND_STATS_UNLOCK(stats)
35 #endif
36 
37 #ifndef MYSQLND_CORE_STATISTICS_TRIGGERS_DISABLED
38 #define MYSQLND_STAT_CALL_TRIGGER(s_array, statistic, val) \
39 			if ((s_array)->triggers[(statistic)] && (s_array)->in_trigger == FALSE) { \
40 				(s_array)->in_trigger = TRUE; \
41 				MYSQLND_STATS_UNLOCK((s_array)); \
42 																						\
43 				(s_array)->triggers[(statistic)]((s_array), (statistic), (val)); \
44 																						\
45 				MYSQLND_STATS_LOCK((s_array)); \
46 				(s_array)->in_trigger = FALSE; \
47 			}
48 #else
49 #define MYSQLND_STAT_CALL_TRIGGER(s_array, statistic, val)
50 #endif /* MYSQLND_CORE_STATISTICS_TRIGGERS_DISABLED */
51 
52 #define MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(stats, statistic, value) \
53 	{ \
54 			MYSQLND_STATS_LOCK(stats); \
55 			(stats)->values[(statistic)] += (value); \
56 			MYSQLND_STAT_CALL_TRIGGER((stats), (statistic), (value)); \
57 			MYSQLND_STATS_UNLOCK(_p_s); \
58 	}
59 
60 #define MYSQLND_DEC_STATISTIC(enabler, stats, statistic) \
61  { \
62 	enum_mysqlnd_collected_stats _s = (statistic);\
63 	MYSQLND_STATS * _p_s = (MYSQLND_STATS *) (stats); \
64 	if ((enabler) && _p_s && _s != _p_s->count) { \
65 		MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s, -1); \
66 	}\
67  }
68 
69 #define MYSQLND_INC_STATISTIC(enabler, stats, statistic) \
70  { \
71 	enum_mysqlnd_collected_stats _s = (statistic);\
72 	MYSQLND_STATS * _p_s = (MYSQLND_STATS *) (stats); \
73 	if ((enabler) && _p_s && _s != _p_s->count) { \
74 		MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s, 1); \
75 	}\
76  }
77 
78 #define MYSQLND_INC_STATISTIC_W_VALUE(enabler, stats, statistic, value) \
79  { \
80 	enum_mysqlnd_collected_stats _s = (statistic);\
81 	MYSQLND_STATS * _p_s = (MYSQLND_STATS *) (stats); \
82 	if ((enabler) && _p_s && _s != _p_s->count) { \
83 		uint64_t v = (uint64_t) (value); \
84 		MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s, v); \
85 	}\
86  }
87 
88 #define MYSQLND_INC_STATISTIC_W_VALUE2(enabler, stats, statistic1, value1, statistic2, value2) \
89  { \
90 	MYSQLND_STATS * _p_s = (MYSQLND_STATS *) (stats); \
91 	if ((enabler) && _p_s) { \
92 		uint64_t v1 = (uint64_t) (value1); \
93 		uint64_t v2 = (uint64_t) (value2); \
94 		enum_mysqlnd_collected_stats _s1 = (statistic1);\
95 		enum_mysqlnd_collected_stats _s2 = (statistic2);\
96 		if (_s1 != _p_s->count) MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s1, v1); \
97 		if (_s2 != _p_s->count) MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s2, v2); \
98 	}\
99  }
100 
101 #define MYSQLND_INC_STATISTIC_W_VALUE3(enabler, stats, statistic1, value1, statistic2, value2, statistic3, value3) \
102  { \
103 	MYSQLND_STATS * _p_s = (MYSQLND_STATS *) (stats); \
104 	if ((enabler) && _p_s) { \
105 		uint64_t v1 = (uint64_t) (value1); \
106 		uint64_t v2 = (uint64_t) (value2); \
107 		uint64_t v3 = (uint64_t) (value3); \
108 		enum_mysqlnd_collected_stats _s1 = (statistic1);\
109 		enum_mysqlnd_collected_stats _s2 = (statistic2);\
110 		enum_mysqlnd_collected_stats _s3 = (statistic3);\
111 		if (_s1 != _p_s->count) MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s1, v1); \
112 		if (_s2 != _p_s->count) MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s2, v2); \
113 		if (_s3 != _p_s->count) MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s3, v3); \
114 	}\
115  }
116 
117 
118 
119 #ifndef MYSQLND_CORE_STATISTICS_DISABLED
120 
121 #define MYSQLND_INC_GLOBAL_STATISTIC(statistic) \
122 	MYSQLND_INC_STATISTIC(MYSQLND_G(collect_statistics), mysqlnd_global_stats, (statistic))
123 
124 #define MYSQLND_DEC_CONN_STATISTIC(conn_stats, statistic) \
125 	MYSQLND_DEC_STATISTIC(MYSQLND_G(collect_statistics), mysqlnd_global_stats, (statistic))
126 
127 #define MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(statistic1, value1, statistic2, value2) \
128 	MYSQLND_INC_STATISTIC_W_VALUE2(MYSQLND_G(collect_statistics), mysqlnd_global_stats, (statistic1), (value1), (statistic2), (value2))
129 
130 #define MYSQLND_INC_CONN_STATISTIC(conn_stats, statistic) \
131 	MYSQLND_INC_STATISTIC(MYSQLND_G(collect_statistics), mysqlnd_global_stats, (statistic)); \
132 	MYSQLND_INC_STATISTIC(MYSQLND_G(collect_statistics), (conn_stats), (statistic));
133 
134 #define MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn_stats, statistic, value) \
135 	MYSQLND_INC_STATISTIC_W_VALUE(MYSQLND_G(collect_statistics), mysqlnd_global_stats, (statistic), (value)); \
136 	MYSQLND_INC_STATISTIC_W_VALUE(MYSQLND_G(collect_statistics), (conn_stats), (statistic), (value));
137 
138 #define MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn_stats, statistic1, value1, statistic2, value2) \
139 	MYSQLND_INC_STATISTIC_W_VALUE2(MYSQLND_G(collect_statistics), mysqlnd_global_stats, (statistic1), (value1), (statistic2), (value2)); \
140 	MYSQLND_INC_STATISTIC_W_VALUE2(MYSQLND_G(collect_statistics), (conn_stats), (statistic1), (value1), (statistic2), (value2));
141 
142 #define MYSQLND_INC_CONN_STATISTIC_W_VALUE3(conn_stats, statistic1, value1, statistic2, value2, statistic3, value3) \
143 	MYSQLND_INC_STATISTIC_W_VALUE3(MYSQLND_G(collect_statistics), mysqlnd_global_stats, (statistic1), (value1), (statistic2), (value2), (statistic3), (value3)); \
144 	MYSQLND_INC_STATISTIC_W_VALUE3(MYSQLND_G(collect_statistics), (conn_stats), (statistic1), (value1), (statistic2), (value2), (statistic3), (value3));
145 
146 #else
147 
148 #define MYSQLND_INC_GLOBAL_STATISTIC(statistic)
149 #define MYSQLND_DEC_CONN_STATISTIC(conn_stats, statistic)
150 #define MYSQLND_INC_GLOBAL_STATISTIC_W_VALUE2(statistic1, value1, statistic2, value2)
151 #define MYSQLND_INC_CONN_STATISTIC(conn_stats, statistic)
152 #define MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn_stats, statistic, value)
153 #define MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn_stats, statistic1, value1, statistic2, value2)
154 #define MYSQLND_INC_CONN_STATISTIC_W_VALUE3(conn_stats, statistic1, value1, statistic2, value2, statistic3, value3)
155 
156 #endif /* MYSQLND_CORE_STATISTICS_DISABLED */
157 
158 PHPAPI void mysqlnd_fill_stats_hash(const MYSQLND_STATS * const stats, const MYSQLND_STRING * names, zval *return_value ZEND_FILE_LINE_DC);
159 
160 PHPAPI void mysqlnd_stats_init(MYSQLND_STATS ** stats, size_t statistic_count, int persistent);
161 PHPAPI void mysqlnd_stats_end(MYSQLND_STATS * stats, int persistent);
162 
163 PHPAPI mysqlnd_stat_trigger mysqlnd_stats_set_trigger(MYSQLND_STATS * const stats, enum_mysqlnd_collected_stats stat, mysqlnd_stat_trigger trigger);
164 PHPAPI mysqlnd_stat_trigger mysqlnd_stats_reset_triggers(MYSQLND_STATS * const stats);
165 
166 #endif	/* MYSQLND_STATISTICS_H */
167 
168 /*
169  * Local variables:
170  * tab-width: 4
171  * c-basic-offset: 4
172  * End:
173  * vim600: noet sw=4 ts=4 fdm=marker
174  * vim<600: noet sw=4 ts=4
175  */
176