Skip to content

Commit 9557c00

Browse files
Sbermacmel
authored andcommitted
perf record --off-cpu: Add --off-cpu-thresh option
Specify the threshold for dumping offcpu samples with --off-cpu-thresh, the unit is milliseconds. Default value is 500ms. Example: perf record --off-cpu --off-cpu-thresh 824 The example above collects direct off-cpu samples where the off-cpu time is longer than 824ms. Committer testing: After commenting out the end off-cpu dump to have just the ones that are added right after the task is scheduled back, and using a threshould of 1000ms, we see some periods (the 5th column, just before "offcpu-time" in the 'perf script' output) that are over 1000.000.000 nanoseconds: root@number:~# perf record --off-cpu --off-cpu-thresh 10000 ^C[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 3.902 MB perf.data (34335 samples) ] root@number:~# perf script <SNIP> Isolated Web Co 59932 [028] 63839.594437: 1000049427 offcpu-time: 7fe63c7976c2 __syscall_cancel_arch_end+0x0 (/usr/lib64/libc.so.6) 7fe63c78c04c __futex_abstimed_wait_common+0x7c (/usr/lib64/libc.so.6) 7fe63c78e928 pthread_cond_timedwait@@GLIBC_2.3.2+0x178 (/usr/lib64/libc.so.6) 5599974a9fe7 mozilla::detail::ConditionVariableImpl::wait_for(mozilla::detail::MutexImpl&, mozilla::BaseTimeDuration<mozilla::TimeDurationValueCalculator> const&)+0xe7 (/usr/lib64/fir> 100000000 [unknown] ([unknown]) swapper 0 [025] 63839.594459: 195724 cycles:P: ffffffffac328270 read_tsc+0x0 ([kernel.kallsyms]) Isolated Web Co 59932 [010] 63839.594466: 1000055278 offcpu-time: 7fe63c7976c2 __syscall_cancel_arch_end+0x0 (/usr/lib64/libc.so.6) 7fe63c78ba24 __syscall_cancel+0x14 (/usr/lib64/libc.so.6) 7fe63c804c4e __poll+0x1e (/usr/lib64/libc.so.6) 7fe633b0d1b8 PollWrapper(_GPollFD*, unsigned int, int) [clone .lto_priv.0]+0xf8 (/usr/lib64/firefox/libxul.so) 10000002c [unknown] ([unknown]) swapper 0 [027] 63839.594475: 134433 cycles:P: ffffffffad4c45d9 irqentry_enter+0x19 ([kernel.kallsyms]) swapper 0 [028] 63839.594499: 215838 cycles:P: ffffffffac39199a switch_mm_irqs_off+0x10a ([kernel.kallsyms]) MediaPD~oder #1 1407676 [027] 63839.594514: 134433 cycles:P: 7f982ef5e69f dct_IV(int*, int, int*)+0x24f (/usr/lib64/libfdk-aac.so.2.0.0) swapper 0 [024] 63839.594524: 267411 cycles:P: ffffffffad4c6ee6 poll_idle+0x56 ([kernel.kallsyms]) MediaSu~sor torvalds#75 1093827 [026] 63839.594555: 332652 cycles:P: 55be753ad030 moz_xmalloc+0x200 (/usr/lib64/firefox/firefox) swapper 0 [027] 63839.594616: 160548 cycles:P: ffffffffad144840 menu_select+0x570 ([kernel.kallsyms]) Isolated Web Co 14019 [027] 63839.595120: 1000050178 offcpu-time: 7fc9537cc6c2 __syscall_cancel_arch_end+0x0 (/usr/lib64/libc.so.6) 7fc9537c104c __futex_abstimed_wait_common+0x7c (/usr/lib64/libc.so.6) 7fc9537c3928 pthread_cond_timedwait@@GLIBC_2.3.2+0x178 (/usr/lib64/libc.so.6) 7fc95372a3c8 pt_TimedWait+0xb8 (/usr/lib64/libnspr4.so) 7fc95372a8d8 PR_WaitCondVar+0x68 (/usr/lib64/libnspr4.so) 7fc94afb1f7c WatchdogMain(void*)+0xac (/usr/lib64/firefox/libxul.so) 7fc947498660 [unknown] ([unknown]) 7fc9535fce88 [unknown] ([unknown]) 7fc94b620e60 WatchdogManager::~WatchdogManager()+0x0 (/usr/lib64/firefox/libxul.so) fff8548387f8b48 [unknown] ([unknown]) swapper 0 [003] 63839.595712: 212948 cycles:P: ffffffffacd5b865 acpi_os_read_port+0x55 ([kernel.kallsyms]) <SNIP> Suggested-by: Arnaldo Carvalho de Melo <[email protected]> Suggested-by: Ian Rogers <[email protected]> Suggested-by: Namhyung Kim <[email protected]> Reviewed-by: Ian Rogers <[email protected]> Signed-off-by: Howard Chu <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Tested-by: Gautam Menghani <[email protected]> Tested-by: Ian Rogers <[email protected]> Acked-by: Namhyung Kim <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: James Clark <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kan Liang <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/r/[email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 74069a0 commit 9557c00

