linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] tools/vm/page-types.c: remove memset() in walk_pfn()
@ 2016-03-08  1:47 Naoya Horiguchi
  2016-03-08  5:12 ` Konstantin Khlebnikov
  0 siblings, 1 reply; 5+ messages in thread
From: Naoya Horiguchi @ 2016-03-08  1:47 UTC (permalink / raw)
  To: linux-mm
  Cc: Andrew Morton, Vladimir Davydov, Konstantin Khlebnikov,
	linux-kernel, Naoya Horiguchi, Naoya Horiguchi

I found that page-types is very slow and my testing shows many timeout errors.
Here's an example with a simple program allocating 1000 thps.

  $ time ./page-types -p $(pgrep -f test_alloc)
  ...
  real    0m17.201s
  user    0m16.889s
  sys     0m0.312s

  $ time ./page-types.patched -p $(pgrep -f test_alloc)
  ...
  real    0m0.182s
  user    0m0.046s
  sys     0m0.135s

Most of time is spent in memset(), which isn't necessary because we check
that the return of kpagecgroup_read() is equal to pages and uninitialized
memory is never used. So we can drop this memset().

Fixes: 954e95584579 ("tools/vm/page-types.c: add memory cgroup dumping and filtering")
Signed-off-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
---
 tools/vm/page-types.c | 2 --
 1 file changed, 2 deletions(-)

diff --git v4.5-rc5-mmotm-2016-02-24-16-18/tools/vm/page-types.c v4.5-rc5-mmotm-2016-02-24-16-18_patched/tools/vm/page-types.c
index dab61c3..c192baf 100644
--- v4.5-rc5-mmotm-2016-02-24-16-18/tools/vm/page-types.c
+++ v4.5-rc5-mmotm-2016-02-24-16-18_patched/tools/vm/page-types.c
@@ -633,8 +633,6 @@ static void walk_pfn(unsigned long voffset,
 	unsigned long pages;
 	unsigned long i;
 
-	memset(cgi, 0, sizeof cgi);
-
 	while (count) {
 		batch = min_t(unsigned long, count, KPAGEFLAGS_BATCH);
 		pages = kpageflags_read(buf, index, batch);
-- 
2.7.0

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] tools/vm/page-types.c: remove memset() in walk_pfn()
  2016-03-08  1:47 [PATCH v1] tools/vm/page-types.c: remove memset() in walk_pfn() Naoya Horiguchi
@ 2016-03-08  5:12 ` Konstantin Khlebnikov
  2016-03-08  5:58   ` Naoya Horiguchi
  0 siblings, 1 reply; 5+ messages in thread
From: Konstantin Khlebnikov @ 2016-03-08  5:12 UTC (permalink / raw)
  To: Naoya Horiguchi
  Cc: linux-mm, Andrew Morton, Vladimir Davydov,
	Linux Kernel Mailing List, Naoya Horiguchi

On Tue, Mar 8, 2016 at 4:47 AM, Naoya Horiguchi
<n-horiguchi@ah.jp.nec.com> wrote:
> I found that page-types is very slow and my testing shows many timeout errors.
> Here's an example with a simple program allocating 1000 thps.
>
>   $ time ./page-types -p $(pgrep -f test_alloc)
>   ...
>   real    0m17.201s
>   user    0m16.889s
>   sys     0m0.312s
>
>   $ time ./page-types.patched -p $(pgrep -f test_alloc)
>   ...
>   real    0m0.182s
>   user    0m0.046s
>   sys     0m0.135s
>
> Most of time is spent in memset(), which isn't necessary because we check
> that the return of kpagecgroup_read() is equal to pages and uninitialized
> memory is never used. So we can drop this memset().

These zeros are used in show_page_range() - for merging pages into ranges.

You could add fast-path for count=1

@@ -633,7 +633,10 @@ static void walk_pfn(unsigned long voffset,
        unsigned long pages;
        unsigned long i;

-       memset(cgi, 0, sizeof cgi);
+       if (count == 1)
+               cgi[0] = 0;
+       else
+               memset(cgi, 0, sizeof cgi);

        while (count) {
                batch = min_t(unsigned long, count, KPAGEFLAGS_BATCH);


>
> Fixes: 954e95584579 ("tools/vm/page-types.c: add memory cgroup dumping and filtering")
> Signed-off-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
> ---
>  tools/vm/page-types.c | 2 --
>  1 file changed, 2 deletions(-)
>
> diff --git v4.5-rc5-mmotm-2016-02-24-16-18/tools/vm/page-types.c v4.5-rc5-mmotm-2016-02-24-16-18_patched/tools/vm/page-types.c
> index dab61c3..c192baf 100644
> --- v4.5-rc5-mmotm-2016-02-24-16-18/tools/vm/page-types.c
> +++ v4.5-rc5-mmotm-2016-02-24-16-18_patched/tools/vm/page-types.c
> @@ -633,8 +633,6 @@ static void walk_pfn(unsigned long voffset,
>         unsigned long pages;
>         unsigned long i;
>
> -       memset(cgi, 0, sizeof cgi);
> -
>         while (count) {
>                 batch = min_t(unsigned long, count, KPAGEFLAGS_BATCH);
>                 pages = kpageflags_read(buf, index, batch);
> --
> 2.7.0
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] tools/vm/page-types.c: remove memset() in walk_pfn()
  2016-03-08  5:12 ` Konstantin Khlebnikov
