Memory controller improve user interface
Subsystems (3)
PATCH 1/1 Memory controller improve user interface
Change the interface to use kilobytes instead of pages. Page sizes can vary across platforms and configurations. A new strategy routine has been added to the resource counters infrastructure to format the data as desired.
Suggested by David Rientjes, Andrew Morton and Herbert Poetzl
---
Documentation/controllers/memory.txt | 7 +++-- include/linux/res_counter.h | 6 ++-- kernel/res_counter.c | 24 +++++++++++++---- mm/memcontrol.c | 47 +++++++++++++++++++++++++++-------- 4 files changed, 64 insertions(+), 20 deletions(-)
mm/memcontrol.c renamed from mm/memcontrol.c~mem-control-make-ui-use-kilobytes +37 -10
@@ -32,6 +32,7 @@struct container_subsys mem_container_subsys;static const int MEM_CONTAINER_RECLAIM_RETRIES =5 ; +static const int MEM_CONTAINER_CHARGE_KB = (PAGE_SIZE >>10 );/* * The memory controller data structure. The memory controller controls both
@@ -312,7 +313,7 @@ int mem_container_charge(struct page *pa* If we created the page_container, we should free it on exceeding * the container limit. */ -while (res_counter_charge (&mem->res,1 )) { +while (res_counter_charge (&mem->res,MEM_CONTAINER_CHARGE_KB )) {if (try_to_free_mem_container_pages (mem))continue ;
@@ -352,7 +353,7 @@ int mem_container_charge(struct page *pakfree (pc); pc = race_pc;atomic_inc (&pc->ref_cnt); -res_counter_uncharge (&mem->res,1 ); +res_counter_uncharge (&mem->res,MEM_CONTAINER_CHARGE_KB );css_put (&mem->css); goto done; }
@@ -417,7 +418,7 @@ void mem_container_uncharge(struct page_css_put (&mem->css);page_assign_page_container (page,NULL );unlock_page_container (page); -res_counter_uncharge (&mem->res,1 ); +res_counter_uncharge (&mem->res,MEM_CONTAINER_CHARGE_KB );spin_lock_irqsave (&mem->lru_lock, flags);list_del_init (&pc->lru);
@@ -426,12 +427,37 @@ void mem_container_uncharge(struct page_ } } -static ssize_t mem_container_read (struct container *cont,struct cftype *cft, -struct file *file,char __user *userbuf,size_t nbytes, -loff_t *ppos) +int mem_container_read_strategy (unsigned long val,char *buf) +{ +return sprintf (buf,"%lu (kB)\n" , val); +} + +int mem_container_write_strategy (char *buf,unsigned long *tmp) +{ + *tmp =memparse (buf, &buf); +if (*buf !='\0' ) +return -EINVAL ; + + *tmp = *tmp >>10 ;/* convert to kilobytes */ +return 0 ; +} + +static ssize_t mem_container_read_usage (struct container *cont, +struct cftype *cft,struct file *file, +char __user *userbuf,size_t nbytes,loff_t *ppos) +{ +return res_counter_read (&mem_container_from_cont (cont)->res, + cft->private, userbuf, nbytes, ppos, + mem_container_read_strategy); +} + +static ssize_t mem_container_read (struct container *cont, +struct cftype *cft,struct file *file, +char __user *userbuf,size_t nbytes,loff_t *ppos) {return res_counter_read (&mem_container_from_cont (cont)->res, - cft->private, userbuf, nbytes, ppos); + cft->private, userbuf, nbytes, ppos, +NULL ); } static ssize_t mem_container_write(struct container *cont, struct cftype *cft,
@@ -439,7 +465,8 @@ static ssize_t mem_container_write(struc size_t nbytes, loff_t *ppos) { return res_counter_write(&mem_container_from_cont(cont)->res, - cft->private, userbuf, nbytes, ppos); + cft->private, userbuf, nbytes, ppos, +mem_container_write_strategy ); } static ssize_t mem_control_type_write(struct container *cont,
@@ -500,13 +527,13 @@ static struct cftype mem_container_files { .name = "usage", .private = RES_USAGE, - .read = mem_container_read, + .read = mem_container_read_usage, }, { .name = "limit", .private = RES_LIMIT, .write = mem_container_write, - .read = mem_container_read, + .read = mem_container_read_usage, }, { .name = "failcnt",
diff -puN include/linux/memcontrol.h~mem-control-make-ui-use-kilobytes include/linux/memcontrol.h
include/linux/res_counter.h renamed from include/linux/res_counter.h~mem-control-make-ui-use-kilobytes +4 -2
@@ -52,9 +52,11 @@ struct res_counter {*/ ssize_t res_counter_read (struct res_counter *counter,int member, -const char __user *buf,size_t nbytes,loff_t *pos); +const char __user *buf,size_t nbytes,loff_t *pos, +int (*read_strategy)(unsigned long val,char *s));ssize_t res_counter_write (struct res_counter *counter,int member, -const char __user *buf,size_t nbytes,loff_t *pos); +const char __user *buf,size_t nbytes,loff_t *pos, +int (*write_strategy)(char *buf,unsigned long *val));/* * the field descriptors. one for each member of res_counter
kernel/res_counter.c renamed from kernel/res_counter.c~mem-control-make-ui-use-kilobytes +18 -6
@@ -76,20 +76,25 @@ static inline unsigned long *res_counter }ssize_t res_counter_read (struct res_counter *counter,int member, -const char __user *userbuf,size_t nbytes,loff_t *pos) +const char __user *userbuf,size_t nbytes,loff_t *pos, +int (*read_strategy)(unsigned long val,char *st_buf)) {unsigned long *val;char buf[64 ], *s; s = buf; val =res_counter_member (counter, member); - s +=sprintf (s,"%lu\n" , *val); +if (read_strategy) + s +=read_strategy (*val, s); +else + s +=sprintf (s,"%lu\n" , *val);return simple_read_from_buffer ((void __user *)userbuf, nbytes, pos, buf, s - buf); }ssize_t res_counter_write (struct res_counter *counter,int member, -const char __user *userbuf,size_t nbytes,loff_t *pos) +const char __user *userbuf,size_t nbytes,loff_t *pos, +int (*write_strategy)(char *st_buf,unsigned long *val)) {int ret;char *buf, *end;
@@ -106,9 +111,16 @@ ssize_t res_counter_write(struct res_cou goto out_free; ret = -EINVAL ; - tmp =simple_strtoul (buf, &end,10 ); -if (*end !='\0' ) - goto out_free; + +if (write_strategy) { +if (write_strategy (buf, &tmp)) { + goto out_free; + } + }else { + tmp =simple_strtoul (buf, &end,10 ); +if (*end !='\0' ) + goto out_free; + } val =res_counter_member (counter, member); *val = tmp;
Documentation/controllers/memory.txt renamed from Documentation/controllers/memory.txt~mem-control-make-ui-use-kilobytes +5 -2
@@ -165,11 +165,14 @@ c. Enable CONFIG_CONTAINER_MEM_CONT Since now we're in the 0 container, We can alter the memory limit: -# echo -n 6000 > /containers/0/memory.limit +# echo -n 4000K > /containers/0/memory.limit + +NOTE: The interface has now changed to display the usage in kilobytes +instead of pages We can check the usage: # cat /containers/0/memory.usage -25 +56 (kB) The memory.failcnt field gives the number of times that the container limit was exceeded.
_
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL
Quoted 65 lines · Balbir Singh
Change the interface to use kilobytes instead of pages. Page sizes can vary across platforms and configurations. A new strategy routine has been added to the resource counters infrastructure to format the data as desired.
Suggested by David Rientjes, Andrew Morton and Herbert Poetzl
Signed-off-by Balbir Singh <[email protected]>---
Documentation/controllers/memory.txt | 7 +++-- include/linux/res_counter.h | 6 ++-- kernel/res_counter.c | 24 +++++++++++++---- mm/memcontrol.c | 47 +++++++++++++++++++++++++++-------- 4 files changed, 64 insertions(+), 20 deletions(-)diff -puN
mm/memcontrol.c~mem-control-make-ui-use-kilobytesmm/memcontrol.cmm/memcontrol.c 2007-08-29 14:36:07.000000000 +0530 renamed from mm/memcontrol.c~mem-control-make-ui-use-kilobytes 2007-08-28 13:20:44.000000000 +0530 +8
-6@@ -32,6 +32,7 @@ struct container_subsys mem_container_subsys; static const int MEM_CONTAINER_RECLAIM_RETRIES = 5; +static const int MEM_CONTAINER_CHARGE_KB = (PAGE_SIZE >> 10); /* * The memory controller data structure. The memory controller controls both@@ -312,7 +313,7 @@ int mem_container_charge(struct page *pa * If we created the page_container, we should free it on exceeding * the container limit. */ - while (res_counter_charge(&mem->res, 1)) { + while (res_counter_charge(&mem->res, MEM_CONTAINER_CHARGE_KB)) { if (try_to_free_mem_container_pages(mem)) continue;@@ -352,7 +353,7 @@ int mem_container_charge(struct page *pa kfree(pc); pc = race_pc; atomic_inc(&pc->ref_cnt); - res_counter_uncharge(&mem->res, 1); + res_counter_uncharge(&mem->res, MEM_CONTAINER_CHARGE_KB); css_put(&mem->css); goto done; }@@ -417,7 +418,7 @@ void mem_container_uncharge(struct page_ css_put(&mem->css); page_assign_page_container(page, NULL); unlock_page_container(page); - res_counter_uncharge(&mem->res, 1); + res_counter_uncharge(&mem->res, MEM_CONTAINER_CHARGE_KB); spin_lock_irqsave(&mem->lru_lock, flags); list_del_init(&pc->lru);@@ -426,12 +427,37 @@ void mem_container_uncharge(struct page_ } } -static ssize_t mem_container_read(struct container *cont, struct cftype *cft, - struct file *file, char __user *userbuf, size_t nbytes, - loff_t *ppos) +int mem_container_read_strategy(unsigned long val, char *buf) +{ + return sprintf(buf, "%lu (kB)\n", val); +}
mm/memcontrol.c 2007-08-29 14:36:07.000000000 +0530 void mem_container_uncharge(struct page_ · line 443
Quoted 10 lines
mm/memcontrol.c 2007-08-29 14:36:07.000000000 +0530 renamed from mm/memcontrol.c~mem-control-make-ui-use-kilobytes 2007-08-28 13:20:44.000000000 +0530 +10
-0+ +int mem_container_write_strategy(char *buf, unsigned long *tmp) +{ + *tmp = memparse(buf, &buf); + if (*buf != '\0') + return -EINVAL; + + *tmp = *tmp >> 10; /* convert to kilobytes */ + return 0; +}
This seems a bit inconsistent - if you write a value to a limit file, then the value that you read back is reduced by a factor of 1024? Having the "(kB)" suffix isn't really a big help to automated middleware.
I'd still be in favour of just reading/writing 64-bit values representing bytes - simple, and unambiguous for programmatic use, and not really any less user-friendly than kilobytes for manual use (since the numbers involved are going to be unwieldly for manual use whether they're in bytes or kB).
Paul
Quoted 72 lines · Paul Menage
Quoted 71 lines · Balbir Singh
Change the interface to use kilobytes instead of pages. Page sizes can vary across platforms and configurations. A new strategy routine has been added to the resource counters infrastructure to format the data as desired.
Suggested by David Rientjes, Andrew Morton and Herbert Poetzl
Signed-off-by Balbir Singh <[email protected]>---
Documentation/controllers/memory.txt | 7 +++-- include/linux/res_counter.h | 6 ++-- kernel/res_counter.c | 24 +++++++++++++---- mm/memcontrol.c | 47 +++++++++++++++++++++++++++-------- 4 files changed, 64 insertions(+), 20 deletions(-)diff -puN
mm/memcontrol.c~mem-control-make-ui-use-kilobytesmm/memcontrol.cmm/memcontrol.c 2007-08-29 14:36:07.000000000 +0530 renamed from mm/memcontrol.c~mem-control-make-ui-use-kilobytes 2007-08-28 13:20:44.000000000 +0530 +15
-6@@ -32,6 +32,7 @@ struct container_subsys mem_container_subsys; static const int MEM_CONTAINER_RECLAIM_RETRIES = 5; +static const int MEM_CONTAINER_CHARGE_KB = (PAGE_SIZE >> 10); /* * The memory controller data structure. The memory controller controls both@@ -312,7 +313,7 @@ int mem_container_charge(struct page *pa * If we created the page_container, we should free it on exceeding * the container limit. */ - while (res_counter_charge(&mem->res, 1)) { + while (res_counter_charge(&mem->res, MEM_CONTAINER_CHARGE_KB)) { if (try_to_free_mem_container_pages(mem)) continue;@@ -352,7 +353,7 @@ int mem_container_charge(struct page *pa kfree(pc); pc = race_pc; atomic_inc(&pc->ref_cnt); - res_counter_uncharge(&mem->res, 1); + res_counter_uncharge(&mem->res, MEM_CONTAINER_CHARGE_KB); css_put(&mem->css); goto done; }@@ -417,7 +418,7 @@ void mem_container_uncharge(struct page_ css_put(&mem->css); page_assign_page_container(page, NULL); unlock_page_container(page); - res_counter_uncharge(&mem->res, 1); + res_counter_uncharge(&mem->res, MEM_CONTAINER_CHARGE_KB); spin_lock_irqsave(&mem->lru_lock, flags); list_del_init(&pc->lru);@@ -426,12 +427,37 @@ void mem_container_uncharge(struct page_ } } -static ssize_t mem_container_read(struct container *cont, struct cftype *cft, - struct file *file, char __user *userbuf, size_t nbytes, - loff_t *ppos) +int mem_container_read_strategy(unsigned long val, char *buf) +{ + return sprintf(buf, "%lu (kB)\n", val); +} + +int mem_container_write_strategy(char *buf, unsigned long *tmp) +{ + *tmp = memparse(buf, &buf); + if (*buf != '\0') + return -EINVAL; +
Quoted 9 lines
mm/memcontrol.c 2007-08-29 14:36:07.000000000 +0530 void mem_container_uncharge(struct page_ · line 443
Quoted 3 lines
mm/memcontrol.c 2007-08-29 14:36:07.000000000 +0530 renamed from mm/memcontrol.c~mem-control-make-ui-use-kilobytes 2007-08-28 13:20:44.000000000 +0530 +3
-0+ *tmp = *tmp >> 10; /* convert to kilobytes */ + return 0; +}This seems a bit inconsistent - if you write a value to a limit file, then the value that you read back is reduced by a factor of 1024? Having the "(kB)" suffix isn't really a big help to automated middleware.
Why is that? Is it because you could write 4M and see it show up as 4096 kilobytes? We'll that can be fixed with another variant of the memparse() utility.
Quoted 6 lines
I'd still be in favour of just reading/writing 64-bit values representing bytes - simple, and unambiguous for programmatic use, and not really any less user-friendly than kilobytes for manual use (since the numbers involved are going to be unwieldly for manual use whether they're in bytes or kB).
64 bit might be an overkill for 32 bit machines. 32 bit machines with PAE cannot use 32 bit values, they need 64 bits. I think KiloBytes is an acceptable metric these days, everybody understands them.
Quoted 5 lines
Paul
_______________________________________________
Containers mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/containers
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL
Quoted 10 lines · Balbir Singh
Quoted 6 lines
This seems a bit inconsistent - if you write a value to a limit file, then the value that you read back is reduced by a factor of 1024? Having the "(kB)" suffix isn't really a big help to automated middleware.
Why is that? Is it because you could write 4M and see it show up as 4096 kilobytes? We'll that can be fixed with another variant of the
memparse()utility.
I was thinking the other way around - you can write 1048576 (i.e. 1MB) to the file and read back 1024. It just seems to me that it's clearer if you write X to the file to get X back.
Quoted 3 lines
64 bit might be an overkill for 32 bit machines. 32 bit machines with PAE cannot use 32 bit values, they need 64 bits.
How is using a 64-bit value for consistency overkill?
As someone pointed out, 4TB machines probably aren't that far around the corner (if they're not here already) so even if you use KB rather than bytes, userspace needs to be using an int64 for this value in case it ends up running as a 32-bit-compiled app on a 64-bit kernel with lots of memory.
Paul
mm/memcontrol.c int mem_container_charge(struct page *pa · line 359
Quoted 11 lines · Balbir Singh
mm/memcontrol.c renamed from mm/memcontrol.c~mem-control-make-ui-use-kilobytes +1
-1@@ -352,7 +353,7 @@ int mem_container_charge(struct page *pakfree (pc); pc = race_pc;atomic_inc (&pc->ref_cnt); -res_counter_uncharge (&mem->res,1 ); +res_counter_uncharge (&mem->res,MEM_CONTAINER_CHARGE_KB );css_put (&mem->css); goto done; }
Do these changes really need to happen anywhere besides the user<->kernel boundary? Why can't internal tracking be in pages?
-- Dave
Quoted 14 lines · Dave Hansen
mm/memcontrol.c int mem_container_charge(struct page *pa · line 359
Quoted 10 lines · Balbir Singh
mm/memcontrol.c renamed from mm/memcontrol.c~mem-control-make-ui-use-kilobytes +1
-1@@ -352,7 +353,7 @@ int mem_container_charge(struct page *pakfree (pc); pc = race_pc;atomic_inc (&pc->ref_cnt); -res_counter_uncharge (&mem->res,1 ); +res_counter_uncharge (&mem->res,MEM_CONTAINER_CHARGE_KB );css_put (&mem->css); goto done; }Do these changes really need to happen anywhere besides the user<->kernel boundary? Why can't internal tracking be in pages?
I've thought about this before. The problem is that a user could set his limit to 10000 bytes, but would then see the usage and limit round to the closest page boundary. This can be confusing to a user.
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL
Quoted 4 lines · Balbir Singh
I've thought about this before. The problem is that a user could set his limit to 10000 bytes, but would then see the usage and limit round to the closest page boundary. This can be confusing to a user.
True, but we're lying if we allow a user to set their limit there, because we can't actually enforce a limit at 8,192 bytes vs 10,000. They're the same limit as far as the kernel is concerned.
Why not just -EINVAL if the value isn't page-aligned? There are plenty of interfaces in the kernel that require userspace to know the page size, so this shouldn't be too difficult.
-- Dave
Quoted 6 lines · Dave Hansen
Quoted 4 lines · Balbir Singh
I've thought about this before. The problem is that a user could set his limit to 10000 bytes, but would then see the usage and limit round to the closest page boundary. This can be confusing to a user.
Quoted 7 lines
True, but we're lying if we allow a user to set their limit there, because we can't actually enforce a limit at 8,192 bytes vs 10,000. They're the same limit as far as the kernel is concerned.
Why not just
-EINVALif the value isn't page-aligned? There are plenty of interfaces in the kernel that require userspace to know the page size, so this shouldn't be too difficult.
I'd argue that having the user's specified limit be truncated to the page size is less confusing than giving an EINVAL if it's not page aligned.
Paul
Quoted 4 lines · Paul Menage
I'd argue that having the user's specified limit be truncated to the page size is less confusing than giving an EINVAL if it's not page aligned.
Do we truncate mmap() values to the nearest page so to not confuse the user? ;)
Imagine a careful application setting and accounting for limits on a long-running system. Might its internal accounting get sufficiently misaligned from the kernel's after a while to cause a problem? Truncating values like that would appear reserve significantly less memory than desired over a long period of time.
-- Dave
Quoted 6 lines · Dave Hansen
Quoted 4 lines · Balbir Singh
I've thought about this before. The problem is that a user could set his limit to 10000 bytes, but would then see the usage and limit round to the closest page boundary. This can be confusing to a user.
Quoted 7 lines
True, but we're lying if we allow a user to set their limit there, because we can't actually enforce a limit at 8,192 bytes vs 10,000. They're the same limit as far as the kernel is concerned.
Why not just
-EINVALif the value isn't page-aligned? There are plenty of interfaces in the kernel that require userspace to know the page size, so this shouldn't be too difficult.
True, mmap() is a good example of such an interface for developers, I am not sure about system admins though.
To quote Andrew
<quote>
Reporting tools could run getpagesize() and do the arithmetic, but we generally try to avoid exposing PAGE_SIZE, HZ, etc to userspace in this manner.
</quote>
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL
Quoted 9 lines · Balbir Singh
True,
mmap()is a good example of such an interface for developers, I am not sure about system admins though.To quote Andrew
<quote>
Reporting tools could rungetpagesize()and do the arithmetic, but we generally try to avoid exposingPAGE_SIZE, HZ, etc to userspace in this manner.
</quote>
Well, rounding to PAGE_SIZE exposes PAGE_SIZE as well, just in a non-intuitive fashion. :)
If we're going to modify what the user specifies, we should probably at least mandate that writes are only a "suggestion" and users must read back the value to ensure what actually got committed.
If we're going to round in any direction, shouldn't we round up? If a user specifies 4097 bytes and uses two pages, we don't want to complain when they hit that second page.
-- Dave
Quoted 8 lines · Dave Hansen
Quoted 3 lines · Paul Menage
I'd argue that having the user's specified limit be truncated to the page size is less confusing than giving an EINVAL if it's not page aligned.
Do we truncate
mmap()values to the nearest page so to not confuse the user? ;)
I think rounding to the closest page size is a better option, but again it can be a bit confusing. I am all for using memparse() to parse the user input as a specification of the memory limit.
The second question of how to store it internally without truncation/ rounding is something we need to agree upon. We also need to see how to display the data back to the user.
I chose kilobytes for two reasons
1. Several people recommended it
2. Herbert mentioned that they've moved to that interface and it was working fine for them.
Signature · 6 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL PS: I am going off to the web to search for some CUI/CLI guidelines.
Quoted 11 lines · Dave Hansen
Quoted 9 lines · Balbir Singh
True,
mmap()is a good example of such an interface for developers, I am not sure about system admins though.To quote Andrew
<quote>
Reporting tools could rungetpagesize()and do the arithmetic, but we generally try to avoid exposingPAGE_SIZE, HZ, etc to userspace in this manner.
</quote>
Quoted 3 lines
Well, rounding to
PAGE_SIZEexposesPAGE_SIZEas well, just in a non-intuitive fashion. :)
Agreed, but the user might choose to ignore it altogether.
Quoted 4 lines
If we're going to modify what the user specifies, we should probably at least mandate that writes are only a "suggestion" and users must read back the value to ensure what actually got committed.
Agreed, excellent suggestion!
Quoted 4 lines
If we're going to round in any direction, shouldn't we round up? If a user specifies 4097 bytes and uses two pages, we don't want to complain when they hit that second page.
Absolutely, I used rounding to mean round up, truncation for rounding down.
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL
Quoted 4 lines · On Thu, 30 Aug 2007 04:07:11 +0530 Balbir Singh <[email protected]>…
1. Several people recommended it
2. Herbert mentioned that they've moved to that interface and it was working fine for them.
I have no strong opinion. But how about Mega bytes ? (too big ?) There will be no rounding up/down problem.
-Kame.
Quoted 10 lines · KAMEZAWA Hiroyuki
Quoted 4 lines · On Thu, 30 Aug 2007 04:07:11 +0530 Balbir Singh…
1. Several people recommended it
2. Herbert mentioned that they've moved to that interface and it was working fine for them.I have no strong opinion. But how about Mega bytes ? (too big ?) There will be no rounding up/down problem.
Here is what I am thinking, allow the user to input bytes/kilobytes/ megabytes or gigabytes. Store the data internally in kilobytes or PFN. I prefer kilobytes (no rounding issues), but while implementing limits we round up to the closest PFN.
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL