mirror of
https://github.com/tbsdtv/linux_media.git
synced 2025-07-23 04:33:26 +02:00
bpf: Compute program stats for sleepable programs
Since sleepable programs don't migrate from the cpu the excution stats can be computed for them as well. Reuse the same infrastructure for both sleepable and non-sleepable programs. run_cnt -> the number of times the program was executed. run_time_ns -> the program execution time in nanoseconds including the off-cpu time when the program was sleeping. Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: KP Singh <kpsingh@kernel.org> Link: https://lore.kernel.org/bpf/20210210033634.62081-4-alexei.starovoitov@gmail.com
This commit is contained in:
committed by
Daniel Borkmann
parent
031d6e02dd
commit
f2dd3b3946
@@ -381,56 +381,70 @@ out:
|
||||
mutex_unlock(&trampoline_mutex);
|
||||
}
|
||||
|
||||
#define NO_START_TIME 0
|
||||
static u64 notrace bpf_prog_start_time(void)
|
||||
{
|
||||
u64 start = NO_START_TIME;
|
||||
|
||||
if (static_branch_unlikely(&bpf_stats_enabled_key))
|
||||
start = sched_clock();
|
||||
return start;
|
||||
}
|
||||
|
||||
/* The logic is similar to BPF_PROG_RUN, but with an explicit
|
||||
* rcu_read_lock() and migrate_disable() which are required
|
||||
* for the trampoline. The macro is split into
|
||||
* call _bpf_prog_enter
|
||||
* call __bpf_prog_enter
|
||||
* call prog->bpf_func
|
||||
* call __bpf_prog_exit
|
||||
*/
|
||||
u64 notrace __bpf_prog_enter(void)
|
||||
__acquires(RCU)
|
||||
{
|
||||
u64 start = 0;
|
||||
|
||||
rcu_read_lock();
|
||||
migrate_disable();
|
||||
if (static_branch_unlikely(&bpf_stats_enabled_key))
|
||||
start = sched_clock();
|
||||
return start;
|
||||
return bpf_prog_start_time();
|
||||
}
|
||||
|
||||
void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start)
|
||||
__releases(RCU)
|
||||
static void notrace update_prog_stats(struct bpf_prog *prog,
|
||||
u64 start)
|
||||
{
|
||||
struct bpf_prog_stats *stats;
|
||||
|
||||
if (static_branch_unlikely(&bpf_stats_enabled_key) &&
|
||||
/* static_key could be enabled in __bpf_prog_enter
|
||||
* and disabled in __bpf_prog_exit.
|
||||
/* static_key could be enabled in __bpf_prog_enter*
|
||||
* and disabled in __bpf_prog_exit*.
|
||||
* And vice versa.
|
||||
* Hence check that 'start' is not zero.
|
||||
* Hence check that 'start' is valid.
|
||||
*/
|
||||
start) {
|
||||
start > NO_START_TIME) {
|
||||
stats = this_cpu_ptr(prog->stats);
|
||||
u64_stats_update_begin(&stats->syncp);
|
||||
stats->cnt++;
|
||||
stats->nsecs += sched_clock() - start;
|
||||
u64_stats_update_end(&stats->syncp);
|
||||
}
|
||||
}
|
||||
|
||||
void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start)
|
||||
__releases(RCU)
|
||||
{
|
||||
update_prog_stats(prog, start);
|
||||
migrate_enable();
|
||||
rcu_read_unlock();
|
||||
}
|
||||
|
||||
void notrace __bpf_prog_enter_sleepable(void)
|
||||
u64 notrace __bpf_prog_enter_sleepable(void)
|
||||
{
|
||||
rcu_read_lock_trace();
|
||||
migrate_disable();
|
||||
might_fault();
|
||||
return bpf_prog_start_time();
|
||||
}
|
||||
|
||||
void notrace __bpf_prog_exit_sleepable(void)
|
||||
void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start)
|
||||
{
|
||||
update_prog_stats(prog, start);
|
||||
migrate_enable();
|
||||
rcu_read_unlock_trace();
|
||||
}
|
||||
|
Reference in New Issue
Block a user