You are not logged in.

#1 2007-08-27 10:41:22

leo2501
Member
From: Buenos Aires, Argentina
Registered: 2007-07-07
Posts: 658

pdflush hanging disks when disk activity is high

Hi everyone! my "problem" is that when im copying or moving larga amounts of data between my partitions/disks my the system response is right, but the apps who show or access those disks hangs for a few seconds including the copy/moving itself, theres a lot of pdflush process that's what conky and htop show me when those little hangs appear... dont know if it has something to do, but the disk is a sata drive inside a usb case formatted as ext3... copying from that disk from the same disk or any other inside mi box (1 80gb IDE drive) cause those pdflush process to appear...

1. is this normal?

2. what is pdflush?

3. what it do exactly?

thanks!!!

EDIT: forgot to mention... cpu activity during those mini hangs is low... 5%-10% or less cpu usage... in a athlon xp 2600+ cpu... and pdflush processes are 0% using cpu... there are 5 or more of those pdflush...

Last edited by leo2501 (2007-08-27 10:44:24)


Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery

Offline

#2 2007-08-27 12:27:35

leo2501
Member
From: Buenos Aires, Argentina
Registered: 2007-07-07
Posts: 658

Re: pdflush hanging disks when disk activity is high

HI AGAIN! i found this but barely understand it... i think it have to do with the number of active "pdflush" processes at a time?... neutral

//************************************************************************************

http://lwn.net/Articles/9521/

pdflush congestion avoidance

From:          Andrew Morton <akpm@digeo.com>
To:          lkml <linux-kernel@vger.kernel.org>
Subject:          [patch] pdflush congestion avoidance
Date:          Wed, 11 Sep 2002 01:30:58 -0700


- Add the `nonblocking' flag to struct writeback_control, and teach
  the writeback paths to honour it.

- Add the `encountered_congestion' flag to struct writeback_control
  and teach the writeback paths to set it.


So as soon as a mapping's backing_dev_info indicates that it is getting
congested, bale out of writeback.  And don't even start writeback
against filesystems whose queues are congested.

- Convert pdflush's background_writeback() function to use
  nonblocking writeback.

This way, a single pdflush thread will circulate around all the
dirty queues, keeping them filled.

- Convert the pdlfush `kupdate' function to do the same thing.


This solves the problem of pdflush thread pool exhaustion.

It solves the problem of pdflush startup latency.

It solves the (minor) problem wherein `kupdate' writeback only writes
back a single disk at a time (it was getting blocked on each queue in
turn).

It probably means that we only ever need a single pdflush thread.




fs/fs-writeback.c         |   40 ++++++++++++++++++++++------------------
fs/mpage.c                |    7 +++++++
include/linux/writeback.h |    2 ++
mm/page-writeback.c       |   37 +++++++++++++++++++++++++++++--------
4 files changed, 60 insertions(+), 26 deletions(-)

--- 2.5.34/fs/mpage.c~nonblocking-pdflush    Tue Sep 10 00:00:20 2002
+++ 2.5.34-akpm/fs/mpage.c    Tue Sep 10 00:00:20 2002
@@ -20,6 +20,7 @@
#include <linux/prefetch.h>
#include <linux/mpage.h>
#include <linux/writeback.h>
+#include <linux/backing-dev.h>
#include <linux/pagevec.h>

/*
@@ -530,6 +531,7 @@ int
mpage_writepages(struct address_space *mapping,
        struct writeback_control *wbc, get_block_t get_block)
{
+    struct backing_dev_info *bdi = mapping->backing_dev_info;
    struct bio *bio = NULL;
    sector_t last_block_in_bio = 0;
    int ret = 0;
@@ -593,6 +595,11 @@ mpage_writepages(struct address_space *m
            }
            if (ret || (--(wbc->nr_to_write) <= 0))
                done = 1;
+            if (wbc->nonblocking && bdi_write_congested(bdi)) {
+                blk_run_queues();
+                wbc->encountered_congestion = 1;
+                done = 1;
+            }
        } else {
            unlock_page(page);
        }
--- 2.5.34/include/linux/writeback.h~nonblocking-pdflush    Tue Sep 10 00:00:20 2002
+++ 2.5.34-akpm/include/linux/writeback.h    Tue Sep 10 00:00:20 2002
@@ -43,6 +43,8 @@ struct writeback_control {
                       older than this */
    long nr_to_write;        /* Write this many pages, and decrement
                       this for each page written */
