Permalink
Browse files

Simple postgres tracer

  • Loading branch information...
1 parent 122e739 commit 41c1316a4f4a08bfb1f8b2b0f87c6d0f6ed64e40 @tmm1 tmm1 committed Jan 29, 2011
Showing with 75 additions and 0 deletions.
  1. +1 −0 ext/memprof.c
  2. +1 −0 ext/tracer.h
  3. +73 −0 ext/tracers/postgres.c
View
@@ -1950,6 +1950,7 @@ Init_memprof()
install_objects_tracer();
install_fd_tracer();
install_mysql_tracer();
+ install_postgres_tracer();
install_memcache_tracer();
install_resources_tracer();
View
@@ -41,6 +41,7 @@ extern void install_malloc_tracer();
extern void install_gc_tracer();
extern void install_fd_tracer();
extern void install_mysql_tracer();
+extern void install_postgres_tracer();
extern void install_objects_tracer();
extern void install_memcache_tracer();
extern void install_resources_tracer();
View
@@ -0,0 +1,73 @@
+#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 "json.h"
+#include "tracer.h"
+#include "tramp.h"
+#include "util.h"
+
+struct memprof_postgres_stats {
+ size_t query_calls;
+};
+
+static struct tracer tracer;
+static struct memprof_postgres_stats stats;
+static void * (*orig_PQexec)(void *postgres, const char *stmt);
+
+static void *
+PQexec_tramp(void *postgres, const char *stmt) {
+ void *ret;
+
+ ret = orig_PQexec(postgres, stmt);
+ stats.query_calls++;
+
+ return ret;
+}
+
+static void
+postgres_trace_start() {
+ static int inserted = 0;
+
+ if (!inserted)
+ inserted = 1;
+ else
+ return;
+
+ orig_PQexec = bin_find_symbol("PQexec", NULL, 1);
+ if (orig_PQexec)
+ insert_tramp("PQexec", PQexec_tramp);
+}
+
+static void
+postgres_trace_stop() {
+}
+
+static void
+postgres_trace_reset() {
+ memset(&stats, 0, sizeof(stats));
+}
+
+static void
+postgres_trace_dump(json_gen gen) {
+ if (stats.query_calls > 0) {
+ json_gen_cstr(gen, "queries");
+ json_gen_integer(gen, stats.query_calls);
+ }
+}
+
+void install_postgres_tracer()
+{
+ tracer.start = postgres_trace_start;
+ tracer.stop = postgres_trace_stop;
+ tracer.reset = postgres_trace_reset;
+ tracer.dump = postgres_trace_dump;
+ tracer.id = "postgres";
+
+ trace_insert(&tracer);
+}

0 comments on commit 41c1316

Please sign in to comment.