vfs: per-superblock unused dentries list
Quoted 2 lines · Jan Blunck
BTW: Dave, I just saw that you already posted some patches but I decided to send my patches anyway.
No problems. Your patches remove bits that I wasn't sure could be removed, but effectively do the same thing. Removing shrink_dcache_anon(), for example. The question is which patchset do we want to carry forward?
I'll add more comments in replies to the various patches...
Cheers,
Dave.
Signature · 3 lines
Dave Chinner R&D Software Enginner SGI Australian Software Group
Re: [patch 4/5] vfs: per superblock dentry stats
In reply to a message not in the archivefs/super.c static struct super_block *alloc_super(v · line 75
Quoted 10 lines · Jan Blunck
Index:
work-2.6/fs/super.c
===================================================================fs/super.c +2
-0@@ -71,6 +71,9 @@ static struct super_block *alloc_super(vINIT_LIST_HEAD (&s->s_instances);INIT_HLIST_HEAD (&s->s_anon);INIT_LIST_HEAD (&s->s_inodes); + s->s_dentry_stat.nr_dentry =0 ; + s->s_dentry_stat.nr_unused =0 ;
No need for these as the superblock is memset to zero....
fs/super.c static struct super_block *alloc_super(v
Quoted 9 lines
fs/super.c +2
-4-struct dentry_stat_t { +struct dentry_stat {int nr_dentry;int nr_unused; -int age_limit;/* age in seconds */ -int want_pages;/* pages requested by system */ -int dummy[2 ]; +int age_limit;/* age in seconds */ };
that changes the size of the structure from 6*sizeof(int) to 3*sizeof(int)....
Quoted 6 lines
fs/super.c +5
-1-extern struct dentry_stat_t dentry_stat; +extern struct dentry_stat global_dentry_stat; + +#define dentry_stat_inc (sb, x) \ +do { \ + global_dentry_stat.x++; \
fs/super.c
Quoted 10 lines
fs/super.c +10
-0+if (sb) \ + (sb)->s_dentry_stat.x++;\ +} while(0) + +#define dentry_stat_dec (sb, x) \ +do { \ + global_dentry_stat.x--; \ + if (sb) \ + (sb)->s_dentry_stat.x--;\ +} while(0)
if (likely(sb)) ???
kernel/sysctl.c static ctl_table fs_table[] = { · line 962
Quoted 11 lines
Index:
work-2.6/kernel/sysctl.c
===================================================================kernel/sysctl.c +1
-1@@ -958,7 +958,7 @@ static ctl_table fs_table[] = { { .ctl_name = FS_DENTRY, .procname = "dentry-state", - .data = &dentry_stat, + .data = &global_dentry_stat, .maxlen = 6*sizeof(int),
With the above change, maxlen = 3*sizeof(int).
Given that's a userspace visible change, should the above structure change use a "int dummy[3];" to keep the global structure and userspace export the same?
Cheers,
Dave.
Signature · 3 lines
Dave Chinner R&D Software Enginner SGI Australian Software Group
Re: [patch 5/5] vfs: per superblock dentry unused list
In reply to a message not in the archiveQuoted 141 lines · Jan Blunck
This patch adds per superblock dentry unused lists. This should speedup the umounting/remounting of filesystems when there are alot of dentries in use.
Signed-off-by Jan Blunck <[email protected]>--- fs/dcache.c | 143 +++++++++++++++++++++-------------------------------- fs/super.c | 1 include/linux/fs.h | 1 3 files changed, 61 insertions(+), 84 deletions(-)Index:
work-2.6/fs/dcache.c
===================================================================fs/dcache.c +33
-41@@ -61,7 +61,6 @@ static kmem_cache_t *dentry_cache __readstatic unsigned int d_hash_mask__read_mostly ;static unsigned int d_hash_shift__read_mostly ;static struct hlist_head *dentry_hashtable__read_mostly ; -static LIST_HEAD (dentry_unused);/* Statistics gathering. */ struct dentry_stat global_dentry_stat = {@@ -167,12 +166,15 @@ repeat:if (dentry->d_op->d_delete (dentry)) goto unhash_it; } +/* Kill dentry without superblock */ +if (unlikely (!dentry->d_sb)) + goto unhash_it;/* Unreachable? Get rid of it */ if (d_unhashed (dentry)) goto kill_it;if (list_empty (&dentry->d_lru)) { dentry->d_flags |=DCACHE_REFERENCED ; -list_add (&dentry->d_lru, &dentry_unused); +list_add (&dentry->d_lru, &dentry->d_sb->s_unused);dentry_stat_inc (dentry->d_sb, nr_unused); }spin_unlock (&dentry->d_lock);@@ -382,19 +384,22 @@ static inline void prune_one_dentry(stru }/** -* prune_dcache - shrink the dcache +* prune_dcache_sb - prune the dcache for a superblock +* @sb: superblock * @count: number of entries to try and free * -* Shrink the dcache. This is done when we need -* more memory. When we need to unmount something -* we call shrink_dcache_sb(). +* Prune the dcache for the specified super block. This +* is called from prune_dcache(). * -* This function may fail to free any resources if -* all the dentries are in use. +* You need to have a reference to the super block and +* should have sb->s_umount locked. This function may fail +* to free any resources if all the dentries are in use. */ - -static void prune_dcache (int count) +static void prune_dcache_sb (struct super_block *sb,int count) { +if (count <=0 ) +return ; +spin_lock (&dcache_lock);for (; count ; count--) {struct dentry *dentry;@@ -402,10 +407,10 @@ static void prune_dcache(int count)cond_resched_lock (&dcache_lock); - tmp = dentry_unused.prev; -if (tmp == &dentry_unused) + tmp = sb->s_unused.prev; +if (tmp == &sb->s_unused)break ; -prefetch (dentry_unused.prev); +prefetch (sb->s_unused.prev); dentry = list_entry(tmp, struct dentry, d_lru);dentry_stat_dec (dentry->d_sb, nr_unused);list_del_init (&dentry->d_lru);@@ -423,7 +428,7 @@ static void prune_dcache(int count)/* If the dentry was recently referenced, don't free it. */ if (dentry->d_flags &DCACHE_REFERENCED ) { dentry->d_flags &= ~DCACHE_REFERENCED ; -list_add (&dentry->d_lru, &dentry_unused); +list_add (&dentry->d_lru, &sb->s_unused);dentry_stat_inc (dentry->d_sb, nr_unused);spin_unlock (&dentry->d_lock);continue ;@@ -433,64 +438,48 @@ static void prune_dcache(int count)spin_unlock (&dcache_lock); } - -/* -* parsing d_hash list does not hlist_for_each_entry_rcu() as it -* done under dcache_lock. +/** +* prune_dcache - shrink the dcache +* @count: number of entries to try and free +* +* Prune the dcache. This is done when we need +* more memory. +* +* This function may fail to free any resources if +* all the dentries are in use. */ -static void select_anon (struct super_block *sb) +static void prune_dcache (int count) { -struct dentry *dentry; -struct hlist_node *lp; - -spin_lock (&dcache_lock); -hlist_for_each_entry (dentry, lp, &sb->s_anon, d_hash) { -if (!list_empty (&dentry->d_lru)) { -dentry_stat_dec (sb, nr_unused); -list_del_init (&dentry->d_lru); - } +struct super_block *sb; +int unused = global_dentry_stat.nr_unused; -/* -* move only zero ref count dentries to the beginning -* (the most recent end) of the unused list -*/ -spin_lock (&dentry->d_lock); -if (!atomic_read (&dentry->d_count)) { -list_add (&dentry->d_lru, &dentry_unused); -dentry_stat_inc (sb, nr_unused); - } -spin_unlock (&dentry->d_lock); - } -spin_unlock (&dcache_lock); -} +if (count <=0 ) +return ;
fs/dcache.c static void prune_dcache(int count) · line 464
Quoted 10 lines
fs/dcache.c +6
-3-static void select_sb (struct super_block *sb) -{ -struct dentry *dentry, *pos; +spin_lock (&sb_lock); + restart: +list_for_each_entry (sb, &super_blocks, s_list) { + sb->s_count++; +spin_unlock (&sb_lock); +down_read (&sb->s_umount);
This should probably be a try-lock. See the comment in writeback_inodes()....
Quoted 5 lines
fs/dcache.c +2
-2+if (sb->s_root) { +int tmp; -spin_lock (&dcache_lock); -list_for_each_entry_safe (dentry, pos, &dentry_unused, d_lru) {
fs/dcache.c static void prune_dcache(int count) · line 473
Quoted 10 lines
fs/dcache.c +6
-4-if (dentry->d_sb != sb) -continue ; -list_del (&dentry->d_lru); -list_add (&dentry->d_lru, &dentry_unused); +/* +* Try to be fair to the unused lists: +* sb_count/sb_unused ~ global_count/global_unused +*/ + tmp = sb->s_dentry_stat.nr_unused/((unused/count)+1 ); +prune_dcache_sb (sb, tmp);
So if count = SHRINK_BATCH = 128, unused is 12800 (for easy maths) and we have 100 unused on the first superbloc, we end up with tmp = 100 / ((12800/128)+1) = 100/101 = 0.
Essentially, if your superblock has less than (global_unused / count) dentries on it, they'll never get shrunk. They need to take at least one dentry off each superblock to ensure that the lru lists are slowly turned over. This is needed to allow pages in the slab pinned by dentries on lesser used or smaller filesystems to be freed before you've trimmed almost every dentry from the superblocks that contain orders of magnitude more dentries...
IOWs, I think that tmp must be >= 1 for all calls here.
Realistically, we are limited in resolution by the way the shrinker works here. When we have a difference of greater than 2 orders of magnitude between the small superblock and the large superblock lists we are either going to trim the small superblock lists too much or not enough....
fs/dcache.c static void select_sb(struct super_block · line 492
Quoted 7 lines
fs/dcache.c +0
-1@@ -499,30 +488,16 @@ static void select_sb(struct super_block* is used to free the dcache before unmounting a file * system */ -void shrink_dcache_sb (struct super_block * sb) {
The only difference between this function and prune_dcache_sb is the handlingof the DCACHE_REFERENCED bit. i built a common function for these, because....
fs/dcache.c void shrink_dcache_parent(struct dentry · line 650
Quoted 7 lines
fs/dcache.c +1
-1@@ -671,7 +646,7 @@ void shrink_dcache_parent(struct dentryint found;while ((found =select_parent (parent)) !=0 ) -prune_dcache (found); +prune_dcache_sb (parent->d_sb, found); }
... prune_dcache_parent() uses the same code as well....
include/linux/fs.h struct super_block { · line 849
Quoted 9 lines
Index:
work-2.6/include/linux/fs.h
===================================================================include/linux/fs.h +1
-0@@ -846,6 +846,7 @@ struct super_block {struct list_head s_io;/* parked for writeback */ struct hlist_head s_anon;/* anonymous dentries for (nfs) exporting */ struct list_head s_files; +struct list_head s_unused;/* unused dentries */
Maybe change that name to something that has "dentry" in the name?
include/linux/fs.h struct super_block { · line 850
Quoted 1 line
include/linux/fs.h +0
-0struct dentry_stat s_dentry_stat;
... especially as the matching stats structure uses that convention...
Cheers,
Dave.
Signature · 3 lines
Dave Chinner R&D Software Enginner SGI Australian Software Group
Re: [patch 4/5] vfs: per superblock dentry stats
In reply to David ChinnerQuoted 27 lines · David Chinner
Review
Quoted 9 lines
(quoted diff) +2
-4-struct dentry_stat_t { +struct dentry_stat { int nr_dentry; int nr_unused; - int age_limit; /* age in seconds */ - int want_pages; /* pages requested by system */ - int dummy[2]; + int age_limit; /* age in seconds */ };that changes the size of the structure from 6*sizeof(int) to 3*sizeof(int)....
kernel/sysctl.c static ctl_table fs_table[] = { · line 962
Quoted 11 lines
Index:
work-2.6/kernel/sysctl.c
===================================================================kernel/sysctl.c +1
-1@@ -958,7 +958,7 @@ static ctl_table fs_table[] = { { .ctl_name = FS_DENTRY, .procname = "dentry-state", - .data = &dentry_stat, + .data = &global_dentry_stat, .maxlen = 6*sizeof(int),With the above change, maxlen = 3*sizeof(int).
Quoted 3 lines
Given that's a userspace visible change, should the above structure change use a "int dummy[3];" to keep the global structure and userspace export the same?
Hmm, probably you are right. I'll change this.
Jan
Re: [patch 5/5] vfs: per superblock dentry unused list
In reply to David ChinnerQuoted 24 lines · David Chinner
Review
Quoted 12 lines
(quoted diff) +6
-6- spin_lock(&dcache_lock); - list_for_each_entry_safe(dentry, pos, &dentry_unused, d_lru) { - if (dentry->d_sb != sb) - continue; - list_del(&dentry->d_lru); - list_add(&dentry->d_lru, &dentry_unused); + /* + * Try to be fair to the unused lists: + * sb_count/sb_unused ~ global_count/global_unused + */ + tmp = sb->s_dentry_stat.nr_unused/((unused/count)+1); + prune_dcache_sb(sb, tmp);So if count =
SHRINK_BATCH= 128, unused is 12800 (for easy maths) and we have 100 unused on the first superbloc, we end up with tmp = 100 / ((12800/128)+1) = 100/101 = 0.Essentially, if your superblock has less than (
global_unused/ count) dentries on it, they'll never get shrunk. They need to take at least one dentry off each superblock to ensure that the lru lists are slowly turned over. This is needed to allow pages in the slab pinned by dentries on lesser used or smaller filesystems to be freed before you've trimmed almost every dentry from the superblocks that contain orders of magnitude more dentries...
Quoted 6 lines
IOWs, I think that tmp must be >= 1 for all calls here.
Realistically, we are limited in resolution by the way the shrinker works here. When we have a difference of greater than 2 orders of magnitude between the small superblock and the large superblock lists we are either going to trim the small superblock lists too much or not enough....
Yeah, I have problems with that part as well. Some of your assumtions are wrong. If the sb.nr_unused count is smaller than 128, the superblock is not shrinked, thats true. But there is a superblock with more than 128 unused dentries (since the global_unused count was 12800). So the prune_dcache() is shrinking that one first. After a few runs, prune_dcache() is shrinking the superblock with 128 unused dentries aswell.
Although, what happens when we have 100 superblocks with 128 unused dentries each ... I have to think about this. The right solution would be to shrink the dentries with the help of their age. But at the moment I don't have any bright ideas in that direction.
Quoted 15 lines
Review static void select_sb(struct super_block · line 492
Quoted 7 lines
(quoted diff) +0
-1@@ -499,30 +488,16 @@ static void select_sb(struct super_block * is used to free the dcache before unmounting a file * system */ - void shrink_dcache_sb(struct super_block * sb) {The only difference between this function and
prune_dcache_sbis the handlingof theDCACHE_REFERENCEDbit. i built a common function for these, because....Quoted 3 lines
(quoted diff) +0
-0@@ -671,7 +646,7 @@ void shrink_dcache_parent(struct dentry int found;
Quoted 6 lines
Review void shrink_dcache_parent(struct dentry · line 650
Quoted 4 lines
(quoted diff) +1
-1while ((found = select_parent(parent)) != 0) - prune_dcache(found); + prune_dcache_sb(parent->d_sb, found); }...
prune_dcache_parent()uses the same code as well....
No. prune_dcache() is working on the unused list in the opposite (reverse) direction. shrink_dcache_sb() (basically my prune_dcache_sb()) is shrinking all unused dentries. In that case it is better to visit the unused list in the normal (forward) direction (~only one pass).
Jan
Re: [patch 5/5] vfs: per superblock dentry unused list
Quoted 33 lines · Jan Blunck
Quoted 24 lines · David Chinner
Review
Quoted 12 lines
(quoted diff) +6
-6- spin_lock(&dcache_lock); - list_for_each_entry_safe(dentry, pos, &dentry_unused, d_lru) { - if (dentry->d_sb != sb) - continue; - list_del(&dentry->d_lru); - list_add(&dentry->d_lru, &dentry_unused); + /* + * Try to be fair to the unused lists: + * sb_count/sb_unused ~ global_count/global_unused + */ + tmp = sb->s_dentry_stat.nr_unused/((unused/count)+1); + prune_dcache_sb(sb, tmp);So if count =
SHRINK_BATCH= 128, unused is 12800 (for easy maths) and we have 100 unused on the first superbloc, we end up with tmp = 100 / ((12800/128)+1) = 100/101 = 0.Essentially, if your superblock has less than (
global_unused/ count) dentries on it, they'll never get shrunk. They need to take at least one dentry off each superblock to ensure that the lru lists are slowly turned over. This is needed to allow pages in the slab pinned by dentries on lesser used or smaller filesystems to be freed before you've trimmed almost every dentry from the superblocks that contain orders of magnitude more dentries...Quoted 6 lines
IOWs, I think that tmp must be >= 1 for all calls here.
Realistically, we are limited in resolution by the way the shrinker works here. When we have a difference of greater than 2 orders of magnitude between the small superblock and the large superblock lists we are either going to trim the small superblock lists too much or not enough....
Quoted 6 lines
Yeah, I have problems with that part as well. Some of your assumtions are wrong. If the sb.nr_unused count is smaller than 128, the superblock is not shrinked, thats true. But there is a superblock with more than 128 unused dentries (since the
global_unusedcount was 12800). So theprune_dcache()is shrinking that one first. After a few runs,prune_dcache()is shrinking the superblock with 128 unused dentries aswell.
You've just described the embodiment of the two order's of magnitude issue I mentioned. That's not a wrong assumption - think of the above case with global_unused count now being 1.28*10^7 instead of 1.28x10^4. How many dentries do you have to free before freeing any on the small superblock if we don't free one per call? (quick
answer: 99.9%).
If we shrink one per call, we've freed all 128 dentries while there is still 1*10^5 dentries on the large list. That seems like a much better balance to make within the constraints of the shrinker resolution we have to work with.
FWIW, if we don't free a dentry per sb per prune_dcache call, any prooblems caused by slab page pinning and fragmetnation get worse than they are now as some dentries will take far, far longer to be freed than others regardless of their age.
Quoted 4 lines
Although, what happens when we have 100 superblocks with 128 unused dentries each ... I have to think about this. The right solution would be to shrink the dentries with the help of their age. But at the moment I don't have any bright ideas in that direction.
Hmm - need to do something with that age_limit field, right? That would imply we need a timestamp in the dentry as well, and we don't shrink any sb that doesn't have dentries older than the age limit. If we scan all the sb's and still have more to free, then we halve the age limit and scan again....
Quoted 22 lines
Quoted 15 lines
Review static void select_sb(struct super_block · line 492
Quoted 7 lines
(quoted diff) +0
-1@@ -499,30 +488,16 @@ static void select_sb(struct super_block * is used to free the dcache before unmounting a file * system */ - void shrink_dcache_sb(struct super_block * sb) {The only difference between this function and
prune_dcache_sbis the handlingof theDCACHE_REFERENCEDbit. i built a common function for these, because....Quoted 3 lines
(quoted diff) +0
-0@@ -671,7 +646,7 @@ void shrink_dcache_parent(struct dentry int found;Quoted 6 lines
Review void shrink_dcache_parent(struct dentry · line 650
Quoted 4 lines
(quoted diff) +1
-1while ((found = select_parent(parent)) != 0) - prune_dcache(found); + prune_dcache_sb(parent->d_sb, found); }...
prune_dcache_parent()uses the same code as well....
Quoted 4 lines
No.
prune_dcache()is working on the unused list in the opposite (reverse) direction.shrink_dcache_sb()(basically myprune_dcache_sb()) is shrinking all unused dentries. In that case it is better to visit the unused list in the normal (forward) direction (~only one pass).
Why? Forward or reverse it's only one traversal to free all dentries
- you go till the list is empty. Either way, with the prefetch of the next entry in the list there's little perfomrance difference once you've got outside some tiny subset of the list that might be hot in cache....
Cheers,
Dave.
Signature · 3 lines
Dave Chinner R&D Software Enginner SGI Australian Software Group
Re: [patch 5/5] vfs: per superblock dentry unused list
Quoted 11 lines · David Chinner
You've just described the embodiment of the two order's of magnitude issue I mentioned. That's not a wrong assumption - think of the above case with
global_unusedcount now being 1.28*10^7 instead of 1.28x10^4. How many dentries do you have to free before freeing any on the small superblock if we don't free one per call? (quick
answer: 99.9%).If we shrink one per call, we've freed all 128 dentries while there is still 1*10^5 dentries on the large list. That seems like a much better balance to make within the constraints of the shrinker resolution we have to work with.
With the effect that the dcache is completely useless for small filesystems as long as there is one big one. Filesystems where regularily a small amount of files is used don't have any cached dentries but the filesystem where someone touched every file still has a lot of dentries in cache although they are never used again.
Quoted 5 lines
Hmm - need to do something with that
age_limitfield, right? That would imply we need a timestamp in the dentry as well, and we don't shrink any sb that doesn't have dentries older than the age limit. If we scan all the sb's and still have more to free, then we halve the age limit and scan again....
This probably is the way to go.
Quoted 10 lines
Quoted 4 lines
No.
prune_dcache()is working on the unused list in the opposite (reverse) direction.shrink_dcache_sb()(basically myprune_dcache_sb()) is shrinking all unused dentries. In that case it is better to visit the unused list in the normal (forward) direction (~only one pass).Why? Forward or reverse it's only one traversal to free all dentries
- you go till the list is empty. Either way, with the prefetch of the next entry in the list there's little perfomrance difference once you've got outside some tiny subset of the list that might be hot in cache....
Ooops, I was still thinking of the global-unused-list here.
Re: [patch 5/5] vfs: per superblock dentry unused list
Quoted 9 lines · Jan Blunck
Quoted 7 lines · David Chinner
You've just described the embodiment of the two order's of magnitude issue I mentioned. That's not a wrong assumption - think of the above case with
global_unusedcount now being 1.28*10^7 instead of 1.28x10^4. How many dentries do you have to free before freeing any on the small superblock if we don't free one per call? (quick
answer: 99.9%).
Quoted 7 lines
Quoted 4 lines
If we shrink one per call, we've freed all 128 dentries while there is still 1*10^5 dentries on the large list. That seems like a much better balance to make within the constraints of the shrinker resolution we have to work with.
With the effect that the dcache is completely useless for small filesystems as long as there is one big one.
Not necessarily. I think that as long as the small filesystem is not being used, then we _should_ be reclaiming slowly from it, regardless of how big the other filesystems are. That's the way the global list ends up working now as the dentries for the small filesystem get purged according to LRU.
Quoted 4 lines
Filesystems where regularily a small amount
of files is used don't have any cached dentries but the filesystem where someone touched every file still has a lot of dentries in cache although they are never used again.
Or alternatively small filesystems with no activity and dentries that will never get used again never get trimmed while the large fielsytem with lots of activity gets trimmed. This can lead to thousands of pages being pinned in slabs that we don't try to free up until we've already free >90% of the overall caches. That's not very appealing, IMO.
So I suggest the question is this - how do you define "not being used"? I guess dentry age or "last dentry was added to list at time X" (recorded in dput()) would be one way of determining this.
Say something like:
dput():
list_add(&dentry->d_lru, &dentry->d_sb->s_unused); dentry->s_sb->s_unused_age = jiffies + (&dentry->d_sb->s_dentry_stat.age_limit * HZ);
prune_dcache():
tmp = sb->s_dentry_stat.nr_unused/((unused/count)+1); if ((tmp == 0) && time_after(jiffies, sb->s_unused_age)) tmp = 1;
That would turn over small unused dentry lists that have not been modified for age_limit seconds. That means that small caches that are being used are not reclaimed prematurely, and those small caches would also be reclaimed after some time if they are not being used. That seems to address both our concerns.....
Thoughts?
Cheers,
Dave.
Signature · 3 lines
Dave Chinner R&D Software Enginner SGI Australian Software Group