-
Notifications
You must be signed in to change notification settings - Fork 0
/
measure_header_bloat
executable file
·372 lines (321 loc) · 9.36 KB
/
measure_header_bloat
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
#!/usr/bin/perl
# This is an attempt at writing a wrapper for running locstat on a
# given set of kernel revisions and "running the numbers" on that,
# i.e. produce a set of statistics.
use strict;
use warnings;
use BSD::Resource;
use Time::HiRes;
use File::Basename;
use YAML::XS qw(LoadFile);
use IPC::Run3;
use Cwd;
use Statistics::Descriptive;
use lib dirname($0);
use Pplight;
use Getopt::Long;
my $params;
my $workdir = getcwd();
my %data;
my $param_file = "params.yaml";
my $output_dir = ".";
my $locstat_path = "$workdir/locstat";
my $make = "make";
GetOptions("p|param=s" => \$param_file,
"o|output-dir=s" => \$output_dir,
"locstat=s" => \$locstat_path)
or die;
read_params($param_file);
# params: (global var)
#
# Global settings:
#
# target: vmlinux (or lib/, or kernel/, or whatever)
# parallel: 4
# repeat: 1 # for timing
#
# Defaults for "simple" revisions:
#
# kerneldir: ...
# kconfig: ...
# ...
# revisions:
# - v3.4
# - v3.5
# - v3.6
# ...
# - v4.0
#
# The revisions element must be an array. Each element should be a
# hash with keys "name", "rev", "kconfig", "kerneldir".
#
# - "rev" is required and should be something which we can "git
# checkout".
#
# - "name" will be used as a pretty version of rev, and can be omitted
# in which case rev is used.
#
# - "out" is used as the output basename (with .yaml appended). This
# also defaults to rev.
#
# - "kerneldir" is the path to the repository to use.
#
# - "kconfig" is a string which either names a "make *config",
# e.g. "defconfig", or a path (relative to $cwd) to a file (this is
# distinguished by whether the string contains a slash, so you may
# want to say ./file). In the former case, we copy the file to
# .config in the kerneldir, and in the latter, we simply run "make
# $kconfig". In either case, this is followed by "make
# silentoldconfig".
#
# If either kconfig or kerneldir are omitted, the global defaults are
# used. As a convenience, an element of the revision array can also be
# a simple string, which is then equivalent to the hash { rev: $str }.
#
my @locstat_columns =
(
'name', 'hcount', # name of .c, #included headers
'csize', 'tsize', 'rsize', # size of .c, total size, ratio
'cloc', 'tloc', 'rloc', # #lines in .c, #total lines, ratio
'crloc', 'trloc', 'rrloc' # "reduced LOC", #lines sans comments and #if 0 blocks
);
my @stat_cols = @locstat_columns[1..$#locstat_columns];
my @stats =
(
['mean', sub {$_[0]->mean()}],
['min', sub {$_[0]->min()}],
['q25', sub {$_[0]->quantile(1)}],
['median', sub {$_[0]->quantile(2)}],
['q75', sub {$_[0]->quantile(3)}],
['max', sub {$_[0]->max()}],
);
my @extra_cols = ('wtime', 'utime', 'stime', 'ntu');
my @out_cols = ('name');
push @out_cols, @extra_cols;
for my $c (@stat_cols) {
for my $s (@stats) {
push @out_cols, "${c}_" . $s->[0];
}
}
print join("\t", @out_cols), "\n";
for (@{$params->{revisions}}) {
my $rev = prepare_revision($_);
next unless $rev;
my $data = do_revision($rev);
chdir $workdir or
die "failed to chdir back to $workdir";
next unless ref($data) eq 'HASH';
if ($output_dir) {
my $output_file = "${output_dir}/locstat_" . $rev->{out} . ".txt";
open(my $fh, '>', $output_file)
or die "unable to open $output_file: $!";
print $fh $data->{locstat};
close($fh);
}
for my $d (@stat_cols) {
my $vals = Statistics::Descriptive::Full->new();
$vals->add_data(@{$data->{$d}});
$vals->sort_data();
for my $s (@stats) {
my $val = $s->[1]->($vals);
$data->{"${d}_" . $s->[0]} = $val;
}
}
print join("\t", map {$data->{$_}} @out_cols), "\n";
}
sub read_params {
my $file = shift;
$params = LoadFile($file);
$params->{kerneldir} //= ".";
$params->{kconfig} //= "defconfig";
$params->{target} //= "vmlinux";
$params->{revisions} //= [ qw(v4.4) ];
$params->{repeat} ||= 1;
$params->{parallel} ||= 4;
for my $w (qw(ARCH CROSS_COMPILE CC)) {
if (defined $params->{$w}) {
$make .= " $w=$params->{$w}";
}
}
}
sub get_times {
my $href = shift;
my $ru;
$ru = getrusage(RUSAGE_CHILDREN);
$href->{utime} = $ru->utime;
$href->{stime} = $ru->stime;
$href->{wtime} = Time::HiRes::time();
}
# run_command: wrapper for run3
#
# Returns a hashref wrapping up the stdout, stderr, and various
# metadata.
sub run_command {
my $args = shift;
my ($stdout, $stderr);
my (%start, %stop);
printf STDERR "Doing [%s]\n", ref $args eq "ARRAY" ? join(" ", @$args) : $args;
$! = 0;
get_times(\%start);
my $ret = run3($args, \undef, \$stdout, \$stderr, {return_if_system_error => 1});
my $exitval = $?;
my $errno = $!;
get_times(\%stop);
die unless $ret;
return {
cmd => ref $args eq "ARRAY" ? join(" ", @$args) : $args,
exitval => $exitval,
errno => $errno,
stdout => $stdout,
stderr => $stderr,
wtime => $stop{wtime}-$start{wtime}, # wallclock time
utime => $stop{utime}-$start{utime}, # user time
stime => $stop{stime}-$start{stime}, # system time
};
}
# If the command succeeded, do nothing, just return 0.
#
# If the command failed to execute, print $! to STDERR and return -1.
#
# If the command did execute, but exited with something other than 1,
# print its stderr to STDERR and return -1.
sub simple_handle_ret {
my $ret = shift;
return 0 if ($ret->{exitval} == 0);
if ($ret->{exitval} == -1) {
printf STDERR "Command '%s' failed: %s\n", $ret->{cmd}, $ret->{errno};
return -1;
}
if ($ret->{exitval} & 127) {
printf STDERR "Command '%s' killed by signal %d\n", $ret->{cmd}, $ret->{exitval} & 127;
return -1;
}
printf STDERR "Command '%s' exited with status %d; stderr:\n", $ret->{cmd}, $ret->{exitval} >> 8;
printf STDERR " %s", $_ for (split /^/, $ret->{stderr});
return -1;
}
sub do_configure {
my $kconfig = shift;
my $ret;
if ($kconfig =~ m#^/#) {
$ret = run_command("cp '$kconfig' .config");
} elsif ($kconfig =~ m#/#) {
$ret = run_command("cp '$workdir/$kconfig' .config");
} else {
$ret = run_command("$make $kconfig");
}
$ret = simple_handle_ret($ret);
return $ret if $ret;
$ret = run_command("$make silentoldconfig");
return simple_handle_ret($ret);
}
sub do_clean {
my $ret = run_command("$make clean");
return simple_handle_ret($ret);
}
sub do_distclean {
my $ret = run_command("$make distclean");
return simple_handle_ret($ret);
}
sub do_checkout {
my $rev = shift;
$rev = $rev->{rev};
my $ret = run_command("git checkout $rev");
return simple_handle_ret($ret);
}
sub do_fill_page_cache {
my $ret = run_command("git grep -q foobar > /dev/null 2> /dev/null");
return 0;
}
# We want to extract a little more information from do_compile and
# do_locstat then just success/fail, so they return the full ret
# object.
sub do_compile {
my $rev = shift;
my $cmd = sprintf("$make -j%d %s", $rev->{parallel}, $rev->{target});
my $ret = run_command($cmd);
return $ret;
}
sub do_locstat {
# XXX: Use xargs if GNU parallel is not installed?
my $cmd = "find . -name '*.o.cmd'" .
" | grep -v '^./scripts/'" .
" | grep -v '^./tools/'" .
" | sort" .
" | parallel -k -m ${locstat_path}";
return run_command($cmd);
}
sub prepare_revision {
my $rev = shift;
if (ref($rev) eq '') {
$rev = { rev => $rev};
}
if (ref($rev) ne 'HASH') {
warn "Configuration error; revision is not a hash nor a string";
return undef;
}
if (not exists $rev->{rev}) {
warn "Configuration error; revision with no rev member";
return undef;
}
$rev->{name} //= $rev->{rev};
$rev->{out} //= $rev->{rev};
$rev->{kerneldir} //= $params->{kerneldir};
$rev->{kconfig} //= $params->{kconfig};
$rev->{target} //= $params->{target};
$rev->{parallel} //= $params->{parallel};
$rev->{repeat} //= $params->{repeat};
if (!$rev->{kerneldir}) {
warn "Revision $rev->{rev} has no kerneldir";
return undef;
}
return $rev;
}
sub do_revision {
# At any sign of trouble, just give up.
my $rev = shift;
my $ret;
my $d;
# Everything from now on is done inside the kerneldir. Our caller
# will chdir back the original work dir, to simplify error paths.
if (!chdir($rev->{kerneldir})) {
warn "Cannot chdir to $rev->{kerneldir}: $!";
return undef;
}
# We do a distclean before checking out the new revision, since
# the various Makefile clean rules may be different.
if (do_distclean() || do_checkout($rev)) {
return undef;
}
if (do_configure($rev->{kconfig})) {
return undef;
}
do_fill_page_cache();
my ($wtime, $utime, $stime) = (0,0,0);
for (my $i = 0; $i < $rev->{repeat}; ++$i) {
return undef if do_clean();
$ret = do_compile($rev);
return undef if (simple_handle_ret($ret));
$wtime += $ret->{wtime};
$utime += $ret->{utime};
$stime += $ret->{stime};
}
$wtime /= $rev->{repeat};
$utime /= $rev->{repeat};
$stime /= $rev->{repeat};
$ret = do_locstat();
return undef if (simple_handle_ret($ret));
$d = {};
$d->{locstat} = $ret->{stdout};
for my $line (split /\n/, $ret->{stdout}) {
my %vals;
@vals{@locstat_columns} = split /\t/, $line;
push @{$d->{$_}}, $vals{$_} for @locstat_columns;
$d->{ntu}++;
}
$d->{name} = $rev->{name};
$d->{wtime} = $wtime;
$d->{utime} = $utime;
$d->{stime} = $stime;
return $d;
}