aboutsummaryrefslogtreecommitdiffstats
path: root/native
diff options
context:
space:
mode:
authorBjoern Brandenburg <bbb@mpi-sws.org>2012-08-06 12:27:32 -0400
committerBjoern Brandenburg <bbb@mpi-sws.org>2012-11-24 08:56:54 -0500
commit6e7c15480d7f5e57636d5648c97c4277eea2c704 (patch)
tree846401491138c2f8bf7487b94f13e7e35c569121 /native
parent0e2edbe1dda2f944cdab0dd896b9434bf386f572 (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/Makefile5
-rw-r--r--native/src/cpu_time.cpp24
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 ?=
36SOFLAGS = -shared 36SOFLAGS = -shared
37endif 37endif
38 38
39# Support for clock_gettime()
40ifeq ($(OS),Linux)
41LIBS += -lrt
42endif
43
39CXXFLAGS = -Wall -Wextra $(DISABLED_WARNINGS) -fPIC $(INCLUDES) 44CXXFLAGS = -Wall -Wextra $(DISABLED_WARNINGS) -fPIC $(INCLUDES)
40LDFLAGS = $(LIBS) 45LDFLAGS = $(LIBS)
41SWIGFLAGS = -python -c++ -outdir . -includeall -Iinclude 46SWIGFLAGS = -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
13double 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