various changes for profiler

This commit is contained in:
Georg Richter 2003-03-08 23:33:12 +00:00
parent 1b9ee1a8ea
commit 15f648c8f9
6 changed files with 997 additions and 403 deletions

View File

@ -35,7 +35,6 @@
#define MYSQLI_USE_RESULT 1
ZEND_DECLARE_MODULE_GLOBALS(mysqli)
static zend_object_handlers mysqli_object_handlers;
/* {{{ php_clear_stmt_bind */
@ -73,25 +72,27 @@ void php_clear_stmt_bind(STMT *stmt)
*/
static void mysqli_objects_dtor(void *object, zend_object_handle handle TSRMLS_DC)
{
mysqli_object *intern = (mysqli_object *)object;
mysqli_object *intern = (mysqli_object *)object;
MYSQLI_RESOURCE *my_res = (MYSQLI_RESOURCE *)intern->ptr;
/* link object */
if (intern->zo.ce == mysqli_link_class_entry) {
MYSQL *mysql = (MYSQL *)intern->ptr;
if (mysql) {
mysql_close(mysql);
if (my_res && my_res->ptr) {
mysql_close(my_res->ptr);
if (MyG(profiler)) {
php_mysqli_profiler_report(my_res->prinfo, 0);
}
}
} else if (intern->zo.ce == mysqli_stmt_class_entry) { /* stmt object */
STMT *stmt = (STMT *)intern->ptr;
if (stmt) {
php_clear_stmt_bind(stmt);
if (my_res && my_res->ptr) {
php_clear_stmt_bind((STMT *)my_res->ptr);
}
} else if (intern->zo.ce == mysqli_result_class_entry) { /* result object */
MYSQL_RES *res = (MYSQL_RES *)intern->ptr;
if (res) {
mysql_free_result(res);
if (my_res && my_res->ptr) {
mysql_free_result(my_res->ptr);
}
}
my_efree(my_res);
zend_objects_destroy_object(object, handle TSRMLS_CC);
}
/* }}} */
@ -176,7 +177,7 @@ static void php_mysqli_init_globals(zend_mysqli_globals *mysqli_globals)
mysqli_globals->default_user = NULL;
mysqli_globals->default_pw = NULL;
mysqli_globals->default_socket = NULL;
memset(&mysqli_globals->profiler, '\0', sizeof(PROFILER));
mysqli_globals->profiler = 0;
}
/* }}} */
@ -337,14 +338,16 @@ PHP_MINFO_FUNCTION(mysqli)
*/
void php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAMETERS, int override_flags)
{
MYSQL_RES *result;
zval *mysql_result;
int fetchtype;
int copyflag;
unsigned int i;
MYSQL_FIELD *fields;
MYSQL_ROW row;
unsigned long *field_len;
MYSQL_RES *result;
zval *mysql_result;
int fetchtype;
int copyflag;
unsigned int i;
MYSQL_FIELD *fields;
MYSQL_ROW row;
unsigned long *field_len;
PR_RESULT *prresult;
PR_COMMAND *prcommand;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|l", &mysql_result, mysqli_result_class_entry, &fetchtype) == FAILURE) {
return;
@ -358,12 +361,15 @@ void php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAMETERS, int override_flags
fetchtype = override_flags;
}
MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result");
MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, prresult, PR_RESULT *, &mysql_result, "mysqli_result");
MYSQLI_PROFILER_COMMAND_START(prcommand, prresult);
fields = mysql_fetch_fields(result);
if (!(row = mysql_fetch_row(result))) {
RETURN_FALSE;
}
array_init(return_value);
field_len = mysql_fetch_lengths(result);
@ -397,6 +403,11 @@ void php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAMETERS, int override_flags
}
}
}
if (MyG(profiler)) {
MYSQLI_PROFILER_COMMAND_RETURNSTRING(prcommand, NULL);
prresult->fetched_rows++;
}
}
/* }}} */

File diff suppressed because it is too large Load Diff

View File

