chiark / gitweb /
debugging for thing that crashed
[innduct.git] / lib / resource.c
1 /*  $Id: resource.c 6135 2003-01-19 01:15:40Z rra $
2 **
3 */
4
5 #include "config.h"
6 #include "clibrary.h"
7 #include "libinn.h"
8
9 #ifdef HAVE_GETRUSAGE
10
11 #include <sys/time.h>
12 #include <sys/resource.h>
13
14 #define TIMEVALasDOUBLE(t)      \
15     ((double)(t).tv_sec + ((double)(t).tv_usec) / 1000000.0)
16
17 int getrusage(int who, struct rusage *rusage);
18
19 int GetResourceUsage(double *usertime, double *systime)
20 {
21     struct rusage       R;
22
23     if (getrusage(RUSAGE_SELF, &R) < 0)
24         return -1;
25     *usertime = TIMEVALasDOUBLE(R.ru_utime);
26     *systime = TIMEVALasDOUBLE(R.ru_stime);
27     return 0;
28 }
29
30 #else /* HAVE_GETRUSAGE */
31
32 #include <sys/param.h>
33 #include <sys/times.h>
34
35 #if     !defined(HZ)
36 #define HZ      60
37 #endif  /* !defined(HZ) */
38
39 #define CPUTIMEasDOUBLE(t1, t2)         ((double)(t1 + t2) / (double)HZ)
40
41 int GetResourceUsage(double *usertime, double *systime)
42 {
43     struct tms  T;
44
45     if (times(&T) == -1)
46         return -1;
47     *usertime = CPUTIMEasDOUBLE(T.tms_utime, T.tms_cutime);
48     *systime = CPUTIMEasDOUBLE(T.tms_stime, T.tms_cstime);
49     return 0;
50 }
51
52 #endif /* !HAVE_GETRUSAGE */