Navigation Menu

Skip to content

Commit

Permalink
Condense all the time functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ice799 committed May 18, 2010
1 parent 2eb5630 commit 5813685
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 55 deletions.
22 changes: 3 additions & 19 deletions ext/memprof.c
Expand Up @@ -13,7 +13,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <sysexits.h> #include <sysexits.h>
#include <time.h>


#include <st.h> #include <st.h>
#include <intern.h> #include <intern.h>
Expand All @@ -36,21 +35,6 @@ static st_table *objs = NULL;
/* /*
* stuff needed for heap dumping * stuff needed for heap dumping
*/ */
static double
rb_timeofday()
{
struct timeval tv;
#ifdef CLOCK_MONOTONIC
struct timespec tp;

if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
return (double)tp.tv_sec + (double)tp.tv_nsec * 1e-9;
}
#endif
gettimeofday(&tv, NULL);
return (double)tv.tv_sec + (double)tv.tv_usec * 1e-6;
}

static VALUE (*rb_classname)(VALUE); static VALUE (*rb_classname)(VALUE);
static RUBY_DATA_FUNC *rb_bm_mark; static RUBY_DATA_FUNC *rb_bm_mark;
static RUBY_DATA_FUNC *rb_blk_free; static RUBY_DATA_FUNC *rb_blk_free;
Expand Down Expand Up @@ -494,9 +478,9 @@ memprof_trace_request(VALUE self, VALUE env)
trace_invoke_all(TRACE_RESET); trace_invoke_all(TRACE_RESET);
trace_invoke_all(TRACE_START); trace_invoke_all(TRACE_START);


secs = trace_get_time(); secs = timeofday();
VALUE ret = rb_yield(Qnil); VALUE ret = rb_yield(Qnil);
secs = trace_get_time() - secs; secs = timeofday() - secs;


trace_invoke_all(TRACE_DUMP); trace_invoke_all(TRACE_DUMP);
trace_invoke_all(TRACE_STOP); trace_invoke_all(TRACE_STOP);
Expand Down Expand Up @@ -917,7 +901,7 @@ obj_dump(VALUE obj, json_gen gen)
if (th->delay == DELAY_INFTY) if (th->delay == DELAY_INFTY)
json_gen_cstr(gen, "infinity"); json_gen_cstr(gen, "infinity");
else else
json_gen_double(gen, th->delay - rb_timeofday()); json_gen_double(gen, th->delay - timeofday());
} }


