Back to index

php5  5.3.10
mysqlnd_statistics.c
Go to the documentation of this file.
00001 /*
00002   +----------------------------------------------------------------------+
00003   | PHP Version 5                                                        |
00004   +----------------------------------------------------------------------+
00005   | Copyright (c) 2006-2012 The PHP Group                                |
00006   +----------------------------------------------------------------------+
00007   | This source file is subject to version 3.01 of the PHP license,      |
00008   | that is bundled with this package in the file LICENSE, and is        |
00009   | available through the world-wide-web at the following url:           |
00010   | http://www.php.net/license/3_01.txt                                  |
00011   | If you did not receive a copy of the PHP license and are unable to   |
00012   | obtain it through the world-wide-web, please send a note to          |
00013   | license@php.net so we can mail you a copy immediately.               |
00014   +----------------------------------------------------------------------+
00015   | Authors: Georg Richter <georg@mysql.com>                             |
00016   |          Andrey Hristov <andrey@mysql.com>                           |
00017   |          Ulf Wendel <uwendel@mysql.com>                              |
00018   +----------------------------------------------------------------------+
00019 */
00020 
00021 /* $Id: mysqlnd_statistics.c 321634 2012-01-01 13:15:04Z felipe $ */
00022 #include "php.h"
00023 #include "mysqlnd.h"
00024 #include "mysqlnd_priv.h"
00025 #include "mysqlnd_statistics.h"
00026 #include "mysqlnd_debug.h"
00027 
00028 
00029 #define STR_W_LEN(str)  str, (sizeof(str) - 1)
00030 
00031 /* {{{ mysqlnd_stats_values_names 
00032  */
00033 
00034 const MYSQLND_STRING mysqlnd_stats_values_names[STAT_LAST] =
00035 {
00036        { STR_W_LEN("bytes_sent") },
00037        { STR_W_LEN("bytes_received") }, 
00038        { STR_W_LEN("packets_sent") },
00039        { STR_W_LEN("packets_received") },
00040        { STR_W_LEN("protocol_overhead_in") },
00041        { STR_W_LEN("protocol_overhead_out") },
00042        { STR_W_LEN("bytes_received_ok_packet") }, 
00043        { STR_W_LEN("bytes_received_eof_packet") }, 
00044        { STR_W_LEN("bytes_received_rset_header_packet") }, 
00045        { STR_W_LEN("bytes_received_rset_field_meta_packet") }, 
00046        { STR_W_LEN("bytes_received_rset_row_packet") }, 
00047        { STR_W_LEN("bytes_received_prepare_response_packet") }, 
00048        { STR_W_LEN("bytes_received_change_user_packet") }, 
00049        { STR_W_LEN("packets_sent_command") }, 
00050        { STR_W_LEN("packets_received_ok") }, 
00051        { STR_W_LEN("packets_received_eof") }, 
00052        { STR_W_LEN("packets_received_rset_header") }, 
00053        { STR_W_LEN("packets_received_rset_field_meta") }, 
00054        { STR_W_LEN("packets_received_rset_row") }, 
00055        { STR_W_LEN("packets_received_prepare_response") }, 
00056        { STR_W_LEN("packets_received_change_user") }, 
00057        { STR_W_LEN("result_set_queries") },
00058        { STR_W_LEN("non_result_set_queries") },
00059        { STR_W_LEN("no_index_used") },
00060        { STR_W_LEN("bad_index_used") },
00061        { STR_W_LEN("slow_queries") },
00062        { STR_W_LEN("buffered_sets") },
00063        { STR_W_LEN("unbuffered_sets") },
00064        { STR_W_LEN("ps_buffered_sets") },
00065        { STR_W_LEN("ps_unbuffered_sets") },
00066        { STR_W_LEN("flushed_normal_sets") },
00067        { STR_W_LEN("flushed_ps_sets") },
00068        { STR_W_LEN("ps_prepared_never_executed") },
00069        { STR_W_LEN("ps_prepared_once_executed") },
00070        { STR_W_LEN("rows_fetched_from_server_normal") },
00071        { STR_W_LEN("rows_fetched_from_server_ps") },
00072        { STR_W_LEN("rows_buffered_from_client_normal") },
00073        { STR_W_LEN("rows_buffered_from_client_ps") },
00074        { STR_W_LEN("rows_fetched_from_client_normal_buffered") },
00075        { STR_W_LEN("rows_fetched_from_client_normal_unbuffered") },
00076        { STR_W_LEN("rows_fetched_from_client_ps_buffered") },
00077        { STR_W_LEN("rows_fetched_from_client_ps_unbuffered") },
00078        { STR_W_LEN("rows_fetched_from_client_ps_cursor") },
00079        { STR_W_LEN("rows_affected_normal") },
00080        { STR_W_LEN("rows_affected_ps") },
00081        { STR_W_LEN("rows_skipped_normal") },
00082        { STR_W_LEN("rows_skipped_ps") },
00083        { STR_W_LEN("copy_on_write_saved") },
00084        { STR_W_LEN("copy_on_write_performed") },
00085        { STR_W_LEN("command_buffer_too_small") },
00086        { STR_W_LEN("connect_success") },
00087        { STR_W_LEN("connect_failure") },
00088        { STR_W_LEN("connection_reused") },
00089        { STR_W_LEN("reconnect") },
00090        { STR_W_LEN("pconnect_success") },
00091        { STR_W_LEN("active_connections") },
00092        { STR_W_LEN("active_persistent_connections") },
00093        { STR_W_LEN("explicit_close") },
00094        { STR_W_LEN("implicit_close") },
00095        { STR_W_LEN("disconnect_close") },
00096        { STR_W_LEN("in_middle_of_command_close") },
00097        { STR_W_LEN("explicit_free_result") },
00098        { STR_W_LEN("implicit_free_result") },
00099        { STR_W_LEN("explicit_stmt_close") },
00100        { STR_W_LEN("implicit_stmt_close") },
00101        { STR_W_LEN("mem_emalloc_count") },
00102        { STR_W_LEN("mem_emalloc_amount") },
00103        { STR_W_LEN("mem_ecalloc_count") },
00104        { STR_W_LEN("mem_ecalloc_amount") },
00105        { STR_W_LEN("mem_erealloc_count") },
00106        { STR_W_LEN("mem_erealloc_amount") },
00107        { STR_W_LEN("mem_efree_count") },
00108        { STR_W_LEN("mem_efree_amount") },
00109        { STR_W_LEN("mem_malloc_count") },
00110        { STR_W_LEN("mem_malloc_amount") },
00111        { STR_W_LEN("mem_calloc_count") },
00112        { STR_W_LEN("mem_calloc_amount") },
00113        { STR_W_LEN("mem_realloc_count") },
00114        { STR_W_LEN("mem_realloc_amount") },
00115        { STR_W_LEN("mem_free_count") },
00116        { STR_W_LEN("mem_free_amount") },
00117        { STR_W_LEN("mem_estrndup_count") },
00118        { STR_W_LEN("mem_strndup_count") },
00119        { STR_W_LEN("mem_estndup_count") },
00120        { STR_W_LEN("mem_strdup_count") },
00121        { STR_W_LEN("proto_text_fetched_null") },
00122        { STR_W_LEN("proto_text_fetched_bit") },
00123        { STR_W_LEN("proto_text_fetched_tinyint") },
00124        { STR_W_LEN("proto_text_fetched_short") },
00125        { STR_W_LEN("proto_text_fetched_int24") },
00126        { STR_W_LEN("proto_text_fetched_int") },
00127        { STR_W_LEN("proto_text_fetched_bigint") },
00128        { STR_W_LEN("proto_text_fetched_decimal") },
00129        { STR_W_LEN("proto_text_fetched_float") },
00130        { STR_W_LEN("proto_text_fetched_double") },
00131        { STR_W_LEN("proto_text_fetched_date") },
00132        { STR_W_LEN("proto_text_fetched_year") },
00133        { STR_W_LEN("proto_text_fetched_time") },
00134        { STR_W_LEN("proto_text_fetched_datetime") },
00135        { STR_W_LEN("proto_text_fetched_timestamp") },
00136        { STR_W_LEN("proto_text_fetched_string") },
00137        { STR_W_LEN("proto_text_fetched_blob") },
00138        { STR_W_LEN("proto_text_fetched_enum") },
00139        { STR_W_LEN("proto_text_fetched_set") },
00140        { STR_W_LEN("proto_text_fetched_geometry") },
00141        { STR_W_LEN("proto_text_fetched_other") },
00142        { STR_W_LEN("proto_binary_fetched_null") },
00143        { STR_W_LEN("proto_binary_fetched_bit") },
00144        { STR_W_LEN("proto_binary_fetched_tinyint") },
00145        { STR_W_LEN("proto_binary_fetched_short") },
00146        { STR_W_LEN("proto_binary_fetched_int24") },
00147        { STR_W_LEN("proto_binary_fetched_int") },
00148        { STR_W_LEN("proto_binary_fetched_bigint") },
00149        { STR_W_LEN("proto_binary_fetched_decimal") },
00150        { STR_W_LEN("proto_binary_fetched_float") },
00151        { STR_W_LEN("proto_binary_fetched_double") },
00152        { STR_W_LEN("proto_binary_fetched_date") },
00153        { STR_W_LEN("proto_binary_fetched_year") },
00154        { STR_W_LEN("proto_binary_fetched_time") },
00155        { STR_W_LEN("proto_binary_fetched_datetime") },
00156        { STR_W_LEN("proto_binary_fetched_timestamp") },
00157        { STR_W_LEN("proto_binary_fetched_string") },
00158        { STR_W_LEN("proto_binary_fetched_blob") },
00159        { STR_W_LEN("proto_binary_fetched_enum") },
00160        { STR_W_LEN("proto_binary_fetched_set") },
00161        { STR_W_LEN("proto_binary_fetched_geometry") },
00162        { STR_W_LEN("proto_binary_fetched_other") },
00163        { STR_W_LEN("init_command_executed_count") },
00164        { STR_W_LEN("init_command_failed_count") },
00165        { STR_W_LEN("com_quit") },
00166        { STR_W_LEN("com_init_db") },
00167        { STR_W_LEN("com_query") },
00168        { STR_W_LEN("com_field_list") },
00169        { STR_W_LEN("com_create_db") },
00170        { STR_W_LEN("com_drop_db") },
00171        { STR_W_LEN("com_refresh") },
00172        { STR_W_LEN("com_shutdown") },
00173        { STR_W_LEN("com_statistics") },
00174        { STR_W_LEN("com_process_info") },
00175        { STR_W_LEN("com_connect") },
00176        { STR_W_LEN("com_process_kill") },
00177        { STR_W_LEN("com_debug") },
00178        { STR_W_LEN("com_ping") },
00179        { STR_W_LEN("com_time") },
00180        { STR_W_LEN("com_delayed_insert") },
00181        { STR_W_LEN("com_change_user") },
00182        { STR_W_LEN("com_binlog_dump") },
00183        { STR_W_LEN("com_table_dump") },
00184        { STR_W_LEN("com_connect_out") },
00185        { STR_W_LEN("com_register_slave") },
00186        { STR_W_LEN("com_stmt_prepare") },
00187        { STR_W_LEN("com_stmt_execute") },
00188        { STR_W_LEN("com_stmt_send_long_data") },
00189        { STR_W_LEN("com_stmt_close") },
00190        { STR_W_LEN("com_stmt_reset") },
00191        { STR_W_LEN("com_stmt_set_option") },
00192        { STR_W_LEN("com_stmt_fetch") },
00193        { STR_W_LEN("com_deamon") },
00194        { STR_W_LEN("bytes_received_real_data_normal") },
00195        { STR_W_LEN("bytes_received_real_data_ps") }
00196 };
00197 /* }}} */
00198 
00199 
00200 /* {{{ mysqlnd_fill_stats_hash */
00201 PHPAPI void
00202 mysqlnd_fill_stats_hash(const MYSQLND_STATS * const stats, const MYSQLND_STRING * names, zval *return_value TSRMLS_DC ZEND_FILE_LINE_DC)
00203 {
00204        unsigned int i;
00205 
00206        mysqlnd_array_init(return_value, stats->count);
00207        for (i = 0; i < stats->count; i++) {
00208 #if MYSQLND_UNICODE
00209               UChar *ustr, *tstr;
00210               int ulen, tlen;
00211 #endif
00212               char tmp[25];
00213 
00214               sprintf((char *)&tmp, MYSQLND_LLU_SPEC, stats->values[i]);
00215 #if MYSQLND_UNICODE
00216               zend_string_to_unicode(UG(utf8_conv), &ustr, &ulen, names[i].s, names[i].l + 1 TSRMLS_CC);
00217               zend_string_to_unicode(UG(utf8_conv), &tstr, &tlen, tmp, strlen(tmp) + 1 TSRMLS_CC);
00218               add_u_assoc_unicode_ex(return_value, IS_UNICODE, ZSTR(ustr), ulen, tstr, 1);
00219               efree(ustr);
00220               efree(tstr);
00221 #else
00222               add_assoc_string_ex(return_value, names[i].s, names[i].l + 1, tmp, 1);
00223 #endif
00224        }
00225 }
00226 /* }}} */
00227 
00228 
00229 /* {{{ _mysqlnd_get_client_stats */
00230 PHPAPI void
00231 _mysqlnd_get_client_stats(zval *return_value TSRMLS_DC ZEND_FILE_LINE_DC)
00232 {
00233        MYSQLND_STATS stats, *stats_ptr = mysqlnd_global_stats;
00234        DBG_ENTER("_mysqlnd_get_client_stats");
00235        if (!stats_ptr) {
00236               memset(&stats, 0, sizeof(stats));
00237               stats_ptr = &stats;
00238        }
00239        mysqlnd_fill_stats_hash(stats_ptr, mysqlnd_stats_values_names, return_value TSRMLS_CC ZEND_FILE_LINE_CC);
00240        DBG_VOID_RETURN;
00241 }
00242 /* }}} */
00243 
00244 
00245 /* {{{ mysqlnd_stats_init */
00246 PHPAPI void
00247 mysqlnd_stats_init(MYSQLND_STATS ** stats, size_t statistic_count)
00248 {
00249        *stats = calloc(1, sizeof(MYSQLND_STATS));
00250        if (*stats == NULL) {
00251               return;
00252        }
00253        (*stats)->values = calloc(statistic_count, sizeof(uint64_t));
00254        (*stats)->triggers = calloc(statistic_count, sizeof(mysqlnd_stat_trigger));
00255        (*stats)->in_trigger = FALSE;
00256        (*stats)->count = statistic_count;
00257 #ifdef ZTS
00258        (*stats)->LOCK_access = tsrm_mutex_alloc();
00259 #endif
00260 
00261 }
00262 /* }}} */
00263 
00264 
00265 /* {{{ mysqlnd_stats_end */
00266 PHPAPI void
00267 mysqlnd_stats_end(MYSQLND_STATS * stats)
00268 {
00269 #ifdef ZTS
00270        tsrm_mutex_free(stats->LOCK_access);
00271 #endif
00272        free(stats->triggers);
00273        free(stats->values);
00274        /* mnd_free will reference LOCK_access and crash...*/
00275        free(stats);
00276 }
00277 /* }}} */
00278 
00279 
00280 /* {{{ mysqlnd_stats_set_trigger */
00281 PHPAPI mysqlnd_stat_trigger
00282 mysqlnd_stats_set_trigger(MYSQLND_STATS * const stats, enum_mysqlnd_collected_stats statistic, mysqlnd_stat_trigger trigger TSRMLS_DC)
00283 {
00284        mysqlnd_stat_trigger ret = NULL;
00285        DBG_ENTER("mysqlnd_stats_set_trigger");
00286        if (stats) {
00287               MYSQLND_STATS_LOCK(stats);
00288               ret = stats->triggers[statistic];
00289               stats->triggers[statistic] = trigger;
00290               MYSQLND_STATS_UNLOCK(stats);
00291        }
00292        DBG_RETURN(ret);
00293 }
00294 /* }}} */
00295 
00296 
00297 /* {{{ mysqlnd_stats_set_handler */
00298 PHPAPI mysqlnd_stat_trigger
00299 mysqlnd_stats_reset_triggers(MYSQLND_STATS * const stats TSRMLS_DC)
00300 {
00301        mysqlnd_stat_trigger ret = NULL;
00302        DBG_ENTER("mysqlnd_stats_reset_trigger");
00303        if (stats) {
00304               MYSQLND_STATS_LOCK(stats);
00305               memset(stats->triggers, 0, stats->count * sizeof(mysqlnd_stat_trigger));
00306               MYSQLND_STATS_UNLOCK(stats);
00307        }
00308        DBG_RETURN(ret);
00309 }
00310 /* }}} */
00311 
00312 
00313 /*
00314  * Local variables:
00315  * tab-width: 4
00316  * c-basic-offset: 4
00317  * End:
00318  * vim600: noet sw=4 ts=4 fdm=marker
00319  * vim<600: noet sw=4 ts=4
00320  */