xref: /PHP-8.4/ext/mysqlnd/mysqlnd_statistics.c (revision b1fd3477)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | https://www.php.net/license/3_01.txt                                 |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Authors: Andrey Hristov <andrey@php.net>                             |
14   |          Ulf Wendel <uw@php.net>                                     |
15   |          Georg Richter <georg@php.net>                               |
16   +----------------------------------------------------------------------+
17 */
18 
19 #include "php.h"
20 #include "mysqlnd.h"
21 #include "mysqlnd_priv.h"
22 #include "mysqlnd_statistics.h"
23 #include "mysqlnd_debug.h"
24 
25 
26 /* {{{ mysqlnd_stats_values_names */
27 const MYSQLND_STRING mysqlnd_stats_values_names[STAT_LAST] =
28 {
29 	{ MYSQLND_STR_W_LEN("bytes_sent") },
30 	{ MYSQLND_STR_W_LEN("bytes_received") },
31 	{ MYSQLND_STR_W_LEN("packets_sent") },
32 	{ MYSQLND_STR_W_LEN("packets_received") },
33 	{ MYSQLND_STR_W_LEN("protocol_overhead_in") },
34 	{ MYSQLND_STR_W_LEN("protocol_overhead_out") },
35 	{ MYSQLND_STR_W_LEN("bytes_received_ok_packet") },
36 	{ MYSQLND_STR_W_LEN("bytes_received_eof_packet") },
37 	{ MYSQLND_STR_W_LEN("bytes_received_rset_header_packet") },
38 	{ MYSQLND_STR_W_LEN("bytes_received_rset_field_meta_packet") },
39 	{ MYSQLND_STR_W_LEN("bytes_received_rset_row_packet") },
40 	{ MYSQLND_STR_W_LEN("bytes_received_prepare_response_packet") },
41 	{ MYSQLND_STR_W_LEN("bytes_received_change_user_packet") },
42 	{ MYSQLND_STR_W_LEN("packets_sent_command") },
43 	{ MYSQLND_STR_W_LEN("packets_received_ok") },
44 	{ MYSQLND_STR_W_LEN("packets_received_eof") },
45 	{ MYSQLND_STR_W_LEN("packets_received_rset_header") },
46 	{ MYSQLND_STR_W_LEN("packets_received_rset_field_meta") },
47 	{ MYSQLND_STR_W_LEN("packets_received_rset_row") },
48 	{ MYSQLND_STR_W_LEN("packets_received_prepare_response") },
49 	{ MYSQLND_STR_W_LEN("packets_received_change_user") },
50 	{ MYSQLND_STR_W_LEN("result_set_queries") },
51 	{ MYSQLND_STR_W_LEN("non_result_set_queries") },
52 	{ MYSQLND_STR_W_LEN("no_index_used") },
53 	{ MYSQLND_STR_W_LEN("bad_index_used") },
54 	{ MYSQLND_STR_W_LEN("slow_queries") },
55 	{ MYSQLND_STR_W_LEN("buffered_sets") },
56 	{ MYSQLND_STR_W_LEN("unbuffered_sets") },
57 	{ MYSQLND_STR_W_LEN("ps_buffered_sets") },
58 	{ MYSQLND_STR_W_LEN("ps_unbuffered_sets") },
59 	{ MYSQLND_STR_W_LEN("flushed_normal_sets") },
60 	{ MYSQLND_STR_W_LEN("flushed_ps_sets") },
61 	{ MYSQLND_STR_W_LEN("ps_prepared_never_executed") },
62 	{ MYSQLND_STR_W_LEN("ps_prepared_once_executed") },
63 	{ MYSQLND_STR_W_LEN("rows_fetched_from_server_normal") },
64 	{ MYSQLND_STR_W_LEN("rows_fetched_from_server_ps") },
65 	{ MYSQLND_STR_W_LEN("rows_buffered_from_client_normal") },
66 	{ MYSQLND_STR_W_LEN("rows_buffered_from_client_ps") },
67 	{ MYSQLND_STR_W_LEN("rows_fetched_from_client_normal_buffered") },
68 	{ MYSQLND_STR_W_LEN("rows_fetched_from_client_normal_unbuffered") },
69 	{ MYSQLND_STR_W_LEN("rows_fetched_from_client_ps_buffered") },
70 	{ MYSQLND_STR_W_LEN("rows_fetched_from_client_ps_unbuffered") },
71 	{ MYSQLND_STR_W_LEN("rows_fetched_from_client_ps_cursor") },
72 	{ MYSQLND_STR_W_LEN("rows_affected_normal") },
73 	{ MYSQLND_STR_W_LEN("rows_affected_ps") },
74 	{ MYSQLND_STR_W_LEN("rows_skipped_normal") },
75 	{ MYSQLND_STR_W_LEN("rows_skipped_ps") },
76 	{ MYSQLND_STR_W_LEN("copy_on_write_saved") },
77 	{ MYSQLND_STR_W_LEN("copy_on_write_performed") },
78 	{ MYSQLND_STR_W_LEN("command_buffer_too_small") },
79 	{ MYSQLND_STR_W_LEN("connect_success") },
80 	{ MYSQLND_STR_W_LEN("connect_failure") },
81 	{ MYSQLND_STR_W_LEN("connection_reused") },
82 	{ MYSQLND_STR_W_LEN("reconnect") },
83 	{ MYSQLND_STR_W_LEN("pconnect_success") },
84 	{ MYSQLND_STR_W_LEN("active_connections") },
85 	{ MYSQLND_STR_W_LEN("active_persistent_connections") },
86 	{ MYSQLND_STR_W_LEN("explicit_close") },
87 	{ MYSQLND_STR_W_LEN("implicit_close") },
88 	{ MYSQLND_STR_W_LEN("disconnect_close") },
89 	{ MYSQLND_STR_W_LEN("in_middle_of_command_close") },
90 	{ MYSQLND_STR_W_LEN("explicit_free_result") },
91 	{ MYSQLND_STR_W_LEN("implicit_free_result") },
92 	{ MYSQLND_STR_W_LEN("explicit_stmt_close") },
93 	{ MYSQLND_STR_W_LEN("implicit_stmt_close") },
94 	{ MYSQLND_STR_W_LEN("mem_emalloc_count") },
95 	{ MYSQLND_STR_W_LEN("mem_emalloc_amount") },
96 	{ MYSQLND_STR_W_LEN("mem_ecalloc_count") },
97 	{ MYSQLND_STR_W_LEN("mem_ecalloc_amount") },
98 	{ MYSQLND_STR_W_LEN("mem_erealloc_count") },
99 	{ MYSQLND_STR_W_LEN("mem_erealloc_amount") },
100 	{ MYSQLND_STR_W_LEN("mem_efree_count") },
101 	{ MYSQLND_STR_W_LEN("mem_efree_amount") },
102 	{ MYSQLND_STR_W_LEN("mem_malloc_count") },
103 	{ MYSQLND_STR_W_LEN("mem_malloc_amount") },
104 	{ MYSQLND_STR_W_LEN("mem_calloc_count") },
105 	{ MYSQLND_STR_W_LEN("mem_calloc_amount") },
106 	{ MYSQLND_STR_W_LEN("mem_realloc_count") },
107 	{ MYSQLND_STR_W_LEN("mem_realloc_amount") },
108 	{ MYSQLND_STR_W_LEN("mem_free_count") },
109 	{ MYSQLND_STR_W_LEN("mem_free_amount") },
110 	{ MYSQLND_STR_W_LEN("mem_estrndup_count") },
111 	{ MYSQLND_STR_W_LEN("mem_strndup_count") },
112 	{ MYSQLND_STR_W_LEN("mem_estrdup_count") },
113 	{ MYSQLND_STR_W_LEN("mem_strdup_count") },
114 	{ MYSQLND_STR_W_LEN("mem_edupl_count") },
115 	{ MYSQLND_STR_W_LEN("mem_dupl_count") },
116 	{ MYSQLND_STR_W_LEN("proto_text_fetched_null") },
117 	{ MYSQLND_STR_W_LEN("proto_text_fetched_bit") },
118 	{ MYSQLND_STR_W_LEN("proto_text_fetched_tinyint") },
119 	{ MYSQLND_STR_W_LEN("proto_text_fetched_short") },
120 	{ MYSQLND_STR_W_LEN("proto_text_fetched_int24") },
121 	{ MYSQLND_STR_W_LEN("proto_text_fetched_int") },
122 	{ MYSQLND_STR_W_LEN("proto_text_fetched_bigint") },
123 	{ MYSQLND_STR_W_LEN("proto_text_fetched_decimal") },
124 	{ MYSQLND_STR_W_LEN("proto_text_fetched_float") },
125 	{ MYSQLND_STR_W_LEN("proto_text_fetched_double") },
126 	{ MYSQLND_STR_W_LEN("proto_text_fetched_date") },
127 	{ MYSQLND_STR_W_LEN("proto_text_fetched_year") },
128 	{ MYSQLND_STR_W_LEN("proto_text_fetched_time") },
129 	{ MYSQLND_STR_W_LEN("proto_text_fetched_datetime") },
130 	{ MYSQLND_STR_W_LEN("proto_text_fetched_timestamp") },
131 	{ MYSQLND_STR_W_LEN("proto_text_fetched_string") },
132 	{ MYSQLND_STR_W_LEN("proto_text_fetched_blob") },
133 	{ MYSQLND_STR_W_LEN("proto_text_fetched_enum") },
134 	{ MYSQLND_STR_W_LEN("proto_text_fetched_set") },
135 	{ MYSQLND_STR_W_LEN("proto_text_fetched_geometry") },
136 	{ MYSQLND_STR_W_LEN("proto_text_fetched_other") },
137 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_null") },
138 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_bit") },
139 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_tinyint") },
140 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_short") },
141 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_int24") },
142 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_int") },
143 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_bigint") },
144 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_decimal") },
145 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_float") },
146 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_double") },
147 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_date") },
148 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_year") },
149 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_time") },
150 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_datetime") },
151 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_timestamp") },
152 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_string") },
153 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_json") },
154 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_blob") },
155 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_enum") },
156 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_set") },
157 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_geometry") },
158 	{ MYSQLND_STR_W_LEN("proto_binary_fetched_other") },
159 	{ MYSQLND_STR_W_LEN("init_command_executed_count") },
160 	{ MYSQLND_STR_W_LEN("init_command_failed_count") },
161 	{ MYSQLND_STR_W_LEN("com_quit") },
162 	{ MYSQLND_STR_W_LEN("com_init_db") },
163 	{ MYSQLND_STR_W_LEN("com_query") },
164 	{ MYSQLND_STR_W_LEN("com_field_list") },
165 	{ MYSQLND_STR_W_LEN("com_create_db") },
166 	{ MYSQLND_STR_W_LEN("com_drop_db") },
167 	{ MYSQLND_STR_W_LEN("com_refresh") },
168 	{ MYSQLND_STR_W_LEN("com_shutdown") },
169 	{ MYSQLND_STR_W_LEN("com_statistics") },
170 	{ MYSQLND_STR_W_LEN("com_process_info") },
171 	{ MYSQLND_STR_W_LEN("com_connect") },
172 	{ MYSQLND_STR_W_LEN("com_process_kill") },
173 	{ MYSQLND_STR_W_LEN("com_debug") },
174 	{ MYSQLND_STR_W_LEN("com_ping") },
175 	{ MYSQLND_STR_W_LEN("com_time") },
176 	{ MYSQLND_STR_W_LEN("com_delayed_insert") },
177 	{ MYSQLND_STR_W_LEN("com_change_user") },
178 	{ MYSQLND_STR_W_LEN("com_binlog_dump") },
179 	{ MYSQLND_STR_W_LEN("com_table_dump") },
180 	{ MYSQLND_STR_W_LEN("com_connect_out") },
181 	{ MYSQLND_STR_W_LEN("com_register_slave") },
182 	{ MYSQLND_STR_W_LEN("com_stmt_prepare") },
183 	{ MYSQLND_STR_W_LEN("com_stmt_execute") },
184 	{ MYSQLND_STR_W_LEN("com_stmt_send_long_data") },
185 	{ MYSQLND_STR_W_LEN("com_stmt_close") },
186 	{ MYSQLND_STR_W_LEN("com_stmt_reset") },
187 	{ MYSQLND_STR_W_LEN("com_stmt_set_option") },
188 	{ MYSQLND_STR_W_LEN("com_stmt_fetch") },
189 	{ MYSQLND_STR_W_LEN("com_deamon") },
190 	{ MYSQLND_STR_W_LEN("bytes_received_real_data_normal") },
191 	{ MYSQLND_STR_W_LEN("bytes_received_real_data_ps") }
192 };
193 /* }}} */
194 
195 
196 /* {{{ mysqlnd_fill_stats_hash */
197 PHPAPI void
mysqlnd_fill_stats_hash(const MYSQLND_STATS * const stats,const MYSQLND_STRING * names,zval * return_value ZEND_FILE_LINE_DC)198 mysqlnd_fill_stats_hash(const MYSQLND_STATS * const stats, const MYSQLND_STRING * names, zval *return_value ZEND_FILE_LINE_DC)
199 {
200 	unsigned int i;
201 
202 	array_init_size(return_value, stats->count);
203 	for (i = 0; i < stats->count; i++) {
204 		char tmp[25];
205 
206 		snprintf(tmp, sizeof(tmp), "%" PRIu64, stats->values[i]);
207 		add_assoc_string_ex(return_value, names[i].s, names[i].l, tmp);
208 	}
209 }
210 /* }}} */
211 
212 
213 /* {{{ mysqlnd_stats_init */
214 PHPAPI void
mysqlnd_stats_init(MYSQLND_STATS ** stats,const size_t statistic_count,const bool persistent)215 mysqlnd_stats_init(MYSQLND_STATS ** stats, const size_t statistic_count, const bool persistent)
216 {
217 	*stats = pecalloc(1, sizeof(MYSQLND_STATS), persistent);
218 	(*stats)->values = pecalloc(statistic_count, sizeof(uint64_t), persistent);
219 	(*stats)->count = statistic_count;
220 #ifdef ZTS
221 	(*stats)->LOCK_access = tsrm_mutex_alloc();
222 #endif
223 }
224 /* }}} */
225 
226 
227 /* {{{ mysqlnd_stats_end */
228 PHPAPI void
mysqlnd_stats_end(MYSQLND_STATS * stats,const bool persistent)229 mysqlnd_stats_end(MYSQLND_STATS * stats, const bool persistent)
230 {
231 #ifdef ZTS
232 	tsrm_mutex_free(stats->LOCK_access);
233 #endif
234 	pefree(stats->values, persistent);
235 	/* mnd_free will reference LOCK_access and crash...*/
236 	pefree(stats, persistent);
237 }
238 /* }}} */
239 
240 
241 /************ MYSQLND specific code **********/
242 
243 /* {{{ _mysqlnd_get_client_stats */
244 PHPAPI void
_mysqlnd_get_client_stats(MYSQLND_STATS * stats_ptr,zval * return_value ZEND_FILE_LINE_DC)245 _mysqlnd_get_client_stats(MYSQLND_STATS * stats_ptr, zval *return_value ZEND_FILE_LINE_DC)
246 {
247 	MYSQLND_STATS stats;
248 	DBG_ENTER("_mysqlnd_get_client_stats");
249 	if (!stats_ptr) {
250 		memset(&stats, 0, sizeof(stats));
251 		stats_ptr = &stats;
252 	}
253 	mysqlnd_fill_stats_hash(stats_ptr, mysqlnd_stats_values_names, return_value ZEND_FILE_LINE_CC);
254 	DBG_VOID_RETURN;
255 }
256 /* }}} */
257