mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jiri Olsa <jolsa@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: Jiri Olsa <jolsa@redhat.com>,
	Arnaldo Carvalho de Melo <acme@redhat.com>,
	Corey Ashford <cjashfor@linux.vnet.ibm.com>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	Ingo Molnar <mingo@elte.hu>, Paul Mackerras <paulus@samba.org>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>
Subject: [PATCH 12/21] perf: Support event inheritance for toggle feature
Date: Wed, 25 Sep 2013 14:50:38 +0200	[thread overview]
Message-ID: <1380113447-17144-13-git-send-email-jolsa@redhat.com> (raw)
In-Reply-To: <1380113447-17144-1-git-send-email-jolsa@redhat.com>

The toggling sets relationship between events - toggle event
carries pointer and reference for toggled event. This needs
to be configured for child events as well.

During the fork events are processed/cloned with no regards
to the toggle setup, so we have no idea what we get first:
toggle or toggled events.

To avoid extra after-fork scanning for toggle events, we
use child pre-allocation whenever the toggling setup is
detected. This way we can pre-set the toggle dependencies
during the event cloning.

Described in following example.

  Consider following setup:
    - 'event A' toggles 'event B'
    - 'event A' holds pointer/ref to 'event B'

  Now we have fork:
  (and have no idea which event gets inherited/cloned first)

  1) the clone order is 'event A' 'event B'
     - 'event A' is processed:
       - 'event_cloned A' is created
       - 'event_cloned A' needs pointer to 'event_cloned B'
          which does not exist yet
       - we pre-allocate 'event_cloned B' and setup 'event_cloned A's
         toggled_event pointer/ref and also save it in 'event B' as
         toggled_child

     - 'event B' is processed
       - we check if toggled_child is allocated
       - we use it as 'event_cloned B'

  2) the order is 'event B' 'event A'
     - 'event B' is processed
       - toggled_child is not allocated, we allocate 'event_cloned B'
         and store it in 'event B' as toggled_child

     - 'event A' is processed
       - create 'event_cloned A'
       - 'event A' is toggling event and have pointer to 'event B'
       - we check if there's already initialized toggled_child in 'event B'
       - initialize 'event_cloned A' toggled_event pointer with
         'event_cloned B' taken toggled_child

Signed-off-by: Jiri Olsa <jolsa@redhat.com>
Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Corey Ashford <cjashfor@linux.vnet.ibm.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 include/linux/perf_event.h |   2 +
 kernel/events/core.c       | 103 +++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 96 insertions(+), 9 deletions(-)

diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index 801ff22..baefb79 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -424,6 +424,8 @@ struct perf_event {
 	enum perf_event_toggle_flag	toggle_flag;
 	int				paused;
 	atomic_t			toggled_cnt;
+	struct perf_event		*toggled_child;
+	int				toggled_child_cnt;
 #endif /* CONFIG_PERF_EVENTS */
 };
 
diff --git a/kernel/events/core.c b/kernel/events/core.c
index fa1d229..edf161b 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -3156,6 +3156,8 @@ static void free_event_rcu(struct rcu_head *head)
 	if (event->ns)
 		put_pid_ns(event->ns);
 	perf_event_free_filter(event);
+	if (event->toggled_child)
+		kfree(event->toggled_child);
 	kfree(event);
 }
 
@@ -6749,6 +6751,9 @@ static int perf_init_event(struct perf_event *event,
 	event->overflow_handler	= overflow_handler;
 	event->overflow_handler_context = context;
 
+	if (parent_event && atomic_read(&parent_event->toggled_cnt))
+		event->toggled_cnt = parent_event->toggled_cnt;
+
 	perf_event__state_init(event);
 
 	pmu = NULL;
@@ -6795,6 +6800,11 @@ err_ns:
 	return err;
 }
 
+static struct perf_event *__perf_event_alloc(void)
+{
+	return kzalloc(sizeof(struct perf_event), GFP_KERNEL);
+}
+
 /*
  * Allocate and initialize a event structure
  */
