xref: /PHP-5.4/ext/mysqlnd/mysqlnd_statistics.c (revision c0d060f5)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 2006-2014 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: Georg Richter <georg@mysql.com>                             |
16   |          Andrey Hristov <andrey@mysql.com>                           |
17   |          Ulf Wendel <uwendel@mysql.com>                              |
18   +----------------------------------------------------------------------+
19 */
20 
21 /* $Id$ */
22 #include "php.h"
23 #include "mysqlnd.h"
24 #include "mysqlnd_priv.h"
25 #include "mysqlnd_statistics.h"
26 #include "mysqlnd_debug.h"
27 
28 
29 /* {{{ mysqlnd_stats_values_names
30  */
31 
32 const MYSQLND_STRING mysqlnd_stats_values_names[STAT_LAST] =
33 {
34 	{ MYSQLND_STR_W_LEN("bytes_sent") },
35 	{ MYSQLND_STR_W_LEN("bytes_received") },
36 	{ MYSQLND_STR_W_LEN("packets_sent") },
37 	{ MYSQLND_STR_W_LEN("packets_received") },
38 	{ MYSQLND_STR_W_LEN("protocol_overhead_in") },
39 	{ MYSQLND_STR_W_LEN("protocol_overhead_out") },
40 	{ MYSQLND_STR_W_LEN("bytes_received_ok_packet") },
41 	{ MYSQLND_STR_W_LEN("bytes_received_eof_packet") },
42 	{ MYSQLND_STR_W_LEN("bytes_received_rset_header_packet") },
43 	{ MYSQLND_STR_W_LEN("bytes_received_rset_field_meta_packet") },
44 	{ MYSQLND_STR_W_LEN("bytes_received_rset_row_packet") },
45 	{ MYSQLND_STR_W_LEN("bytes_received_prepare_response_packet") },
46 	{ MYSQLND_STR_W_LEN("bytes_received_change_user_packet") },
47 	{ MYSQLND_STR_W_LEN("packets_sent_command") },
48 	{ MYSQLND_STR_W_LEN("packets_received_ok") },
49 	{ MYSQLND_STR_W_LEN("packets_received_eof") },
50 	{ MYSQLND_STR_W_LEN("packets_received_rset_header") },
51 	{ MYSQLND_STR_W_LEN("packets_received_rset_field_meta") },
52 	{ MYSQLND_STR_W_LEN("packets_received_rset_row") },
53 	{ MYSQLND_STR_W_LEN("packets_received_prepare_response") },
54 	{ MYSQLND_STR_W_LEN("packets_received_change_user") },
55 	{ MYSQLND_STR_W_LEN("result_set_queries") },
56 	{ MYSQLND_STR_W_LEN("non_result_set_queries") },
57 	{ MYSQLND_STR_W_LEN("no_index_used") },
58 	{ MYSQLND_STR_W_LEN("bad_index_used") },
59 	{ MYSQLND_STR_W_LEN("slow_queries") },
60 	{ MYSQLND_STR_W_LEN("buffered_sets") },
61 	{ MYSQLND_STR_W_LEN("unbuffered_sets") },
62 	{ MYSQLND_STR_W_LEN("ps_buffered_sets") },
63 	{ MYSQLND_STR_W_LEN("ps_unbuffered_sets") },
64 	{ MYSQLND_STR_W_LEN("flushed_normal_sets") },
65 	{ MYSQLND_STR_W_LEN("flushed_ps_sets") },
66 	{ MYSQLND_STR_W_LEN("ps_prepared_never_executed") },
67 	{ MYSQLND_STR_W_LEN("ps_prepared_once_executed") },
68 	{ MYSQLND_STR_W_LEN("rows_fetched_from_server_normal") },
69 	{ MYSQLND_STR_W_LEN("rows_fetched_from_server_ps") },
70 	{ MYSQLND_STR_W_LEN("rows_buffered_from_client_normal") },
71 	{ MYSQLND_STR_W_LEN("rows_buffered_from_client_ps") },
72 	{ MYSQLND_STR_W_LEN("rows_fetched_from_client_normal_buffered") },
73 	{ MYSQLND_STR_W_LEN("rows_fetched_from_client_normal_unbuffered") },
74 	{ MYSQLND_STR_W_LEN("rows_fetched_from_client_ps_buffered") },
75 	{ MYSQLND_STR_W_LEN("rows_fetched_from_client_ps_unbuffered") },
76 	{ MYSQLND_STR_W_LEN("rows_fetched_from_client_ps_cursor") },
77 	{ MYSQLND_STR_W_LEN("rows_affected_normal") },
78 	{ MYSQLND_STR_W_LEN("rows_affected_ps") },
79 	{ MYSQLND_STR_W_LEN("rows_skipped_normal") },
80 	{ MYSQLND_STR_W_LEN("rows_skipped_ps") },
81 	{ MYSQLND_STR_W_LEN("copy_on_write_saved") },
82 	{ MYSQLND_STR_W_LEN("copy_on_write_performed") },
83 	{ MYSQLND_STR_W_LEN("command_buffer_too_small") },
84 	{ MYSQLND_STR_W_LEN("connect_success") },
85 	{ MYSQLND_STR_W_LEN("connect_failure") },
86 	{ MYSQLND_STR_W_LEN("connection_reused") },
87 	{ MYSQLND_STR_W_LEN("reconnect") },
88 	{ MYSQLND_STR_W_LEN("pconnect_success") },
89 	{ MYSQLND_STR_W_LEN("active_connections") },
90 	{ MYSQLND_STR_W_LEN("active_persistent_connections") },
91 	{ MYSQLND_STR_W_LEN("explicit_close") },
92 	{ MYSQLND_STR_W_LEN("implicit_close") },
93 	{ MYSQLND_STR_W_LEN("disconnect_close") },
94 	{ MYSQLND_STR_W_LEN("in_middle_of_command_close") },
95 	{ MYSQLND_STR_W_LEN("explicit_free_result") },
96 	{ MYSQLND_STR_W_LEN("implicit_free_result") },
97 	{ MYSQLND_STR_W_LEN("explicit_stmt_close") },
98 	{ MYSQLND_STR_W_LEN("implicit_stmt_close") },
99 	{ MYSQLND_STR_W_LEN("mem_emalloc_count") },
100 	{ MYSQLND_STR_W_LEN("mem_emalloc_amount") },
101 	{ MYSQLND_STR_W_LEN("mem_ecalloc_count") },
102 	{ MYSQLND_STR_W_LEN("mem_ecalloc_amount") },
103 	{ MYSQLND_STR_W_LEN("mem_erealloc_count") },
104 	{ MYSQLND_STR_W_LEN("mem_erealloc_amount") },
105 	{ MYSQLND_STR_W_LEN("mem_efree_count") },
106 	{ MYSQLND_STR_W_LEN("mem_efree_amount") },
107 	{ MYSQLND_STR_W_LEN("mem_malloc_count") },
108 	{ MYSQLND_STR_W_LEN("mem_malloc_amount") },
109 	{ MYSQLND_STR_W_LEN("mem_calloc_count") },
110 	{ MYSQLND_STR_W_LEN("mem_calloc_amount") },
111 	{ MYSQLND_STR_W_LEN("mem_realloc_count") },
112 	{ MYSQLND_STR_W_LEN("mem_realloc_amount") },
113 	{ MYSQLND_STR_W_LEN("mem_free_count") },
114 	{ MYSQLND_STR_W_LEN("mem_free_amount") },
115 	{ MYSQLND_STR_W_LEN("mem_estrndup_count") },
116 	{ MYSQLND_STR_W_LEN("mem_strndup_count") },
117 	{ MYSQLND_STR_W_LEN("mem_estndup_count") },
118 	{ MYSQLND_STR_W_LEN("mem_strdup_count") },
119 	{ MYSQLND_STR_W_LEN("proto_text_fetched_null") },
120 	{ MYSQLND_STR_W_LEN("proto_text_fetched_bit") },
121 	{ MYSQLND_STR_W_LEN("proto_text_fetched_tinyint") },
122 	{ MYSQLND_STR_W_LEN("proto_text_fetched_short") },
123 	{ MYSQLND_STR_W_LEN("proto_text_fetched_int24") },
124 	{ MYSQLND_STR_W_LEN("proto_text_fetched_int") },
125 	{ MYSQLND_STR_W_LEN("proto_text_fetched_bigint") },
126 	{ MYSQLND_STR_W_LEN("proto_text_fetched_decimal") },
127 	{ MYSQLND_STR_W_LEN("proto_text_fetched_float") },
128 	{ MYSQLND_STR_W_LEN("proto_text_fetched_double") },
129 	{ MYSQLND_STR_W_LEN("proto_text_fetched_date") },
130 	{ MYSQLND_STR_W_LEN("proto_text_fetched_year") },
131 	{ MYSQLND_STR_W_LEN("proto_text_fetched_time") },
132 	{ MYSQLND_STR_W_LEN("proto_text_fetched_datetime") },
133 	{ MYSQLND_STR_W_LEN("proto_text_fetched_timestamp") },
134 	{ MYSQLND_STR_W_LEN("proto_text_fetched_string") },
135 	{ MYSQLND_STR_W_LEN("proto_text_fetched_blob") },
136 	{ MYSQLND_STR_W_LEN("proto_text_fetched_enum") },
137 	{ MYSQLND_STR_W_LEN("proto_text_fetched_set") },
138 	{ MYSQLND_STR_W_LEN("proto_text_fetched_geometry") },
139 	{ MYSQLND_STR_W_LEN("proto_text_fetched_other") },
140 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_null") },
141 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_bit") },
142 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_tinyint") },
143 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_short") },
144 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_int24") },
145 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_int") },
146 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_bigint") },
147 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_decimal") },
148 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_float") },
149 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_double") },
150 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_date") },
151 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_year") },
152 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_time") },
153 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_datetime") },
154 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_timestamp") },
155 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_string") },
156 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_blob") },
157 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_enum") },
158 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_set") },
159 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_geometry") },
160 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_other") },
161 	{ MYSQLND_STR_W_LEN("init_command_executed_count") },
162 	{ MYSQLND_STR_W_LEN("init_command_failed_count") },
163 	{ MYSQLND_STR_W_LEN("com_quit") },
164 	{ MYSQLND_STR_W_LEN("com_init_db") },
165 	{ MYSQLND_STR_W_LEN("com_query") },
166 	{ MYSQLND_STR_W_LEN("com_field_list") },
167 	{ MYSQLND_STR_W_LEN("com_create_db") },
168 	{ MYSQLND_STR_W_LEN("com_drop_db") },
169 	{ MYSQLND_STR_W_LEN("com_refresh") },
170 	{ MYSQLND_STR_W_LEN("com_shutdown") },
171 	{ MYSQLND_STR_W_LEN("com_statistics") },
172 	{ MYSQLND_STR_W_LEN("com_process_info") },
173 	{ MYSQLND_STR_W_LEN("com_connect") },
174 	{ MYSQLND_STR_W_LEN("com_process_kill") },
175 	{ MYSQLND_STR_W_LEN("com_debug") },
176 	{ MYSQLND_STR_W_LEN("com_ping") },
177 	{ MYSQLND_STR_W_LEN("com_time") },
178 	{ MYSQLND_STR_W_LEN("com_delayed_insert") },
179 	{ MYSQLND_STR_W_LEN("com_change_user") },
180 	{ MYSQLND_STR_W_LEN("com_binlog_dump") },
181 	{ MYSQLND_STR_W_LEN("com_table_dump") },
182 	{ MYSQLND_STR_W_LEN("com_connect_out") },
183 	{ MYSQLND_STR_W_LEN("com_register_slave") },
184 	{ MYSQLND_STR_W_LEN("com_stmt_prepare") },
185 	{ MYSQLND_STR_W_LEN("com_stmt_execute") },
186 	{ MYSQLND_STR_W_LEN("com_stmt_send_long_data") },
187 	{ MYSQLND_STR_W_LEN("com_stmt_close") },
188 	{ MYSQLND_STR_W_LEN("com_stmt_reset") },
189 	{ MYSQLND_STR_W_LEN("com_stmt_set_option") },
190 	{ MYSQLND_STR_W_LEN("com_stmt_fetch") },
191 	{ MYSQLND_STR_W_LEN("com_deamon") },
192 	{ MYSQLND_STR_W_LEN("bytes_received_real_data_normal") },
193 	{ MYSQLND_STR_W_LEN("bytes_received_real_data_ps") }
194 };
195 /* }}} */
196 
197 
198 /* {{{ mysqlnd_fill_stats_hash */
199 PHPAPI void
mysqlnd_fill_stats_hash(const MYSQLND_STATS * const stats,const MYSQLND_STRING * names,zval * return_value TSRMLS_DC ZEND_FILE_LINE_DC)200 mysqlnd_fill_stats_hash(const MYSQLND_STATS * const stats, const MYSQLND_STRING * names, zval *return_value TSRMLS_DC ZEND_FILE_LINE_DC)
201 {
202 	unsigned int i;
203 
204 	mysqlnd_array_init(return_value, stats->count);
205 	for (i = 0; i < stats->count; i++) {
206 #if MYSQLND_UNICODE
207 		UChar *ustr, *tstr;
208 		int ulen, tlen;
209 #endif
210 		char tmp[25];
211 
212 		sprintf((char *)&tmp, MYSQLND_LLU_SPEC, stats->values[i]);
213 #if MYSQLND_UNICODE
214 		zend_string_to_unicode(UG(utf8_conv), &ustr, &ulen, names[i].s, names[i].l + 1 TSRMLS_CC);
215 		zend_string_to_unicode(UG(utf8_conv), &tstr, &tlen, tmp, strlen(tmp) + 1 TSRMLS_CC);
216 		add_u_assoc_unicode_ex(return_value, IS_UNICODE, ZSTR(ustr), ulen, tstr, 1);
217 		efree(ustr);
218 		efree(tstr);
219 #else
220 		add_assoc_string_ex(return_value, names[i].s, names[i].l + 1, tmp, 1);
221 #endif
222 	}
223 }
224 /* }}} */
225 
226 
227 /* {{{ _mysqlnd_get_client_stats */
228 PHPAPI void
_mysqlnd_get_client_stats(zval * return_value TSRMLS_DC ZEND_FILE_LINE_DC)229 _mysqlnd_get_client_stats(zval *return_value TSRMLS_DC ZEND_FILE_LINE_DC)
230 {
231 	MYSQLND_STATS stats, *stats_ptr = mysqlnd_global_stats;
232 	DBG_ENTER("_mysqlnd_get_client_stats");
233 	if (!stats_ptr) {
234 		memset(&stats, 0, sizeof(stats));
235 		stats_ptr = &stats;
236 	}
237 	mysqlnd_fill_stats_hash(stats_ptr, mysqlnd_stats_values_names, return_value TSRMLS_CC ZEND_FILE_LINE_CC);
238 	DBG_VOID_RETURN;
239 }
240 /* }}} */
241 
242 
243 /* {{{ mysqlnd_stats_init */
244 PHPAPI void
mysqlnd_stats_init(MYSQLND_STATS ** stats,size_t statistic_count)245 mysqlnd_stats_init(MYSQLND_STATS ** stats, size_t statistic_count)
246 {
247 	*stats = calloc(1, sizeof(MYSQLND_STATS));
248 	if (*stats == NULL) {
249 		return;
250 	}
251 	(*stats)->values = calloc(statistic_count, sizeof(uint64_t));
252 	(*stats)->triggers = calloc(statistic_count, sizeof(mysqlnd_stat_trigger));
253 	(*stats)->in_trigger = FALSE;
254 	(*stats)->count = statistic_count;
255 #ifdef ZTS
256 	(*stats)->LOCK_access = tsrm_mutex_alloc();
257 #endif
258 }
259 /* }}} */
260 
261 
262 /* {{{ mysqlnd_stats_end */
263 PHPAPI void
mysqlnd_stats_end(MYSQLND_STATS * stats)264 mysqlnd_stats_end(MYSQLND_STATS * stats)
265 {
266 #ifdef ZTS
267 	tsrm_mutex_free(stats->LOCK_access);
268 #endif
269 	free(stats->triggers);
270 	free(stats->values);
271 	/* mnd_free will reference LOCK_access and crash...*/
272 	free(stats);
273 }
274 /* }}} */
275 
276 
277 /* {{{ mysqlnd_stats_set_trigger */
278 PHPAPI mysqlnd_stat_trigger
mysqlnd_stats_set_trigger(MYSQLND_STATS * const stats,enum_mysqlnd_collected_stats statistic,mysqlnd_stat_trigger trigger TSRMLS_DC)279 mysqlnd_stats_set_trigger(MYSQLND_STATS * const stats, enum_mysqlnd_collected_stats statistic, mysqlnd_stat_trigger trigger TSRMLS_DC)
280 {
281 	mysqlnd_stat_trigger ret = NULL;
282 	DBG_ENTER("mysqlnd_stats_set_trigger");
283 	if (stats) {
284 		MYSQLND_STATS_LOCK(stats);
285 		ret = stats->triggers[statistic];
286 		stats->triggers[statistic] = trigger;
287 		MYSQLND_STATS_UNLOCK(stats);
288 	}
289 	DBG_RETURN(ret);
290 }
291 /* }}} */
292 
293 
294 /* {{{ mysqlnd_stats_set_handler */
295 PHPAPI mysqlnd_stat_trigger
mysqlnd_stats_reset_triggers(MYSQLND_STATS * const stats TSRMLS_DC)296 mysqlnd_stats_reset_triggers(MYSQLND_STATS * const stats TSRMLS_DC)
297 {
298 	mysqlnd_stat_trigger ret = NULL;
299 	DBG_ENTER("mysqlnd_stats_reset_trigger");
300 	if (stats) {
301 		MYSQLND_STATS_LOCK(stats);
302 		memset(stats->triggers, 0, stats->count * sizeof(mysqlnd_stat_trigger));
303 		MYSQLND_STATS_UNLOCK(stats);
304 	}
305 	DBG_RETURN(ret);
306 }
307 /* }}} */
308 
309 
310 /*
311  * Local variables:
312  * tab-width: 4
313  * c-basic-offset: 4
314  * End:
315  * vim600: noet sw=4 ts=4 fdm=marker
316  * vim<600: noet sw=4 ts=4
317  */
318