File tree

6 files changed

+41
-1
lines changed

6 files changed

+41
-1
lines changed

tools/perf/Documentation/perf-record.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,15 @@ filtered through the mask provided by -C option.
842842
only, as of now. So the applications built without the frame
843843
pointer might see bogus addresses.
844844

845+
off-cpu profiling consists two types of samples: direct samples, which
846+
share the same behavior as regular samples, and the accumulated
847+
samples, stored in BPF stack trace map, presented after all the regular
848+
samples.
849+
850+
--off-cpu-thresh::
851+
Once a task's off-cpu time reaches this threshold (in milliseconds), it
852+
generates a direct off-cpu sample. The default is 500ms.
853+
845854
--setup-filter=<action>::
846855
Prepare BPF filter to be used by regular users. The action should be
847856
either "pin" or "unpin". The filter can be used after it's pinned.

tools/perf/builtin-record.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3162,6 +3162,28 @@ static int record__parse_mmap_pages(const struct option *opt,
31623162
return ret;
31633163
}
31643164

3165+
static int record__parse_off_cpu_thresh(const struct option *opt,
3166+
const char *str,
3167+
int unset __maybe_unused)
3168+
{
3169+
struct record_opts *opts = opt->value;
3170+
char *endptr;
3171+
u64 off_cpu_thresh_ms;
3172+
3173+
if (!str)
3174+
return -EINVAL;
3175+
3176+
off_cpu_thresh_ms = strtoull(str, &endptr, 10);
3177+
3178+
/* the threshold isn't string "0", yet strtoull() returns 0, parsing failed */
3179+
if (*endptr || (off_cpu_thresh_ms == 0 && strcmp(str, "0")))
3180+
return -EINVAL;
3181+
else
3182+
opts->off_cpu_thresh_ns = off_cpu_thresh_ms * NSEC_PER_MSEC;
3183+
3184+
return 0;
3185+
}
3186+
31653187
void __weak arch__add_leaf_frame_record_opts(struct record_opts *opts __maybe_unused)
31663188
{
31673189
}
@@ -3355,6 +3377,7 @@ static struct record record = {
33553377
.ctl_fd = -1,
33563378
.ctl_fd_ack = -1,
33573379
.synth = PERF_SYNTH_ALL,
3380+
.off_cpu_thresh_ns = OFFCPU_THRESH,
33583381
},
33593382
};
33603383

@@ -3582,6 +3605,9 @@ static struct option __record_options[] = {
35823605
OPT_BOOLEAN(0, "off-cpu", &record.off_cpu, "Enable off-cpu analysis"),
35833606
OPT_STRING(0, "setup-filter", &record.filter_action, "pin|unpin",
35843607
"BPF filter action"),
3608+
OPT_CALLBACK(0, "off-cpu-thresh", &record.opts, "ms",
3609+
"Dump off-cpu samples if off-cpu time exceeds this threshold (in milliseconds). (Default: 500ms)",
3610+
record__parse_off_cpu_thresh),
35853611
OPT_END()
35863612
};
35873613

tools/perf/util/bpf_off_cpu.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "util/strlist.h"
1515
#include <bpf/bpf.h>
1616
#include <internal/xyarray.h>
17+
#include <linux/time64.h>
1718

1819
#include "bpf_skel/off_cpu.skel.h"
1920

@@ -292,6 +293,8 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target,
292293
}
293294
}
294295

296+
skel->bss->offcpu_thresh_ns = opts->off_cpu_thresh_ns;
297+
295298
err = off_cpu_bpf__attach(skel);
296299
if (err) {
297300
pr_err("Failed to attach off-cpu BPF skeleton\n");

tools/perf/util/bpf_skel/off_cpu.bpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const volatile bool uses_cgroup_v1 = false;
124124

125125
int perf_subsys_id = -1;
126126

127-
__u64 offcpu_thresh_ns = 500000000ull;
127+
__u64 offcpu_thresh_ns;
128128

129129
/*
130130
* Old kernel used to call it task_struct->state and now it's '__state'.

tools/perf/util/off_cpu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct record_opts;
1616
PERF_SAMPLE_PERIOD | PERF_SAMPLE_RAW | \
1717
PERF_SAMPLE_CGROUP)
1818

19+
#define OFFCPU_THRESH 500000000ULL
1920

2021
#ifdef HAVE_BPF_SKEL
2122
int off_cpu_prepare(struct evlist *evlist, struct target *target,

tools/perf/util/record.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ struct record_opts {
8080
int synth;
8181
int threads_spec;
8282
const char *threads_user_spec;
83+
u64 off_cpu_thresh_ns;
8384
};
8485

8586
extern const char * const *record_usage;

0 commit comments

Comments
 (0)