fluent-plugin-perf-tools 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rubocop.yml +26 -0
  4. data/.ruby-version +1 -0
  5. data/CHANGELOG.md +5 -0
  6. data/CODE_OF_CONDUCT.md +84 -0
  7. data/Gemfile +5 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +43 -0
  10. data/Rakefile +17 -0
  11. data/bin/console +15 -0
  12. data/bin/setup +8 -0
  13. data/fluent-plugin-perf-tools.gemspec +48 -0
  14. data/lib/fluent/plugin/in_perf_tools.rb +42 -0
  15. data/lib/fluent/plugin/perf_tools/cachestat.rb +65 -0
  16. data/lib/fluent/plugin/perf_tools/command.rb +30 -0
  17. data/lib/fluent/plugin/perf_tools/version.rb +9 -0
  18. data/lib/fluent/plugin/perf_tools.rb +11 -0
  19. data/perf-tools/LICENSE +339 -0
  20. data/perf-tools/README.md +205 -0
  21. data/perf-tools/bin/bitesize +1 -0
  22. data/perf-tools/bin/cachestat +1 -0
  23. data/perf-tools/bin/execsnoop +1 -0
  24. data/perf-tools/bin/funccount +1 -0
  25. data/perf-tools/bin/funcgraph +1 -0
  26. data/perf-tools/bin/funcslower +1 -0
  27. data/perf-tools/bin/functrace +1 -0
  28. data/perf-tools/bin/iolatency +1 -0
  29. data/perf-tools/bin/iosnoop +1 -0
  30. data/perf-tools/bin/killsnoop +1 -0
  31. data/perf-tools/bin/kprobe +1 -0
  32. data/perf-tools/bin/opensnoop +1 -0
  33. data/perf-tools/bin/perf-stat-hist +1 -0
  34. data/perf-tools/bin/reset-ftrace +1 -0
  35. data/perf-tools/bin/syscount +1 -0
  36. data/perf-tools/bin/tcpretrans +1 -0
  37. data/perf-tools/bin/tpoint +1 -0
  38. data/perf-tools/bin/uprobe +1 -0
  39. data/perf-tools/deprecated/README.md +1 -0
  40. data/perf-tools/deprecated/execsnoop-proc +150 -0
  41. data/perf-tools/deprecated/execsnoop-proc.8 +80 -0
  42. data/perf-tools/deprecated/execsnoop-proc_example.txt +46 -0
  43. data/perf-tools/disk/bitesize +175 -0
  44. data/perf-tools/examples/bitesize_example.txt +63 -0
  45. data/perf-tools/examples/cachestat_example.txt +58 -0
  46. data/perf-tools/examples/execsnoop_example.txt +153 -0
  47. data/perf-tools/examples/funccount_example.txt +126 -0
  48. data/perf-tools/examples/funcgraph_example.txt +2178 -0
  49. data/perf-tools/examples/funcslower_example.txt +110 -0
  50. data/perf-tools/examples/functrace_example.txt +341 -0
  51. data/perf-tools/examples/iolatency_example.txt +350 -0
  52. data/perf-tools/examples/iosnoop_example.txt +302 -0
  53. data/perf-tools/examples/killsnoop_example.txt +62 -0
  54. data/perf-tools/examples/kprobe_example.txt +379 -0
  55. data/perf-tools/examples/opensnoop_example.txt +47 -0
  56. data/perf-tools/examples/perf-stat-hist_example.txt +149 -0
  57. data/perf-tools/examples/reset-ftrace_example.txt +88 -0
  58. data/perf-tools/examples/syscount_example.txt +297 -0
  59. data/perf-tools/examples/tcpretrans_example.txt +93 -0
  60. data/perf-tools/examples/tpoint_example.txt +210 -0
  61. data/perf-tools/examples/uprobe_example.txt +321 -0
  62. data/perf-tools/execsnoop +292 -0
  63. data/perf-tools/fs/cachestat +167 -0
  64. data/perf-tools/images/perf-tools_2016.png +0 -0
  65. data/perf-tools/iolatency +296 -0
  66. data/perf-tools/iosnoop +296 -0
  67. data/perf-tools/kernel/funccount +146 -0
  68. data/perf-tools/kernel/funcgraph +259 -0
  69. data/perf-tools/kernel/funcslower +248 -0
  70. data/perf-tools/kernel/functrace +192 -0
  71. data/perf-tools/kernel/kprobe +270 -0
  72. data/perf-tools/killsnoop +263 -0
  73. data/perf-tools/man/man8/bitesize.8 +70 -0
  74. data/perf-tools/man/man8/cachestat.8 +111 -0
  75. data/perf-tools/man/man8/execsnoop.8 +104 -0
  76. data/perf-tools/man/man8/funccount.8 +76 -0
  77. data/perf-tools/man/man8/funcgraph.8 +166 -0
  78. data/perf-tools/man/man8/funcslower.8 +129 -0
  79. data/perf-tools/man/man8/functrace.8 +123 -0
  80. data/perf-tools/man/man8/iolatency.8 +116 -0
  81. data/perf-tools/man/man8/iosnoop.8 +169 -0
  82. data/perf-tools/man/man8/killsnoop.8 +100 -0
  83. data/perf-tools/man/man8/kprobe.8 +162 -0
  84. data/perf-tools/man/man8/opensnoop.8 +113 -0
  85. data/perf-tools/man/man8/perf-stat-hist.8 +111 -0
  86. data/perf-tools/man/man8/reset-ftrace.8 +49 -0
  87. data/perf-tools/man/man8/syscount.8 +96 -0
  88. data/perf-tools/man/man8/tcpretrans.8 +93 -0
  89. data/perf-tools/man/man8/tpoint.8 +140 -0
  90. data/perf-tools/man/man8/uprobe.8 +168 -0
  91. data/perf-tools/misc/perf-stat-hist +223 -0
  92. data/perf-tools/net/tcpretrans +311 -0
  93. data/perf-tools/opensnoop +280 -0
  94. data/perf-tools/syscount +192 -0
  95. data/perf-tools/system/tpoint +232 -0
  96. data/perf-tools/tools/reset-ftrace +123 -0
  97. data/perf-tools/user/uprobe +390 -0
  98. metadata +349 -0
