forked from dtrace4linux/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.pl
More file actions
executable file
·398 lines (338 loc) · 9.34 KB
/
tests.pl
File metadata and controls
executable file
·398 lines (338 loc) · 9.34 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#! /usr/bin/perl
# $Header:$
# Author : Paul Fox
# Date: April 2011
# 20141214 PDF Fix "dmesg" test not actually working.
use strict;
use warnings;
use File::Basename;
use FileHandle;
use Getopt::Long;
use IO::File;
use POSIX;
my $exit_code = 0;
my $ctrl_c = 0;
my $master_dmesg;
my $oops;
#######################################################################
# Command line switches. #
#######################################################################
my %opts = (
count => 1,
loop => 1000,
);
######################################################################
# Watch for a kernel panic - we should try and abort asap. #
######################################################################
sub check_oops
{
my $oops = 0;
my $fh = new FileHandle("dmesg | ");
while (<$fh>) {
$oops++ if /Oops:/;
}
return $oops;
}
sub commify {
local $_ = shift;
1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
return $_;
}
sub do_child
{ my $ppid = shift;
$ppid = $$ if !$ppid;
while (!$ctrl_c && -d "/proc/$ppid") {
my $fh = new FileHandle("/etc/hosts");
my $str = <$fh>;
new FileHandle("/etc/hosts-nonexistant");
###############################################
# We may/may not have each binary. #
###############################################
if (-f "build/sys64") {
system("build/sys64");
system("strace build/sys64 >/dev/null 2>&1");
}
if (-f "build/sys32") {
system("build/sys32");
system("strace build/sys32 >/dev/null 2>&1");
}
system("du /proc >/dev/null 2>&1");
if (fork() == 0) {
exit(0);
}
wait;
}
}
######################################################################
# Read the tests.d file and run each test. #
######################################################################
sub do_tests
{
my @tests;
my $fname = "tests.d";
$fname = "tests/tests.d" if ! -f $fname;
my $fh = new FileHandle($fname);
die "Cannot open $fname -- $!" if !$fh;
my $name = '';
my $note = '';
while (<$fh>) {
chomp;
if (/^name:\s+(.*)$/) {
$name = $1;
next;
}
if (/^note:\s+(.*$)/) {
if ($1) {
$note = $1;
next;
}
$note = '';
while (<$fh>) {
chomp;
last if !/^\t/;
$note .= "$_\n";
}
next;
}
if (/^d:/) {
my $d = '';
while (<$fh>) {
chomp;
last if !/^\t/;
$d .= "$_\n";
}
my %info;
$info{name} = $name;
$info{note} = $note;
$info{d} = $d;
push @tests, \%info;
next;
}
}
###############################################
# Fork children to keep us busy. We want #
# all cpus busy. #
###############################################
my $ncpu = `grep 'processor\t:' /proc/cpuinfo | wc -l`;
chomp($ncpu);
my $ppid = $$;
my %pids;
for (my $i = 0; $i < $ncpu; $i++) {
my $pid = fork();
if ($pid == 0) {
do_child($ppid);
exit(0);
}
$pids{$pid} = 1;
}
if ($opts{child}) {
print "No dtrace testing - but waiting for children\n";
pause();
exit(0);
}
print "Tests:\n";
foreach my $info (@tests) {
my $dm = get_dmesg();
if ($dm ne $master_dmesg) {
my $fh = new FileHandle(">/tmp/dmesg.orig");
print $fh $master_dmesg;
$fh = new FileHandle(">/tmp/dmesg.now");
print $fh $dm;
$fh->close();
print "Warning: dmesg output changed...aborting tests so you can verify\n";
print "diff /tmp/dmesg.orig /tmp/dmesg.now\n";
system("diff /tmp/dmesg.orig /tmp/dmesg.now");
exit(1);
}
if (@ARGV) {
my $found = 0;
foreach my $arg (@ARGV) {
$found = 1 if $arg eq $info->{name};
}
next if !$found;
}
print time_string() . "Test: ", $info->{name}, "\n";
my $d = $info->{d};
my $loop = $opts{loop};
$d =~ s/\${loop}/$loop/g;
if ($d eq '') {
print "Something wrong with this test. Came out as blank!\n";
exit(1);
}
my $cmd = $opts{dtrace} ? "dtrace -n '$d'" : "build/dtrace -n '$d'";
my $ret = spawn($cmd, $info->{name});
$exit_code ||= $ret;
dump_stats();
}
kill SIGKILL, $_ foreach keys(%pids);
while (wait > 0) {
}
}
sub dump_stats
{
my $fh = new FileHandle("/proc/dtrace/stats");
while (<$fh>) {
chomp;
my ($name, $val) = split(/=/);
$val = commify($val);
print "$name=$val\n";
}
}
######################################################################
# Read kernel messages so we can detect something happening. #
######################################################################
sub get_dmesg
{
my $fh = new FileHandle("dmesg |");
my $str = '';
my $str1;
while (1) {
my $n = sysread($fh, $str1, 64 * 1024);
last if !$n;
$str .= $str1;
}
return $str;
}
######################################################################
# Main entry point. #
######################################################################
sub main
{
Getopt::Long::Configure('require_order');
Getopt::Long::Configure('no_ignore_case');
usage() unless GetOptions(\%opts,
'child',
'count=s',
'dtrace',
'help',
'loop=s',
);
usage() if $opts{help};
$| = 1;
$SIG{INT} = sub { $ctrl_c = 1; exit(0); };
if ($opts{child}) {
do_child();
exit(0);
}
my $print_msg = ! -f ".test.prompt";
new FileHandle(">.test.prompt");
if ($print_msg) {
print <<EOF;
You are about to run a series of tests which attempt to do reasonable
coverage of dtrace in core areas. This deliberately involves forcing
page faults and GPFs in the kernel, in a recoverable and safe way.
Each test is logged to the /tmp/ directory with a file with the same
name as the test. You mostly dont need to worry about the output of a test,
except if your kernel crashes.
Progress messages and occasional output is presented, so that you can
feel secure knowing your system is still running.
These tests may crash your kernel - and better to know this up front before
you assume dtrace/linux is production worthy.
These tests will become less "noisy" and will be extended with additional
use cases. Dont worry about errors like:
dtrace: error on enabled probe ID 2 (ID 274009: syscall:x64:open:entry): invalid address (0xffff880013769ed8) in action #7
in the output for now. These will be tidied up.
Press <Enter> if you understand the above and would like to continue:
EOF
my $ans = <STDIN>;
}
if (! -f "/proc/dtrace/stats") {
print "dtrace driver does not appear to be loaded.\n";
exit(1);
}
###############################################
# Grab a copy of dmesg. If anything #
# changes during testing, abort the tests. #
###############################################
$master_dmesg = get_dmesg();
for (my $i = 0; $i < $opts{count}; $i++) {
do_tests();
}
print time_string() . "All tests completed.\n";
print <<EOF if $print_msg;
You can look at /proc/dtrace/stats to see the number of interrupts
and probes executed. You really want "probe_recursion" to be a small
or zero number. At present, it may be non-zero due to timer interrupts
interrupting another probe in progress.
For the 1/2 counters - "1" means we got the interrupt but it wasnt
for us; the "2" counter means we took a dtrace handled interrupt.
int3: breakpoint trap. (Used by fbt provider)
gpf: general protection fault. (Shouldnt see these normally).
pf: page faults. will see these for badaddr probes, e.g. copyinstr(arg)
snp: segment not present. (Shouldnt see these normally).
EOF
# dump_stats();
exit($exit_code);
}
######################################################################
# Execute command but try and avoid flooding terminal with output #
# from dtrace tests. #
######################################################################
sub spawn
{ my $cmd = shift;
my $name = shift;
my $user = getpwuid(getuid());
my $fname = "/tmp/test-$user.$name.log";
unlink($fname);
if (-f $fname) {
print "Couldnt remove $fname - maybe a permission issue.\n";
exit(1);
}
$cmd .= " >$fname 2>&1";
print $cmd, "\n";
if (fork() == 0) {
exec $cmd;
}
print time_string() . "=== Test: $name\n";
while (1) {
my $kid = waitpid(-1, WNOHANG);
if ($kid > 0) {
system("tail $fname");
return $?;
}
sleep(1);
my $fh = new FileHandle("/proc/dtrace/stats");
my $probes = <$fh>;
chomp($probes);
my ($pname, $val) = split(/=/, $probes);
$fh = new FileHandle("/proc/loadavg");
my $lav = <$fh>;
chomp($lav);
printf time_string() . " $pname=%15s Load: $lav\n",
commify($val);
}
}
sub time_string
{
return strftime("%Y%m%d %H:%M:%S ", localtime);
}
#######################################################################
# Print out command line usage. #
#######################################################################
sub usage
{
print <<EOF;
tests.pl - run a series of regression tests.
Usage: tests.pl [<test-name>]
Tool to run the tests in a script file (tests/tests.d) which are
small D scripts, typically used during development, to validate that
various probe features work properly on a running kernel.
If these scripts panic your kernel, please report which test is
failing.
When run with no arguments, the list of test names is listed.
Testing will be aborted if this script detects a problem, e.g.
kernel messages as a result of probing.
Switches:
-child Just run the child test - dont run dtrace at same time.
-loop NN Loop NN times instead of max $opts{loop} times.
-count NN Repeat all (or specified) tests NN times (default 1).
Examples:
\$ tests.pl
Run all tests
\$ tests.pl -count 20 BEGIN-fbt:exec-time
Run specified test 20 times.
EOF
exit(1);
}
main();
0;