if (th->wait_for & WAIT_JOIN) { if (th->wait_for & WAIT_JOIN) {
Expand Down
17 changes: 0 additions & 17 deletions ext/tracer.c
Expand Up @@ -2,8 +2,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/time.h>
#include <time.h>


#include "json.h" #include "json.h"
#include "tracer.h" #include "tracer.h"
Expand Down Expand Up @@ -117,18 +115,3 @@ trace_get_output()
{ {
return tracing_json_gen; return tracing_json_gen;
} }

double
trace_get_time()
{
struct timeval tv;
#ifdef CLOCK_MONOTONIC
struct timespec tp;

if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
return (double)tp.tv_sec + (double)tp.tv_nsec * 1e-9;
}
#endif
gettimeofday(&tv, NULL);
return (double)tv.tv_sec + (double)tv.tv_usec * 1e-6;
}
3 changes: 0 additions & 3 deletions ext/tracer.h
Expand Up @@ -36,9 +36,6 @@ trace_set_output(json_gen gen);
json_gen json_gen
trace_get_output(); trace_get_output();


double
trace_get_time();

/* for now, these will live here */ /* for now, these will live here */
extern void install_malloc_tracer(); extern void install_malloc_tracer();
extern void install_gc_tracer(); extern void install_gc_tracer();
Expand Down
16 changes: 8 additions & 8 deletions ext/tracers/fd.c
Expand Up @@ -42,10 +42,10 @@ read_tramp(int fildes, void *buf, size_t nbyte) {
int err; int err;
ssize_t ret; ssize_t ret;


secs = trace_get_time(); secs = timeofday();
ret = read(fildes, buf, nbyte); ret = read(fildes, buf, nbyte);
err = errno; err = errno;
secs = trace_get_time() - secs; secs = timeofday() - secs;


stats.read_time += secs; stats.read_time += secs;
stats.read_calls++; stats.read_calls++;
Expand All @@ -63,10 +63,10 @@ write_tramp(int fildes, const void *buf, size_t nbyte) {
int err; int err;
ssize_t ret; ssize_t ret;


secs = trace_get_time(); secs = timeofday();
ret = write(fildes, buf, nbyte); ret = write(fildes, buf, nbyte);
err = errno; err = errno;
secs = trace_get_time() - secs; secs = timeofday() - secs;


stats.write_time += secs; stats.write_time += secs;
stats.write_calls++; stats.write_calls++;
Expand All @@ -83,10 +83,10 @@ connect_tramp(int socket, const struct sockaddr *address, socklen_t address_len)
double secs = 0; double secs = 0;
int err, ret; int err, ret;


secs = trace_get_time(); secs = timeofday();
ret = connect(socket, address, address_len); ret = connect(socket, address, address_len);
err = errno; err = errno;
secs = trace_get_time() - secs; secs = timeofday() - secs;


stats.connect_time += secs; stats.connect_time += secs;
stats.connect_calls++; stats.connect_calls++;
Expand All @@ -101,10 +101,10 @@ select_tramp(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, stru
double secs = 0; double secs = 0;
int ret, err; int ret, err;


secs = trace_get_time(); secs = timeofday();
ret = select(nfds, readfds, writefds, errorfds, timeout); ret = select(nfds, readfds, writefds, errorfds, timeout);
err = errno; err = errno;
secs = trace_get_time() - secs; secs = timeofday() - secs;


stats.select_time += secs; stats.select_time += secs;
stats.select_calls++; stats.select_calls++;
Expand Down
4 changes: 2 additions & 2 deletions ext/tracers/gc.c
Expand Up @@ -25,9 +25,9 @@ gc_tramp()
{ {
double secs = 0; double secs = 0;


secs = trace_get_time(); secs = timeofday();
orig_garbage_collect(); orig_garbage_collect();
secs = trace_get_time() - secs; secs = timeofday() - secs;


stats.gc_time += secs; stats.gc_time += secs;
stats.gc_calls++; stats.gc_calls++;
Expand Down
4 changes: 2 additions & 2 deletions ext/tracers/mysql.c
Expand Up @@ -26,9 +26,9 @@ real_query_tramp(void *mysql, const char *stmt_str, unsigned long length) {
double secs = 0; double secs = 0;
int ret; int ret;


secs = trace_get_time(); secs = timeofday();
ret = orig_real_query(mysql, stmt_str, length); ret = orig_real_query(mysql, stmt_str, length);
secs = trace_get_time() - secs; secs = timeofday() - secs;


stats.query_time += secs; stats.query_time += secs;
stats.query_calls++; stats.query_calls++;
Expand Down
26 changes: 22 additions & 4 deletions ext/util.c
@@ -1,10 +1,13 @@
#include <stdlib.h>
#include <time.h>
#include <util.h>

#include <sys/time.h>

/* This is the CRC function used by GNU. Stripped executables may contain a /* This is the CRC function used by GNU. Stripped executables may contain a
* section .gnu_debuglink which holds the name of an elf object with debug * section .gnu_debuglink which holds the name of an elf object with debug
* information and a checksum. * information and a checksum.
*/ * !!!! DO NOT MODIFY THIS FUNCTION !!!!
#include <stdlib.h>
#include <util.h>
/* !!!! DO NOT MODIFY THIS FUNCTION !!!!
* TODO create specs for this! * TODO create specs for this!
*/ */
unsigned long unsigned long
Expand Down Expand Up @@ -71,3 +74,18 @@ gnu_debuglink_crc32(unsigned long crc, unsigned char *buf, size_t len)
crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8); crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
return ~crc & 0xffffffff; return ~crc & 0xffffffff;
} }

double
timeofday()
{
struct timeval tv;
#ifdef CLOCK_MONOTONIC
struct timespec tp;

if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
return (double)tp.tv_sec + (double)tp.tv_nsec * 1e-9;
}
#endif
gettimeofday(&tv, NULL);
return (double)tv.tv_sec + (double)tv.tv_usec * 1e-6;
}
5 changes: 5 additions & 0 deletions ext/util.h
Expand Up @@ -73,4 +73,9 @@ struct memprof_config {
unsigned long unsigned long
gnu_debuglink_crc32 (unsigned long crc, unsigned char *buf, size_t len); gnu_debuglink_crc32 (unsigned long crc, unsigned char *buf, size_t len);


/* Use this function for time tracking. It will (interally) try to use an
* appropriately granual timing function.
*/
double
timeofday();
#endif #endif

0 comments on commit 5813685

Please sign in to comment.