Thread: WAL: O_DIRECT and multipage-writer

WAL: O_DIRECT and multipage-writer

From
ITAGAKI Takahiro
Date:
Hello, all.

I think that there is room for improvement in WAL.
Here is a patch for it.
  - Multiple pages are written in one write() if it is contiguous.
  - Add 'open_direct' to wal_sync_method.

WAL writer writes one page in one write(). This is not efficient
when wal_sync_method is 'open_sync', because the writer waits for
IO completions at each write(). Multipage-writer can reduce syscalls
and improve IO throughput.

'open_direct' uses O_DIRECT instead of O_SYNC. O_DIRECT implies synchronous
writing, so it may show the tendency like open_sync. But maybe it can reduce
memcpy() and save OS's disk cache memory.

I benchmarked this patch with pgbench. It works well and
improved 50% of tps on my machine. WAL seems to be bottle-neck
on machines with poor disks.

This patch has not yet tested enough. I would like it to be examined much
and taken into PostgreSQL.

There are still many TODOs:
  * Is this logic really correct?
  - O_DIRECT_BUFFER_ALIGN should be adjusted to runtime, not compile time.
  - Consider to use writev() instead of write().
    Buffers are noncontiguous when WAL ring buffer rotates.
  - If wan_sync_method is not open_direct, XLOG_EXTRA_BUFFERS can be 0.


Sincerely,
ITAGAKI Takahiro



-- pgbench result --

$ ./pgbench -s 100 -c 50 -t 400

- 8.0.0 default + fsync:
    tps = 20.630632 (including connections establishing)
    tps = 20.636768 (excluding connections establishing)
- multipage-writer + open_direct:
    tps = 33.761917 (including connections establishing)
    tps = 33.778320 (excluding connections establishing)

Environment:
  OS     : Linux kernel 2.6.9
  CPU    : Pentium 4 3GHz
  disk   : ATA 5400rpm (Data and WAL are placed on same partition.)
  memory : 1GB
  config : shared_buffers=10000, wal_buffers=256,
           XLOG_SEG_SIZE=256MB, checkpoint_segment=4

---
ITAGAKI Takahiro <itagaki.takahiro@lab.ntt.co.jp>
NTT Cyber Space Laboratories
Nippon Telegraph and Telephone Corporation.

Attachment

Re: WAL: O_DIRECT and multipage-writer

From
"Michael Paesold"
Date:
ITAGAKI Takahiro wrote:

> I think that there is room for improvement in WAL.
> Here is a patch for it.

I think you should resend your patch as a context diff (diff -c). Otherwise
it's hard to see what your patch does.

Best Regards,
Michael Paesold


Re: WAL: O_DIRECT and multipage-writer

From
ITAGAKI Takahiro
Date:
Excuse me.
I resend the patch with diff -c.


On Tue, 25 Jan 2005 10:30:01 +0100
"Michael Paesold" <mpaesold@gmx.at> wrote:

> ITAGAKI Takahiro wrote:
>
> > I think that there is room for improvement in WAL.
> > Here is a patch for it.
>
> I think you should resend your patch as a context diff (diff -c). Otherwise
> it's hard to see what your patch does.

---
ITAGAKI Takahiro <itagaki.takahiro@lab.ntt.co.jp>
NTT Cyber Space Laboratories
Nippon Telegraph and Telephone Corporation.

Re: WAL: O_DIRECT and multipage-writer

From
Tom Lane
Date:
ITAGAKI Takahiro <itagaki.takahiro@lab.ntt.co.jp> writes:
> I resend the patch with diff -c.

What does XLOG_EXTRA_BUFFERS accomplish?

Also, I'm worried that you broke something by not updating
Write->curridx immediately in XLogWrite.  There certainly isn't going
to be any measurable performance boost from keeping that in a local
variable, so why take any risk?

            regards, tom lane

Re: [HACKERS] WAL: O_DIRECT and multipage-writer

From
Mark Wong
Date:
Hi everyone,

I gave this a try with DBT-2, but got a core dump on our ia64 system.
I hope this isn't a random thing, like I ran into previously.  Maybe
I'll try again, but postgres dumped core.  Binary and core here:
    http://developer.osdl.org/markw/pgsql/core/2morefiles.tar.bz2

