Skip to contentSkip to conversation

per-task-predictive-write-throttling-1

PATCH In review Andrea Arcangeli 4 participants 7 messages Sep 14, 2005 – Sep 16, 2005

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 per-task-predictive-write-throttling-1

Hello,

I wrote a patch to try avoiding making dirty about half the ram of the computer when a single task is writing to disk (like during untarring or rsyncing). I try to detect how many pages this task wrote durign the last dirty_ratio_centisecs (sysctl configurable), and I assume that those pages are already dirty (since they'll be soon anyway).

This way a write-hog task will not lock dirty half the cache for no good reason and other tasks will be allowed to use the dirty cache to avoid blocking during writes. It's not like the write will not be noticeable, but it should become substantially more responsive.

I did a basic test to verify that the performance of the write-hog task didn't suffer, the bandwidth remains the same (difference of 2 seconds over 1 min and 20sec of workload). [though this should be tested in other configurations, it may not be stable yet, the dirty level can go down to zero to the point of nr_reclaimable reaching 0, hence the check needed to avoid locking up in blk_congestion_wait]

/proc/<pid>/future_pages tells about the current prediction. /proc/sys/vm/dirty_ratio_centisecs enables/disables the feature, setting it to 0 makes the kernel behave like current mainline, no difference, setting it close to zero means almost disabled, large values means very enabled, a reasonable value is 5 sec, predicting too much in the future may not lead the best results in real life as you can guess ;).

The below example is run on a 1G box. In the below example there is no way you'll get the workload done in less than one second with this feature enabled, while with the feature enabled it scores less than 1 second all the time.

Note if you try yourself, please make sure to wait just a few seconds (watch the "Dirty" level quickly decreasing in /proc/meminfo and /proc/pidof cp/future_pages quickly increasing) after you started cp, the heuristic takes a few seconds to get into full effect.

I'm running this on my desktop too, so far so good, but it's not well tested, and it might have corner cases. It shall be tested with dbench too (not that I would say this is good or bad depending on dbench though, beacuse this may actually increase the fariness of the workload giving each task a similar portion of dirty cache depending on the I/O that each task is doing).

I increased a bit dirty_background_ratio (from 10 to 30) so that it can more likely reach a point where cp is the one doing all the work. Not sure if it's really necessary, but it had the same 30 setting for both tests below.

Patch is overall not very complex.

xeon:~ # cat /proc/sys/vm/dirty_ratio_centisecs
500
xeon:~ # cp /dev/zero /tmp/ &
[1] 6490
xeon:~ # dd if=/dev/zero of=/dev/null bs=1 count=1
1+0 records in
1+0 records out
xeon:~ # time for i in seq 10; do dd if=/dev/zero of=/tmp/tmp/$i bs=10M count=1 2>/dev/null ; echo -n .; done; rm /tmp/tmp/*
..........

real    0m0.514s
user    0m0.004s
sys     0m0.512s
xeon:~ # time for i in `seq 10`; do dd if=/dev/zero of=/tmp/tmp/$i bs=10M count=1 2>/dev/null ; echo -n .; done; rm /tmp/tmp/*
..........
real    0m0.550s
user    0m0.008s
sys     0m0.544s

