Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 12 Mar 2012 16:00:58 +0100
From:      Svatopluk Kraus <onwahe@gmail.com>
To:        hackers@freebsd.org
Subject:   [vfs] buf_daemon() slows down write() severely on low-speed CPU
Message-ID:  <CAFHCsPVqNCYj-obQqS4iyKR-xK0AaRJU_6KX=fccEK4U8NaktQ@mail.gmail.com>

next in thread | raw e-mail | index | archive | help
Hi,

   I have solved a following problem. If a big file (according to
'hidirtybuffers') is being written, the write speed is very poor.

   It's observed on system with elan 486 and 32MB RAM (i.e., low speed
CPU and not too much memory) running FreeBSD-9.

   Analysis: 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 as a
chance to acquire the file vnode lock is very low. A number of dirty
buffers grows up very slow and with each new dirty buffer slower,
because buf_daemon() eats more and more CPU time by looping on dirty
buffers queue (with very low or no effect).

   This slowing down effect is started by buf_daemon() itself, when
'numdirtybuffers' reaches 'lodirtybuffers' threshold and buf_daemon()
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 reach
'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 worse.
Finally and with very slow speed, the 'hidirtybuffers' or
'dirtybufthresh' is reached, the dirty buffers are flushed, and
everything starts from beginning...

   On the system, a buffer size is 512 bytes and the default
thresholds are following:

   vfs.hidirtybuffers = 134
   vfs.lodirtybuffers = 67
   vfs.dirtybufthresh = 120

   For example, a 2MB file is copied into flash disk in about 3
minutes and 15 second. If dirtybufthresh is set to 40, the copy time
is about 20 seconds.

   My solution is a mix of three things:
   1. Suppresion of buf_daemon() wakeup by setting bd_request to 1 in
the main buf_daemon() loop.
   2. Increment of buf_daemon() fast timeout from hz/10 to hz/4.
   3. Tuning dirtybufthresh to (((lodirtybuffers + hidirtybuffers) /
2) - 15) magic.

   The mention copy time is about 30 seconds now.

   The described problem is just for information to anyone who can be
interested in. Comments are welcome. However, the bd_request thing is
more general.

   bd_request (despite its description) should be 0 only when
buf_daemon() is in sleep(). Otherwise, wakeup() on &bd_request channel
is useless. Therefore, setting bd_request to 1 in the main
buf_daemon() loop is correct and better as it saves time spent by
wakeup() on not existing channel.

          Svata



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAFHCsPVqNCYj-obQqS4iyKR-xK0AaRJU_6KX=fccEK4U8NaktQ>