diff options
| author | Bjoern Brandenburg <bbb@mpi-sws.org> | 2012-08-06 12:27:32 -0400 |
|---|---|---|
| committer | Bjoern Brandenburg <bbb@mpi-sws.org> | 2012-11-24 08:56:54 -0500 |
| commit | 6e7c15480d7f5e57636d5648c97c4277eea2c704 (patch) | |
| tree | 846401491138c2f8bf7487b94f13e7e35c569121 /native | |
| parent | 0e2edbe1dda2f944cdab0dd896b9434bf386f572 (diff) | |
Add support for clock_gettime
Use the more accurate clock under Linux when available. getrusage()
unfortunately only has a resolution of 1/HZ (i.e., one jiffie), which
is too coarse-grained in many cases.
Diffstat (limited to 'native')
| -rw-r--r-- | native/Makefile | 5 | ||||
| -rw-r--r-- | native/src/cpu_time.cpp | 24 |
2 files changed, 29 insertions, 0 deletions
diff --git a/native/Makefile b/native/Makefile index f9fbb75..80c8310 100644 --- a/native/Makefile +++ b/native/Makefile | |||
| @@ -36,6 +36,11 @@ PYTHON_LIB ?= | |||
| 36 | SOFLAGS = -shared | 36 | SOFLAGS = -shared |
| 37 | endif | 37 | endif |
| 38 | 38 | ||
| 39 | # Support for clock_gettime() | ||
| 40 | ifeq ($(OS),Linux) | ||
| 41 | LIBS += -lrt | ||
| 42 | endif | ||
| 43 | |||
| 39 | CXXFLAGS = -Wall -Wextra $(DISABLED_WARNINGS) -fPIC $(INCLUDES) | 44 | CXXFLAGS = -Wall -Wextra $(DISABLED_WARNINGS) -fPIC $(INCLUDES) |
| 40 | LDFLAGS = $(LIBS) | 45 | LDFLAGS = $(LIBS) |
| 41 | SWIGFLAGS = -python -c++ -outdir . -includeall -Iinclude | 46 | SWIGFLAGS = -python -c++ -outdir . -includeall -Iinclude |
diff --git a/native/src/cpu_time.cpp b/native/src/cpu_time.cpp index a7dfa6d..d003f22 100644 --- a/native/src/cpu_time.cpp +++ b/native/src/cpu_time.cpp | |||
| @@ -1,9 +1,32 @@ | |||
| 1 | 1 | ||
| 2 | #include <time.h> | ||
| 2 | #include <sys/time.h> | 3 | #include <sys/time.h> |
| 3 | #include <sys/resource.h> | 4 | #include <sys/resource.h> |
| 4 | 5 | ||
| 5 | #include "cpu_time.h" | 6 | #include "cpu_time.h" |
| 6 | 7 | ||
| 8 | |||
| 9 | #if _POSIX_C_SOURCE >= 199309L | ||
| 10 | |||
| 11 | // use clock_xxx() API | ||
| 12 | |||
| 13 | double get_cpu_usage(void) | ||
| 14 | { | ||
| 15 | struct timespec ts; | ||
| 16 | |||
| 17 | if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) | ||
| 18 | { | ||
| 19 | return ts.tv_sec + ts.tv_nsec / 1E9; | ||
| 20 | } | ||
| 21 | else | ||
| 22 | return 0.0; | ||
| 23 | } | ||
| 24 | |||
| 25 | |||
| 26 | #else | ||
| 27 | |||
| 28 | // fall back to getrusage() | ||
| 29 | |||
| 7 | #ifdef RUSAGE_THREAD | 30 | #ifdef RUSAGE_THREAD |
| 8 | // This is a Linuxism... | 31 | // This is a Linuxism... |
| 9 | #define ACCOUNTING_SCOPE RUSAGE_THREAD | 32 | #define ACCOUNTING_SCOPE RUSAGE_THREAD |
| @@ -23,3 +46,4 @@ double get_cpu_usage(void) | |||
| 23 | return 0.0; | 46 | return 0.0; |
| 24 | } | 47 | } |
| 25 | 48 | ||
| 49 | #endif | ||