#0  FunctionCall2 (flinfo=0x0, arg1=0, arg2=0) at fmgr.c:1141
1141            result = FunctionCallInvoke(&fcinfo);
(gdb) bt
#0  FunctionCall2 (flinfo=0x0, arg1=0, arg2=0) at fmgr.c:1141
#1  0x40000000003bdb80 in FunctionCall2 (flinfo=Cannot access memory at address 0x0
) at fmgr.c:1141
#2  0x40000000003bdb80 in FunctionCall2 (flinfo=Cannot access memory at address 0x0
) at fmgr.c:1141

Over and over again, so I'll keep the backtrace short.

Mark

Re: [HACKERS] WAL: O_DIRECT and multipage-writer

From
ITAGAKI Takahiro
Date:
Thanks for testing, Mark!

> I gave this a try with DBT-2, but got a core dump on our ia64 system.
> I hope this isn't a random thing, like I ran into previously.  Maybe
> I'll try again, but postgres dumped core.

Sorry, this seems to be my patch's bug.
Which datatype did you compile with? LP64, ILP64, or LLP64?
If you used LLP64, I think the cause is buffer alignment routine
because of sizeof(long) != sizeof(void*).

I'll fix it soon...


ITAGAKI Takahiro


Re: [HACKERS] WAL: O_DIRECT and multipage-writer

From
Mark Wong
Date:
Hmm... I don't remember specifying a datatype.  I suppose whatever the
default one is. :)

I'll be happy to test again, just let me know.

Mark

On Fri, Jan 28, 2005 at 06:28:32AM +0900, ITAGAKI Takahiro wrote:
> Thanks for testing, Mark!
>
> > I gave this a try with DBT-2, but got a core dump on our ia64 system.
> > I hope this isn't a random thing, like I ran into previously.  Maybe
> > I'll try again, but postgres dumped core.
>
> Sorry, this seems to be my patch's bug.
> Which datatype did you compile with? LP64, ILP64, or LLP64?
> If you used LLP64, I think the cause is buffer alignment routine
> because of sizeof(long) != sizeof(void*).
>
> I'll fix it soon...
>
>
> ITAGAKI Takahiro

Re: [HACKERS] WAL: O_DIRECT and multipage-writer (+ memory leak)

From
ITAGAKI Takahiro
Date:
Hello everyone.

I fixed two bugs in the patch that I sent before.
Check and test new one, please.

1. Fix update timing of Write->curridx. (pointed by Tom)
     Change to update it soon after write().

2. Fix buffer alignment routine on 64bit cpu. (pointed by Mark)
     I checked it on Xeon EM64T and it worked properly, but I don't have IA64...


BTW, I found memory leak in BootStrapXLOG(). The buffer allocated by malloc()
is not free()ed. ISSUE_BOOTSTRAP_MEMORYLEAK in this patch points out it.
(But this leak is not serious, because this function is called only once.)


ITAGAKI Takahiro

Attachment

Re: [HACKERS] WAL: O_DIRECT and multipage-writer

From
Bruce Momjian
Date:
This thread has been saved for the 8.1 release:

    http://momjian.postgresql.org/cgi-bin/pgpatches2

---------------------------------------------------------------------------

