All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pavel Emelyanov <xemul-GEFAQzZX7r8dnm+yROfE0A@public.gmane.org>
To: "Serge E. Hallyn" <serue-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
Cc: Linux Containers
	<containers-qjLDD68F18O7TbgM5vRIOg@public.gmane.org>,
	Andrew Morton
	<akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org>,
	Paul Menage <menage-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>,
	Linux Kernel Mailing List
	<linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>
Subject: Re: [PATCH][DOCUMENTATION] Minimal controller code for a quick	start
Date: Fri, 08 Feb 2008 12:28:26 +0300	[thread overview]
Message-ID: <47AC20BA.4070906@openvz.org> (raw)
In-Reply-To: <20080207204704.GA16628-6s5zFf/epYL1ENwx4SLHqw@public.gmane.org>

Serge E. Hallyn wrote:
> Quoting Pavel Emelyanov (xemul-GEFAQzZX7r8dnm+yROfE0A@public.gmane.org):
>> The Documentation/cgroups.txt file contains the info on how
>> to write some controller for cgroups subsystem, but even with
>> this, one need to write quite a lot of code before developing
>> the core (or copy-n-paste it from some other place).
>>
>> I propose to put this minimal controller into Documentation
>> directory to let people copy-n-paste a) from a known place and 
>> b) a small piece of code.
>>
>> Besides, many people learn better reading an example rather
>> than/along with a document.
>>
>> Signed-off-by: Pavel Emelyanov <xemul-GEFAQzZX7r8dnm+yROfE0A@public.gmane.org>
> 
> Actually I thought that was the main point of kernel/cgroup_debug.c?

This one doesn't show how to use generic read/write for files,
but a bit more advanced read_uint/write_uiant, it doesn't declare
its own cgroup with extra fields and doesn't show how to get the
one from task.

Hmm... This one is even more minimal than my :)

> -serge
> 
>> ---
>>
>> diff --git a/Documentation/cgroups.txt b/Documentation/cgroups.txt
>> index 42d7c4c..66068dc 100644
>> --- a/Documentation/cgroups.txt
>> +++ b/Documentation/cgroups.txt
>> @@ -531,6 +531,9 @@ and root cgroup. Currently this will only involve movement between
>>  the default hierarchy (which never has sub-cgroups) and a hierarchy
>>  that is being created/destroyed (and hence has no sub-cgroups).
>>
>> +For a quick start you may want to look at the
>> +Documentation/controllers/example.c file.
>> +
>>  4. Questions
>>  ============
>>
>> diff --git a/Documentation/controllers/example.c b/Documentation/controllers/example.c
>> new file mode 100644
>> index 0000000..4a73c77
>> --- /dev/null
>> +++ b/Documentation/controllers/example.c
>> @@ -0,0 +1,134 @@
>> +/*
>> + * Documentation/controllers/example.c - A simple controller
>> + *
>> + * Copy and make s/foo/$SUBSYS_NAME/g in it to get a minimal
>> + * working code. Don't forget to add a SUBSYS(foo) line in the
>> + * include/linux/cgroup_subsys.h file.
>> + *
>> + */
>> +
>> +#include <linux/cgroup.h>
>> +
>> +/*
>> + * the foo main structure - it is used to store any info, that
>> + * is required from the group of tasks
>> + */
>> +
>> +struct foo_cgroup {
>> +	/*
>> +	 * put your fields here
>> +	 */
>> +
>> +	struct cgroup_subsys_state css;
>> +
>> +	/*
>> +	 * ... or/and here
>> +	 */
>> +};
>> +
>> +/*
>> + * helpers to get the foo_cgroup from a task and a control group
>> + */
>> +
>> +static inline struct foo_cgroup *foo_from_css(struct cgroup_subsys_state *css)
>> +{
>> +	return container_of(css, struct foo_cgroup, css);
>> +}
>> +
>> +static inline struct foo_cgroup *foo_from_cgroup(struct cgroup *cg)
>> +{
>> +	return foo_from_css(cgroup_subsys_state(cg, foo_subsys_id));
>> +}
>> +
>> +static inline struct foo_cgroup *foo_from_task(struct task_struct *p)
>> +{
>> +	return foo_from_css(task_subsys_state(p, foo_subsys_id));
>> +}
>> +
>> +/*
>> + * foo files
>> + */
>> +
>> +static ssize_t foo_bar_read(struct cgroup *cg, struct cftype *cft,
>> +		struct file *file, char __user *userbuf,
>> +		size_t nbytes, loff_t *ppos)
>> +{
>> +	struct foo_cgroup *foo;
>> +
>> +	foo = foo_from_cgroup(cg);
>> +
>> +	/*
>> +	 * produce some output
>> +	 */
>> +
>> +	return nbytes;
>> +}
>> +
>> +static ssize_t foo_bar_write(struct cgroup *cg, struct cftype *cft,
>> +		struct file *file, const char __user *userbuf,
>> +		size_t nbytes, loff_t *ppos)
>> +{
>> +	struct foo_cgroup *foo;
>> +
>> +	foo = foo_from_cgroup(cg);
>> +
>> +	/*
>> +	 * read and tune the foo
>> +	 */
>> +
>> +	return nbytes;
>> +}
>> +
>> +static struct cftype foo_files[] = {
>> +	{
>> +		.name = "bar",
>> +		.read = foo_bar_read,
>> +		.write = foo_bar_write,
>> +	},
>> +};
>> +
>> +/*
>> + * foo subsystem basic callbacks
>> + */
>> +
>> +static struct cgroup_subsys_state *foo_create(struct cgroup_subsys *cs,
>> +		struct cgroup *cg)
>> +{
>> +	struct foo_cgroup *foo;
>> +
>> +	foo = kmalloc(sizeof(struct foo_cgroup), GFP_KERNEL);
>> +	if (foo == NULL)
>> +		return NULL;
>> +
>> +	/*
>> +	 * initialize your fields
>> +	 */
>> +
>> +	return &foo->css;
>> +}
>> +
>> +static void foo_destroy(struct cgroup_subsys *cs, struct cgroup *cg)
>> +{
>> +	struct foo_cgroup *foo;
>> +
>> +	foo = foo_from_cgroup(cg);
>> +
>> +	/*
>> +	 * clean your fields
>> +	 */
>> +
>> +	kfree(foo);
>> +}
>> +
>> +static int foo_populate(struct cgroup_subsys *cs, struct cgroup *cg)
>> +{
>> +	return cgroup_add_files(cg, cs, foo_files, ARRAY_SIZE(foo_files));
>> +}
>> +
>> +struct cgroup_subsys foo_subsys = {
>> +	.name = "foo",
>> +	.subsys_id = foo_subsys_id,
>> +	.create = foo_create,
>> +	.destroy = foo_destroy,
>> +	.populate = foo_populate,
>> +};
>> _______________________________________________
>> Containers mailing list
>> Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
>> https://lists.linux-foundation.org/mailman/listinfo/containers
> 

