perf: Remove orphaned children events
Subsystems (2)
Jiri Olsa
reviewercover perf: Remove orphaned children events
hi,
adding a support to remove orphaned children events, so they do not eat resources once parent event is gone as discussed here:
lkml.kernel.org/r/[email protected]
and here:
http://marc.info/?l=linux-kernel&m=140568679618148&w=2
Also reachable in here:
git://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
perf/core_orphans_removal
thanks,
jirka
--- include/linux/perf_event.h | 4 ++ kernel/events/core.c | 116 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 113 insertions(+), 7 deletions(-)
Jiri Olsa
reviewerPATCH 1/3 perf: Do not allow to create kernel events without handler
Force kernel events to specify the handler, because there's no use for kernel perf event without it.
--- kernel/events/core.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/events/core.c +3 -0
@@ -7355,6 +7355,9 @@ perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,struct perf_event *event;int err; +if (WARN_ON (!overflow_handler)) +return NULL ; +/* * Get the target context (task or percpu): */
Signature · 1 line
1.8.3.1
Peter Zijlstra
maintainerRe: [PATCH 1/3] perf: Do not allow to create kernel events without handler
Quoted 3 lines · Jiri Olsa
Force kernel events to specify the handler, because there's no use for kernel perf event without it.
I think I found a reason; although there is currently no such user, the simple counting events, they don't have overflow handlers at all.
I think I did on once, but never merged that code because it was a quick dev hack to create nice changelog numbers etc..
/me goes dig... found it:
--- include/linux/perf_event.h | 1 kernel/events/core.c | 22 +++++++- kernel/sched/clock.c | 118 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 3 deletions(-)
include/linux/perf_event.h +1 -0
@@ -561,6 +561,7 @@ extern void perf_pmu_migrate_context(str int src_cpu, int dst_cpu);extern u64 perf_event_read_value (struct perf_event *event,u64 *enabled,u64 *running); +extern u64 perf_event_read (struct perf_event *event);struct perf_sample_data {
kernel/events/core.c +19 -3
@@ -2973,15 +2973,31 @@ static inline u64 perf_event_count(strucreturn local64_read (&event->count) +atomic64_read (&event->child_count); } -static u64 perf_event_read (struct perf_event *event) +u64 perf_event_read (struct perf_event *event) {/* * If event is enabled and currently active on a CPU, update the * value in the event structure: */ if (event->state ==PERF_EVENT_STATE_ACTIVE ) { -smp_call_function_single (event->oncpu, - __perf_event_read, event,1 ); +/* +* If the event is for the current task, its guaranteed that we +* never need the cross cpu call, and therefore can allow this +* to be called with IRQs disabled. +* +* Avoids the warning otherwise generated by +* smp_call_function_single(). +*/ +if (event->ctx->task == current) { +unsigned long flags; + +local_irq_save (flags); +__perf_event_read (event); +local_irq_restore (flags); + }else { +smp_call_function_single (event->oncpu, + __perf_event_read, event,1 ); + } }else if (event->state ==PERF_EVENT_STATE_INACTIVE ) {struct perf_event_context *ctx = event->ctx;unsigned long flags;
kernel/sched/clock.c +118 -0
121 lines not on this page · raw message
Re: [PATCH 1/3] perf: Do not allow to create kernel events without handler
Quoted 7 lines · Peter Zijlstra
Quoted 3 lines · Jiri Olsa
Force kernel events to specify the handler, because there's no use for kernel perf event without it.
I think I found a reason; although there is currently no such user, the simple counting events, they don't have overflow handlers at all.
ah right.. ok
jirka
Jiri Olsa
reviewerPATCH 2/3 perf: Set owner pointer for kernel events
In reply to Jiri OlsaAdding fake EVENT_OWNER_KERNEL owner pointer value for kernel perf events, so we could distinguish it from user events, which needs special care in following patch.
--- kernel/events/core.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-)
kernel/events/core.c +26 -6
@@ -119,6 +119,13 @@ static int cpu_function_call(int cpu, int (*func) (void *info), void *info)return data.ret; } +#define EVENT_OWNER_KERNEL ((void *) -1) + +static bool is_kernel_event (struct perf_event *event) +{ +return event->owner ==EVENT_OWNER_KERNEL ; +} +#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ PERF_FLAG_FD_OUTPUT |\ PERF_FLAG_PID_CGROUP |\
@@ -3312,16 +3319,12 @@ static void free_event(struct perf_event *event) }/* -* Called when the last reference to the file is gone. +* Remove user event from the owner task. */ -static void put_event (struct perf_event *event) +static void perf_remove_from_owner (struct perf_event *event) { -struct perf_event_context *ctx = event->ctx;struct task_struct *owner; -if (!atomic_long_dec_and_test (&event->refcount)) -return ; -rcu_read_lock (); owner =ACCESS_ONCE (event->owner);/*
@@ -3354,6 +3357,20 @@ static void put_event(struct perf_event *event)mutex_unlock (&owner->perf_event_mutex);put_task_struct (owner); } +} + +/* +* Called when the last reference to the file is gone. +*/ +static void put_event (struct perf_event *event) +{ +struct perf_event_context *ctx = event->ctx; + +if (!atomic_long_dec_and_test (&event->refcount)) +return ; + +if (!is_kernel_event (event)) +perf_remove_from_owner (event);WARN_ON_ONCE (ctx->parent_ctx);/*
@@ -7369,6 +7386,9 @@ perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, goto err; } +/* Mark owner so we could distinguish it from user events. */ + event->owner =EVENT_OWNER_KERNEL ; +account_event (event); ctx =find_get_context (event->pmu, task, cpu);
Signature · 1 line
1.8.3.1
tip-bot for Jiri Olsa
merge bot[tip:perf/core] perf: Set owner pointer for kernel events
perf: Set owner pointer for kernel events
Commit-ID: f86977620ee4635f26befcf436700493a38ce002 Gitweb: http://git.kernel.org/tip/f86977620ee4635f26befcf436700493a38ce002 Author: Jiri Olsa <[email protected]> AuthorDate: Fri, 1 Aug 2014 14:33:01 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Wed, 13 Aug 2014 07:51:03 +0200
perf: Set owner pointer for kernel events
Adding fake EVENT_OWNER_KERNEL owner pointer value for kernel perf events, so we could distinguish it from user events, which needs special care in following patch.
--- kernel/events/core.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-)
kernel/events/core.c +26 -6
@@ -119,6 +119,13 @@ static int cpu_function_call(int cpu, int (*func) (void *info), void *info)return data.ret; } +#define EVENT_OWNER_KERNEL ((void *) -1) + +static bool is_kernel_event (struct perf_event *event) +{ +return event->owner ==EVENT_OWNER_KERNEL ; +} +#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ PERF_FLAG_FD_OUTPUT |\ PERF_FLAG_PID_CGROUP |\
@@ -3312,16 +3319,12 @@ static void free_event(struct perf_event *event) }/* -* Called when the last reference to the file is gone. +* Remove user event from the owner task. */ -static void put_event (struct perf_event *event) +static void perf_remove_from_owner (struct perf_event *event) { -struct perf_event_context *ctx = event->ctx;struct task_struct *owner; -if (!atomic_long_dec_and_test (&event->refcount)) -return ; -rcu_read_lock (); owner =ACCESS_ONCE (event->owner);/*
@@ -3354,6 +3357,20 @@ static void put_event(struct perf_event *event)mutex_unlock (&owner->perf_event_mutex);put_task_struct (owner); } +} + +/* +* Called when the last reference to the file is gone. +*/ +static void put_event (struct perf_event *event) +{ +struct perf_event_context *ctx = event->ctx; + +if (!atomic_long_dec_and_test (&event->refcount)) +return ; + +if (!is_kernel_event (event)) +perf_remove_from_owner (event);WARN_ON_ONCE (ctx->parent_ctx);/*
@@ -7366,6 +7383,9 @@ perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, goto err; } +/* Mark owner so we could distinguish it from user events. */ + event->owner =EVENT_OWNER_KERNEL ; +account_event (event); ctx =find_get_context (event->pmu, task, cpu);
Jiri Olsa
reviewerPATCH 3/3 perf: Add queued work to remove orphaned child events
In reply to Jiri OlsaIn cases when the owner task exits before the workload and the workload made some forks, all the events stay in until the last workload process exits. Thats' because each child event holds parent reference.
We want to release all children events once the parent is gone, because at that time there's no process to read them anyway, so they're just eating resources.
This removal races with process exit, which removes all events and fork, which clone events. To be clear of those two, adding work queue to remove orphaned child for context in case such event is detected.
Using delayed work queue (with delay == 1), because we queue this work under perf scheduler callbacks. Normal work queue tries to wake up the queue process, which deadlocks on rq->lock in this place.
Also preventing clones from abandoned parent event.
--- include/linux/perf_event.h | 4 +++ kernel/events/core.c | 87 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 1 deletion(-)
include/linux/perf_event.h +4 -0
@@ -52,6 +52,7 @@ struct perf_guest_info_callbacks {#include <linux/atomic.h> #include <linux/sysfs.h> #include <linux/perf_regs.h> +#include <linux/workqueue.h> #include <asm/local.h> struct perf_callchain_entry {
@@ -507,6 +508,9 @@ struct perf_event_context {int nr_cgroups;/* cgroup evts */ int nr_branch_stack;/* branch_stack evt */ struct rcu_head rcu_head; + +struct delayed_work orphans_remove; +bool orphans_remove_sched; };/*
kernel/events/core.c +86 -1
129 lines not on this page · raw message
Signature · 1 line
1.8.3.1
tip-bot for Jiri Olsa
merge bot[tip:perf/core] perf: Add queued work to remove orphaned child events
perf: Add queued work to remove orphaned child events
Commit-ID: fadfe7be6e50de7f03913833b33c56cd8fb66bac Gitweb: http://git.kernel.org/tip/fadfe7be6e50de7f03913833b33c56cd8fb66bac Author: Jiri Olsa <[email protected]> AuthorDate: Fri, 1 Aug 2014 14:33:02 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Wed, 13 Aug 2014 07:51:04 +0200
perf: Add queued work to remove orphaned child events
In cases when the owner task exits before the workload and the workload made some forks, all the events stay in until the last workload process exits. Thats' because each child event holds parent reference.
We want to release all children events once the parent is gone, because at that time there's no process to read them anyway, so they're just eating resources.
This removal races with process exit, which removes all events and fork, which clone events. To be clear of those two, adding work queue to remove orphaned child for context in case such event is detected.
Using delayed work queue (with delay == 1), because we queue this work under perf scheduler callbacks. Normal work queue tries to wake up the queue process, which deadlocks on rq->lock in this place.
Also preventing clones from abandoned parent event.
--- include/linux/perf_event.h | 4 +++ kernel/events/core.c | 87 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 1 deletion(-)
include/linux/perf_event.h +4 -0
@@ -52,6 +52,7 @@ struct perf_guest_info_callbacks {#include <linux/atomic.h> #include <linux/sysfs.h> #include <linux/perf_regs.h> +#include <linux/workqueue.h> #include <asm/local.h> struct perf_callchain_entry {
@@ -507,6 +508,9 @@ struct perf_event_context {int nr_cgroups;/* cgroup evts */ int nr_branch_stack;/* branch_stack evt */ struct rcu_head rcu_head; + +struct delayed_work orphans_remove; +bool orphans_remove_sched; };/*
kernel/events/core.c +86 -1
129 lines not on this page · raw message