ITAGAKI Takahiro wrote:
> Hello, all.
>
> I think that there is room for improvement in WAL.
> Here is a patch for it.
>   - Multiple pages are written in one write() if it is contiguous.
>   - Add 'open_direct' to wal_sync_method.
>
> WAL writer writes one page in one write(). This is not efficient
> when wal_sync_method is 'open_sync', because the writer waits for
> IO completions at each write(). Multipage-writer can reduce syscalls
> and improve IO throughput.
>
> 'open_direct' uses O_DIRECT instead of O_SYNC. O_DIRECT implies synchronous
> writing, so it may show the tendency like open_sync. But maybe it can reduce
> memcpy() and save OS's disk cache memory.
>
> I benchmarked this patch with pgbench. It works well and
> improved 50% of tps on my machine. WAL seems to be bottle-neck
> on machines with poor disks.
>
> This patch has not yet tested enough. I would like it to be examined much
> and taken into PostgreSQL.
>
> There are still many TODOs:
>   * Is this logic really correct?
>   - O_DIRECT_BUFFER_ALIGN should be adjusted to runtime, not compile time.
>   - Consider to use writev() instead of write().
>     Buffers are noncontiguous when WAL ring buffer rotates.
>   - If wan_sync_method is not open_direct, XLOG_EXTRA_BUFFERS can be 0.
>
>
> Sincerely,
> ITAGAKI Takahiro
>
>
>
> -- pgbench result --
>
> $ ./pgbench -s 100 -c 50 -t 400
>
> - 8.0.0 default + fsync:
>     tps = 20.630632 (including connections establishing)
>     tps = 20.636768 (excluding connections establishing)
> - multipage-writer + open_direct:
>     tps = 33.761917 (including connections establishing)
>     tps = 33.778320 (excluding connections establishing)
>
> Environment:
>   OS     : Linux kernel 2.6.9
>   CPU    : Pentium 4 3GHz
>   disk   : ATA 5400rpm (Data and WAL are placed on same partition.)
>   memory : 1GB
>   config : shared_buffers=10000, wal_buffers=256,
>            XLOG_SEG_SIZE=256MB, checkpoint_segment=4
>
> ---
> ITAGAKI Takahiro <itagaki.takahiro@lab.ntt.co.jp>
> NTT Cyber Space Laboratories
> Nippon Telegraph and Telephone Corporation.

[ Attachment, skipping... ]

>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
>                http://www.postgresql.org/docs/faq

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