WARNING: multiple messages have this Message-ID (diff)
From: Pavel Emelyanov <xemul@openvz.org>
To: "Serge E. Hallyn" <serue@us.ibm.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Linux Containers <containers@lists.osdl.org>,
	Paul Menage <menage@google.com>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH][DOCUMENTATION] Minimal controller code for a quick	start
Date: Fri, 08 Feb 2008 12:28:26 +0300	[thread overview]
Message-ID: <47AC20BA.4070906@openvz.org> (raw)
In-Reply-To: <20080207204704.GA16628@sergelap.ibm.com>

Serge E. Hallyn wrote:
> Quoting Pavel Emelyanov (xemul@openvz.org):
>> The Documentation/cgroups.txt file contains the info on how
>> to write some controller for cgroups subsystem, but even with
>> this, one need to write quite a lot of code before developing
>> the core (or copy-n-paste it from some other place).
>>
>> I propose to put this minimal controller into Documentation
>> directory to let people copy-n-paste a) from a known place and 
>> b) a small piece of code.
>>
>> Besides, many people learn better reading an example rather
>> than/along with a document.
>>
>> Signed-off-by: Pavel Emelyanov <xemul@openvz.org>
> 
> Actually I thought that was the main point of kernel/cgroup_debug.c?

This one doesn't show how to use generic read/write for files,
but a bit more advanced read_uint/write_uiant, it doesn't declare
its own cgroup with extra fields and doesn't show how to get the
one from task.

Hmm... This one is even more minimal than my :)

