Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 4 Apr 2012 23:18:25 +0200
From:      Svatopluk Kraus <onwahe@gmail.com>
To:        Konstantin Belousov <kostikbel@gmail.com>
Cc:        hackers@freebsd.org
Subject:   Re: [vfs] buf_daemon() slows down write() severely on low-speed CPU
Message-ID:  <CAFHCsPUbSqRhB6qM9EOVg=tFZA7vpPhwUi3%2BWFRxUFyp_X0%2BKg@mail.gmail.com>
In-Reply-To: <20120321203828.GW2358@deviant.kiev.zoral.com.ua>
References:  <CAFHCsPVqNCYj-obQqS4iyKR-xK0AaRJU_6KX=fccEK4U8NaktQ@mail.gmail.com> <20120312181921.GF75778@deviant.kiev.zoral.com.ua> <CAFHCsPWZD065A0su_LJn8Q4RW1pft_DobbsgSph1NNZ=mNXYYw@mail.gmail.com> <20120315112959.GP75778@deviant.kiev.zoral.com.ua> <CAFHCsPXZ7MmaVpfN0H%2BaY5ubvY3L_guN2ugUOZ-wEnhKuJV=tw@mail.gmail.com> <20120321203828.GW2358@deviant.kiev.zoral.com.ua>

next in thread | previous in thread | raw e-mail | index | archive | help
2012/3/21 Konstantin Belousov <kostikbel@gmail.com>:
> On Thu, Mar 15, 2012 at 08:00:41PM +0100, Svatopluk Kraus wrote:
>> 2012/3/15 Konstantin Belousov <kostikbel@gmail.com>:
>> > On Tue, Mar 13, 2012 at 01:54:38PM +0100, Svatopluk Kraus wrote:
>> >> On Mon, Mar 12, 2012 at 7:19 PM, Konstantin Belousov
>> >> <kostikbel@gmail.com> wrote:
>> >> > On Mon, Mar 12, 2012 at 04:00:58PM +0100, Svatopluk Kraus wrote:
>> >> >> Hi,
>> >> >>
>> >> >> =A0 =A0I have solved a following problem. If a big file (according=
 to
>> >> >> 'hidirtybuffers') is being written, the write speed is very poor.
>> >> >>
>> >> >> =A0 =A0It's observed on system with elan 486 and 32MB RAM (i.e., l=
ow speed
>> >> >> CPU and not too much memory) running FreeBSD-9.
>> >> >>
>> >> >> =A0 =A0Analysis: A file is being written. All or almost all dirty =
buffers
>> >> >> belong to the file. The file vnode is almost all time locked by
>> >> >> writing process. The buf_daemon() can not flush any dirty buffer a=
s a
>> >> >> chance to acquire the file vnode lock is very low. A number of dir=
ty
>> >> >> buffers grows up very slow and with each new dirty buffer slower,
>> >> >> because buf_daemon() eats more and more CPU time by looping on dir=
ty
>> >> >> buffers queue (with very low or no effect).
>> >> >>
>> >> >> =A0 =A0This slowing down effect is started by buf_daemon() itself,=
 when
