mysqlnd_statistics.c 12 KB

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