From: "Kirill A. Shutemov" <kirill@shutemov.name>
To: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
LKML <linux-kernel@vger.kernel.org>,
Containers <containers@lists.osdl.org>,
Li Zefan <lizf@cn.fujitsu.com>,
Johannes Weiner <hannes@cmpxchg.org>,
Aditya Kali <adityakali@google.com>,
Oleg Nesterov <oleg@redhat.com>,
Kay Sievers <kay.sievers@vrfy.org>,
Tim Hockin <thockin@hockin.org>, Tejun Heo <tj@kernel.org>
Subject: Re: [PATCH 06/11] cgroups: Add res counter common ancestor searching
Date: Sun, 18 Sep 2011 22:59:32 +0300 [thread overview]
Message-ID: <20110918195932.GD28057@shutemov.name> (raw)
In-Reply-To: <1315869091-18933-7-git-send-email-fweisbec@gmail.com>
On Tue, Sep 13, 2011 at 01:11:26AM +0200, Frederic Weisbecker wrote:
> Add a new API to find the common ancestor between two resource
> counters. This includes the passed resource counter themselves.
>
> Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
> Acked-by: Paul Menage <paul@paulmenage.org>
> Cc: Li Zefan <lizf@cn.fujitsu.com>
> Cc: Johannes Weiner <hannes@cmpxchg.org>
> Cc: Aditya Kali <adityakali@google.com>
> Cc: Oleg Nesterov <oleg@redhat.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Kay Sievers <kay.sievers@vrfy.org>
> Cc: Tim Hockin <thockin@hockin.org>
> Cc: Tejun Heo <tj@kernel.org>
> ---
> include/linux/res_counter.h | 3 +++
> kernel/res_counter.c | 22 ++++++++++++++++++++++
> 2 files changed, 25 insertions(+), 0 deletions(-)
>
> diff --git a/include/linux/res_counter.h b/include/linux/res_counter.h
> index de4ba29..558f39b 100644
> --- a/include/linux/res_counter.h
> +++ b/include/linux/res_counter.h
> @@ -147,6 +147,9 @@ static inline void res_counter_uncharge(struct res_counter *counter,
> res_counter_uncharge_until(counter, NULL, val);
> }
>
> +struct res_counter *res_counter_common_ancestor(struct res_counter *l,
> + struct res_counter *r);
> +
> /**
> * res_counter_margin - calculate chargeable space of a counter
> * @cnt: the counter
> diff --git a/kernel/res_counter.c b/kernel/res_counter.c
> index 0cc9c7e..22f07cc 100644
> --- a/kernel/res_counter.c
> +++ b/kernel/res_counter.c
> @@ -91,6 +91,28 @@ void res_counter_uncharge_until(struct res_counter *counter,
> local_irq_restore(flags);
> }
>
> +/*
> + * Walk through r1 and r2 parents and try to find the closest common one
> + * between both. If none is found, it returns NULL.
> + */
> +struct res_counter *
> +res_counter_common_ancestor(struct res_counter *r1, struct res_counter *r2)
> +{
> + struct res_counter *iter;
> +
> + while (r1) {
> + iter = r2;
> + while (iter) {
> + if (iter == r1)
> + return iter;
> + iter = iter->parent;
> + }
> +
> + r1 = r1->parent;
> + }
> +
> + return NULL;
> +}
Your implementation is O(n*m).
What about:
struct res_counter *
res_counter_common_ancestor(struct res_counter *r1, struct res_counter *r2)
{
struct res_counter *iter;
int r1_depth = 0, r2_depth = 0;
for (iter = r1; iter; iter = iter->parent)
r1_depth++;
for (iter = r2; iter; iter = iter->parent)
r2_depth++;
while (r1_depth > r2_depth) {
r1 = r1->parent;
r1_depth--;
}
while (r2_depth > r1_depth) {
r2 = r2->parent;
r2_depth--;
}
while (r1 != r2) {
r1 = r1->parent;
r2 = r2->parent;
}
return r1;
}
Untested. It's O(n+m).
--
Kirill A. Shutemov
next prev parent reply other threads:[~2011-09-18 19:59 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-09-12 23:11 [PATCH 00/11 v5] cgroups: Task counter subsystem Frederic Weisbecker
2011-09-12 23:11 ` [PATCH 01/11] cgroups: Add res_counter_write_u64() API Frederic Weisbecker
2011-09-12 23:11 ` [PATCH 02/11] cgroups: New resource counter inheritance API Frederic Weisbecker
2011-09-12 23:11 ` [PATCH 03/11] cgroups: Add previous cgroup in can_attach_task/attach_task callbacks Frederic Weisbecker
2011-09-12 23:11 ` [PATCH 04/11] cgroups: New cancel_attach_task subsystem callback Frederic Weisbecker
2011-09-12 23:11 ` [PATCH 05/11] cgroups: Ability to stop res charge propagation on bounded ancestor Frederic Weisbecker
2011-09-12 23:11 ` [PATCH 06/11] cgroups: Add res counter common ancestor searching Frederic Weisbecker
2011-09-18 19:59 ` Kirill A. Shutemov [this message]
2011-09-30 12:42 ` Frederic Weisbecker
2011-10-01 15:07 ` Frederic Weisbecker
2011-10-01 15:19 ` Kirill A. Shutemov
2011-09-12 23:11 ` [PATCH 07/11] res_counter: Allow charge failure pointer to be null Frederic Weisbecker
2011-09-12 23:11 ` [PATCH 08/11] cgroups: Pull up res counter charge failure interpretation to caller Frederic Weisbecker
2011-09-12 23:11 ` [PATCH 09/11] cgroups: Add a task counter subsystem Frederic Weisbecker
2011-09-18 20:27 ` Kirill A. Shutemov
2011-09-30 12:54 ` Frederic Weisbecker
2011-09-30 21:03 ` Kirill A. Shutemov
2011-10-01 13:03 ` Frederic Weisbecker
2011-09-12 23:11 ` [PATCH 10/11] cgroups: Allow subsystems to cancel a fork Frederic Weisbecker
2011-09-12 23:11 ` [PATCH 11/11] cgroups: Convert task counter to use the subsys fork callback Frederic Weisbecker
2011-09-13 14:32 ` [PATCH 00/11 v5] cgroups: Task counter subsystem Peter Zijlstra
2011-09-13 14:37 ` Frederic Weisbecker
2011-09-13 14:49 ` Peter Zijlstra
2011-09-13 15:01 ` Frederic Weisbecker
2011-09-13 15:21 ` Frederic Weisbecker
2011-09-13 22:23 ` Andrew Morton
2011-09-15 13:56 ` Frederic Weisbecker
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=20110918195932.GD28057@shutemov.name \
--to=kirill@shutemov.name \
--cc=adityakali@google.com \
--cc=akpm@linux-foundation.org \
--cc=containers@lists.osdl.org \
--cc=fweisbec@gmail.com \
--cc=hannes@cmpxchg.org \
--cc=kay.sievers@vrfy.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lizf@cn.fujitsu.com \
--cc=oleg@redhat.com \
--cc=thockin@hockin.org \
--cc=tj@kernel.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