All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gleb Natapov <gleb@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>, Ingo Molnar <mingo@elte.hu>
Subject: [PATCH RFC] perf, core: disable pmu while context rotation only if needed
Date: Tue, 15 Nov 2011 13:34:14 +0200	[thread overview]
Message-ID: <20111115113414.GD3225@redhat.com> (raw)

Currently pmu is disabled and re-enabled on each timer interrupt even
when no rotation or frequency adjustment is needed. On Intel CPU this
results in two writes into PERF_GLOBAL_CTRL MSR per tick. On bare metal
it does not cause significant slowdown, but when running perf in a virtual
machine it leads to 20% slowdown on my machine.

Signed-off-by: Gleb Natapov <gleb@redhat.com>
diff --git a/kernel/events/core.c b/kernel/events/core.c
index bdcd413..83c87d8 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -2317,7 +2317,7 @@ static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
 	}
 }
 
-static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
+static bool perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period, bool pmu_disabled)
 {
 	struct perf_event *event;
 	struct hw_perf_event *hwc;
@@ -2347,6 +2347,11 @@ static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
 		if (!event->attr.freq || !event->attr.sample_freq)
 			continue;
 
+		if (!pmu_disabled) {
+			perf_pmu_disable(ctx->pmu);
+			pmu_disabled = true;
+		}
+
 		event->pmu->read(event);
 		now = local64_read(&event->count);
 		delta = now - hwc->freq_count_stamp;
@@ -2355,6 +2360,8 @@ static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
 		if (delta > 0)
 			perf_adjust_period(event, period, delta);
 	}
+
+	return pmu_disabled;
 }
 
 /*
@@ -2380,6 +2387,7 @@ static void perf_rotate_context(struct perf_cpu_context *cpuctx)
 	u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
 	struct perf_event_context *ctx = NULL;
 	int rotate = 0, remove = 1;
+	bool pmu_disabled = false;
 
 	if (cpuctx->ctx.nr_events) {
 		remove = 0;
@@ -2395,10 +2403,9 @@ static void perf_rotate_context(struct perf_cpu_context *cpuctx)
 	}
 
 	perf_ctx_lock(cpuctx, cpuctx->task_ctx);
-	perf_pmu_disable(cpuctx->ctx.pmu);
-	perf_ctx_adjust_freq(&cpuctx->ctx, interval);
+	pmu_disabled = perf_ctx_adjust_freq(&cpuctx->ctx, interval, pmu_disabled);
 	if (ctx)
-		perf_ctx_adjust_freq(ctx, interval);
+		pmu_disabled = perf_ctx_adjust_freq(ctx, interval, pmu_disabled);
 
 	if (!rotate)
 		goto done;
@@ -2417,7 +2424,8 @@ done:
 	if (remove)
 		list_del_init(&cpuctx->rotation_list);
 
-	perf_pmu_enable(cpuctx->ctx.pmu);
+	if (pmu_disabled)
+		perf_pmu_enable(cpuctx->ctx.pmu);
 	perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
 }
 
--
			Gleb.

             reply	other threads:[~2011-11-15 11:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-15 11:34 Gleb Natapov [this message]
2011-11-15 12:07 ` [PATCH RFC] perf, core: disable pmu while context rotation only if needed Peter Zijlstra
2011-11-15 12:38   ` Gleb Natapov
2011-11-15 12:55     ` Peter Zijlstra
2011-11-22 10:56       ` Gleb Natapov
2011-11-22 10:59         ` Peter Zijlstra

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=20111115113414.GD3225@redhat.com \
    --to=gleb@redhat.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.