1 /**************************************************************************/
2 /*                                                                        */
3 /*                                 OCaml                                  */
4 /*                                                                        */
5 /*             Xavier Leroy, projet Cristal, INRIA Rocquencourt           */
6 /*                                                                        */
7 /*   Copyright 1996 Institut National de Recherche en Informatique et     */
8 /*     en Automatique.                                                    */
9 /*                                                                        */
10 /*   All rights reserved.  This file is distributed under the terms of    */
11 /*   the GNU Lesser General Public License version 2.1, with the          */
12 /*   special exception on linking described in the file LICENSE.          */
13 /*                                                                        */
14 /**************************************************************************/
15 
16 #include <caml/mlvalues.h>
17 #include <caml/alloc.h>
18 #include <caml/memory.h>
19 #include "unixsupport.h"
20 #include <time.h>
21 #include <sys/types.h>
22 #include <sys/times.h>
23 #ifdef HAS_GETRUSAGE
24 #include <sys/time.h>
25 #include <sys/resource.h>
26 #endif
27 
28 #ifndef CLK_TCK
29 #ifdef HZ
30 #define CLK_TCK HZ
31 #else
32 #define CLK_TCK 60
33 #endif
34 #endif
35 
unix_times(value unit)36 CAMLprim value unix_times(value unit)
37 {
38 #ifdef HAS_GETRUSAGE
39 
40   value res;
41   struct rusage ru;
42 
43   res = caml_alloc_small(4 * Double_wosize, Double_array_tag);
44 
45   getrusage (RUSAGE_SELF, &ru);
46   Store_double_field (res, 0, ru.ru_utime.tv_sec + ru.ru_utime.tv_usec / 1e6);
47   Store_double_field (res, 1, ru.ru_stime.tv_sec + ru.ru_stime.tv_usec / 1e6);
48   getrusage (RUSAGE_CHILDREN, &ru);
49   Store_double_field (res, 2, ru.ru_utime.tv_sec + ru.ru_utime.tv_usec / 1e6);
50   Store_double_field (res, 3, ru.ru_stime.tv_sec + ru.ru_stime.tv_usec / 1e6);
51   return res;
52 
53 #else
54 
55   value res;
56   struct tms buffer;
57 
58   times(&buffer);
59   res = caml_alloc_small(4 * Double_wosize, Double_array_tag);
60   Store_double_field(res, 0, (double) buffer.tms_utime / CLK_TCK);
61   Store_double_field(res, 1, (double) buffer.tms_stime / CLK_TCK);
62   Store_double_field(res, 2, (double) buffer.tms_cutime / CLK_TCK);
63   Store_double_field(res, 3, (double) buffer.tms_cstime / CLK_TCK);
64   return res;
65 
66 #endif
67 }
68