+    int nonblocking;        /* Don't get stuck on request queues */
+    int encountered_congestion;    /* An output: a queue is full */
};
   
void writeback_inodes(struct writeback_control *wbc);
--- 2.5.34/mm/page-writeback.c~nonblocking-pdflush    Tue Sep 10 00:00:20 2002
+++ 2.5.34-akpm/mm/page-writeback.c    Tue Sep 10 00:00:20 2002
@@ -21,6 +21,7 @@
#include <linux/init.h>
#include <linux/sysrq.h>
#include <linux/backing-dev.h>
+#include <linux/blkdev.h>
#include <linux/mpage.h>
#include <linux/notifier.h>
#include <linux/smp.h>
@@ -172,21 +173,30 @@ static void background_writeout(unsigned
        .sync_mode    = WB_SYNC_NONE,
        .older_than_this = NULL,
        .nr_to_write    = 0,
+        .nonblocking    = 1,
    };

    CHECK_EMERGENCY_SYNC

    background_thresh = (dirty_background_ratio * total_pages) / 100;
-
-    do {
+    for ( ; ; ) {
        struct page_state ps;
+
        get_page_state(&ps);
        if (ps.nr_dirty < background_thresh && min_pages <= 0)
            break;
+        wbc.encountered_congestion = 0;
        wbc.nr_to_write = MAX_WRITEBACK_PAGES;
        writeback_inodes(&wbc);
        min_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
-    } while (wbc.nr_to_write <= 0);
+        if (wbc.nr_to_write == MAX_WRITEBACK_PAGES) {
+            /* Wrote nothing */
+            if (wbc.encountered_congestion)
+                blk_congestion_wait(WRITE, HZ/10);
+            else
+                break;
+        }
+    }
    blk_run_queues();
}