^[[A
^[[A
xeon:~ # time for i in seq 10; do dd if=/dev/zero of=/tmp/tmp/$i bs=10M count=1 2>/dev/null ; echo -n .; done; rm /tmp/tmp/*
..........

real    0m0.501s
user    0m0.012s
sys     0m0.488s
^[[A
xeon:~ # time for i in `seq 10`; do dd if=/dev/zero of=/tmp/tmp/$i bs=10M count=1 2>/dev/null ; echo -n .; done; rm /tmp/tmp/*
..........
real    0m0.921s
user    0m0.008s
sys     0m0.488s
xeon:~ # time for i in `seq 10`; do dd if=/dev/zero of=/tmp/tmp/$i bs=10M count=1 2>/dev/null ; echo -n .; done; rm /tmp/tmp/*
..........
real    0m0.473s
user    0m0.008s
sys     0m0.464s

xeon:~ # fg
cp /dev/zero /tmp/
xeon:~ # rm /tmp/zero
xeon:~ # cat /proc/sys/vm/dirty_ratio_centisecs
500
xeon:~ # echo 0 > /proc/sys/vm/dirty_ratio_centisecs
xeon:~ # cat /proc/sys/vm/dirty_ratio_centisecs
0
xeon:~ # cp /dev/zero /tmp/ &
[1] 6592
xeon:~ # dd if=/dev/zero of=/dev/null bs=1 count=1
1+0 records in
1+0 records out
xeon:~ # time for i in seq 10; do dd if=/dev/zero of=/tmp/tmp/$i bs=10M count=1 2>/dev/null ; echo -n .; done; rm /tmp/tmp/*
..........

real    0m5.205s
user    0m0.004s
sys     0m0.580s
xeon:~ # time for i in `seq 10`; do dd if=/dev/zero of=/tmp/tmp/$i bs=10M count=1 2>/dev/null ; echo -n .; done; rm /tmp/tmp/*
..........
real    0m8.023s
user    0m0.008s
sys     0m0.608s
xeon:~ # time for i in `seq 10`; do dd if=/dev/zero of=/tmp/tmp/$i bs=10M count=1 2>/dev/null ; echo -n .; done; rm /tmp/tmp/*
..........
real    0m4.455s
user    0m0.008s
sys     0m0.640s
xeon:~ # time for i in `seq 10`; do dd if=/dev/zero of=/tmp/tmp/$i bs=10M count=1 2>/dev/null ; echo -n .; done; rm /tmp/tmp/*
..........
real    0m10.646s
user    0m0.012s
sys     0m0.732s
xeon:~ # time for i in `seq 10`; do dd if=/dev/zero of=/tmp/tmp/$i bs=10M count=1 2>/dev/null ; echo -n .; done; rm /tmp/tmp/*
..........
real    0m3.525s
user    0m0.008s
sys     0m0.624s
Documentation/filesystems/proc.txt +9 -0
diff -r 8b591846b274 Documentation/filesystems/proc.txt
@@ -1162,6 +1162,15 @@
 a process which is generating disk writes will itself start writing out dirty
 data.
 
+dirty_ratio_centisecs
+-----------------
+
+Throttle the I/O if the per-task writing bandwidth is high enough for
+the dirty_ratio to be reached in less than dirty_ratio_centisecs. This
+makes the write throttling per-process and avoids making too much
+memory dirty at the same time. Ideally in the future we should add
+some feedback from the backing_dev_info to know the max disk bandwidth.
+
 dirty_writeback_centisecs
 -------------------------
 
Documentation/sysctl/vm.txt +5 -3
diff -r 8b591846b274 Documentation/sysctl/vm.txt
@@ -19,6 +19,7 @@
 - overcommit_memory
 - page-cluster
 - dirty_ratio
+- dirty_ratio_centisecs
 - dirty_background_ratio
 - dirty_expire_centisecs
 - dirty_writeback_centisecs
@@ -29,9 +30,10 @@
 
 ==============================================================
 
-dirty_ratio, dirty_background_ratio, dirty_expire_centisecs,
-dirty_writeback_centisecs, vfs_cache_pressure, laptop_mode,
-block_dump, swap_token_timeout:
+dirty_ratio, dirty_ratio_centisecs, dirty_background_ratio,
+dirty_expire_centisecs, dirty_writeback_centisecs,
+vfs_cache_pressure, laptop_mode, block_dump,
+swap_token_timeout:
 
 See Documentation/filesystems/proc.txt
 
fs/proc/base.c +15 -0
diff -r 8b591846b274 fs/proc/base.c
@@ -122,6 +122,7 @@
 #endif
 	PROC_TGID_OOM_SCORE,
 	PROC_TGID_OOM_ADJUST,
+	PROC_TGID_FUTURE_DIRTY,
 	PROC_TID_INO,
 	PROC_TID_STATUS,
 	PROC_TID_MEM,
@@ -160,6 +161,7 @@
 #endif
 	PROC_TID_OOM_SCORE,
 	PROC_TID_OOM_ADJUST,
+	PROC_TID_FUTURE_DIRTY,
 
 	/* Add new entries before this */
 	PROC_TID_FD_DIR = 0x8000,	/* 0x8000-0xffff */
@@ -210,6 +212,7 @@
 #endif
 	E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
 	E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
+	E(PROC_TGID_FUTURE_DIRTY,"future_dirty",S_IFREG|S_IRUGO),
 #ifdef CONFIG_AUDITSYSCALL
 	E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
 #endif
@@ -250,6 +253,7 @@
 #endif
 	E(PROC_TID_OOM_SCORE,  "oom_score",S_IFREG|S_IRUGO),
 	E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
+	E(PROC_TID_FUTURE_DIRTY,"future_dirty",S_IFREG|S_IRUGO),
 #ifdef CONFIG_AUDITSYSCALL
 	E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
 #endif
@@ -463,6 +467,12 @@
 	do_posix_clock_monotonic_gettime(&uptime);
 	points = badness(task, uptime.tv_sec);
 	return sprintf(buffer, "%lu\n", points);
+}
+
+static int proc_future_dirty(struct task_struct *task, char *buffer)
+{
+	return sprintf(buffer, "%lu\n",
+		       task->balance_dirty_state.future_dirty);
 }
 
 /************************************************************************/
@@ -1664,6 +1674,11 @@
 		case PROC_TGID_OOM_ADJUST:
 			inode->i_fop = &proc_oom_adjust_operations;
 			break;
+		case PROC_TID_FUTURE_DIRTY:
+		case PROC_TGID_FUTURE_DIRTY:
+			inode->i_fop = &proc_info_file_operations;
+			ei->op.proc_read = proc_future_dirty;
+			break;
 #ifdef CONFIG_AUDITSYSCALL
 		case PROC_TID_LOGINUID:
 		case PROC_TGID_LOGINUID:
include/linux/sched.h +8 -0
diff -r 8b591846b274 include/linux/sched.h
@@ -573,6 +573,12 @@
 struct io_context;			/* See blkdev.h */
 void exit_io_context(void);
 struct cpuset;
+
+struct balance_dirty_state {
+	unsigned long future_dirty;
+	unsigned long last_sync; /* jiffies */
+	int rate_limit;
+};
 
 #define NGROUPS_SMALL		32
 #define NGROUPS_PER_BLOCK	((int)(PAGE_SIZE / sizeof(gid_t)))
@@ -766,6 +772,8 @@
 
 	unsigned long ptrace_message;
 	siginfo_t *last_siginfo; /* For ptrace use.  */
+
+	struct balance_dirty_state balance_dirty_state;
 /*
  * current io wait handle: wait queue entry to use for io waits
  * If this thread is processing aio, this points at the waitqueue
include/linux/sysctl.h +1 -0
diff -r 8b591846b274 include/linux/sysctl.h
@@ -180,6 +180,7 @@
 	VM_VFS_CACHE_PRESSURE=26, /* dcache/icache reclaim pressure */
 	VM_LEGACY_VA_LAYOUT=27, /* legacy/compatibility virtual address space layout */
 	VM_SWAP_TOKEN_TIMEOUT=28, /* default time for token time out */
+	VM_DIRTY_RATIO_CS=29,	/* dirty_ratio_centisecs */
 };
 
 
include/linux/writeback.h +1 -0
diff -r 8b591846b274 include/linux/writeback.h
@@ -93,6 +93,7 @@
 /* These are exported to sysctl. */
 extern int dirty_background_ratio;
 extern int vm_dirty_ratio;
+extern int vm_dirty_ratio_centisecs;
 extern int dirty_writeback_centisecs;
 extern int dirty_expire_centisecs;
 extern int block_dump;
kernel/sysctl.c +8 -0
diff -r 8b591846b274 kernel/sysctl.c
@@ -725,6 +725,14 @@
 		.procname	= "dirty_expire_centisecs",
 		.data		= &dirty_expire_centisecs,
 		.maxlen		= sizeof(dirty_expire_centisecs),
+		.mode		= 0644,
+		.proc_handler	= &proc_dointvec,
+	},
+	{
+		.ctl_name	= VM_DIRTY_RATIO_CS,
+		.procname	= "dirty_ratio_centisecs",
+		.data		= &vm_dirty_ratio_centisecs,
+		.maxlen		= sizeof(vm_dirty_ratio_centisecs),
 		.mode		= 0644,
 		.proc_handler	= &proc_dointvec,
 	},
mm/page-writeback.c +58 -6

113 lines not on this page · raw message

#1 lore raw
Quoted 18 lines · Andrea Arcangeli

I wrote a patch to try avoiding making dirty about half the ram of the computer when a single task is writing to disk (like during untarring or rsyncing). I try to detect how many pages this task wrote durign the last dirty_ratio_centisecs (sysctl configurable), and I assume that those pages are already dirty (since they'll be soon anyway).

This way a write-hog task will not lock dirty half the cache for no good reason and other tasks will be allowed to use the dirty cache to avoid blocking during writes. It's not like the write will not be noticeable, but it should become substantially more responsive.

I did a basic test to verify that the performance of the write-hog task didn't suffer, the bandwidth remains the same (difference of 2 seconds over 1 min and 20sec of workload). [though this should be tested in other configurations, it may not be stable yet, the dirty level can go down to zero to the point of nr_reclaimable reaching 0, hence the check needed to avoid locking up in blk_congestion_wait]

Quoted 6 lines

/proc/<pid>/future_pages tells about the current prediction. /proc/sys/vm/dirty_ratio_centisecs enables/disables the feature, setting it to 0 makes the kernel behave like current mainline, no difference, setting it close to zero means almost disabled, large values means very enabled, a reasonable value is 5 sec, predicting too much in the future may not lead the best results in real life as you can guess ;).

Nice idea!

I suspect the reason 5 seconds is good is probably because it's the same value as dirty_writeback_centisecs.

I think this patch will sit nicely in my tree thanks :).

Cheers,
Con

#2 lore raw
Quoted 6 lines · Con Kolivas

Nice idea!

I suspect the reason 5 seconds is good is probably because it's the same value as dirty_writeback_centisecs.

I think this patch will sit nicely in my tree thanks :).

Fine ;) it's in -mm too, I hope it's stable and it doesn't introduce regressions ;).