> -serge
> 
>> ---
>>
>> diff --git a/Documentation/cgroups.txt b/Documentation/cgroups.txt
>> index 42d7c4c..66068dc 100644
>> --- a/Documentation/cgroups.txt
>> +++ b/Documentation/cgroups.txt
>> @@ -531,6 +531,9 @@ and root cgroup. Currently this will only involve movement between
>>  the default hierarchy (which never has sub-cgroups) and a hierarchy
>>  that is being created/destroyed (and hence has no sub-cgroups).
>>
>> +For a quick start you may want to look at the
>> +Documentation/controllers/example.c file.
>> +
>>  4. Questions
>>  ============
>>
>> diff --git a/Documentation/controllers/example.c b/Documentation/controllers/example.c
>> new file mode 100644
>> index 0000000..4a73c77
>> --- /dev/null
>> +++ b/Documentation/controllers/example.c
>> @@ -0,0 +1,134 @@
>> +/*
>> + * Documentation/controllers/example.c - A simple controller
>> + *
>> + * Copy and make s/foo/$SUBSYS_NAME/g in it to get a minimal
>> + * working code. Don't forget to add a SUBSYS(foo) line in the
>> + * include/linux/cgroup_subsys.h file.
>> + *
>> + */
>> +
>> +#include <linux/cgroup.h>
>> +
>> +/*
>> + * the foo main structure - it is used to store any info, that
>> + * is required from the group of tasks
>> + */
>> +
>> +struct foo_cgroup {
>> +	/*
>> +	 * put your fields here
>> +	 */
>> +
>> +	struct cgroup_subsys_state css;
>> +
>> +	/*
>> +	 * ... or/and here
>> +	 */
>> +};
>> +
>> +/*
>> + * helpers to get the foo_cgroup from a task and a control group
>> + */
>> +
>> +static inline struct foo_cgroup *foo_from_css(struct cgroup_subsys_state *css)
>> +{
>> +	return container_of(css, struct foo_cgroup, css);
>> +}
>> +
>> +static inline struct foo_cgroup *foo_from_cgroup(struct cgroup *cg)
>> +{
>> +	return foo_from_css(cgroup_subsys_state(cg, foo_subsys_id));
>> +}
>> +
>> +static inline struct foo_cgroup *foo_from_task(struct task_struct *p)
>> +{
>> +	return foo_from_css(task_subsys_state(p, foo_subsys_id));
>> +}
>> +
>> +/*
>> + * foo files
>> + */
>> +
>> +static ssize_t foo_bar_read(struct cgroup *cg, struct cftype *cft,
>> +		struct file *file, char __user *userbuf,
>> +		size_t nbytes, loff_t *ppos)
>> +{
>> +	struct foo_cgroup *foo;
>> +
>> +	foo = foo_from_cgroup(cg);
>> +
>> +	/*
>> +	 * produce some output
>> +	 */
>> +
>> +	return nbytes;
>> +}
>> +
>> +static ssize_t foo_bar_write(struct cgroup *cg, struct cftype *cft,
>> +		struct file *file, const char __user *userbuf,
>> +		size_t nbytes, loff_t *ppos)
>> +{
>> +	struct foo_cgroup *foo;
>> +
>> +	foo = foo_from_cgroup(cg);
>> +
>> +	/*
>> +	 * read and tune the foo
>> +	 */
>> +
>> +	return nbytes;
>> +}
>> +
>> +static struct cftype foo_files[] = {
>> +	{
>> +		.name = "bar",
>> +		.read = foo_bar_read,
>> +		.write = foo_bar_write,
>> +	},
>> +};
>> +
>> +/*
>> + * foo subsystem basic callbacks
>> + */
>> +
>> +static struct cgroup_subsys_state *foo_create(struct cgroup_subsys *cs,
>> +		struct cgroup *cg)
>> +{
>> +	struct foo_cgroup *foo;
>> +
>> +	foo = kmalloc(sizeof(struct foo_cgroup), GFP_KERNEL);
>> +	if (foo == NULL)
>> +		return NULL;
>> +
>> +	/*
>> +	 * initialize your fields
>> +	 */
>> +
>> +	return &foo->css;
>> +}
>> +
>> +static void foo_destroy(struct cgroup_subsys *cs, struct cgroup *cg)
>> +{
>> +	struct foo_cgroup *foo;
>> +
>> +	foo = foo_from_cgroup(cg);
>> +
>> +	/*
>> +	 * clean your fields
>> +	 */
>> +
>> +	kfree(foo);
>> +}
>> +
>> +static int foo_populate(struct cgroup_subsys *cs, struct cgroup *cg)
>> +{
>> +	return cgroup_add_files(cg, cs, foo_files, ARRAY_SIZE(foo_files));
>> +}
>> +
>> +struct cgroup_subsys foo_subsys = {
>> +	.name = "foo",
>> +	.subsys_id = foo_subsys_id,
>> +	.create = foo_create,
>> +	.destroy = foo_destroy,
>> +	.populate = foo_populate,
>> +};
>> _______________________________________________
>> Containers mailing list
>> Containers@lists.linux-foundation.org
>> https://lists.linux-foundation.org/mailman/listinfo/containers
> 


  parent reply	other threads:[~2008-02-08  9:28 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-07 15:37 [PATCH][DOCUMENTATION] Minimal controller code for a quick start Pavel Emelyanov
2008-02-07 20:28 ` Peter Zijlstra
2008-02-07 20:49   ` Paul Menage
     [not found]     ` <6599ad830802071249g45cb2d6eybd00c1bef07cf5aa-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2008-02-07 20:52       ` Peter Zijlstra
2008-02-07 20:52         ` Peter Zijlstra
2008-02-07 20:50   ` Serge E. Hallyn
2008-02-07 20:50     ` Serge E. Hallyn
2008-02-07 20:45 ` Paul Menage
2008-02-08  8:09   ` Pavel Emelyanov
2008-02-07 20:47 ` Serge E. Hallyn
     [not found]   ` <20080207204704.GA16628-6s5zFf/epYL1ENwx4SLHqw@public.gmane.org>
2008-02-08  9:28     ` Pavel Emelyanov [this message]
2008-02-08  9:28       ` Pavel Emelyanov

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=47AC20BA.4070906@openvz.org \
    --to=xemul-gefaqzzx7r8dnm+yrofe0a@public.gmane.org \
    --cc=akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org \
    --cc=containers-qjLDD68F18O7TbgM5vRIOg@public.gmane.org \
    --cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=menage-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org \
    --cc=serue-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.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 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.