@ 2016-03-08  5:58   ` Naoya Horiguchi
  2016-03-09  4:28     ` Konstantin Khlebnikov
  0 siblings, 1 reply; 5+ messages in thread
From: Naoya Horiguchi @ 2016-03-08  5:58 UTC (permalink / raw)
  To: Konstantin Khlebnikov
  Cc: linux-mm, Andrew Morton, Vladimir Davydov,
	Linux Kernel Mailing List, Naoya Horiguchi

On Tue, Mar 08, 2016 at 08:12:09AM +0300, Konstantin Khlebnikov wrote:
> On Tue, Mar 8, 2016 at 4:47 AM, Naoya Horiguchi
> <n-horiguchi@ah.jp.nec.com> wrote:
> > I found that page-types is very slow and my testing shows many timeout errors.
> > Here's an example with a simple program allocating 1000 thps.
> >
> >   $ time ./page-types -p $(pgrep -f test_alloc)
> >   ...
> >   real    0m17.201s
> >   user    0m16.889s
> >   sys     0m0.312s
> >
> >   $ time ./page-types.patched -p $(pgrep -f test_alloc)
> >   ...
> >   real    0m0.182s
> >   user    0m0.046s
> >   sys     0m0.135s
> >
> > Most of time is spent in memset(), which isn't necessary because we check
> > that the return of kpagecgroup_read() is equal to pages and uninitialized
> > memory is never used. So we can drop this memset().
> 
> These zeros are used in show_page_range() - for merging pages into ranges.

Hi Konstantin,

Thank you for the response. The below code does solve the problem, so that's fine.

But I don't understand how the zeros are used. show_page_range() is called
via add_page() which is called for i=0 to i=pages-1, and the buffer cgi is
already filled for the range [i, pages-1] by kpagecgroup_read(), so even if
without zero initialization, kpagecgroup_read() properly fills zeros, right?
IOW, is there any problem if we don't do this zero initialization?

Thanks,
Naoya Horiguchi