BTW, I tested dbench (not after a fresh mke2fs so there might be minor fragmentation variations) and it doesn't seem affected by it (it's in the noise range) and I take it as a good thing. dbench keeps looking weird, if you notice the lower bandwidth of 273 completed in 12m13s while the higher bandwidth of 291 completed in 12m15s... perhaps it prints the best bandwith of the passes and it's not an average.

set to 500:

Throughput 288.217 MB/sec 50 procs

real    12m14.921s
user    9m49.185s
sys     25m38.896s

Throughput 306.644 MB/sec 50 procs

real    12m15.696s
user    10m18.207s
sys     26m39.704s

set to 0:

Throughput 291.415 MB/sec 50 procs

real    12m15.668s
user    9m50.229s
sys     26m58.341s

Throughput 273.05 MB/sec 50 procs

real    12m13.098s
user    9m28.964s
sys     23m58.430s

xeon:/mnt #

#3 lore raw

Hi Andrea,

Quoted 8 lines

BTW, I tested dbench (not after a fresh mke2fs so there might be minor fragmentation variations) and it doesn't seem affected by it (it's in the noise range) and I take it as a good thing. dbench keeps looking weird, if you notice the lower bandwidth of 273 completed in 12m13s while the higher bandwidth of 291 completed in 12m15s... perhaps it prints the best bandwith of the passes and it's not an average.