@@ -223,25 +233,36 @@ static void wb_kupdate(unsigned long arg
    unsigned long oldest_jif;
    unsigned long start_jif;
    unsigned long next_jif;
+    long nr_to_write;
    struct page_state ps;
    struct writeback_control wbc = {
        .bdi        = NULL,
        .sync_mode    = WB_SYNC_NONE,
        .older_than_this = &oldest_jif,
        .nr_to_write    = 0,
+        .nonblocking    = 1,
    };

    sync_supers();
-    get_page_state(&ps);

+    get_page_state(&ps);
    oldest_jif = jiffies - (dirty_expire_centisecs * HZ) / 100;
    start_jif = jiffies;
    next_jif = start_jif + (dirty_writeback_centisecs * HZ) / 100;
-    wbc.nr_to_write = ps.nr_dirty;
-    writeback_inodes(&wbc);
+    nr_to_write = ps.nr_dirty;
+    while (nr_to_write > 0) {
+        wbc.encountered_congestion = 0;
+        wbc.nr_to_write = MAX_WRITEBACK_PAGES;
+        writeback_inodes(&wbc);
+        if (wbc.nr_to_write == MAX_WRITEBACK_PAGES) {
+            if (wbc.encountered_congestion)
+                blk_congestion_wait(WRITE, HZ);
+            else
+                break;    /* All the old data is written */
+        }
+        nr_to_write -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
+    }
    blk_run_queues();
-    yield();
-
    if (time_before(next_jif, jiffies + HZ))
        next_jif = jiffies + HZ;
    mod_timer(&wb_timer, next_jif);
--- 2.5.34/fs/fs-writeback.c~nonblocking-pdflush    Tue Sep 10 00:00:20 2002
+++ 2.5.34-akpm/fs/fs-writeback.c    Tue Sep 10 00:00:20 2002
@@ -220,44 +220,52 @@ __writeback_single_inode(struct inode *i
  *
  * FIXME: this linear search could get expensive with many fileystems.  But
  * how to fix?  We need to go from an address_space to all inodes which share
- * a queue with that address_space.
+ * a queue with that address_space.  (Easy: have a global "dirty superblocks"
+ * list).
  *
  * The inodes to be written are parked on sb->s_io.  They are moved back onto
  * sb->s_dirty as they are selected for writing.  This way, none can be missed
  * on the writer throttling path, and we get decent balancing between many
- * thrlttled threads: we don't want them all piling up on __wait_on_inode.
+ * throlttled threads: we don't want them all piling up on __wait_on_inode.
  */
static void
sync_sb_inodes(struct super_block *sb, struct writeback_control *wbc)
{
-    struct list_head *tmp;
-    struct list_head *head;
    const unsigned long start = jiffies;    /* livelock avoidance */

    list_splice_init(&sb->s_dirty, &sb->s_io);
-    head = &sb->s_io;
-    while ((tmp = head->prev) != head) {
-        struct inode *inode = list_entry(tmp, struct inode, i_list);
+    while (!list_empty(&sb->s_io)) {
+        struct inode *inode = list_entry(sb->s_io.prev,
+                        struct inode, i_list);
        struct address_space *mapping = inode->i_mapping;
-        struct backing_dev_info *bdi;
+        struct backing_dev_info *bdi = mapping->backing_dev_info;
        int really_sync;

-        if (wbc->bdi && mapping->backing_dev_info != wbc->bdi) {
+        if (wbc->nonblocking && bdi_write_congested(bdi)) {
+            wbc->encountered_congestion = 1;
            if (sb != blockdev_superblock)
-                break;        /* inappropriate superblock */
+                break;        /* Skip a congested fs */
            list_move(&inode->i_list, &sb->s_dirty);
-            continue;        /* not this blockdev */
+            continue;        /* Skip a congested blockdev */
+        }
+
+        if (wbc->bdi && bdi != wbc->bdi) {
+            if (sb != blockdev_superblock)
+                break;        /* fs has the wrong queue */
+            list_move(&inode->i_list, &sb->s_dirty);
+            continue;        /* blockdev has wrong queue */
        }

        /* Was this inode dirtied after sync_sb_inodes was called? */
        if (time_after(mapping->dirtied_when, start))
            break;

+        /* Was this inode dirtied too recently? */
        if (wbc->older_than_this && time_after(mapping->dirtied_when,
                        *wbc->older_than_this))
-            goto out;
+            break;

-        bdi = mapping->backing_dev_info;
+        /* Is another pdflush already flushing this queue? */
        if (current_is_pdflush() && !writeback_acquire(bdi))
            break;

@@ -278,11 +286,7 @@ sync_sb_inodes(struct super_block *sb, s
        if (wbc->nr_to_write <= 0)
            break;
    }
-out:
-    /*
-     * Leave any unwritten inodes on s_io.
-     */
-    return;
+    return;        /* Leave any unwritten inodes on s_io */
}

/*

.
-


Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery

Offline

#3 2007-08-27 12:35:20

leo2501
Member
From: Buenos Aires, Argentina
Registered: 2007-07-07
Posts: 658

Re: pdflush hanging disks when disk activity is high

Auto SOLVED tongue for anybody interested...

http://www.westnet.com/~gsmith/content/ … dflush.htm

//*************************************************************************************
The Linux Page Cache and pdflush:
Theory of Operation and Tuning for Write-Heavy Loads

As you write out data ultimately intended for disk, Linux caches this information in an area of memory called the page cache. You can find out basic info about the page cache using tools like free, vmstat or top. See http://gentoo-wiki.com/FAQ_Linux_Memory_Management to learn how to interpret top's memory information, or atop to get an improved version.

Full information about the page cache only shows up by looking at /proc/meminfo. Here is a sample from a system with 4GB of RAM:

MemTotal:      3950112 kB
MemFree:        622560 kB
Buffers:         78048 kB
Cached:        2901484 kB
SwapCached:          0 kB
Active:        3108012 kB
Inactive:        55296 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:      3950112 kB
LowFree:        622560 kB
SwapTotal:     4198272 kB
SwapFree:      4198244 kB
Dirty:             416 kB
Writeback:           0 kB
Mapped:         999852 kB
Slab:            57104 kB
Committed_AS:  3340368 kB
PageTables:       6672 kB
VmallocTotal: 536870911 kB
VmallocUsed:     35300 kB
VmallocChunk: 536835611 kB
HugePages_Total:     0
HugePages_Free:      0
Hugepagesize:     2048 kB

The size of the page cache itself is the "Cached" figure here, in this example it's 2.9GB. As pages are written, the size of the "Dirty" section will increase. Once writes to disk have begun, you'll see the "Writeback" figure go up until the write is finished. It can be very hard to actually catch the Writeback value going high, as its value is very transient and only increases during the brief period when I/O is queued but not yet written.

Linux usually writes data out of the page cache using a process called pdflush. At any moment, between 2 and 8 pdflush threads are running on the system. You can monitor how many are active by looking at /proc/sys/vm/nr_pdflush_threads. Whenever all existing pdflush threads are busy for at least one second, an additional pdflush daemon is spawned. The new ones try to write back data to device queues that are not congested, aiming to have each device that's active get its own thread flushing data to that device. Each time a second has passed without any pdflush activity, one of the threads is removed. There are tunables for adjusting the minimum and maximum number of pdflush processes, but it's very rare they need to be adjusted.
pdflush tunables
Exactly what each pdflush thread does is controlled by a series of parameters in /proc/sys/vm:

/proc/sys/vm/dirty_writeback_centisecs (default 500): In hundredths of a second, this is how often pdflush wakes up to write data to disk. The default wakes up the two (or more) active threads twice a second.

There can be undocumented behavior that thwarts attempts to decrease dirty_writeback_centisecs in an attempt to make pdflush more aggressive. For example, in early 2.6 kernels, the Linux mm/page-writeback.c code includes logic that's described as "if a writeback event takes longer than a dirty_writeback_centisecs interval, then leave a one-second gap". In general, this "congestion" logic in the kernel is documented only by the kernel source itself, and how it operates can vary considerably depending on which kernel you are running. Because of all this, it's unlikely you'll gain much benefit from lowering the writeback time; the thread spawning code assures that they will automatically run themselves as often as is practical to try and meet the other requirements.

The first thing pdflush works on is writing pages that have been dirty for longer than it deems acceptable. This is controlled by:

/proc/sys/vm/dirty_expire_centiseconds (default 3000): In hundredths of a second, how long data can be in the page cache before it's considered expired and must be written at the next opportunity. Note that this default is very long: a full 30 seconds. That means that under normal circumstances, unless you write enough to trigger the other pdflush method, Linux won't actually commit anything you write until 30 seconds later.

The second thing pdflush will work on is writing pages if memory is low. This is controlled by:

/proc/sys/vm/dirty_background_ratio (default 10): Maximum percentage of active that can be filled with dirty pages before pdflush begins to write them

Note that some kernel versions may internally put a lower bound on this value at 5%.

Most of the documentation you'll find about this parameter suggests it's in terms of total memory, but a look at the source code shows this isn't true. In terms of the meminfo output, the code actually looks at

MemFree + Cached - Mapped

So on the system above, where this figure gives 2.5GB, with the default of 10% the system actually begins writing when the total for Dirty pages is slightly less than 250MB--not the 400MB you'd expect based on the total memory figure.

Summary: when does pdflush write?
In the default configuration, then, data written to disk will sit in memory until either a) they're more than 30 seconds old, or b) the dirty pages have consumed more than 10% of the active, working memory. If you are writing heavily, once you reach the dirty_background_ratio driven figure worth of dirty memory, you may find that all your writes are driven by that limit. It's fairly easy to get in a situation where pages are always being written out by that mechanism well before they are considered expired by the dirty_expire_centiseconds mechanism.

Other than laptop_mode, which changes several parameters to optimize for keeping the hard drive spinning as infrequently as possible (see http://www.samwel.tk/laptop_mode/ for more information) those are all the important kernel tunables that control the pdflush threads.
Process page writes
There is another parameter involved though that can spill over into management of user processes:

/proc/sys/vm/dirty_ratio (default 40): Maximum percentage of total memory that can be filled with dirty pages before processes are forced to write dirty buffers themselves during their time slice instead of being allowed to do more writes.

Note that all processes are blocked for writes when this happens, not just the one that filled the write buffers. This can cause what is perceived as an unfair behavior where one "write-hog" process can block all I/O on the system. The classic way to trigger this behavior is to execute a script that does "dd if=/dev/zero of=hog" and watch what happens. See Kernel Korner: I/O Schedulers for examples showing this behavior.
Tuning Recommendations for write-heavy operations
The usual issue that people who are writing heavily encouter is that Linux buffers too much information at once, in its attempt to improve efficiency. This is particularly troublesome for operations that require synchronizing the filesystem using system calls like fsync. If there is a lot of data in the buffer cace when this call is made, the system can freeze for quite some time to process the sync.

Another common issue is that because so much must be written before any phyiscal writes start, the I/O appears more bursty than would seem optimal. You'll have long periods where no physical writes happen at all, as the large page cache is filled, followed by writes at the highest speed the device can achieve once one of the pdflush triggers is tripped.

dirty_background_ratio: Primary tunable to adjust, probably downward. If your goal is to reduce the amount of data Linux keeps cached in memory, so that it writes it more consistently to the disk rather than in a batch, lowering dirty_background_ratio is the most effective way to do that. It is more likely the default is too large in situations where the system has large amounts of memory and/or slow physical I/O.

dirty_ratio: Secondary tunable to adjust only for some workloads. Applications that can cope with their writes being blocked altogether might benefit from substantially lowering this value. See "Warnings" below before adjusting.

dirty_expire_centisecs: Test lowering, but not to extremely low levels. Attempting to speed how long pages sit dirty in memory can be accomplished here, but this will considerably slow average I/O speed because of how much less efficient this is. This is particularly true on systems with slow physical I/O to disk. Because of the way the dirty page writing mechanism works, trying to lower this value to be very quick (less than a few seconds) is unlikely to work well. Constantly trying to write dirty pages out will just trigger the I/O congestion code more frequently.

dirty_writeback_centisecs: Leave alone. The timing of pdflush threads set by this parameter is so complicated by rules in the kernel code for things like write congestion that adjusting this tunable is unlikely to cause any real effect. It's generally advisable to keep it at the default so that this internal timing tuning matches the frequency at which pdflush runs.
Swapping
By default, Linux will aggressively swap processes out of physical memory onto disk in order to keep the disk cache as large as possible. This means that pages that haven't been used recently will be pushed into swap long before the system even comes close to running out of memory, which is an unexpected behavior compared to some operating systems. The /proc/sys/vm/swappiness parameter controls how aggressive Linux is in this area.

As good a description as you'll find of the numeric details of this setting is in section 4.15 of http://people.redhat.com/nhorman/papers/rhel4_vm.pdf It's based on a combination of how much of memory is mapped (that total is in /proc/meminfo) as well as how difficult it has been for the virtual memory manager to find pages to use.

A value of 0 will avoid ever swapping out just for caching space. Using 100 will always favor making the disk cache bigger. Most distributions set this value to be 60, tuned toward moderately aggressive swapping to increase disk cache.

The optimal setting here is very dependant on workload. In general, high values maximize throughput: how much work your system gets down during a unit of time. Low values favor latency: getting a quick response time from applications. Some desktop users so favor low latency that they set swappiness to 0, so that user applications are never swapped to disk (as can happen when the system is executing background tasks while the user is away). That's perfectly reasonable if the amount of memory in the system exceeds the usual working set for the applications used. Servers that are very active and usually throughput bound could justify setting it to 100. On the flip side, a desktop system that is so limited in memory that every active byte helps might also prefer a setting of 100.

Since the size of the disk cache directly determines things like how much dirty data Linux will allow in memory, adjusting swappiness can greatly influence that behavior even though it's not directly tied to that.

Warnings
-There is a currently outstanding Linux kernel bug that is rare and difficult to trigger even intentionally on most kernel versions. However, it is easier to encounter when reducing dirty_ratio setting below its default. An introduction to the issue starts at http://lkml.org/lkml/2006/12/28/171 and comments about it not being specific to the current kernel release are at http://lkml.org/lkml/2006/12/28/131

-The standard Linux memory allocation behavior uses an "overcommit" setting that allows processes to allocate more memory than is actually available were they to all ask for their pages at once. This is aimed at increasing the amount of memory available for the page cache, but can be dangerous for some types of applications. See http://www.linuxinsight.com/proc_sys_vm … emory.html for a note on the settings you can adjust. An example of an application that can have issues when overcommit is turned on is PostgreSQL; see "Linux Memory Overcommit" at http://www.postgresql.org/docs/current/ … urces.html for their warnings on this subject.
References: page cache
Neil Horman, "Understanding Virtual Memory in Red Hat Enterprise Linux 4" http://people.redhat.com/nhorman/papers/rhel4_vm.pdf

Daniel P. Bovet and Marco Cesati, "Understanding the Linux Kernel, 3rd edition", chapter 15 "The Page Cache". Available on the web at http://www.linux-security.cn/ebooks/ulk3-html/

Robert Love, "Linux Kernel Development, 2nd edition", chapter 15 "The Page Cache and Page Writeback"

"Runtime Memory Management", http://tree.celinuxforum.org/CelfPubWik … easurement

"Red Hat Enterprise Linux-Specific [Memory] Information", http://www.redhat.com/docs/manuals/ente … lspec.html

"Tuning Swapiness", http://kerneltrap.org/node/3000

"FAQ Linux Memory Management", http://gentoo-wiki.com/FAQ_Linux_Memory_Management

From the Linux kernel tree:

    * Documentation/filesystems/proc.txt (the meminfo documentation there originally from http://lwn.net/Articles/28345/)
    * Documentation/sysctl/vm.txt
    * Mm/page-writeback.c

References: I/O scheduling
While not directly addressed here, the I/O scheduling algorithms in Linux actually handle the writes themselves, and some knowledge or tuning of them may be synergistic with adjusting the parameters here. Adjusting the scheduler only makes sense in the context where you've already configured the page cache flushing correctly for your workload.

D. John Shakshober, "Choosing an I/O Scheduler for Red Hat Enterprise Linux 4 and the 2.6 Kernel" http://www.redhat.com/magazine/008jun05 … chedulers/

Robert Love, "Kernel Korner: I/O Schedulers", http://www.linuxjournal.com/article/6931

Seelam, Romero, and Teller, "Enhancements to Linux I/O Scheduling", http://linux.inet.hr/files/ols2005/seelam-reprint.pdf

Heger, D., Pratt, S., "Workload Dependent Performance Evaluation of the Linux 2.6 I/O Schedulers", http://linux.inet.hr/files/ols2004/pratt-reprint.pdf
Upcoming Linux work in progress
-There is a patch in testing from SuSE that adds a parameter called dirty_ratio_centisecs to the kernel tuning which fine-tunes the write-throttling behavior. See "Patch: per-task predictive write throttling" at http://lwn.net/Articles/152277/ and Andrea Arcangeli's article (which has a useful commentary on the existing write throttling code) at http://www.lugroma.org/contenuti/eventi … rnel26.pdf

-SuSE also has suggested a patch at http://lwn.net/Articles/216853/ that allows setting the dirty_ratio settings below the current useful range, aimed at systems with very large memory capacity. The commentary on this patch also has some helpful comments on improving dirty buffer writing, although it is fairly specific to ext3 filesystems.

-The stock 2.6.22 Linux kernel has substantially reduced the default values for the dirty memory parameters. dirty_background_ratio defaulted to 10, now it defaults to 5. vm_dirty_ratio defaulted to 40, now it's 10

-A recent lively discussion on the Linux kernel mailing list discusses some of the limitations of the fsync mechanism when using ext3.
Copyright 2007 Gregory Smith. Last update 8/08/2007.


Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery

Offline

Board footer

Powered by FluxBB