> You could add fast-path for count=1
> 
> @@ -633,7 +633,10 @@ static void walk_pfn(unsigned long voffset,
>         unsigned long pages;
>         unsigned long i;
> 
> -       memset(cgi, 0, sizeof cgi);
> +       if (count == 1)
> +               cgi[0] = 0;
> +       else
> +               memset(cgi, 0, sizeof cgi);
> 
>         while (count) {
>                 batch = min_t(unsigned long, count, KPAGEFLAGS_BATCH);
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] tools/vm/page-types.c: remove memset() in walk_pfn()
  2016-03-08  5:58   ` Naoya Horiguchi
@ 2016-03-09  4:28     ` Konstantin Khlebnikov
  2016-03-09  5:22       ` Naoya Horiguchi
  0 siblings, 1 reply; 5+ messages in thread
From: Konstantin Khlebnikov @ 2016-03-09  4:28 UTC (permalink / raw)
  To: Naoya Horiguchi
  Cc: linux-mm, Andrew Morton, Vladimir Davydov,
	Linux Kernel Mailing List, Naoya Horiguchi

On Tue, Mar 8, 2016 at 8:58 AM, Naoya Horiguchi
<n-horiguchi@ah.jp.nec.com> wrote:
> On Tue, Mar 08, 2016 at 08:12:09AM +0300, Konstantin Khlebnikov wrote:
>> On Tue, Mar 8, 2016 at 4:47 AM, Naoya Horiguchi
>> <n-horiguchi@ah.jp.nec.com> wrote:
>> > I found that page-types is very slow and my testing shows many timeout errors.
>> > Here's an example with a simple program allocating 1000 thps.
>> >
>> >   $ time ./page-types -p $(pgrep -f test_alloc)
>> >   ...
>> >   real    0m17.201s
>> >   user    0m16.889s
>> >   sys     0m0.312s
>> >
>> >   $ time ./page-types.patched -p $(pgrep -f test_alloc)
>> >   ...
>> >   real    0m0.182s
>> >   user    0m0.046s
>> >   sys     0m0.135s
>> >
>> > Most of time is spent in memset(), which isn't necessary because we check
>> > that the return of kpagecgroup_read() is equal to pages and uninitialized
>> > memory is never used. So we can drop this memset().
>>
>> These zeros are used in show_page_range() - for merging pages into ranges.
>
> Hi Konstantin,
>
> Thank you for the response. The below code does solve the problem, so that's fine.
>
> But I don't understand how the zeros are used. show_page_range() is called
> via add_page() which is called for i=0 to i=pages-1, and the buffer cgi is
> already filled for the range [i, pages-1] by kpagecgroup_read(), so even if
> without zero initialization, kpagecgroup_read() properly fills zeros, right?
> IOW, is there any problem if we don't do this zero initialization?

kpagecgroup_read() reads only if kpagecgroup were opened,
/proc/kpagecgroup might even not exist. Probably it's better to fill
them with zeros here.
Pre-memset was an optimization - it fills buffer only once instead on
each kpagecgroup_read() call.

>
> Thanks,
> Naoya Horiguchi
>
>> You could add fast-path for count=1
>>
>> @@ -633,7 +633,10 @@ static void walk_pfn(unsigned long voffset,
>>         unsigned long pages;
>>         unsigned long i;
>>
>> -       memset(cgi, 0, sizeof cgi);
>> +       if (count == 1)
>> +               cgi[0] = 0;
>> +       else
>> +               memset(cgi, 0, sizeof cgi);
>>
>>         while (count) {
>>                 batch = min_t(unsigned long, count, KPAGEFLAGS_BATCH);
>>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] tools/vm/page-types.c: remove memset() in walk_pfn()
  2016-03-09  4:28     ` Konstantin Khlebnikov
@ 2016-03-09  5:22       ` Naoya Horiguchi
  0 siblings, 0 replies; 5+ messages in thread
From: Naoya Horiguchi @ 2016-03-09  5:22 UTC (permalink / raw)
  To: Konstantin Khlebnikov
  Cc: Naoya Horiguchi, linux-mm, Andrew Morton, Vladimir Davydov,
	Linux Kernel Mailing List

On Wed, Mar 09, 2016 at 07:28:21AM +0300, Konstantin Khlebnikov wrote:
> On Tue, Mar 8, 2016 at 8:58 AM, Naoya Horiguchi
> <n-horiguchi@ah.jp.nec.com> wrote:
> > On Tue, Mar 08, 2016 at 08:12:09AM +0300, Konstantin Khlebnikov wrote:
> >> On Tue, Mar 8, 2016 at 4:47 AM, Naoya Horiguchi
> >> <n-horiguchi@ah.jp.nec.com> wrote:
> >> > I found that page-types is very slow and my testing shows many timeout errors.
> >> > Here's an example with a simple program allocating 1000 thps.
> >> >
> >> >   $ time ./page-types -p $(pgrep -f test_alloc)
> >> >   ...
> >> >   real    0m17.201s
> >> >   user    0m16.889s
> >> >   sys     0m0.312s
> >> >
> >> >   $ time ./page-types.patched -p $(pgrep -f test_alloc)
> >> >   ...
> >> >   real    0m0.182s
> >> >   user    0m0.046s
> >> >   sys     0m0.135s
> >> >
> >> > Most of time is spent in memset(), which isn't necessary because we check
> >> > that the return of kpagecgroup_read() is equal to pages and uninitialized
> >> > memory is never used. So we can drop this memset().
> >>
> >> These zeros are used in show_page_range() - for merging pages into ranges.
> >
> > Hi Konstantin,
> >
> > Thank you for the response. The below code does solve the problem, so that's fine.
> >
> > But I don't understand how the zeros are used. show_page_range() is called
> > via add_page() which is called for i=0 to i=pages-1, and the buffer cgi is
> > already filled for the range [i, pages-1] by kpagecgroup_read(), so even if
> > without zero initialization, kpagecgroup_read() properly fills zeros, right?
> > IOW, is there any problem if we don't do this zero initialization?
> 
> kpagecgroup_read() reads only if kpagecgroup were opened,
> /proc/kpagecgroup might even not exist. Probably it's better to fill
> them with zeros here.
> Pre-memset was an optimization - it fills buffer only once instead on
> each kpagecgroup_read() call.

Ah, OK.

So here's ver.2.

Thanks,
Naoya
---
From: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
Subject: [PATCH v2] tools/vm/page-types.c: avoid memset() in walk_pfn() when count == 1

I found that page-types is very slow and my testing shows many timeout errors.
Here's an example with a simple program allocating 1000 thps.

  $ time ./page-types -p $(pgrep -f test_alloc)
  ...
  real    0m17.201s
  user    0m16.889s
  sys     0m0.312s

Most of time is spent in memset(). Currently memset() clears over whole buffer
for every walk_pfn() call, which is inefficient when walk_pfn() is called from
walk_vma(), because in that case walk_pfn() is called for each pfn.
So this patch limits the zero initialization only for the first element.

  $ time ./page-types.patched -p $(pgrep -f test_alloc)
  ...
  real    0m0.182s
  user    0m0.046s
  sys     0m0.135s

Fixes: 954e95584579 ("tools/vm/page-types.c: add memory cgroup dumping and filtering")
Signed-off-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
Suggested-by: Konstantin Khlebnikov <koct9i@gmail.com>
---
 tools/vm/page-types.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/tools/vm/page-types.c b/tools/vm/page-types.c
index dab61c377f54..e92903fc7113 100644
--- a/tools/vm/page-types.c
+++ b/tools/vm/page-types.c
@@ -633,7 +633,15 @@ static void walk_pfn(unsigned long voffset,
 	unsigned long pages;
 	unsigned long i;
 
-	memset(cgi, 0, sizeof cgi);
+	/*
+	 * kpagecgroup_read() reads only if kpagecgroup were opened, but
+	 * /proc/kpagecgroup might even not exist, so it's better to fill
+	 * them with zeros here.
+	 */
+	if (count == 1)
+		cgi[0] = 0;
+	else
+		memset(cgi, 0, sizeof cgi);
 
 	while (count) {
 		batch = min_t(unsigned long, count, KPAGEFLAGS_BATCH);
-- 
2.4.3

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-03-09  5:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-08  1:47 [PATCH v1] tools/vm/page-types.c: remove memset() in walk_pfn() Naoya Horiguchi
2016-03-08  5:12 ` Konstantin Khlebnikov
2016-03-08  5:58   ` Naoya Horiguchi
2016-03-09  4:28     ` Konstantin Khlebnikov
2016-03-09  5:22       ` Naoya Horiguchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).