From b2165c26d73fd3ba8fc1cf33745b5732ea25e47f Mon Sep 17 00:00:00 2001 From: startx2017 Date: Mon, 9 Oct 2017 08:10:06 -0400 Subject: added a tool to measure time spent in various functions --- src/include/common.h | 2 ++ src/lib/common.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/include/common.h b/src/include/common.h index 4c517e427..80f55803d 100644 --- a/src/include/common.h +++ b/src/include/common.h @@ -124,6 +124,8 @@ static inline unsigned long long getticks(void) { #endif } +void timetrace_start(void); +float timetrace_end(void); int join_namespace(pid_t pid, char *type); int name2pid(const char *name, pid_t *pid); char *pid_proc_comm(const pid_t pid); diff --git a/src/lib/common.c b/src/lib/common.c index b44563733..3d7fc5ffa 100644 --- a/src/lib/common.c +++ b/src/lib/common.c @@ -282,3 +282,42 @@ int pid_hidepid(void) { fclose(fp); return 0; } + +//************************** +// time trace based on getticks function +//************************** +static int tt_not_implemented = 0; // not implemented for the current architecture +static unsigned long long tt_1ms = 0; +static unsigned long long tt = 0; // start time + +void timetrace_start(void) { + if (tt_not_implemented) + return; + unsigned long long t1 = getticks(); + if (t1 == 0) { + tt_not_implemented = 1; + return; + } + + if (tt_1ms == 0) { + usleep(1000); // sleep 1 ms + unsigned long long t2 = getticks(); + tt_1ms = t2 - t1; + if (tt_1ms == 0) { + tt_not_implemented = 1; + return; + } + } + + tt = getticks(); +} + +float timetrace_end(void) { + if (tt_not_implemented) + return 0; + + unsigned long long delta = getticks() - tt; + assert(tt_1ms); + + return (float) delta / (float) tt_1ms; +} -- cgit v1.2.3-54-g00ecf