From memory, if you are using a recent version of dbench it runs for a

constant amount of time. The old version ran a constant number of operations (and so runtimes varied based on your performance).

Anton

#4 lore raw
Quoted 2 lines · Anton Blanchard
Quoted 1 line

From memory, if you are using a recent version of dbench it runs for a

constant amount of time. The old version ran a constant number of

Thanks for the info, it was the recent version, so the bandwidth number is the important one (and it's a bit higher with the feature enabled, but it may be just noise).

#5 lore raw
In reply to Andrea Arcangeli
Quoted 4 lines · Andrea Arcangeli

Hello,

I wrote a patch to try avoiding making dirty about half the ram of the computer when a single task is writing to disk (like during untarring or

Why is that useful? Don't we want (in general) to cache as many dirty pages as possible in the hope that some of them will be re-dirtied (thus avoiding additional write) or truncated (avoiding write altogether)?

Nikita.

#6 lore raw
Quoted 1 line · Nikita Danilov

Why is that useful? Don't we want (in general) to cache as many dirty

It allows all tasks to have a chance to take advantage of the writeback cache, not only the first one like now.

Quoted 2 lines

pages as possible in the hope that some of them will be re-dirtied (thus avoiding additional write) or truncated (avoiding write altogether)?

We could add an option to increase the per-task future_dirty pages only if we had to allocate a new pagecache (i.e. cache miss), that would leave the rewrite behaviour unchanged, but still if we've a huge write-hog, we should reserve some cache for other people anyway, so I'm unsure if we should allow rewriter-hogs to use half the ram and leave nothing to the other potential small writers. Certainly accounting for future_dirty only on cache-misses on the radix-tree would still catch the bad untarring and cp beahviour just fine, so that might be good enough and I would certainly agree that's more conservative approach.

Initially every task has access to the whole dirty cache, even "cp /dev/zero ." initially allocates half the ram, but then if it keeps writing that fast, the dirty cache decreases, to give a chance to other tasks to avoid blocking while writing.

If the write-hog becomes reasoanable and it slowdown writing, it can then use more dirty cache again.