Skip to content
Browse files

Condense all the time functions.

  • Loading branch information...
1 parent 2eb5630 commit 581368559a9b58e8ff6b9a20cdfadfb5aa674baa @ice799 committed
Showing with 42 additions and 55 deletions.
  1. +3 −19 ext/memprof.c
  2. +0 −17 ext/tracer.c
  3. +0 −3 ext/tracer.h
  4. +8 −8 ext/tracers/fd.c
  5. +2 −2 ext/tracers/gc.c
  6. +2 −2 ext/tracers/mysql.c
  7. +22 −4 ext/util.c
  8. +5 −0 ext/util.h
View
22 ext/memprof.c
@@ -13,7 +13,6 @@
#include <stdlib.h>
#include <unistd.h>
#include <sysexits.h>
-#include <time.h>
#include <st.h>
#include <intern.h>
@@ -36,21 +35,6 @@ static st_table *objs = NULL;
/*
* 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 RUBY_DATA_FUNC *rb_bm_mark;
static RUBY_DATA_FUNC *rb_blk_free;
@@ -494,9 +478,9 @@ memprof_trace_request(VALUE self, VALUE env)
trace_invoke_all(TRACE_RESET);
trace_invoke_all(TRACE_START);
- secs = trace_get_time();
+ secs = timeofday();
VALUE ret = rb_yield(Qnil);
- secs = trace_get_time() - secs;
+ secs = timeofday() - secs;
trace_invoke_all(TRACE_DUMP);
trace_invoke_all(TRACE_STOP);
@@ -917,7 +901,7 @@ obj_dump(VALUE obj, json_gen gen)
if (th->delay == DELAY_INFTY)
json_gen_cstr(gen, "infinity");
else
- json_gen_double(gen, th->delay - rb_timeofday());
+ json_gen_double(gen, th->delay - timeofday());
}
if (th->wait_for & WAIT_JOIN) {
View
17 ext/tracer.c
@@ -2,8 +2,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <sys/time.h>
-#include <time.h>
#include "json.h"
#include "tracer.h"
@@ -117,18 +115,3 @@ trace_get_output()
{
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;
-}
View
3 ext/tracer.h
@@ -36,9 +36,6 @@ trace_set_output(json_gen gen);
json_gen
trace_get_output();
-double
-trace_get_time();
-
/* for now, these will live here */
extern void install_malloc_tracer();
extern void install_gc_tracer();
View
16 ext/tracers/fd.c
@@ -42,10 +42,10 @@ read_tramp(int fildes, void *buf, size_t nbyte) {
int err;
ssize_t ret;
- secs = trace_get_time();
+ secs = timeofday();
ret = read(fildes, buf, nbyte);
err = errno;
- secs = trace_get_time() - secs;
+ secs = timeofday() - secs;
stats.read_time += secs;
stats.read_calls++;
@@ -63,10 +63,10 @@ write_tramp(int fildes, const void *buf, size_t nbyte) {
int err;
ssize_t ret;
- secs = trace_get_time();
+ secs = timeofday();
ret = write(fildes, buf, nbyte);
err = errno;
- secs = trace_get_time() - secs;
+ secs = timeofday() - secs;
stats.write_time += secs;
stats.write_calls++;
@@ -83,10 +83,10 @@ connect_tramp(int socket, const struct sockaddr *address, socklen_t address_len)
double secs = 0;
int err, ret;
- secs = trace_get_time();
+ secs = timeofday();
ret = connect(socket, address, address_len);
err = errno;
- secs = trace_get_time() - secs;
+ secs = timeofday() - secs;
stats.connect_time += secs;
stats.connect_calls++;
@@ -101,10 +101,10 @@ select_tramp(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, stru
double secs = 0;
int ret, err;
- secs = trace_get_time();
+ secs = timeofday();
ret = select(nfds, readfds, writefds, errorfds, timeout);
err = errno;
- secs = trace_get_time() - secs;
+ secs = timeofday() - secs;
stats.select_time += secs;
stats.select_calls++;
View
4 ext/tracers/gc.c
@@ -25,9 +25,9 @@ gc_tramp()
{
double secs = 0;
- secs = trace_get_time();
+ secs = timeofday();
orig_garbage_collect();
- secs = trace_get_time() - secs;
+ secs = timeofday() - secs;
stats.gc_time += secs;
stats.gc_calls++;
View
4 ext/tracers/mysql.c
@@ -26,9 +26,9 @@ real_query_tramp(void *mysql, const char *stmt_str, unsigned long length) {
double secs = 0;
int ret;
- secs = trace_get_time();
+ secs = timeofday();
ret = orig_real_query(mysql, stmt_str, length);
- secs = trace_get_time() - secs;
+ secs = timeofday() - secs;
stats.query_time += secs;
stats.query_calls++;
View
26 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
* section .gnu_debuglink which holds the name of an elf object with debug
* information and a checksum.
- */
-#include <stdlib.h>
-#include <util.h>
-/* !!!! DO NOT MODIFY THIS FUNCTION !!!!
+ * !!!! DO NOT MODIFY THIS FUNCTION !!!!
* TODO create specs for this!
*/
unsigned long
@@ -71,3 +74,18 @@ gnu_debuglink_crc32(unsigned long crc, unsigned char *buf, size_t len)
crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
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;
+}
View
5 ext/util.h
@@ -73,4 +73,9 @@ struct memprof_config {
unsigned long
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

0 comments on commit 5813685

Please sign in to comment.
Something went wrong with that request. Please try again.