-
Notifications
You must be signed in to change notification settings - Fork 0
/
rusage.m
68 lines (61 loc) · 1.92 KB
/
rusage.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
:- module rusage.
:- interface.
:- import_module io, int.
:- type who ---> self ; children.
:- type usage
---> usage(
user_sec :: int,
user_usec :: int,
system_sec :: int,
system_usec :: int,
max_rss :: float,
minor_faults :: int,
major_faults :: int,
in_blocks :: int,
out_blocks :: int,
vol_context :: int,
invol_context :: int
).
:- pred getrusage(who::in, usage::out, io::di, io::uo) is det.
:- implementation.
:- pragma foreign_decl("C", "
#include <sys/time.h>
#include <sys/resource.h>
").
getrusage(Who, usage(USec, UUsec, SSec, SUSec, MaxRSS, MinFlt, MajFlt, InBlock, OutBlock, VolCTX, InvolCTX), !IO) :-
c_getrusage(Who, USec, UUsec, SSec, SUSec, MaxRSS, MinFlt, MajFlt, InBlock, OutBlock, VolCTX, InvolCTX, !IO).
:- pragma foreign_enum("C", who/0, [
self - "RUSAGE_SELF",
children - "RUSAGE_CHILDREN"
]).
:- pred c_getrusage(who, int, int, int, int, float, int, int, int, int, int, int, io, io).
:- mode c_getrusage(in, out, out, out, out, out, out, out, out, out, out, out, di, uo) is det.
:- pragma foreign_proc("C",
c_getrusage(
Who::in,
USec::out, UUsec::out, SSec::out, SUsec::out,
MaxRSS::out,
MinFlt::out, MajFlt::out,
InBlock::out, OutBlock::out,
VolCTX::out, InvolCTX::out,
_IO0::di, _IO::uo),
[will_not_call_mercury, promise_pure],
"
struct rusage usage;
getrusage(Who, &usage);
USec = usage.ru_utime.tv_sec;
UUsec = usage.ru_utime.tv_usec;
SSec = usage.ru_stime.tv_sec;
SUsec = usage.ru_stime.tv_usec;
#ifdef __APPLE__
MaxRSS = usage.ru_maxrss / 1024.0;
#else
MaxRSS = usage.ru_maxrss;
#endif
MinFlt = usage.ru_minflt;
MajFlt = usage.ru_majflt;
InBlock = usage.ru_inblock;
OutBlock = usage.ru_oublock;
VolCTX = usage.ru_nvcsw;
InvolCTX = usage.ru_nivcsw;
").