Re: [HACKERS] WAL: O_DIRECT and multipage-writer (+ memory

From
Bruce Momjian
Date:
This has been saved for the 8.1 release:

    http://momjian.postgresql.org/cgi-bin/pgpatches2

---------------------------------------------------------------------------

ITAGAKI Takahiro wrote:
> Hello everyone.
>
> I fixed two bugs in the patch that I sent before.
> Check and test new one, please.
>
> 1. Fix update timing of Write->curridx. (pointed by Tom)
>      Change to update it soon after write().
>
> 2. Fix buffer alignment routine on 64bit cpu. (pointed by Mark)
>      I checked it on Xeon EM64T and it worked properly, but I don't have IA64...
>
>
> BTW, I found memory leak in BootStrapXLOG(). The buffer allocated by malloc()
> is not free()ed. ISSUE_BOOTSTRAP_MEMORYLEAK in this patch points out it.
> (But this leak is not serious, because this function is called only once.)
>
>
> ITAGAKI Takahiro

[ Attachment, skipping... ]

>
> ---------------------------(end of broadcast)---------------------------
> TIP 7: don't forget to increase your free space map settings

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

Re: [HACKERS] WAL: O_DIRECT and multipage-writer (+ memory leak)

From
Mark Wong
Date:
On Thu, Feb 03, 2005 at 07:25:55PM +0900, ITAGAKI Takahiro wrote:
> Hello everyone.
>
> I fixed two bugs in the patch that I sent before.
> Check and test new one, please.

Ok, finally got back into the office and was able to run 1 set of
tests.

So the new baseline result with 8.0.1:
        http://www.osdl.org/projects/dbt2dev/results/dev4-010/309/
        Throughput: 3639.97

Results with the patch but open_direct not set:
        http://www.osdl.org/projects/dbt2dev/results/dev4-010/308/
        Throughput: 3494.72

Results with the patch and open_direct set:
        http://www.osdl.org/projects/dbt2dev/results/dev4-010/312/
        Throughput: 3489.69

You can verify that the wall_sync_method is set to open_direct under
the "database parameters" link, but I'm wondering if I missed
something.  It looks a little odd the the performance dropped.

Mark

Re: [HACKERS] WAL: O_DIRECT and multipage-writer (+

From
Simon Riggs
Date:
On Tue, 2005-03-01 at 13:53 -0800, Mark Wong wrote:
> On Thu, Feb 03, 2005 at 07:25:55PM +0900, ITAGAKI Takahiro wrote:
> > Hello everyone.
> >
> > I fixed two bugs in the patch that I sent before.
> > Check and test new one, please.
>
> Ok, finally got back into the office and was able to run 1 set of
> tests.
>
> So the new baseline result with 8.0.1:
>         http://www.osdl.org/projects/dbt2dev/results/dev4-010/309/
>         Throughput: 3639.97
>
> Results with the patch but open_direct not set:
>         http://www.osdl.org/projects/dbt2dev/results/dev4-010/308/
>         Throughput: 3494.72
>
> Results with the patch and open_direct set:
>         http://www.osdl.org/projects/dbt2dev/results/dev4-010/312/
>         Throughput: 3489.69
>
> You can verify that the wall_sync_method is set to open_direct under
> the "database parameters" link, but I'm wondering if I missed
> something.  It looks a little odd the the performance dropped.

Is there anything more to say on this?
Is it case-closed, or is there further work underway - I can't see any
further chat on this thread.

These results show it doesn't work better on larger systems. The
original testing showed it worked better on smaller systems - is there
still scope to include this for smaller configs?

If not, thanks for taking the time to write the patch and investigate
whether changes in this area would help. Not every performance patch
improves things, but that doesn't mean we shouldn't try...

Best Regards, Simon Riggs



Re: [HACKERS] WAL: O_DIRECT and multipage-writer

From
Mark Wong
Date:
On Tue, Jan 25, 2005 at 06:06:23PM +0900, ITAGAKI Takahiro wrote:
> Environment:
>   OS     : Linux kernel 2.6.9
>   CPU    : Pentium 4 3GHz
>   disk   : ATA 5400rpm (Data and WAL are placed on same partition.)
>   memory : 1GB
>   config : shared_buffers=10000, wal_buffers=256,
>            XLOG_SEG_SIZE=256MB, checkpoint_segment=4

Hi Itagaki,

In light of this thread, have you compared the performance on
Linux-2.4?

Direct io on block device has performance regression on 2.6.x kernel
http://www.ussg.iu.edu/hypermail/linux/kernel/0503.1/0328.html

Mark

Re: [HACKERS] WAL: O_DIRECT and multipage-writer (+

From
Bruce Momjian
Date:
Yes, I assume that the patch to group the writes isn't something we want
right now, and the one for O_DIRECT is going to need an additional
fsync, and I have asked for testing on that.

I have posted a patch that I think fixes the memory leak reported and am
waiting for feedback on that.

---------------------------------------------------------------------------

Simon Riggs wrote:
> On Tue, 2005-03-01 at 13:53 -0800, Mark Wong wrote:
> > On Thu, Feb 03, 2005 at 07:25:55PM +0900, ITAGAKI Takahiro wrote:
> > > Hello everyone.
> > >
> > > I fixed two bugs in the patch that I sent before.
> > > Check and test new one, please.
> >
> > Ok, finally got back into the office and was able to run 1 set of
> > tests.
> >
> > So the new baseline result with 8.0.1:
> >         http://www.osdl.org/projects/dbt2dev/results/dev4-010/309/
> >         Throughput: 3639.97
> >
> > Results with the patch but open_direct not set:
> >         http://www.osdl.org/projects/dbt2dev/results/dev4-010/308/
> >         Throughput: 3494.72
> >
> > Results with the patch and open_direct set:
> >         http://www.osdl.org/projects/dbt2dev/results/dev4-010/312/
> >         Throughput: 3489.69
> >
> > You can verify that the wall_sync_method is set to open_direct under
> > the "database parameters" link, but I'm wondering if I missed
> > something.  It looks a little odd the the performance dropped.
>
> Is there anything more to say on this?
> Is it case-closed, or is there further work underway - I can't see any
> further chat on this thread.
>
> These results show it doesn't work better on larger systems. The
> original testing showed it worked better on smaller systems - is there
> still scope to include this for smaller configs?
>
> If not, thanks for taking the time to write the patch and investigate
> whether changes in this area would help. Not every performance patch
> improves things, but that doesn't mean we shouldn't try...
>
> Best Regards, Simon Riggs
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
>

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

Re: [HACKERS] WAL: O_DIRECT and multipage-writer (+ memory

From
Bruce Momjian
Date:
ITAGAKI Takahiro wrote:
> Hello everyone.
>
> I fixed two bugs in the patch that I sent before.
> Check and test new one, please.
>
> 1. Fix update timing of Write->curridx. (pointed by Tom)
>      Change to update it soon after write().
>
> 2. Fix buffer alignment routine on 64bit cpu. (pointed by Mark)
>      I checked it on Xeon EM64T and it worked properly, but I don't have IA64...
>
>
> BTW, I found memory leak in BootStrapXLOG(). The buffer allocated by malloc()
> is not free()ed. ISSUE_BOOTSTRAP_MEMORYLEAK in this patch points out it.
> (But this leak is not serious, because this function is called only once.)

Does the following patch fix the memory leak you described?

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/backend/access/transam/xlog.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/access/transam/xlog.c,v
retrieving revision 1.195
diff -c -c -r1.195 xlog.c
*** src/backend/access/transam/xlog.c    2 Jun 2005 05:55:28 -0000    1.195
--- src/backend/access/transam/xlog.c    5 Jun 2005 03:38:23 -0000
***************
*** 3754,3759 ****
--- 3754,3760 ----
      BootStrapCLOG();
      BootStrapSUBTRANS();
      BootStrapMultiXact();
+     free(buffer);
  }

  static char *

Re: [HACKERS] WAL: O_DIRECT and multipage-writer (+ memory

From
Tom Lane
Date:
Bruce Momjian <pgman@candle.pha.pa.us> writes:
>> BTW, I found memory leak in BootStrapXLOG(). The buffer allocated by malloc()
>> is not free()ed. ISSUE_BOOTSTRAP_MEMORYLEAK in this patch points out it.
>> (But this leak is not serious, because this function is called only once.)

> Does the following patch fix the memory leak you described?

You realize this is a waste of code, no?  It's not like the bootstrap
subprocess frees every single bit of memory it ever allocates, and even
less like it'd be profitable to try to make it do so ... the memory
will go away anyway when the subprocess exits.

            regards, tom lane

Memory leak in BootStrapXLOG()

From
ITAGAKI Takahiro
Date:
Bruce Momjian <pgman@candle.pha.pa.us> wrote:

> > BTW, I found memory leak in BootStrapXLOG(). The buffer allocated by malloc()
> > is not free()ed. ISSUE_BOOTSTRAP_MEMORYLEAK in this patch points out it.
> > (But this leak is not serious, because this function is called only once.)
>
> Does the following patch fix the memory leak you described?

Yes, the revised patch has no leak by using stack instead of malloc().
This leak is trivial, but anyway direct io needs an aligned buffer.
IMHO any of the following is ok.

[A] 1st patch
char *buffer;
void* buffer0;
buffer0 = malloc(BLCKSZ + XLOG_EXTRA_BUFFERS);
buffer = (char *) XLOG_BUFFERS_ALIGN(buffer0);
free(buffer0);

[B] 2nd patch
char *buffer;
char  buffer0[BLCKSZ + XLOG_EXTRA_BUFFERS + MAXIMUM_ALIGNOF];
buffer = XLOG_BUFFERS_ALIGN(buffer0);

[C] following code is simple if we don't care the memory leak.
char *buffer;
buffer = XLOG_BUFFERS_ALIGN( malloc(BLCKSZ + XLOG_EXTRA_BUFFERS) );


---
ITAGAKI Takahiro
NTT Cyber Space Laboratories



Re: [HACKERS] WAL: O_DIRECT and multipage-writer (+ memory

From
Bruce Momjian
Date:
Tom Lane wrote:
> Bruce Momjian <pgman@candle.pha.pa.us> writes:
> >> BTW, I found memory leak in BootStrapXLOG(). The buffer allocated by malloc()
> >> is not free()ed. ISSUE_BOOTSTRAP_MEMORYLEAK in this patch points out it.
> >> (But this leak is not serious, because this function is called only once.)
>
> > Does the following patch fix the memory leak you described?
>
> You realize this is a waste of code, no?  It's not like the bootstrap
> subprocess frees every single bit of memory it ever allocates, and even
> less like it'd be profitable to try to make it do so ... the memory
> will go away anyway when the subprocess exits.

I guess, but the person reported a leak so I figured I would fix it.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

Re: [HACKERS] WAL: O_DIRECT and multipage-writer (+ memory

From
Tom Lane
Date:
Bruce Momjian <pgman@candle.pha.pa.us> writes:
> Tom Lane wrote:
>> You realize this is a waste of code, no?

> I guess, but the person reported a leak so I figured I would fix it.

I don't really care whether there's a free() there or not --- what
bothers me is that calling it a leak shows a fundamental
misunderstanding of the environment in which that code runs, which
is likely to lead to proposed changes that have less trivial
consequences.

            regards, tom lane