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