>> >> >> 'numdirtybuffers' reaches 'lodirtybuffers' threshold and buf_daemo=
n()
>> >> >> is waked up by own timeout. The timeout fires at 'hz' period, but
>> >> >> starts to fire at 'hz/10' immediately as buf_daemon() fails to rea=
ch
>> >> >> 'lodirtybuffers' threshold. When 'numdirtybuffers' (now slowly)
>> >> >> reaches ((lodirtybuffers + hidirtybuffers) / 2) threshold, the
>> >> >> buf_daemon() can be waked up within bdwrite() too and it's much wo=
rse.
>> >> >> Finally and with very slow speed, the 'hidirtybuffers' or
>> >> >> 'dirtybufthresh' is reached, the dirty buffers are flushed, and
>> >> >> everything starts from beginning...
>> >> > Note that for some time, bufdaemon work is distributed among bufdae=
mon
>> >> > thread itself and any thread that fails to allocate a buffer, esp.
>> >> > a thread that owns vnode lock and covers long queue of dirty buffer=
s.
>> >>
>> >> However, the problem starts when numdirtybuffers reaches
>> >> lodirtybuffers count and ends around hidirtybuffers count. There are
>> >> still plenty of free buffers in system.
>> >>
>> >> >>
>> >> >> =A0 =A0On the system, a buffer size is 512 bytes and the default
>> >> >> thresholds are following:
>> >> >>
>> >> >> =A0 =A0vfs.hidirtybuffers =3D 134
>> >> >> =A0 =A0vfs.lodirtybuffers =3D 67
>> >> >> =A0 =A0vfs.dirtybufthresh =3D 120
>> >> >>
>> >> >> =A0 =A0For example, a 2MB file is copied into flash disk in about =
3
>> >> >> minutes and 15 second. If dirtybufthresh is set to 40, the copy ti=
me
>> >> >> is about 20 seconds.
>> >> >>
>> >> >> =A0 =A0My solution is a mix of three things:
>> >> >> =A0 =A01. Suppresion of buf_daemon() wakeup by setting bd_request =
to 1 in
>> >> >> the main buf_daemon() loop.
>> >> > I cannot understand this. Please provide a patch that shows what do
>> >> > you mean there.
>> >> >
>> >> =A0 =A0 =A0 curthread->td_pflags |=3D TDP_NORUNNINGBUF | TDP_BUFNEED;
>> >> =A0 =A0 =A0 mtx_lock(&bdlock);
>> >> =A0 =A0 =A0 for (;;) {
>> >> - =A0 =A0 =A0 =A0 =A0 =A0 bd_request =3D 0;
>> >> + =A0 =A0 =A0 =A0 =A0 =A0 bd_request =3D 1;
>> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 mtx_unlock(&bdlock);
>> > Is this a complete patch ? The change just causes lost wakeups for buf=
daemon,
>> > nothing more.
>> Yes, it's a complete patch. And exactly, it causes lost wakeups which ar=
e:
>> 1. !! UNREASONABLE !!, because bufdaemon is not sleeping,
>> 2. not wanted, because it looks that it's correct behaviour for the
>> sleep with hz/10 period. However, if the sleep with hz/10 period is
>> expected to be waked up by bd_wakeup(), then bd_request should be set
>> to 0 just before sleep() call, and then bufdaemon behaviour will be
>> clear.
> No, your description is wrong.
>
> If bufdaemon is unable to flush enough buffers and numdirtybuffers still
> greater then lodirtybuffers, then bufdaemon enters qsleep state
> without resetting bd_request, with timeouts of one tens of second.
> Your patch will cause all wakeups for this case to be lost. This is
> exactly the situation when we want bufdaemon to run harder to avoid
> possible deadlocks, not to slow down.

OK. Let's focus to bufdaemon implementation. Now, qsleep state is
entered with random bd_request value. If someone calls bd_wakeup()
during bufdaemon iteration over dirty buffers queues, then bd_request
is set to 1. Otherwise, bd_request remains 0. I.e., sometimes qsleep
state only can be timeouted, sometimes it can be waked up by
bd_wakeup(). So, this random behaviour is what is wanted?

>> All stuff around bd_request and bufdaemon sleep is under bd_lock, so
>> if bd_request is 0 and bufdaemon is not sleeping, then all wakeups are
>> unreasonable! The patch is about that mainly.
> Wakeups itself are very cheap for the running process. Mostly, it comes
> down to locking sleepq and waking all threads that are present in the
> sleepq blocked queue. If there is no threads in queue, nothing is done.

Are you serious? Is spin mutex really cheap? Many calls are cheap, but
they are not any matter where.

Svata



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAFHCsPUbSqRhB6qM9EOVg=tFZA7vpPhwUi3%2BWFRxUFyp_X0%2BKg>