Skip to content

Commit

Permalink
trace reads on file descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm1 committed Apr 14, 2010
1 parent 4d6f553 commit 7349658
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions ext/memprof.c
Expand Up @@ -1835,6 +1835,7 @@ Init_memprof()
install_malloc_tracer();
install_gc_tracer();
install_objcount_tracer();
install_fd_tracer();

gc_hook = Data_Wrap_Struct(rb_cObject, sourcefile_marker, NULL, NULL);
rb_global_variable(&gc_hook);
Expand Down
88 changes: 88 additions & 0 deletions ext/tracers/fd.c
@@ -0,0 +1,88 @@
#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_fd_stats {
size_t read_calls;
double read_time;
size_t read_requested_bytes;
size_t read_actual_bytes;

size_t readv_calls;
size_t write_calls;
size_t writev_calls;
};

static struct tracer tracer;
static struct memprof_fd_stats memprof_fd_stats;

static ssize_t
read_tramp(int fildes, void *buf, size_t nbyte) {
struct timeval start, end;
double secs = 0;
int err;
ssize_t ret;

gettimeofday (&start, NULL);
ret = read(fildes, buf, nbyte);
err = errno;
gettimeofday (&end, NULL);

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

memprof_fd_stats.read_time += secs;
memprof_fd_stats.read_calls++;
memprof_fd_stats.read_requested_bytes += nbyte;
if (ret > 0)
memprof_fd_stats.read_actual_bytes += ret;

errno = err;
return ret;
}

static void
fd_trace_start() {
struct tramp_st2_entry tmp;
tmp.addr = read_tramp;
bin_update_image("read", &tmp, NULL);
}

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

static void
fd_trace_reset() {
memset(&memprof_fd_stats, 0, sizeof(memprof_fd_stats));
}

static void
fd_trace_dump() {
fprintf(stderr, "================ FDs ======================================\n");
fprintf(stderr, " # read calls: %zd\n", memprof_fd_stats.read_calls);
fprintf(stderr, " time in read: %fs\n", memprof_fd_stats.read_time);
fprintf(stderr, " bytes: %zd requested / %zd actual\n", memprof_fd_stats.read_requested_bytes, memprof_fd_stats.read_actual_bytes);
fprintf(stderr, "===========================================================\n\n");
}

void install_fd_tracer()
{
tracer.start = fd_trace_start;
tracer.stop = fd_trace_stop;
tracer.reset = fd_trace_reset;
tracer.dump = fd_trace_dump;
tracer.id = strdup("fd_tracer");

trace_insert(&tracer);
}

0 comments on commit 7349658

Please sign in to comment.