/
Telemetry.pm
142 lines (126 loc) Β· 4.57 KB
/
Telemetry.pm
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# An attempt at providing an API to nqp::getrusage.
class Telemetry::Period { ... }
class Telemetry {
has int $!cpu-user;
has int $!cpu-sys;
has int $!wallclock;
my num $start = Rakudo::Internals.INITTIME;
multi method new(Telemetry:) { nqp::create(self).SET-SELF }
method SET-SELF() {
my \rusage = nqp::getrusage;
$!cpu-user = nqp::atpos_i(rusage, nqp::const::RUSAGE_UTIME_SEC) * 1000000
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_UTIME_MSEC);
$!cpu-sys = nqp::atpos_i(rusage, nqp::const::RUSAGE_STIME_SEC) * 1000000
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_STIME_MSEC);
$!wallclock = nqp::fromnum_I(1000000*nqp::sub_n(nqp::time_n,$start),Int);
self
}
proto method cpu() { * }
multi method cpu(Telemetry:U:) is raw {
my \rusage = nqp::getrusage;
nqp::atpos_i(rusage, nqp::const::RUSAGE_UTIME_SEC) * 1000000
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_UTIME_MSEC)
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_STIME_SEC) * 1000000
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_STIME_MSEC)
}
multi method cpu(Telemetry:D:) is raw {
nqp::add_i($!cpu-user,$!cpu-sys)
}
proto method cpu-user() { * }
multi method cpu-user(Telemetry:U:) is raw {
my \rusage = nqp::getrusage;
nqp::atpos_i(rusage, nqp::const::RUSAGE_UTIME_SEC) * 1000000
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_UTIME_MSEC)
}
multi method cpu-user(Telemetry:D:) is raw { $!cpu-user }
proto method cpu-sys() { * }
multi method cpu-sys(Telemetry:U:) is raw {
my \rusage = nqp::getrusage;
nqp::atpos_i(rusage, nqp::const::RUSAGE_STIME_SEC) * 1000000
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_STIME_MSEC)
}
multi method cpu-sys(Telemetry:D:) is raw { $!cpu-sys }
proto method wallclock() { * }
multi method wallclock(Telemetry:U:) is raw {
nqp::fromnum_I(1000000 * nqp::time_n,Int)
}
multi method wallclock(Telemetry:D:) is raw { $!wallclock }
proto method Period() { * }
multi method Period(Telemetry:U \SELF:) is raw {
if nqp::iscont(SELF) {
SELF = SELF.new;
nqp::create(Telemetry::Period)
}
else {
die "Must use container of type Telemetry"
}
}
multi method Period(Telemetry:D:) is raw {
my int $cpu-user = $!cpu-user;
my int $cpu-sys = $!cpu-sys;
my int $wallclock = $!wallclock;
self.SET-SELF;
Telemetry::Period.new(
nqp::sub_i($!cpu-user,$cpu-user),
nqp::sub_i($!cpu-sys,$cpu-sys),
nqp::sub_i($!wallclock,$wallclock)
)
}
multi method Str(Telemetry:D:) {
$!wallclock ?? "$.cpu / $!wallclock" !! "cpu / wallclock"
}
multi method gist(Telemetry:D:) {
$!wallclock ?? "$.cpu / $!wallclock" !! "cpu / wallclock"
}
}
class Telemetry::Period is Telemetry {
multi method new(Telemetry::Period:
int :$cpu-user,
int :$cpu-sys,
int :$wallclock
) {
self.new($cpu-user, $cpu-sys, $wallclock)
}
multi method new(Telemetry::Period:
int $cpu-user,
int $cpu-sys,
int $wallclock
) {
my $period := nqp::create(Telemetry::Period);
nqp::bindattr_i($period,Telemetry,'$!cpu-user', $cpu-user);
nqp::bindattr_i($period,Telemetry,'$!cpu-sys', $cpu-sys);
nqp::bindattr_i($period,Telemetry,'$!wallclock',$wallclock);
$period
}
multi method perl(Telemetry::Period:D:) {
"Telemetry::Period.new(:cpu-user({
nqp::getattr_i(self,Telemetry,'$!cpu-user')
}), :cpu-sys({
nqp::getattr_i(self,Telemetry,'$!cpu-sys')
}), :wallclock({
nqp::getattr_i(self,Telemetry,'$!wallclock')
}))"
}
}
multi sub infix:<->(Telemetry:U $a, Telemetry:U $b) {
Telemetry::Period.new(0,0,0)
}
multi sub infix:<->(Telemetry:D $a, Telemetry:U $b) { $a - $b.new }
multi sub infix:<->(Telemetry:U $a, Telemetry:D $b) { $a.new - $b }
multi sub infix:<->(Telemetry:D $a, Telemetry:D $b) {
Telemetry::Period.new(
nqp::sub_i(
nqp::getattr_i(nqp::decont($a),Telemetry,'$!cpu-user'),
nqp::getattr_i(nqp::decont($b),Telemetry,'$!cpu-user')
),
nqp::sub_i(
nqp::getattr_i(nqp::decont($a),Telemetry,'$!cpu-sys'),
nqp::getattr_i(nqp::decont($b),Telemetry,'$!cpu-sys')
),
nqp::sub_i(
nqp::getattr_i(nqp::decont($a),Telemetry,'$!wallclock'),
nqp::getattr_i(nqp::decont($b),Telemetry,'$!wallclock')
)
)
}
# vim: ft=perl6 expandtab sw=4