@@ -6809,7 +6819,7 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
 	struct perf_event *event;
 	int err;
 
-	event = kzalloc(sizeof(*event), GFP_KERNEL);
+	event = __perf_event_alloc();
 	if (!event)
 		return ERR_PTR(-ENOMEM);
 
@@ -7048,6 +7058,10 @@ perf_event_set_toggle(struct perf_event *event,
 	if (toggled_event->ctx->task != ctx->task)
 		return -EINVAL;
 
+	/* Temporary hack for toggled_child_cnt */
+	if (toggled_event->attr.inherit !=  event->attr.inherit)
+		return -EINVAL;
+
 	event->overflow_handler = perf_event_toggle_overflow;
 	event->toggle_flag      = get_toggle_flag(flags);
 	event->toggled_event    = toggled_event;
@@ -7686,6 +7700,66 @@ void perf_event_delayed_put(struct task_struct *task)
 		WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
 }
 
+static void
+perf_event_toggled_set_child(struct perf_event *event,
+			     struct perf_event *child)
+{
+	event->toggled_child     = child;
+	event->toggled_child_cnt = atomic_read(&event->toggled_cnt) + 1;
+}
+
+static void perf_event_toggled_child_put(struct perf_event *parent)
+{
+	int cnt = --parent->toggled_child_cnt;
+
+	WARN_ON_ONCE(cnt < 0);
+	WARN_ON_ONCE(!parent->toggled_child);
+
+	if (!cnt)
+		parent->toggled_child = NULL;
+}
+
+static int
+perf_event_inherit_toggle(struct perf_event *event,
+			  struct perf_event *parent)
+{
+	struct perf_event *toggled = parent->toggled_event;
+	struct perf_event *toggled_child = parent->toggled_child;
+
+
+	/*
+	 * This @event is toggled by the childs of the its parent's togglers.
+	 * If this child is inherited before its togglers, declare it so.
+	 */
+	if (atomic_read(&event->toggled_cnt)) {
+		if (!parent->toggled_child)
+			perf_event_toggled_set_child(parent, event);
+		perf_event_toggled_child_put(parent);
+	}
+
+	/*
+	 * This @event toggles the child of the event toggled by its @parent.
+	 * If it's inherited before its toggled event, pre-allocate the toggled
+	 * In any case, declare and attach the toggled to the @event.
+	 */
+	if (toggled) {
+		toggled_child = toggled->toggled_child;
+		if (!toggled_child) {
+			toggled_child = __perf_event_alloc();
+			if (!toggled_child)
+				return -ENOMEM;
+			perf_event_toggled_set_child(toggled, toggled_child);
+		}
+
+		/* set inherited toggling */
+		event->toggled_event = toggled_child;
+		event->toggle_flag   = parent->toggle_flag;
+		perf_event_toggled_child_put(toggled);
+	}
+
+	return 0;
+}
+
 /*
  * inherit a event from parent task to child task:
  */
