-
Notifications
You must be signed in to change notification settings - Fork 355
/
accessed-files
executable file
·160 lines (127 loc) · 3.29 KB
/
accessed-files
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
#!/usr/bin/env perl
# Copyright (C) Yichun Zhang (agentzh)
use 5.006001;
use strict;
use warnings;
use Getopt::Long qw( GetOptions );
GetOptions("a=s", \(my $stap_args),
"d", \(my $dump_src),
"h", \(my $help),
"l=i", \(my $limit),
"p=i", \(my $pid),
"r", \(my $check_reads),
"w", \(my $check_writes),
"t=i", \(my $time))
or die usage();
if ($help) {
print usage();
exit;
}
if (!$pid) {
die "No user process pid specified by the -p option\n";
}
if (!$limit) {
$limit = 10;
}
if (!defined $stap_args) {
$stap_args = '';
}
if (!defined $check_reads && !defined $check_writes) {
die "Neither -r nor -w options are specified.\n";
}
my ($probes, $operation);
if (defined $check_reads) {
if (defined $check_writes) {
$probes = "vfs.read.return, vfs.write.return";
$operation = "reads/writes";
} else {
$probes = "vfs.read.return";
$operation = "reads";
}
} else {
$probes = "vfs.write.return";
$operation = "writes";
}
if ($^O ne 'linux') {
die "Only linux is supported but I am on $^O.\n";
}
my $exec_file = "/proc/$pid/exe";
if (!-f $exec_file) {
die "Nginx process $pid is not running or ",
"you do not have enough permissions.\n";
}
my $exec_path = readlink $exec_file;
my $postamble = <<_EOC_;
probe end {
if (!found) {
println("No file $operation observed so far.")
} else {
printf("\\n=== Top $limit file $operation ===\\n")
i = 0
foreach (path in stats- limit $limit) {
printf("#%d: %d times, %d bytes $operation in file %s.\\n",
++i, \@count(stats[path]), \@sum(stats[path]), path)
}
}
}
_EOC_
my $guide;
if (defined $time) {
$guide = "Please wait for $time seconds.";
$postamble .= <<_EOC_;
probe timer.s($time) {
exit()
}
_EOC_
} else {
$guide = "Hit Ctrl-C to end.";
}
my $preamble = <<_EOC_;
global stats
global found
probe begin
{
printf("Tracing %d ($exec_path)...\\n$guide\\n", target())
}
_EOC_
chop $preamble;
my $stap_src = <<_EOC_;
$preamble
probe $probes {
if (pid() == target() && \$return > 0) {
path = __file_filename(file)
//println(path)
stats[path] <<< \$return
found++;
}
}
$postamble
_EOC_
if ($dump_src) {
print $stap_src;
exit;
}
open my $in, "|stap --skip-badvars $stap_args -x $pid -"
or die "Cannot run stap: $!\n";
print $in $stap_src;
close $in;
sub usage {
return <<'_EOC_';
Usage:
accessed-files [optoins]
Options:
-a <args> Pass extra arguments to the stap utility.
-d Dump out the systemtap script source.
-h Print this usage.
-l <count> Limiting the number of different file names printed.
(Default to 10.)
-p <pid> Specify the user worker process pid.
-r Trace file reads.
-w Trace file writes.
-t <seconds> Specify the time period in seconds for sampling
Examples:
accessed-files -p 12345 -t 5 -r
accessed-files -p 12345 -w -l 20
accessed-files -p 12345 -w -r -a '-DMAXACTION=100000'
_EOC_
}