public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Robert Richter <robert.richter@amd.com>
Cc: Ingo Molnar <mingo@elte.hu>,
	Stephane Eranian <eranian@google.com>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 1/2] perf, x86: Implement event scheduler helper functions
Date: Wed, 14 Sep 2011 16:43:28 +0200	[thread overview]
Message-ID: <1316011408.5040.20.camel@twins> (raw)
In-Reply-To: <1315666143-7106-2-git-send-email-robert.richter@amd.com>

On Sat, 2011-09-10 at 16:49 +0200, Robert Richter wrote:
> This patch introduces x86 perf scheduler code helper functions. We
> need this to later add more complex functionality to support
> overlapping counter constraints (next patch).
> 
> The algorithm is modified so that the range of weight values is now
> generated from the constraints. There shouldn't be other functional
> changes.
> 
> With the helper functions the scheduler is controlled. There are
> functions to initialize, traverse the event list, find unused counters
> etc. The scheduler keeps its own state.
> 
> Cc: Stephane Eranian <eranian@google.com>
> Signed-off-by: Robert Richter <robert.richter@amd.com>
> ---
>  arch/x86/kernel/cpu/perf_event.c |  158 +++++++++++++++++++++++++-------------
>  1 files changed, 105 insertions(+), 53 deletions(-)
> 
> diff --git a/arch/x86/kernel/cpu/perf_event.c b/arch/x86/kernel/cpu/perf_event.c
> index 594d425..44ec767 100644
> --- a/arch/x86/kernel/cpu/perf_event.c
> +++ b/arch/x86/kernel/cpu/perf_event.c
> @@ -790,18 +790,118 @@ static inline int is_x86_event(struct perf_event *event)
>  	return event->pmu == &pmu;
>  }
>  
> +struct sched_state {
> +	int	weight;
> +	int	event;
> +	int	counter;
> +	int	unassigned;
> +	unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
> +};

Maybe add a few comments here? Took me a while to figure out unassigned
is the number of unassigned events.

> +static struct sched_state *perf_sched_find_counter(struct perf_sched *sched)
> +{
> +	struct event_constraint *c;
> +	int idx;
> +
> +	if (!sched->state.unassigned)
> +		return NULL;

So bail when we're done and there's nothing left to assign.

> +	c = sched->constraints[sched->state.event];
> +
> +	idx = sched->state.counter;

Which is typically 0, but this could be a restart, at which point we
continue looking where we left off.

> +	/* for each bit in idxmsk starting from idx */
> +	while (idx < X86_PMC_IDX_MAX) {
> +		idx = find_next_bit(c->idxmsk, X86_PMC_IDX_MAX, idx);
> +		if (idx == X86_PMC_IDX_MAX)
> +			break;
> +		if (!__test_and_set_bit(idx, sched->state.used))
> +			break;
> +		idx++;
> +	}

#define for_each_set_bit_continue(bit, addr, size) 	\
	for( ; (bit) < (size);				\
	       (bit) = find_next_bit((addr), (size), (bit) + 1))

	for_each_set_bit_continue(idx, c->idxmask, X86_PMC_IDX_MAX) {
		if (!__test_and_set_bit(idx, sched->state.used))
			break;
	}

> +	sched->state.counter = idx;
> +
> +	if (idx >= X86_PMC_IDX_MAX)
> +		return NULL;

OK, so its important to assign idx to counter even if we're too big,
because of the restart, right? That wants a comment.

> +
> +	return &sched->state;
> +}
> +
> +static int perf_sched_next_event(struct perf_sched *sched)
> +{
> +	struct event_constraint *c;
> +
> +	if (!sched->state.unassigned || !--sched->state.unassigned)
> +		return 0;

Shouldn't we avoid getting here if there's nothing to do? I get
the !--unassigned case, but am a bit puzzled by the !unassigned case.

> +	do {
> +		/* next event */
> +		sched->state.event++;
> +		if (sched->state.event >= sched->max_events) {
> +			/* next weight */
> +			sched->state.event = 0;
> +			sched->state.weight++;
> +			if (sched->state.weight > sched->max_weight)
> +				return 0;
> +		}
> +		c = sched->constraints[sched->state.event];
> +	} while (c->weight != sched->state.weight);
> +
> +	sched->state.counter = 0;	/* start with first counter */
> +
> +	return 1;
> +}

fair enough..

Looks ok otherwise, just a tad hard to grok in one go.. a few comments
could go a long way. I'm sure I'll have forgotten how it works in a few
weeks.


  reply	other threads:[~2011-09-14 14:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-10 14:49 [PATCH 0/2] perf, x86: handle overlapping counters Robert Richter
2011-09-10 14:49 ` [PATCH 1/2] perf, x86: Implement event scheduler helper functions Robert Richter
2011-09-14 14:43   ` Peter Zijlstra [this message]
2011-09-10 14:49 ` [PATCH 2/2] perf, x86: Fix event scheduler for constraints with Robert Richter
2011-09-14 14:45   ` 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=1316011408.5040.20.camel@twins \
    --to=peterz@infradead.org \
    --cc=eranian@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=robert.richter@amd.com \
    /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