@@ -0,0 +1,192 @@
1
+ #!/bin/bash
2
+ #
3
+ # syscount - count system calls.
4
+ # Written using Linux perf_events (aka "perf").
5
+ #
6
+ # This is a proof-of-concept using perf_events capabilities for older kernel
7
+ # versions, that lack custom in-kernel aggregations. Once they exist, this
8
+ # script can be substantially rewritten and improved (lower overhead).
9
+ #
10
+ # USAGE: syscount [-chv] [-t top] {-p PID|-d seconds|command}
11
+ #
12
+ # Run "syscount -h" for full usage.
13
+ #
14
+ # REQUIREMENTS: Linux perf_events: add linux-tools-common, run "perf", then
15
+ # add any additional packages it requests. Also needs awk.
16
+ #
17
+ # OVERHEADS: Modes that report syscall names only (-c, -cp PID, -cd secs) have
18
+ # lower overhead, since they use in-kernel counts. Other modes which report
19
+ # process IDs (-cv) or process names (default) create a perf.data file for
20
+ # post processing, and you will see messages about it doing this. Beware of
21
+ # the file size (test for short durations, or use -c to see counts based on
22
+ # in-kernel counters), and gauge overheads based on the perf.data size.
23
+ #
24
+ # Note that this script delibrately does not pipe perf record into
25
+ # perf script, which would avoid perf.data, because it can create a feedback
26
+ # loop where the perf script syscalls are recorded. Hopefully there will be a
27
+ # fix for this in a later perf version, so perf.data can be skipped, or other
28
+ # kernel features to aggregate by process name in-kernel directly (eg, via
29
+ # eBPF, ktap, or SystemTap).
30
+ #
31
+ # From perf-tools: https://github.com/brendangregg/perf-tools
32
+ #
33
+ # See the syscount(8) man page (in perf-tools) for more info.
34
+ #
35
+ # COPYRIGHT: Copyright (c) 2014 Brendan Gregg.
36
+ #
37
+ # This program is free software; you can redistribute it and/or
38
+ # modify it under the terms of the GNU General Public License
39
+ # as published by the Free Software Foundation; either version 2
40
+ # of the License, or (at your option) any later version.
41
+ #
42
+ # This program is distributed in the hope that it will be useful,
43
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
44
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45
+ # GNU General Public License for more details.
46
+ #
47
+ # You should have received a copy of the GNU General Public License
48
+ # along with this program; if not, write to the Free Software Foundation,
49
+ # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
50
+ #
51
+ # (http://www.gnu.org/copyleft/gpl.html)
52
+ #
53
+ # 07-Jul-2014 Brendan Gregg Created this.
54
+
55
+ # default variables
56
+ opt_count=0; opt_pid=0; opt_verbose=0; opt_cmd=0; opt_duration=0; opt_tail=0
57
+ tnum=; pid=; duration=; cmd=; cpus=-a; opts=; tcmd=cat; ttext=
58
+ trap '' INT QUIT TERM PIPE HUP
59
+
60
+ stdout_workaround=1 # needed for older perf versions
61
+ write_workaround=1 # needed for perf versions that trace their own writes
62
+
63
+ ### parse options
64
+ while getopts cd:hp:t:v opt
65
+ do
66
+ case $opt in
67
+ c) opt_count=1 ;;
68
+ d) opt_duration=1; duration=$OPTARG ;;
69
+ p) opt_pid=1; pid=$OPTARG ;;
70
+ t) opt_tail=1; tnum=$OPTARG ;;
71
+ v) opt_verbose=1 ;;
72
+ h|?) cat <<-END >&2
73
+ USAGE: syscount [-chv] [-t top] {-p PID|-d seconds|command}
74
+ syscount # count by process name
75
+ -c # show counts by syscall name
76
+ -h # this usage message
77
+ -v # verbose: shows PID
78
+ -p PID # trace this PID only
79
+ -d seconds # duration of trace
80
+ -t num # show top number only
81
+ command # run and trace this command
82
+ eg,
83
+ syscount # syscalls by process name
84
+ syscount -c # syscalls by syscall name
85
+ syscount -d 5 # trace for 5 seconds
86
+ syscount -cp 923 # syscall names for PID 923
87
+ syscount -c ls # syscall names for "ls"
88
+
89
+ See the man page and example file for more info.
90
+ END
91
+ exit 1
92
+ esac
93
+ done
94
+ shift $(( $OPTIND - 1 ))
95
+
96
+ ### option logic
97
+ if (( $# > 0 )); then
98
+ opt_cmd=1
99
+ cmd="$@"
100
+ cpus=
101
+ fi
102
+ if (( opt_pid + opt_duration + opt_cmd > 1 )); then
103
+ echo >&2 "ERROR: Pick one of {-p PID|-n name|-d seconds|command}"
104
+ exit 1
105
+ fi
106
+ if (( opt_tail )); then
107
+ tcmd="tail -$tnum"
108
+ ttext=" Top $tnum only."
109
+ fi
110
+ if (( opt_duration )); then
111
+ cmd="sleep $duration"
112
+ echo "Tracing for $duration seconds.$ttext.."
113
+ fi
114
+ if (( opt_pid )); then
115
+ cpus=
116
+ cmd="-p $pid"
117
+ echo "Tracing PID $pid.$ttext.. Ctrl-C to end."
118
+ fi
119
+ (( opt_cmd )) && echo "Tracing while running: \"$cmd\".$ttext.."
120
+ (( opt_pid + opt_duration + opt_cmd == 0 )) && \
121
+ echo "Tracing.$ttext.. Ctrl-C to end."
122
+ (( stdout_workaround )) && opts="-o /dev/stdout"
123
+
124
+ ulimit -n 32768 # often needed
125
+
126
+ ### execute syscall name mode
127
+ if (( opt_count && ! opt_verbose )); then
128
+ : ${cmd:=sleep 999999}
129
+ out=$(perf stat $opts -e 'syscalls:sys_enter_*' $cpus $cmd)
130
+ printf "%-17s %8s\n" "SYSCALL" "COUNT"
131
+ echo "$out" | awk '
132
+ $1 && $2 ~ /syscalls:/ {
133
+ sub("syscalls:sys_enter_", ""); sub(":", "")
134
+ gsub(",", "")
135
+ printf "%-17s %8s\n", $2, $1
136
+ }' | sort -n -k2 | $tcmd
137
+ exit
138
+ fi
139
+
140
+ ### execute syscall name with pid mode
141
+ if (( opt_count && opt_verbose )); then
142
+ if (( write_workaround )); then
143
+ # this list must end in write to associate the filter
144
+ tp=$(perf list syscalls:sys_enter_* | awk '
145
+ $1 != "syscalls:sys_enter_write" && $1 ~ /syscalls:/ { printf "-e %s ", $1 }')
146
+ tp="$tp -e syscalls:sys_enter_write"
147
+ sh -c "perf record $tp --filter 'common_pid != '\$\$ $cpus $cmd"
148
+ else
149
+ perf record 'syscalls:sys_enter_*' $cpus $cmd
150
+ # could also pipe direct to perf script
151
+ fi
152
+
153
+ printf "%-6s %-16s %-17s %8s\n" "PID" "COMM" "SYSCALL" "COUNT"
154
+ perf script --fields pid,comm,event | awk '$1 != "#" {
155
+ sub("syscalls:sys_enter_", ""); sub(":", "")
156
+ a[$1 ";" $2 ";" $3]++
157
+ }
158
+ END {
159
+ for (k in a) {
160
+ split(k, b, ";");
161
+ printf "%-6s %-16s %-17s %8d\n", b[2], b[1], b[3], a[k]
162
+ }
163
+ }' | sort -n -k4 | $tcmd
164
+ exit
165
+ fi
166
+
167
+ ### execute process name mode
168
+ tp="-e raw_syscalls:sys_enter"
169
+ if (( write_workaround )); then
170
+ sh -c "perf record $tp --filter 'common_pid != '\$\$ $cpus $cmd"
171
+ else
172
+ perf record $tp $cpus $cmd
173
+ fi
174
+
175
+ if (( opt_verbose )); then
176
+ printf "%-6s %-16s %8s\n" "PID" "COMM" "COUNT"
177
+ perf script --fields pid,comm | awk '$1 != "#" { a[$1 ";" $2]++ }
178
+ END {
179
+ for (k in a) {
180
+ split(k, b, ";");
181
+ printf "%-6s %-16s %8d\n", b[2], b[1], a[k]
182
+ }
183
+ }' | sort -n -k3 | $tcmd
184
+ else
185
+ printf "%-16s %8s\n" "COMM" "COUNT"
186
+ perf script --fields comm | awk '$1 != "#" { a[$1]++ }
187
+ END {
188
+ for (k in a) {
189
+ printf "%-16s %8d\n", k, a[k]
190
+ }
191
+ }' | sort -n -k2 | $tcmd
192
+ fi
@@ -0,0 +1,232 @@
1
+ #!/bin/bash
2
+ #
3
+ # tpoint - trace a given tracepoint. Static tracing.
4
+ # Written using Linux ftrace.
5
+ #
6
+ # This will enable a given tracepoint, print events, then disable the tracepoint
7
+ # when the program ends. This is like a simple version of the "perf" command for
8
+ # printing live tracepoint events only. Wildcards are currently not supported.
9
+ # If this is insufficient for any reason, use the perf command instead.
10
+ #
11
+ # USAGE: ./tpoint [-hHsv] [-d secs] [-p pid] [-L tid] tracepoint [filter]
12
+ # ./tpoint -l
13
+ #
14
+ # Run "tpoint -h" for full usage.
15
+ #
16
+ # I wrote this because I often needed a quick way to dump stack traces for a
17
+ # given tracepoint.
18
+ #
19
+ # OVERHEADS: Relative to the frequency of traced events. You might want to
20
+ # check their frequency beforehand, using perf_events. Eg:
21
+ #
22
+ # perf stat -e block:block_rq_issue -a sleep 5
23
+ #
24
+ # To count occurrences of that tracepoint for 5 seconds.
25
+ #
26
+ # REQUIREMENTS: FTRACE and tracepoints, which you may already have on recent
27
+ # kernel versions.
28
+ #
29
+ # From perf-tools: https://github.com/brendangregg/perf-tools
30
+ #
31
+ # See the tpoint(8) man page (in perf-tools) for more info.
32
+ #
33
+ # COPYRIGHT: Copyright (c) 2014 Brendan Gregg.
34
+ #
35
+ # This program is free software; you can redistribute it and/or
36
+ # modify it under the terms of the GNU General Public License
37
+ # as published by the Free Software Foundation; either version 2
38
+ # of the License, or (at your option) any later version.
39
+ #
40
+ # This program is distributed in the hope that it will be useful,
41
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
42
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43
+ # GNU General Public License for more details.
44
+ #
45
+ # You should have received a copy of the GNU General Public License
46
+ # along with this program; if not, write to the Free Software Foundation,
47
+ # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
48
+ #
49
+ # (http://www.gnu.org/copyleft/gpl.html)
50
+ #
51
+ # 22-Jul-2014 Brendan Gregg Created this.
52
+
53
+ ### default variables
54
+ tracing=/sys/kernel/debug/tracing
55
+ flock=/var/tmp/.ftrace-lock; wroteflock=0
56
+ opt_duration=0; duration=; opt_pid=0; pid=; opt_tid=0; tid=
57
+ opt_filter=0; filter=; opt_view=0; opt_headers=0; opt_stack=0; dmesg=2
58
+ trap ':' INT QUIT TERM PIPE HUP # sends execution to end tracing section
59
+
60
+ function usage {
61
+ cat <<-END >&2
62
+ USAGE: tpoint [-hHsv] [-d secs] [-p PID] [-L TID] tracepoint [filter]
63
+ tpoint -l
64
+ -d seconds # trace duration, and use buffers
65
+ -p PID # PID to match on event
66
+ -L TID # thread id to match on event
67
+ -v # view format file (don't trace)
68
+ -H # include column headers
69
+ -l # list all tracepoints
70
+ -s # show kernel stack traces
71
+ -h # this usage message
72
+ eg,
73
+ tpoint -l | grep open
74
+ # find tracepoints containing "open"
75
+ tpoint syscalls:sys_enter_open
76
+ # trace open() syscall entry
77
+ tpoint block:block_rq_issue
78
+ # trace block I/O issue
79
+ tpoint -s block:block_rq_issue
80
+ # show kernel stacks
81
+
82
+ See the man page and example file for more info.
83
+ END
84
+ exit
85
+ }
86
+
87
+ function warn {
88
+ if ! eval "$@"; then
89
+ echo >&2 "WARNING: command failed \"$@\""
90
+ fi
91
+ }
92
+
93
+ function end {
94
+ # disable tracing
95
+ echo 2>/dev/null
96
+ echo "Ending tracing..." 2>/dev/null
97
+ cd $tracing
98
+ warn "echo 0 > $tdir/enable"
99
+ if (( opt_filter )); then
100
+ warn "echo 0 > $tdir/filter"
101
+ fi
102
+ (( opt_stack )) && warn "echo 0 > options/stacktrace"
103
+ warn "echo > trace"
104
+ (( wroteflock )) && warn "rm $flock"
105
+ }
106
+
107
+ function die {
108
+ echo >&2 "$@"
109
+ exit 1
110
+ }
111
+
112
+ function edie {
113
+ # die with a quiet end()
114
+ echo >&2 "$@"
115
+ exec >/dev/null 2>&1
116
+ end
117
+ exit 1
118
+ }
119
+
120
+ ### process options
121
+ while getopts d:hHlp:L:sv opt
122
+ do
123
+ case $opt in
124
+ d) opt_duration=1; duration=$OPTARG ;;
125
+ p) opt_pid=1; pid=$OPTARG ;;
126
+ L) opt_tid=1; tid=$OPTARG ;;
127
+ H) opt_headers=1 ;;
128
+ l) opt_list=1 ;;
129
+ s) opt_stack=1 ;;
130
+ v) opt_view=1 ;;
131
+ h|?) usage ;;
132
+ esac
133
+ done
134
+ if (( !opt_list )); then
135
+ shift $(( $OPTIND - 1 ))
136
+ (( $# )) || usage
137
+ tpoint=$1
138
+ shift
139
+ if (( $# )); then
140
+ opt_filter=1
141
+ filter=$1
142
+ fi
143
+ fi
144
+
145
+ ### option logic
146
+ (( opt_pid + opt_filter + opt_tid > 1 )) && \
147
+ die "ERROR: use at most one of -p, -L, or filter."
148
+ (( opt_duration && opt_view )) && die "ERROR: use either -d or -v."
149
+ if (( opt_pid )); then
150
+ # convert to filter
151
+ opt_filter=1
152
+ # ftrace common_pid is thread id from user's perspective
153
+ for tid in /proc/$pid/task/*; do
154
+ filter="$filter || common_pid == ${tid##*/}"
155
+ done
156
+ filter=${filter:3} # trim leading ' || ' (four characters)
157
+ fi
158
+ if (( opt_tid )); then
159
+ opt_filter=1
160
+ filter="common_pid == $tid"
161
+ fi
162
+ if (( !opt_view && !opt_list )); then
163
+ if (( opt_duration )); then
164
+ echo "Tracing $tpoint for $duration seconds (buffered)..."
165
+ else
166
+ echo "Tracing $tpoint. Ctrl-C to end."
167
+ fi
168
+ fi
169
+
170
+ ### check permissions
171
+ cd $tracing || die "ERROR: accessing tracing. Root user? Kernel has FTRACE?
172
+ debugfs mounted? (mount -t debugfs debugfs /sys/kernel/debug)"
173
+
174
+ ### do list tracepoints
175
+ if (( opt_list )); then
176
+ cd events
177
+ for tp in */*; do
178
+ # skip filter/enable files
179
+ [[ -f $tp ]] && continue
180
+ echo ${tp/\//:}
181
+ done
182
+ exit
183
+ fi
184
+
185
+ ### check tracepoints
186
+ tdir=events/${tpoint/:/\/}
187
+ [[ -e $tdir ]] || die "ERROR: tracepoint $tpoint not found. Exiting"
188
+
189
+ ### view
190
+ if (( opt_view )); then
191
+ cat $tdir/format
192
+ exit
193
+ fi
194
+
195
+ ### ftrace lock
196
+ [[ -e $flock ]] && die "ERROR: ftrace may be in use by PID $(cat $flock) $flock"
197
+ echo $$ > $flock || die "ERROR: unable to write $flock."
198
+ wroteflock=1
199
+
200
+ ### setup and begin tracing
201
+ echo nop > current_tracer
202
+ if (( opt_filter )); then
203
+ if ! echo "$filter" > $tdir/filter; then
204
+ edie "ERROR: setting filter or -p. Exiting."
205
+ fi
206
+ fi
207
+ if (( opt_stack )); then
208
+ if ! echo 1 > options/stacktrace; then
209
+ edie "ERROR: enabling stack traces (-s). Exiting"
210
+ fi
211
+ fi
212
+ if ! echo 1 >> $tdir/enable; then
213
+ edie "ERROR: enabling tracepoint $tprobe. Exiting."
214
+ fi
215
+
216
+ ### print trace buffer
217
+ warn "echo > trace"
218
+ if (( opt_duration )); then
219
+ sleep $duration
220
+ if (( opt_headers )); then
221
+ cat trace
222
+ else
223
+ grep -v '^#' trace
224
+ fi
225
+ else
226
+ # trace_pipe lack headers, so fetch them from trace
227
+ (( opt_headers )) && cat trace
228
+ cat trace_pipe
229
+ fi
230
+
231
+ ### end tracing
232
+ end
@@ -0,0 +1,123 @@
1
+ #!/bin/bash
2
+ #
3
+ # reset-ftrace - reset state of ftrace, disabling all tracing.
4
+ # Written for Linux ftrace.
5
+ #
6
+ # This may only be of use to ftrace hackers who, in the process of developing
7
+ # ftrace software, often get the subsystem into a partially active state, and
8
+ # would like a quick way to reset state. Check the end of this script for the
9
+ # actually files reset, and add more if you need.
10
+ #
11
+ # USAGE: ./reset-ftrace [-fhq]
12
+ #
13
+ # REQUIREMENTS: FTRACE CONFIG.
14
+ #
15
+ # From perf-tools: https://github.com/brendangregg/perf-tools
16
+ #
17
+ # See the reset-ftrace(8) man page (in perf-tools) for more info.
18
+ #
19
+ # COPYRIGHT: Copyright (c) 2014 Brendan Gregg.
20
+ #
21
+ # This program is free software; you can redistribute it and/or
22
+ # modify it under the terms of the GNU General Public License
23
+ # as published by the Free Software Foundation; either version 2
24
+ # of the License, or (at your option) any later version.
25
+ #
26
+ # This program is distributed in the hope that it will be useful,
27
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
28
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29
+ # GNU General Public License for more details.
30
+ #
31
+ # You should have received a copy of the GNU General Public License
32
+ # along with this program; if not, write to the Free Software Foundation,
33
+ # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34
+ #
35
+ # (http://www.gnu.org/copyleft/gpl.html)
36
+ #
37
+ # 20-Jul-2014 Brendan Gregg Created this.
38
+
39
+ tracing=/sys/kernel/debug/tracing
40
+ flock=/var/tmp/.ftrace-lock
41
+ opt_force=0; opt_quiet=0
42
+
43
+ function usage {
44
+ cat <<-END >&2
45
+ USAGE: reset-ftrace [-fhq]
46
+ -f # force: delete ftrace lock file
47
+ -q # quiet: reset, but say nothing
48
+ -h # this usage message
49
+ eg,
50
+ reset-ftrace # disable active ftrace session
51
+ END
52
+ exit
53
+ }
54
+
55
+ function warn {
56
+ if ! eval "$@"; then
57
+ echo >&2 "WARNING: command failed \"$@\""
58
+ fi
59
+ }
60
+
61
+ function die {
62
+ echo >&2 "$@"
63
+ exit 1
64
+ }
65
+
66
+ function vecho {
67
+ (( opt_quiet )) && return
68
+ echo "$@"
69
+ }
70
+
71
+ # write to file
72
+ function writefile {
73
+ file=$1
74
+ string=$2 # optional
75
+ if [[ ! -w $file ]]; then
76
+ echo >&2 "WARNING: file $file not writable/exists. Skipping."
77
+ return
78
+ fi
79
+ vecho "$file, before:"
80
+ (( ! opt_quiet )) && cat -n $file
81
+ warn "echo $string > $file"
82
+ vecho "$file, after:"
83
+ (( ! opt_quiet )) && cat -n $file
84
+ vecho
85
+ }
86
+
87
+ ### process options
88
+ while getopts fhq opt
89
+ do
90
+ case $opt in
91
+ f) opt_force=1 ;;
92
+ q) opt_quiet=1 ;;
93
+ h|?) usage ;;
94
+ esac
95
+ done
96
+ shift $(( $OPTIND - 1 ))
97
+
98
+ ### ftrace lock
99
+ if [[ -e $flock ]]; then
100
+ if (( opt_force )); then
101
+ warn rm $flock
102
+ else
103
+ echo -e >&2 "ERROR: ftrace lock ($flock) exists. It shows" \
104
+ "ftrace may be in use by PID $(cat $flock).\nDouble check" \
105
+ "to see if that PID is still active. If not, consider" \
106
+ "using -f to force a reset. Exiting."
107
+ exit 1
108
+ fi
109
+ fi
110
+
111
+ ### reset ftrace state
112
+ vecho "Reseting ftrace state..."
113
+ vecho
114
+ cd $tracing || die "ERROR: accessing tracing. Root user? Kernel has FTRACE?"
115
+ writefile current_tracer nop
116
+ writefile set_ftrace_filter
117
+ writefile set_graph_function
118
+ writefile set_ftrace_pid
119
+ writefile events/enable 0
120
+ writefile tracing_thresh 0
121
+ writefile kprobe_events
122
+ writefile tracing_on 1
123
+ vecho "Done."