Skip to content

Commit

Permalink
trace mysql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm1 committed Apr 14, 2010
1 parent 7349658 commit 66a8aa8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions ext/memprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -1836,6 +1836,7 @@ Init_memprof()
install_gc_tracer();
install_objcount_tracer();
install_fd_tracer();
install_mysql_tracer();

gc_hook = Data_Wrap_Struct(rb_cObject, sourcefile_marker, NULL, NULL);
rb_global_variable(&gc_hook);
Expand Down
76 changes: 76 additions & 0 deletions ext/tracers/mysql.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

#include "arch.h"
#include "bin_api.h"
#include "tracer.h"
#include "tramp.h"
#include "util.h"

struct memprof_mysql_stats {
size_t query_calls;
double query_time;
};

static struct tracer tracer;
static struct memprof_mysql_stats memprof_mysql_stats;
static int (*orig_real_query)(void *mysql, const char *stmt_str, unsigned long length);

static int
real_query_tramp(void *mysql, const char *stmt_str, unsigned long length) {
struct timeval start, end;
double secs = 0;
int ret;

gettimeofday (&start, NULL);
ret = orig_real_query(mysql, stmt_str, length);
gettimeofday (&end, NULL);

secs += end.tv_sec - start.tv_sec;
secs += (end.tv_usec - start.tv_usec) / 1000000.0;

memprof_mysql_stats.query_time += secs;
memprof_mysql_stats.query_calls++;

return ret;
}

static void
mysql_trace_start() {
struct tramp_st2_entry tmp;
tmp.addr = real_query_tramp;
bin_update_image("mysql_real_query", &tmp, (void **)(&orig_real_query));
}

static void
mysql_trace_stop() {
// TODO: figure out how to undo the tramp
}

static void
mysql_trace_reset() {
memset(&memprof_mysql_stats, 0, sizeof(memprof_mysql_stats));
}

static void
mysql_trace_dump() {
fprintf(stderr, "================ Mysql ====================================\n");
fprintf(stderr, " # queries: %zd\n", memprof_mysql_stats.query_calls);
fprintf(stderr, " time querying: %fs\n", memprof_mysql_stats.query_time);
fprintf(stderr, "===========================================================\n\n");
}

void install_mysql_tracer()
{
tracer.start = mysql_trace_start;
tracer.stop = mysql_trace_stop;
tracer.reset = mysql_trace_reset;
tracer.dump = mysql_trace_dump;
tracer.id = strdup("mysql_tracer");

trace_insert(&tracer);
}

0 comments on commit 66a8aa8

Please sign in to comment.