@@ -7697,8 +7771,16 @@ inherit_event(struct perf_event *parent_event,
 	      struct perf_event *group_leader,
 	      struct perf_event_context *child_ctx)
 {
-	struct perf_event *child_event;
+	struct perf_event *child_event, *orig_parent_event = parent_event;
 	unsigned long flags;
+	int err;
+
+	child_event = parent_event->toggled_child;
+	if (!child_event) {
+		child_event = __perf_event_alloc();
+		if (!child_event)
+			return ERR_PTR(-ENOMEM);
+	}
 
 	/*
 	 * Instead of creating recursive hierarchies of events,
@@ -7709,13 +7791,16 @@ inherit_event(struct perf_event *parent_event,
 	if (parent_event->parent)
 		parent_event = parent_event->parent;
 
-	child_event = perf_event_alloc(&parent_event->attr,
-					   parent_event->cpu,
-					   child,
-					   group_leader, parent_event,
-				           NULL, NULL);
-	if (IS_ERR(child_event))
-		return child_event;
+	err = perf_init_event(child_event, &parent_event->attr,
+			      parent_event->cpu, child,
+			      group_leader, parent_event, NULL, NULL);
+	if (err)
+		return ERR_PTR(err);
+
+	if (perf_event_inherit_toggle(child_event, orig_parent_event)) {
+		free_event(child_event);
+		return NULL;
+	}
 
 	if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
 		free_event(child_event);
-- 
1.7.11.7


  parent reply	other threads:[~2013-09-25 12:51 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-25 12:50 [RFC 00/21] perf tools: Add toggling events support Jiri Olsa
2013-09-25 12:50 ` [PATCH 01/21] perf tools: Introduce perf_evlist__wait_workload function Jiri Olsa
2013-09-25 12:50 ` [PATCH 02/21] perf tools: Separate sys_perf_event_open call into evsel_open Jiri Olsa
2013-09-25 12:50 ` [PATCH 03/21] perf x86: Update event count properly for read syscall Jiri Olsa
2013-09-25 12:50 ` [PATCH 04/21] perf: Move event state initialization before/behind the pmu add/del calls Jiri Olsa
2013-09-25 12:50 ` [PATCH 05/21] perf: Add event toggle sys_perf_event_open interface Jiri Olsa
2013-09-25 22:36   ` Sukadev Bhattiprolu
2013-09-26 12:27     ` Jiri Olsa
2013-09-25 12:50 ` [PATCH 06/21] perf: Add event toggle ioctl interface Jiri Olsa
2013-09-25 19:46   ` Vince Weaver
2013-09-26 12:30     ` Jiri Olsa
2013-09-26 13:03       ` Vince Weaver
2013-09-25 12:50 ` [PATCH 07/21] perf: Toggle whole group in toggle event overflow Jiri Olsa
2013-09-25 12:50 ` [PATCH 08/21] perf: Add new 'paused' attribute Jiri Olsa
2013-09-25 22:41   ` Sukadev Bhattiprolu
2013-09-26 12:24     ` Jiri Olsa
2013-09-25 12:50 ` [PATCH 09/21] perf: Account toggle masters for toggled event Jiri Olsa
2013-09-25 12:50 ` [PATCH 10/21] perf: Be more specific on pmu related event init naming Jiri Olsa
2013-09-25 12:50 ` [PATCH 11/21] perf: Split allocation and initialization code Jiri Olsa
2013-09-25 12:50 ` Jiri Olsa [this message]
2013-09-25 12:50 ` [PATCH 13/21] perf tests: Adding event simple toggling test Jiri Olsa
2013-09-25 12:50 ` [PATCH 14/21] perf tests: Adding event group " Jiri Olsa
2013-09-25 12:50 ` [PATCH 15/21] perf tests: Adding event inherit " Jiri Olsa
2013-09-25 12:50 ` [PATCH 16/21] perf tools: Allow numeric event to change name via name term Jiri Olsa
2013-09-25 12:50 ` [PATCH 17/21] perf tools: Add event_config_optional parsing rule Jiri Olsa
2013-09-25 12:50 ` [PATCH 18/21] perf tools: Rename term related parsing function/variable properly Jiri Olsa
2013-09-25 12:50 ` [PATCH 19/21] perf tools: Carry term string value for symbols events Jiri Olsa
2013-09-25 12:50 ` [PATCH 20/21] perf tools: Add support to parse event on/off toggle terms Jiri Olsa
2013-09-25 12:50 ` [PATCH 21/21] perf tools: Add record/stat support for toggling events Jiri Olsa
2013-09-25 19:12 ` [RFC 00/21] perf tools: Add toggling events support Andi Kleen
2013-09-26  7:03   ` Ingo Molnar
2013-09-26 15:45     ` Andi Kleen
2013-09-26 12:11   ` Jiri Olsa
2013-09-26 11:31 ` Stephane Eranian
2013-09-26 12:20   ` Jiri Olsa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1380113447-17144-13-git-send-email-jolsa@redhat.com \
    --to=jolsa@redhat.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@redhat.com \
    --cc=cjashfor@linux.vnet.ibm.com \
    --cc=fweisbec@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=paulus@samba.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®