/
Kernel.pm6
187 lines (168 loc) Β· 5.69 KB
/
Kernel.pm6
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# The Kernel class and its methods, underlying $*KERNEL, are a work in progress.
# It is very hard to capture data about a changing universe in a stable API.
# If you find errors for your hardware or OS distribution, please report them
# with the values that you expected and how to get them in your situation.
class Kernel does Systemic {
has Str $.release;
has Str $!hardware;
has Str $!arch;
has Int $!bits;
sub uname($opt) {
state $has_uname = "/bin/uname".IO.x || "/usr/bin/uname".IO.x;
$has_uname ?? qqx/uname $opt/.chomp !! 'unknown';
}
submethod BUILD(:$!auth = "unknown" --> Nil) { }
method name {
$!name //= do {
given $*DISTRO.name {
when 'mswin32' {
'win32'
}
default {
lc uname '-s';
}
}
}
}
method version {
$!version //= Version.new( do {
given $*DISTRO.name {
when 'freebsd' {
uname '-r'; # -K -U not introduced until 10.0
}
when 'macosx' {
my $unamev = uname '-v';
$unamev ~~ m/^Darwin \s+ Kernel \s+ Version \s+ (<[\d\.]>+)/
?? ~$0
!! $unamev.chomp;
}
default {
given $.name {
when 'linux' {
# somewhat counter-intuitively the '-r' is what
# most people think of the kernel version
uname '-r';
}
default {
uname '-v';
}
}
}
}
} );
}
method release {
$!release //= do {
given $*DISTRO.name {
when any <openbsd netbsd dragonfly> { # needs adapting
uname '-r';
}
default {
uname '-v';
}
}
}
}
method hardware {
$!hardware //= do {
given $*DISTRO.name {
default {
uname '-m';
}
}
}
}
method arch {
$!arch //= do {
given $*DISTRO.name {
when 'raspbian' {
uname '-m';
}
default {
uname '-p';
}
}
}
}
method archname {
self.hardware ~ '-' ~ self.name
}
method bits {
$!bits //= $.hardware ~~ m/_64|w|amd64/ ?? 64 !! 32; # naive approach
}
has @!signals; # Signal
#?if jvm
method signals (Kernel:D:) {
@!signals //= [2, 9]
}
#?endif
has $!signals-setup-lock = Lock.new;
#?if moar
has $!signals-setup = False;
method signals (Kernel:D:) {
unless $!signals-setup {
$!signals-setup-lock.protect: {
unless $!signals-setup {
my @names;
if self.name eq 'win32' {
# These are the ones libuv emulates on Windows.
@names = flat "", <INT BREAK HUP WINCH>;
} else {
if self.name eq 'openbsd' {
# otherwise it uses a shell buildin
@names = flat "", qx!/bin/kill -l!.words;
}
else {
@names = flat "", qx/kill -l/.words;
}
@names.splice(1,1) if @names[1] eq "0"; # Ubuntu fudge
@names.=map({.uc}) if $*KERNEL.name eq 'dragonfly';
}
for Signal.^enum_value_list -> $signal {
my $name = substr($signal.key,3);
if @names.first( * eq $name, :k ) -> $index {
@!signals[$index] = $signal;
}
}
$!signals-setup = True;
}
}
}
@!signals
}
#?endif
has %!signals-by-Str;
has $!signals-by-Str-setup = False;
proto method signal (|) {*}
multi method signal(Kernel:D: Str:D $signal --> Int:D) {
unless $!signals-by-Str-setup {
$!signals-setup-lock.protect: {
unless $!signals-by-Str-setup {
nqp::stmts(
(my int $els = @.signals.elems),
(my int $i = -1),
nqp::while(
nqp::isgt_i($els, $i = nqp::add_i($i, 1)),
($_ := @!signals.AT-POS($i)).defined
&& %!signals-by-Str.ASSIGN-KEY(.Str, nqp::decont($i))));
$!signals-by-Str-setup = True;
}
}
}
%!signals-by-Str{$signal} // %!signals-by-Str{"SIG$signal"} // Int;
}
multi method signal(Kernel:D: Signal:D \signal --> Int:D) { signal.value }
multi method signal(Kernel:D: Int:D \signal --> Int:D) { signal }
method cpu-cores() is raw { nqp::cpucores }
method cpu-usage() 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)
}
}
Rakudo::Internals.REGISTER-DYNAMIC: '$*KERNEL', {
PROCESS::<$KERNEL> := Kernel.new;
}
# vim: ft=perl6 expandtab sw=4