-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
Kernel.pm6
215 lines (185 loc) Β· 5.99 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# 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 is built(:bind);
has Str $!hardware is built(:bind);
has Str $!arch is built(:bind);
has Int $!bits is built(:bind);
#?if !jvm
has $!uname;
method !uname {
$!uname ?? $!uname !! ($!uname := nqp::uname())
}
#?endif
method !uname-s(--> Str:D) {
#?if !jvm
nqp::atpos_s(self!uname, nqp::const::UNAME_SYSNAME)
#?endif
#?if jvm
try shell('uname -s', :out, :!err).out.slurp(:close).chomp;
#?endif
}
method !uname-r(--> Str:D) {
#?if !jvm
nqp::atpos_s(self!uname, nqp::const::UNAME_RELEASE)
#?endif
#?if jvm
try shell('uname -r', :out, :!err).out.slurp(:close).chomp;
#?endif
}
method !uname-v(--> Str:D) {
#?if !jvm
nqp::atpos_s(self!uname, nqp::const::UNAME_VERSION)
#?endif
#?if jvm
try shell('uname -v', :out, :!err).out.slurp(:close).chomp;
#?endif
}
method !uname-m(--> Str:D) {
#?if !jvm
nqp::atpos_s(self!uname, nqp::const::UNAME_MACHINE)
#?endif
#?if jvm
try shell('uname -m', :out, :!err).out.slurp(:close).chomp;
#?endif
}
method !uname-p(--> Str:D) {
# TODO: find a way to get this without shelling out
try shell("uname -p", :out, :!err).out.slurp(:close).chomp;
}
method name(--> Str:D) {
$!name eq 'unknown' ?? self!name($*DISTRO.name) !! $!name
}
method !name(Str:D \distro --> Str:D) {
# https://github.com/rakudo/rakudo/issues/3436
nqp::bind($!name,distro eq 'mswin32'
?? 'win32'
!! distro eq 'browser'
?? 'browser'
!! self!uname-s.lc
)
}
method version(--> Version:D) {
$!version
?? $!version
# https://github.com/rakudo/rakudo/issues/3436
!! nqp::bind($!version,self!uname-v.Version)
}
method release(--> Str:D) {
# somewhat counter-intuitively the UNAME_RELEASE is what
# most people think of the kernel version
$!release ?? $!release !! ($!release := self!uname-r)
}
method hardware(--> Str:D) {
$!hardware ?? $!hardware !! ($!hardware := self!uname-m)
}
method arch {
$!arch ?? $!arch !! self!arch($*DISTRO.name)
}
method !arch(Str:D \distro --> Str:D) {
$!arch := distro eq 'raspbian'
?? self!uname-m
!! distro eq 'browser'
?? self!uname-m
!! self!uname-p
}
method archname(--> Str:D) {
self.hardware ~ '-' ~ self.name
}
method bits(--> Int:D) {
$!bits
?? $!bits
# naive approach
!! ($!bits := $.hardware ~~ m/_64|w|amd64/ ?? 64 !! 32);
}
method hostname(--> Str:D) {
nqp::p6box_s(nqp::gethostname)
}
has @!signals; # Signal
has $!signals-setup-lock = Lock.new;
has $!signals-setup = False;
method signals (Kernel:D:) {
unless $!signals-setup {
$!signals-setup-lock.protect: {
unless $!signals-setup {
my \arr = nqp::list(Nil);
my int $els = nqp::add_i(Signal.enums.values.max, 1);
my int $i = 1;
nqp::while(
nqp::islt_i($i, $els),
nqp::bindpos(arr, $i, Signal($i) // Nil),
++$i
);
@!signals = |arr;
$!signals-setup = True;
}
}
}
@!signals
}
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 {
my int $els = @.signals.elems;
my int $i = -1;
nqp::while(
nqp::isgt_i($els,++$i),
($_ := @!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(--> Int) { nqp::cpucores }
my $cpu-cores-but-one := nqp::null;
method cpu-cores-but-one() is implementation-detail {
nqp::ifnull(
$cpu-cores-but-one,
$cpu-cores-but-one := max nqp::cpucores() - 1, 1
)
}
method cpu-usage(--> Int) is raw {
my int @rusage;
nqp::getrusage(@rusage);
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)
}
method free-memory(--> Int) {
nqp::freemem()
}
my $total-mem := nqp::null();
method total-memory(--> Int) {
nqp::ifnull(
$total-mem,
nqp::bind($total-mem,nqp::p6box_i(nqp::totalmem()))
)
}
my $endian := nqp::null;
method endian(--> Endian:D) {
nqp::ifnull(
$endian,
nqp::bind($endian,nqp::if(
blob8.new(0,1).read-int16(0) == 1, # hacky way to find out
BigEndian,
LittleEndian
))
)
}
}
Rakudo::Internals.REGISTER-DYNAMIC: '$*KERNEL', {
PROCESS::<$KERNEL> := Kernel.new;
}
# vim: expandtab shiftwidth=4