@ -86,6 +86,7 @@ function_entry mysqli_functions[] = {
PHP_FE(mysqli_ping, NULL)
PHP_FE(mysqli_prepare, NULL)
PHP_FE(mysqli_prepare_result, NULL)
PHP_FE(mysqli_profiler, NULL)
PHP_FE(mysqli_query, NULL)
PHP_FE(mysqli_read_query_result, NULL)
PHP_FE(mysqli_real_connect, NULL)
@ -102,7 +103,6 @@ function_entry mysqli_functions[] = {
PHP_FE(mysqli_send_long_data, NULL)
PHP_FE(mysqli_send_query, NULL)
PHP_FALIAS(mysqli_set_opt, mysqli_options, NULL)
PHP_FE(mysqli_set_profiler_opt, NULL)
PHP_FE(mysqli_slave_query, NULL)
PHP_FE(mysqli_ssl_set, NULL)
PHP_FE(mysqli_stat, NULL)

View File

@ -33,11 +33,14 @@
open a connection to a mysql server */
PHP_FUNCTION(mysqli_connect)
{
MYSQL *mysql;
zval *object = getThis();
char *hostname = NULL, *username=NULL, *passwd=NULL, *dbname=NULL, *socket=NULL;
unsigned int hostname_len, username_len, passwd_len, dbname_len, socket_len;
unsigned int port=0;
MYSQL *mysql;
MYSQLI_RESOURCE *mysqli_resource;
PR_MYSQL *prmysql = NULL;
zval *object = getThis();
char *hostname = NULL, *username=NULL, *passwd=NULL, *dbname=NULL, *socket=NULL;
unsigned int hostname_len, username_len, passwd_len, dbname_len, socket_len;
unsigned int port=0;
struct timeval starttime;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ssssls", &hostname, &hostname_len, &username, &username_len,
&passwd, &passwd_len, &dbname, &dbname_len, &port, &socket, &socket_len) == FAILURE) {
@ -59,6 +62,10 @@ PHP_FUNCTION(mysqli_connect)
}
mysql = mysql_init(NULL);
if (MyG(profiler)){
gettimeofday(&starttime, NULL);
}
if (mysql_real_connect(mysql,hostname,username,passwd,dbname,port,socket,0) == NULL) {
/* Save error messages */
@ -70,10 +77,24 @@ PHP_FUNCTION(mysqli_connect)
RETURN_FALSE;
}
if (MyG(profiler)) {
prmysql = (PR_MYSQL *)MYSQLI_PROFILER_NEW(NULL, MYSQLI_PR_MYSQL, 0);
php_mysqli_profiler_timediff(starttime, &prmysql->header.elapsedtime);
MYSQLI_PROFILER_STARTTIME(prmysql);
prmysql->hostname = estrdup(hostname);
prmysql->username = estrdup(username);
prmysql->thread_id = mysql->thread_id;
}
mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
mysqli_resource->ptr = (void *)mysql;
mysqli_resource->prinfo = prmysql;
if (!object) {
MYSQLI_RETURN_RESOURCE(mysql, mysqli_link_class_entry);
MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_link_class_entry);
} else {
((mysqli_object *) zend_object_store_get_object(object TSRMLS_CC))->ptr = mysql;
((mysqli_object *) zend_object_store_get_object(object TSRMLS_CC))->ptr = mysqli_resource;
}
}
/* }}} */
@ -109,46 +130,70 @@ PHP_FUNCTION(mysqli_fetch_object)
/* {{{ proto resource mysqli_query(resource link, string query, [int resultmode])
*/
PHP_FUNCTION(mysqli_query) {
MYSQL *mysql;
zval *mysql_link;
MYSQL_RES *result;
char *query = NULL;
unsigned int query_len;
unsigned int resultmode = 0;
MYSQL *mysql;
zval *mysql_link;
MYSQLI_RESOURCE *mysqli_resource;
MYSQL_RES *result;
PR_MYSQL *prmysql;
PR_QUERY *prquery;
PR_RESULT *prresult;
char *query = NULL;
unsigned int query_len;
unsigned int resultmode = 0;
struct timeval starttime;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|l", &mysql_link, mysqli_link_class_entry, &query, &query_len, &resultmode) == FAILURE) {
return;
}
MYSQLI_FETCH_RESOURCE(mysql, MYSQL *, &mysql_link, "mysqli_link");
MYSQLI_FETCH_RESOURCE(mysql, MYSQL*, prmysql, PR_MYSQL *, &mysql_link, "mysqli_link");
/* profiler reports */
if (MyG(profiler.active)) {
MYSQLI_PROFILER_HEADER(query);
MYSQLI_PROFILER_EXPLAIN(mysql,query);
MYSQLI_PROFILER_GETTIME;
if (mysql_real_query(mysql, query, query_len)){
RETURN_FALSE;
/* profiling information */
if (MyG(profiler)) {
prquery = (PR_QUERY *)MYSQLI_PROFILER_NEW(prmysql, MYSQLI_PR_QUERY, 1);
prquery->explain.query = my_estrdup(query);
if (!strncasecmp("select", query, 6)){
if (!(MYSQLI_PROFILER_EXPLAIN(&prquery->explain, &prquery->header, mysql, query))) {
RETURN_FALSE;
}
}
MYSQLI_PROFILER_REPORTTIME;
}
else {
if (mysql_real_query(mysql, query, query_len)) {
RETURN_FALSE;
}
if (mysql_real_query(mysql, query, query_len)) {
RETURN_FALSE;
}
if (MyG(profiler)) {
MYSQLI_PROFILER_ELAPSEDTIME(prquery);
prquery->insertid = mysql_insert_id(mysql);
prquery->affectedrows = mysql_affected_rows(mysql);
}
if (!mysql_field_count(mysql)) {
RETURN_FALSE;
}
/* profiler result information */
if (MyG(profiler)) {
gettimeofday(&starttime, NULL);
prresult = (PR_RESULT *)MYSQLI_PROFILER_NEW(prquery, MYSQLI_PR_RESULT, 1);
}
result = (resultmode == MYSQLI_USE_RESULT) ? mysql_use_result(mysql) : mysql_store_result(mysql);
if (result && MyG(profiler)) {
MYSQLI_PROFILER_ELAPSEDTIME(prresult);
prresult->rows = result->row_count;
prresult->columns = result->field_count;
}
if (!result) {
RETURN_FALSE;
}
if (MyG(profiler.active)) {
MYSQLI_PROFILER_REPORT_RESULT(result);
}
MYSQLI_RETURN_RESOURCE(result, mysqli_result_class_entry);
mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
mysqli_resource->ptr = (void *)result;
mysqli_resource->prinfo = (void *)prresult;
MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_result_class_entry);
}
/* }}} */

View File

@ -27,89 +27,356 @@
#include "ext/standard/info.h"
#include "php_mysqli.h"
#define DIVIDER "************************************************************"
#define DIVIDER1 "------------------------------------------------------------"
/* {{{ void php_mysqli_profiler_header(char *query) */
void php_mysqli_profiler_header(char *query)
/* {{{ PR_COMMON *php_mysqli_profiler_new_object(PR_COMMON *parent, unsigned int type, unsigned int settime) */
PR_COMMON *php_mysqli_profiler_new_object(PR_COMMON *parent, unsigned int type, unsigned int settime)
{
printf("%s\n", DIVIDER);
printf("File: %s\nLine: %d\n", zend_get_executed_filename(TSRMLS_C), zend_get_executed_lineno(TSRMLS_C));
printf("Function: %s\n", get_active_function_name(TSRMLS_C));
if (query) {
printf("SQL: %s\n", query);
PR_COMMON *prnew, *child;
switch (type) {
case MYSQLI_PR_MYSQL:
prnew = (PR_COMMON *)ecalloc(1, sizeof(PR_MYSQL));
break;
case MYSQLI_PR_QUERY:
prnew = (PR_COMMON *)ecalloc(1, sizeof(PR_QUERY));
break;
case MYSQLI_PR_STMT:
prnew = (PR_COMMON *)ecalloc(1, sizeof(PR_STMT));
break;
case MYSQLI_PR_COMMAND:
prnew = (PR_COMMON *)ecalloc(1, sizeof(PR_COMMAND));
break;
case MYSQLI_PR_RESULT:
prnew = (PR_COMMON *)ecalloc(1, sizeof(PR_RESULT));
break;
}
prnew->header.type = type;
prnew->header.filename = estrdup(zend_get_executed_filename(TSRMLS_C));
prnew->header.lineno = zend_get_executed_lineno(TSRMLS_C);
prnew->header.functionname = estrdup(get_active_function_name(TSRMLS_C));
if (settime) {
gettimeofday(&prnew->header.starttime, NULL);
}
if (!parent) {
return prnew;
}
if (!parent->header.child) {
parent->header.child = (void *)prnew;
return (prnew);
}
child = parent->header.child;
while (child->header.next != NULL) {
child = child->header.next;
}
child->header.next = (void *)prnew;
return (prnew);
}
/* }}} */
/* {{{ void php_mysqli_profiler_result(MYSQL_RES *) */
void php_mysqli_profiler_result_info(MYSQL_RES *res)
{
printf("%s\nRows returned: %d\n", DIVIDER1, mysql_num_rows(res));
}
/* }}} */
/* {{{ void php_mysqli_profiler_explain(MYSQL *, char *) */
void php_mysqli_profiler_explain(MYSQL *mysql, char *query)
/* {{{ int *php_mysqli_profiler_explain(PR_EXPLAIN *, PR_HEADER *, MYSQL *, char *) */
int php_mysqli_profiler_explain(PR_EXPLAIN *explain, PR_HEADER *header, MYSQL *mysql, char *query)
{
MYSQL_RES *res;
MYSQL_ROW row;
MYSQL_FIELD *fields;
unsigned int i;
char *newquery = (char *)emalloc(strlen(query) + 10);
sprintf (newquery, "EXPLAIN %s", query);
mysql_real_query(mysql, newquery, strlen(newquery));
efree (newquery);
if (mysql_errno(mysql)) {
printf ("%s\nError (%d): %s\n", DIVIDER1, mysql_errno(mysql), mysql_error(mysql));
return;
header->error = mysql_errno(mysql);
header->errormsg = my_estrdup(mysql_error(mysql));
return 0;
}
res = mysql_use_result(mysql);
res = mysql_store_result(mysql);
printf ("%s\nEXPLAIN:\n", DIVIDER1);
fields = mysql_fetch_fields(res);
while ((row = mysql_fetch_row(res))) {
for (i=0; i < mysql_num_fields(res); i++) {
printf ("%20s: %s\n", fields[i].name, row[i]);
}
printf("\n");
if (!(explain->exp_cnt = mysql_num_rows(res))) {
return 0;
}
explain->exp_table = (char **)emalloc(sizeof(char *) * explain->exp_cnt);
explain->exp_type = (char **)emalloc(sizeof(char *) * explain->exp_cnt);
explain->exp_key = (char **)emalloc(sizeof(char *) * explain->exp_cnt);
explain->exp_rows = (ulong *)emalloc(sizeof(ulong) * explain->exp_cnt);
for (i=0; i < explain->exp_cnt; i++) {
row = mysql_fetch_row(res);
explain->exp_table[i] = my_estrdup(row[2]);
explain->exp_type[i] = my_estrdup(row[3]);
explain->exp_key[i] = my_estrdup(row[4]);
explain->exp_rows[i] = atol(row[8]);
}
mysql_free_result(res);
return 1;
}
/* }}} */
/* {{{ void php_mysqli_profiler_timediff(struct timeval, struct timeval *) */
void php_mysqli_profiler_timediff(struct timeval starttime, struct timeval *elapsed)
{
struct timeval end;
gettimeofday(&end, NULL);
elapsed->tv_sec = end.tv_sec - starttime.tv_sec;
elapsed->tv_usec = end.tv_usec - starttime.tv_usec;
if (elapsed->tv_usec < 0) {
--(elapsed->tv_sec);
elapsed->tv_usec = 1000000;
}
return;
}
/* }}} */
/* {{{ void php_mysqli_profiler_elapsed_time() */
void php_mysqli_profiler_elapsed_time()
/* {{{ char *php_mysqli_profiler_indent(int) */
char *php_mysqli_profiler_indent(int i)
{
struct timeval end, elapsed;
gettimeofday(&end, NULL);
elapsed.tv_sec = end.tv_sec - MyG(profiler.start.tv_sec);
elapsed.tv_usec = end.tv_usec - MyG(profiler.start.tv_usec);
if (elapsed.tv_usec < 0) {
--(elapsed.tv_sec);
elapsed.tv_usec = 1000000;
}
printf("%s\nElapsed time: %3d.%06d seconds\n", DIVIDER1, elapsed.tv_sec, elapsed.tv_usec);
char *ret = (char *)ecalloc(i*4+1, sizeof(char));
memset(ret, 0x20, i*4);
return ret;
}
/* }}} */
/* {{{ proto void mysqli_set_profiler_opt (bool profiler)
/* {{{ void php_mysqli_profiler_report_header(PR_HEADER, char *) */
void php_mysqli_profiler_report_header(PR_HEADER header, char *ident)
{
switch (header.type) {
case MYSQLI_PR_MYSQL:
printf("%s[Connection]\n", ident);
php_mysqli_profiler_timediff(header.starttime, &header.lifetime);
break;
case MYSQLI_PR_QUERY:
printf("%s[Query]\n", ident);
break;
case MYSQLI_PR_STMT:
printf("%s[Statement]\n", ident);
php_mysqli_profiler_timediff(header.starttime, &header.lifetime);
break;
case MYSQLI_PR_RESULT:
printf("%s[Resultset]\n", ident);
php_mysqli_profiler_timediff(header.starttime, &header.lifetime);
break;
case MYSQLI_PR_COMMAND:
printf("%s[Command]\n", ident);
break;
}
printf ("%sFunction: %s\n", ident, header.functionname);
printf ("%sFile: %s\n", ident, header.filename);
printf ("%sLine: %d\n", ident, header.lineno);
printf ("%sExecution time: %ld.%06ld\n", ident, header.elapsedtime.tv_sec, header.elapsedtime.tv_usec);
if (header.lifetime.tv_sec + header.lifetime.tv_usec) {
printf ("%sLife time: %ld.%06ld\n", ident, header.lifetime.tv_sec, header.lifetime.tv_usec);
}
if (header.error) {
printf("%sError: %s (%ld)\n", ident, header.errormsg, header.error);
}
/* free header */
my_efree(header.functionname);
my_efree(header.filename);
my_efree(header.errormsg);
}
/* }}} */
/* {{{ void php_mysqli_profiler_report_explain(PR_EXPLAIN, char *) */
void php_mysqli_profiler_report_explain(PR_EXPLAIN explain, char *ident)
{
int i;
if (explain.query) {
printf("%sQuery: %s\n", ident, explain.query);
}
if (explain.exp_cnt) {
printf("%sTable(s):", ident);
for (i=0; i < explain.exp_cnt; i++) {
printf(" %s%c", explain.exp_table[i], (i == explain.exp_cnt - 1) ? '\n' : ',');
my_efree(explain.exp_table[i]);
}
printf("%sJoin-Types(s):", ident);
for (i=0; i < explain.exp_cnt; i++) {
printf(" %s%c", explain.exp_type[i], (i == explain.exp_cnt - 1) ? '\n' : ',');
my_efree(explain.exp_type[i]);
}
printf("%sKey(s):", ident);
for (i=0; i < explain.exp_cnt; i++) {
printf(" %s%c", explain.exp_key[i], (i == explain.exp_cnt - 1) ? '\n' : ',');
my_efree(explain.exp_key[i]);
}
printf("%sRow(s):", ident);
for (i=0; i < explain.exp_cnt; i++) {
printf(" %ld%c", explain.exp_rows[i], (i == explain.exp_cnt - 1) ? '\n' : ',');
}
my_efree(explain.exp_table);
my_efree(explain.exp_type);
my_efree(explain.exp_key);
my_efree(explain.exp_rows);
}
/* free explain */
my_efree(explain.query);
}
/* }}} */
/* {{{ php_mysqli_profiler_report_mysql(PR_MYSQL *, int) */
void php_mysqli_profiler_report_mysql(PR_MYSQL *prmysql, int depth)
{
char *ident = php_mysqli_profiler_indent(depth);
php_mysqli_profiler_report_header(prmysql->header, ident);
printf ("%sHost: %s\n", ident, prmysql->hostname);
printf ("%sUser: %s\n", ident, prmysql->username);
printf ("%sThread-id: %d\n", ident, prmysql->thread_id);
if (!prmysql->closed) {
printf ("%sWarning: connection wasn't closed by mysqli_close()\n", ident);
}
printf("\n");
my_efree(prmysql->hostname);
my_efree(prmysql->username);
efree(ident);
}
/* }}} */
/* {{{ void php_mysqli_profiler_report_query(PR_QUERY *, int) */
void php_mysqli_profiler_report_query(PR_QUERY *prquery, int depth)
{
char *ident = php_mysqli_profiler_indent(depth);
php_mysqli_profiler_report_header(prquery->header, ident);
php_mysqli_profiler_report_explain(prquery->explain, ident);
if (prquery->affectedrows > 0) {
printf ("%saffected rows: %ld\n", ident, prquery->affectedrows);
}
if (prquery->insertid) {
printf ("%sinsert id: %ld\n", ident, prquery->insertid);
}
printf("\n");
efree(ident);
}
/* }}} */
/* {{{ void php_mysqli_profiler_report_stmt(PR_STMT *, int) */
void php_mysqli_profiler_report_stmt(PR_STMT *prstmt, int depth)
{
char *ident = php_mysqli_profiler_indent(depth);
php_mysqli_profiler_report_header(prstmt->header, ident);
php_mysqli_profiler_report_explain(prstmt->explain, ident);
printf("\n");
efree(ident);
}
/* }}} */
/* {{{ void php_mysqli_profiler_report_result(PR_RESULT *, int) */
void php_mysqli_profiler_report_result(PR_RESULT *prresult, int depth)
{
char *ident = php_mysqli_profiler_indent(depth);
php_mysqli_profiler_report_header(prresult->header, ident);
printf ("%sColumns: %d\n", ident, prresult->columns);
printf ("%sRows: %ld\n", ident, prresult->rows);
printf ("%sFetched rows: %ld\n", ident, prresult->fetched_rows);
if (!prresult->closed) {
printf ("%sWarning: resultset wasn't closed by mysqli_free_result()\n", ident);
}
printf("\n");
efree(ident);
}
/* }}} */
/* {{{ void php_mysqli_profiler_report_command(PR_COMMAND *, int) */
void php_mysqli_profiler_report_command(PR_COMMAND *prcommand, int depth)
{
char *ident = php_mysqli_profiler_indent(depth);
php_mysqli_profiler_report_header(prcommand->header, ident);
if (prcommand->returnvalue) {
printf("%sReturnvalue: %s\n", ident, prcommand->returnvalue);
efree(prcommand->returnvalue);
}
printf("\n");
efree(ident);
}
/* }}} */
/* {{{ php_mysqli_profiler_report(PR_COMMON *, int) */
void php_mysqli_profiler_report(PR_COMMON *current, int depth)
{
PR_COMMON *child;
PR_COMMON *next;
switch (current->header.type) {
case MYSQLI_PR_MYSQL:
{
PR_MYSQL *prmysql = (PR_MYSQL *)current;
php_mysqli_profiler_report_mysql(prmysql, depth);
}
break;
case MYSQLI_PR_COMMAND:
{
PR_COMMAND *prcommand = (PR_COMMAND *)current;
child = NULL;
php_mysqli_profiler_report_command(prcommand, depth);
}
break;
case MYSQLI_PR_RESULT:
{
PR_RESULT *prresult = (PR_RESULT *)current;
php_mysqli_profiler_report_result(prresult, depth);
}
break;
case MYSQLI_PR_STMT:
case MYSQLI_PR_STMT_RESULT:
{
PR_STMT *prstmt = (PR_STMT *)current;
php_mysqli_profiler_report_stmt(prstmt, depth);
}
break;
case MYSQLI_PR_QUERY:
case MYSQLI_PR_QUERY_RESULT:
{
PR_QUERY *prquery = (PR_QUERY *)current;
php_mysqli_profiler_report_query(prquery, depth);
}
break;
}
child = current->header.child;
if (child) {
php_mysqli_profiler_report(child, depth+1);
}
next = (current->header.next) ? current->header.next : NULL;
if (next) {
php_mysqli_profiler_report(next, depth);
}
efree(current);
return;
}
/* }}} */
/* {{{ proto void mysqli_profiler (bool profiler)
*/
PHP_FUNCTION(mysqli_set_profiler_opt)
PHP_FUNCTION(mysqli_profiler)
{
int flags;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &flags) == FAILURE) {
return;
}
MyG(profiler.active) = flags;
MyG(profiler) = flags;
return;
}

View File

@ -36,9 +36,6 @@
#ifndef PHP_MYSQLI_H
#define PHP_MYSQLI_H
#define MYSQLI_PR_TYPE_QUERY 0
#define MYSQLI_PR_TYPE_PREPARE 1
typedef struct {
ulong buflen;
@ -56,22 +53,98 @@ typedef struct {
} STMT;
typedef struct {
char active;
struct timeval start;
unsigned int count[2];
ulong min_row_val[2];
ulong max_row_val[2];
ulong row_val[2];
double min_elapsed[2];
double max_elapsed[2];
double elapsed[2];
} PROFILER;
void *prinfo; /* profiler info */
void *ptr; /* resource: (mysql, result, stmt) */
} MYSQLI_RESOURCE;
/* common profiler header struct */
typedef struct {
unsigned int type;
void *child;
void *next;
char *filename;
unsigned int lineno;
char *functionname;
struct timeval starttime;
struct timeval elapsedtime;
struct timeval lifetime;
char *errormsg;
ulong error;
} PR_HEADER;
/* explain output */
typedef struct {
char *query;
unsigned int exp_cnt;
char **exp_table;
char **exp_type;
char **exp_key;
ulong *exp_rows;
} PR_EXPLAIN;
/* common */
typedef struct {
PR_HEADER header;
} PR_COMMON;
/* connection */
typedef struct {
PR_HEADER header;
unsigned int thread_id;
char *hostname;
char *username;
unsigned int closed;
} PR_MYSQL;
/* resultset */
typedef struct {
PR_HEADER header;
unsigned int columns;
ulong rows;
ulong fields;
ulong fetched_rows;
unsigned int closed;
} PR_RESULT;
/* command */
/* TODO: return values */
typedef struct {
PR_HEADER header;
ulong returntype;
void *returnvalue;
} PR_COMMAND;
/* query */
typedef struct {
PR_HEADER header;
PR_EXPLAIN explain;
ulong affectedrows;
ulong insertid;
} PR_QUERY;
/* statement */
typedef struct {
PR_HEADER header;
PR_EXPLAIN explain;
unsigned int param_cnt;
unsigned int field_cnt;
} PR_STMT;
typedef struct _mysqli_object {
zend_object zo;
void *ptr;
} mysqli_object; /* extends zend_object */
#define MYSQLI_PR_MYSQL 0
#define MYSQLI_PR_QUERY 1
#define MYSQLI_PR_QUERY_RESULT 2
#define MYSQLI_PR_STMT 3
#define MYSQLI_PR_STMT_RESULT 4
#define MYSQLI_PR_RESULT 5
#define MYSQLI_PR_COMMAND 6
#define phpext_mysqli_ptr &mysqli_module_entry
#ifdef PHP_WIN32
@ -95,10 +168,11 @@ extern void php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAMETERS, int flag);
extern void php_clear_stmt_bind(STMT *stmt);
/* Profiler functions */
extern void php_mysqli_profiler_report(PR_COMMON *, int);
extern PR_COMMON *php_mysqli_profiler_new_object(PR_COMMON *parent, unsigned int type, unsigned int settime);
extern void php_mysqli_profiler_result_info(MYSQL_RES *res);
void php_mysqli_profiler_explain(MYSQL *mysql, char *query);
void php_mysqli_profiler_header(char *query);
void php_mysqli_profiler_elapsed_time();
extern int php_mysqli_profiler_explain(PR_EXPLAIN *explain, PR_HEADER *header, MYSQL *mysql, char *query);
extern void php_mysqli_profiler_timediff(struct timeval start, struct timeval *elapsed);
zend_class_entry *mysqli_link_class_entry;
zend_class_entry *mysqli_stmt_class_entry;
@ -135,13 +209,16 @@ PHP_MYSQLI_EXPORT(zend_object_value) mysqli_objects_new(zend_class_entry * TSRML
MYSQLI_REGISTER_RESOURCE_EX(__ptr, object, __ce)\
}
#define MYSQLI_FETCH_RESOURCE(__ptr, __type, __id, __name) \
#define MYSQLI_FETCH_RESOURCE(__ptr, __type, __prptr, __prtype, __id, __name) \
{ \
MYSQLI_RESOURCE *my_res; \
mysqli_object *intern = (mysqli_object *)zend_object_store_get_object(*(__id) TSRMLS_CC);\
if (!(__ptr = (__type)intern->ptr)) {\
if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {\
php_error(E_WARNING, "Couldn't fetch %s", intern->zo.ce->name);\
RETURN_NULL();\
}\
__ptr = (__type)my_res->ptr; \
__prptr = (__prtype)my_res->prinfo; \
if (!strcmp((char *)__name, "mysqli_stmt")) {\
if (!((STMT *)__ptr)->stmt->mysql) {\
php_error(E_WARNING, "Statement isn't valid anymore");\
@ -153,6 +230,7 @@ PHP_MYSQLI_EXPORT(zend_object_value) mysqli_objects_new(zend_class_entry * TSRML
#define MYSQLI_CLEAR_RESOURCE(__id) \
{ \
mysqli_object *intern = (mysqli_object *)zend_object_store_get_object(*(__id) TSRMLS_CC);\
efree(intern->ptr); \
intern->ptr = NULL; \
}
@ -246,6 +324,7 @@ PHP_FUNCTION(mysqli_ping);
PHP_FUNCTION(mysqli_prepare);
PHP_FUNCTION(mysqli_query);
PHP_FUNCTION(mysqli_prepare_result);
PHP_FUNCTION(mysqli_profiler);
PHP_FUNCTION(mysqli_read_query_result);
PHP_FUNCTION(mysqli_real_connect);
PHP_FUNCTION(mysqli_real_query);
@ -259,7 +338,6 @@ PHP_FUNCTION(mysqli_rpl_query_type);
PHP_FUNCTION(mysqli_select_db);
PHP_FUNCTION(mysqli_send_long_data);
PHP_FUNCTION(mysqli_send_query);
PHP_FUNCTION(mysqli_set_profiler_opt);
PHP_FUNCTION(mysqli_slave_query);
PHP_FUNCTION(mysqli_ssl_set);
PHP_FUNCTION(mysqli_stat);
@ -285,7 +363,7 @@ ZEND_BEGIN_MODULE_GLOBALS(mysqli)
char *default_socket;
long error_no;
char *error_msg;
PROFILER profiler;
unsigned int profiler;
ZEND_END_MODULE_GLOBALS(mysqli)
#ifdef ZTS
@ -294,17 +372,36 @@ ZEND_END_MODULE_GLOBALS(mysqli)
#define MyG(v) (mysqli_globals.v)
#endif
#define MYSQLI_PROFILER_GETTIME gettimeofday(&MyG(profiler.start), NULL)
#define MYSQLI_PROFILER_REPORTTIME php_mysqli_profiler_elapsed_time()
#define MYSQLI_PROFILER_HEADER(query) php_mysqli_profiler_header(query)
#define MYSQLI_PROFILER_REPORT_RESULT(res) php_mysqli_profiler_result_info(res)
#define MYSQLI_PROFILER_EXPLAIN(mysql,query) \
if (!strncasecmp("select", query, 6)){ \
php_mysqli_profiler_explain(mysql,query); \
if (mysql_errno(mysql)) { \
RETURN_FALSE; \
} \
#define my_estrdup(x) (x) ? estrdup(x) : NULL
#define my_efree(x) if (x) efree(x)
/****** PROFILER MACROS *******/
#define MYSQLI_PROFILER_STARTTIME(ptr) gettimeofday(&ptr##->header.starttime, NULL)
#define MYSQLI_PROFILER_ELAPSEDTIME(ptr) php_mysqli_profiler_timediff(ptr##->header.starttime, &ptr##->header.elapsedtime)
#define MYSQLI_PROFILER_LIFETIME(ptr) php_mysqli_profiler_timediff((ptr)->starttime, &(ptr)->lifetime)
#define MYSQLI_PROFILER_NEW(parent, type, time) php_mysqli_profiler_new_object((PR_COMMON *)parent, type, time)
#define MYSQLI_PROFILER_COMMAND_START(cmd,parent)\
if (MyG(profiler))\
{\
cmd = (PR_COMMAND *)php_mysqli_profiler_new_object((PR_COMMON *)parent, MYSQLI_PR_COMMAND,1);\
}
#define MYSQLI_PROFILER_COMMAND_RETURNLONG(cmd, value)\
if (MyG(profiler))\
{\
char tmp[30];\
sprintf ((char *)&tmp, "%ld", value);\
MYSQLI_PROFILER_ELAPSEDTIME(cmd);\
cmd##->returnvalue = my_estrdup(tmp);\
}
#define MYSQLI_PROFILER_COMMAND_RETURNSTRING(cmd, value)\
if (MyG(profiler))\
{\
MYSQLI_PROFILER_ELAPSEDTIME(cmd);\
cmd##->returnvalue = my_estrdup(value);\
}
#define MYSQLI_PROFILER_EXPLAIN(explain,header,mysql,query) php_mysqli_profiler_explain(explain,header, mysql, query)
ZEND_EXTERN_MODULE_GLOBALS(mysqli);