Skip to contentSkip to conversation

Tuning parameters for inode, dcache, and pagecache

PATCH New Indoh Takao 1 participant 1 message Mar 13, 2003

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

PATCH 1/1 Tuning parameters for inode, dcache, and pagecache

Hello.

Linux doesn't have parameters which control the resources, such as page-cache, dentry-cache. Therefore, these resources waste free area of memory. Especially, page-cache grows until all memory is used. So, I made a patch to add new tunable parameters as follows.

- /proc/sys/fs/inode-max : maximum number of inode in the memory
- /proc/sys/fs/dentry-max : maximum number of dentry-cache
- /proc/sys/vm/pgcache-max : maximum number of pages used as pagecache

The attached patch is for 2.5.64. If you don't set a value to these parameters, these resources are not limited. (The default of each parameter is INT_MAX or ULONG_MAX)

Please comment.

Thanks.
--------------------------------------------------
Takao Indoh
E-Mail : [email protected]

fs/dcache.c +81 -0
diff -Nur linux-2.5.64/fs/dcache.c linux-2.5.64-new/fs/dcache.c
@@ -53,6 +53,7 @@
 /* Statistics gathering. */
 struct dentry_stat_t dentry_stat = {
 	.age_limit = 45,
+	.max_dentry = INT_MAX,
 };
 
 static void d_callback(void *arg)
@@ -395,6 +396,60 @@
 	spin_unlock(&dcache_lock);
 }
 
+
+/**
+ * purne_nr_dcache - Try to free `nr_dcache' dcaches
+ * @nr_dcache: number of dcache to free
+ * @forced: If not 0, free dcache forcibly
+ *
+ * This function tries to free dcache until all entry are scanned
+ * or `nr_dcache' entry are freed.
+ *
+ * This frees dcache forcibly if needed.
+ */
+
+void prune_nr_dcache(int nr_dcache, int forced)
+{
+	int nr_freed;
+	int nr_to_scan = dentry_stat.nr_unused;
+
+	spin_lock(&dcache_lock);
+	for(nr_freed = 0; nr_to_scan&&(nr_freed < nr_dcache); nr_to_scan--) {
+		struct dentry *dentry;
+		struct list_head *tmp;
+
+		tmp = dentry_unused.prev;
+		if (tmp == &dentry_unused)
+			break;
+		list_del_init(tmp);
+		dentry_stat.nr_unused--;
+		dentry = list_entry(tmp, struct dentry, d_lru);
+
+		spin_lock(&dentry->d_lock);
+		if (!forced) {
+			/* If the dentry was recently referenced,
+			    don't free it. */
+			if (dentry->d_vfs_flags & DCACHE_REFERENCED) {
+				dentry->d_vfs_flags &= ~DCACHE_REFERENCED;
+
+				/* don't add non zero d_count dentries
+				 * back to d_lru list
+				 */
+				if (!atomic_read(&dentry->d_count)) {
+					list_add(&dentry->d_lru, &dentry_unused);
+					dentry_stat.nr_unused++;
+				}
+				spin_unlock(&dentry->d_lock);
+				continue;
+			}
+		}
+		prune_one_dentry(dentry);
+		nr_freed++;
+	}
+	spin_unlock(&dcache_lock);
+	return;
+}
+
 /*
  * Shrink the dcache for the specified super block.
  * This allows us to unmount a device without disturbing
@@ -664,6 +719,9 @@
 }
 
 #define NAME_ALLOC_LEN(len)	((len+16) & ~15)
+#define NR_DENTRY_TO_FREE \
+	max((dentry_stat.nr_dentry - dentry_stat.max_dentry + 1), \
+	    (dentry_stat.nr_unused >> 3))
 
 /**
  * d_alloc	-	allocate a dcache entry
@@ -681,6 +739,27 @@
 	struct dentry *dentry;
 	struct qstr * qstr;
 
+	/*
+	 * Check the number of dentry.
+	 * If it exceeds maximum, shrink dentry.
+	 */
+	if (dentry_stat.nr_dentry < dentry_stat.max_dentry)
+		goto start_alloc;
+
+	/* STEP1: just shrinking dentry */
+	prune_nr_dcache(NR_DENTRY_TO_FREE, 0);
+	if (dentry_stat.nr_dentry < dentry_stat.max_dentry)
+		goto start_alloc;
+
+	/* STEP2: shrinking dentry forcibly */
+	prune_nr_dcache(NR_DENTRY_TO_FREE, 1);
+	if (dentry_stat.nr_dentry < dentry_stat.max_dentry)
+		goto start_alloc;
+
+	printk(KERN_ERR "The number of dentry exceeded maximum.\n");
+	return NULL;
+
+start_alloc:
 	dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL); 
 	if (!dentry)
 		return NULL;
@@ -741,6 +820,8 @@
 
 	return dentry;
 }
+
+#undef NR_DENTRY_TO_FREE
 
 /**
  * d_instantiate - fill in inode information for a dentry
fs/inode.c +73 -1

101 lines not on this page · raw message

include/linux/dcache.h +2 -1
diff -Nur linux-2.5.64/include/linux/dcache.h linux-2.5.64-new/include/linux/dcache.h
@@ -39,7 +39,8 @@
 	int nr_unused;
 	int age_limit;          /* age in seconds */
 	int want_pages;         /* pages requested by system */
-	int dummy[2];
+	int max_dentry;
+	int dummy[1];
 };
 extern struct dentry_stat_t dentry_stat;
 
include/linux/fs.h +2 -1
diff -Nur linux-2.5.64/include/linux/fs.h linux-2.5.64-new/include/linux/fs.h
@@ -58,7 +58,8 @@
 struct inodes_stat_t {
 	int nr_inodes;
 	int nr_unused;
-	int dummy[5];
+	int max_inodes;
+	int dummy[4];
 };
 extern struct inodes_stat_t inodes_stat;
 
include/linux/gfp.h +1 -0
diff -Nur linux-2.5.64/include/linux/gfp.h linux-2.5.64-new/include/linux/gfp.h
@@ -18,6 +18,7 @@
 #define __GFP_FS	0x80	/* Can call down to low-level FS? */
 #define __GFP_COLD	0x100	/* Cache-cold page required */
 #define __GFP_NOWARN	0x200	/* Suppress page allocation failure warning */
+#define __GFP_PGCACHE   0x400   /* Page-cache required */
 
 #define GFP_ATOMIC	(__GFP_HIGH)
 #define GFP_NOIO	(__GFP_WAIT)
include/linux/mm.h +1 -0
diff -Nur linux-2.5.64/include/linux/mm.h linux-2.5.64-new/include/linux/mm.h
@@ -22,6 +22,7 @@
 extern unsigned long num_physpages;
 extern void * high_memory;
 extern int page_cluster;
+extern unsigned long max_pgcache;
 
 #include <asm/page.h>
 #include <asm/pgtable.h>
include/linux/page-flags.h +5 -0
diff -Nur linux-2.5.64/include/linux/page-flags.h linux-2.5.64-new/include/linux/page-flags.h
@@ -74,6 +74,7 @@
 #define PG_mappedtodisk		17	/* Has blocks allocated on-disk */
 #define PG_reclaim		18	/* To be reclaimed asap */
 #define PG_compound		19	/* Part of a compound page */
+#define PG_pgcache		20	/* Page is used as pagecache */
 
 /*
  * Global page accounting.  One instance per CPU.  Only unsigned longs are
@@ -255,6 +256,10 @@
 #define PageCompound(page)	test_bit(PG_compound, &(page)->flags)
 #define SetPageCompound(page)	set_bit(PG_compound, &(page)->flags)
 #define ClearPageCompound(page)	clear_bit(PG_compound, &(page)->flags)
+
+#define PagePgcache(page)      test_bit(PG_pgcache, &(page)->flags)
+#define SetPagePgcache(page)   set_bit(PG_pgcache, &(page)->flags)
+#define ClearPagePgcache(page) clear_bit(PG_pgcache, &(page)->flags)
 
 /*
  * The PageSwapCache predicate doesn't use a PG_flag at this time,
include/linux/pagemap.h +3 -2
diff -Nur linux-2.5.64/include/linux/pagemap.h linux-2.5.64-new/include/linux/pagemap.h
@@ -29,12 +29,12 @@
 
 static inline struct page *page_cache_alloc(struct address_space *x)
 {
-	return alloc_pages(x->gfp_mask, 0);
+	return alloc_pages(x->gfp_mask|__GFP_PGCACHE, 0);
 }
 
 static inline struct page *page_cache_alloc_cold(struct address_space *x)
 {
-	return alloc_pages(x->gfp_mask|__GFP_COLD, 0);
+	return alloc_pages(x->gfp_mask|__GFP_COLD|__GFP_PGCACHE, 0);
 }
 
 typedef int filler_t(void *, struct page *);
@@ -80,6 +80,7 @@
 	list_add(&page->list, &mapping->clean_pages);
 	page->mapping = mapping;
 	page->index = index;
+	SetPagePgcache(page);
 
 	mapping->nrpages++;
 	inc_page_state(nr_pagecache);
include/linux/sysctl.h +2 -0
diff -Nur linux-2.5.64/include/linux/sysctl.h linux-2.5.64-new/include/linux/sysctl.h
@@ -155,6 +155,7 @@
 	VM_HUGETLB_PAGES=18,	/* int: Number of available Huge Pages */
 	VM_SWAPPINESS=19,	/* Tendency to steal mapped memory */
 	VM_LOWER_ZONE_PROTECTION=20,/* Amount of protection of lower zones */
+	VM_MAXPGCACHE=21,/* maximum number of page used as pagecache */
 };
 
 
@@ -576,6 +577,7 @@
 	FS_LEASE_TIME=15,	/* int: maximum time to wait for a lease break */
 	FS_DQSTATS=16,	/* disc quota usage statistics */
 	FS_XFS=17,	/* struct: control xfs parameters */
+	FS_MAXDENTRY=18,	/* int:maximum number of dcache that can be allocated */
 };
 
 /* /proc/sys/fs/quota/ */
kernel/sysctl.c +7 -0
diff -Nur linux-2.5.64/kernel/sysctl.c linux-2.5.64-new/kernel/sysctl.c
@@ -319,6 +319,8 @@
 	 &sysctl_lower_zone_protection, sizeof(sysctl_lower_zone_protection),
 	 0644, NULL, &proc_dointvec_minmax, &sysctl_intvec, NULL, &zero,
 	 NULL, },
+	{VM_MAXPGCACHE, "pgcache-max", &max_pgcache, sizeof(unsigned long),
+	 0644, NULL,&proc_dointvec_minmax, &sysctl_intvec, NULL,&zero,NULL},
 	{0}
 };
 
@@ -331,6 +333,9 @@
 	 0444, NULL, &proc_dointvec},
 	{FS_STATINODE, "inode-state", &inodes_stat, 7*sizeof(int),
 	 0444, NULL, &proc_dointvec},
+	{FS_MAXINODE, "inode-max", &inodes_stat.max_inodes, sizeof(int), 0644, NULL,
+	 &proc_dointvec_minmax, &sysctl_intvec, NULL,
+	 &zero, NULL},
 	{FS_NRFILE, "file-nr", &files_stat, 3*sizeof(int),
 	 0444, NULL, &proc_dointvec},
 	{FS_MAXFILE, "file-max", &files_stat.max_files, sizeof(int),
@@ -349,6 +354,8 @@
 	 sizeof(int), 0644, NULL, &proc_dointvec},
 	{FS_LEASE_TIME, "lease-break-time", &lease_break_time, sizeof(int),
 	 0644, NULL, &proc_dointvec},
+	{FS_MAXDENTRY, "dentry-max", &dentry_stat.max_dentry, sizeof(int),
+	 0644, NULL,&proc_dointvec_minmax, &sysctl_intvec, NULL,&zero,NULL},
 	{0}
 };
 
mm/filemap.c +3 -2
diff -Nur linux-2.5.64/mm/filemap.c linux-2.5.64-new/mm/filemap.c
@@ -86,6 +86,7 @@
 	radix_tree_delete(&mapping->page_tree, page->index);
 	list_del(&page->list);
 	page->mapping = NULL;
+	ClearPagePgcache(page);
 
 	mapping->nrpages--;
 	dec_page_state(nr_pagecache);
@@ -437,7 +438,7 @@
 	page = find_lock_page(mapping, index);
 	if (!page) {
 		if (!cached_page) {
-			cached_page = alloc_page(gfp_mask);
+			cached_page = alloc_page(gfp_mask|__GFP_PGCACHE);
 			if (!cached_page)
 				return NULL;
 		}
@@ -507,7 +508,7 @@
 		return NULL;
 	}
 	gfp_mask = mapping->gfp_mask & ~__GFP_FS;
-	page = alloc_pages(gfp_mask, 0);
+	page = alloc_pages(gfp_mask|__GFP_PGCACHE, 0);
 	if (page && add_to_page_cache_lru(page, mapping, index, gfp_mask)) {
 		page_cache_release(page);
 		page = NULL;
mm/page_alloc.c +43 -0

61 lines not on this page · raw message

mm/swap_state.c +1 -1
diff -Nur linux-2.5.64/mm/swap_state.c linux-2.5.64-new/mm/swap_state.c
@@ -360,7 +360,7 @@
 		 * Get a new page to read into from swap.
 		 */
 		if (!new_page) {
-			new_page = alloc_page(GFP_HIGHUSER);
+			new_page = alloc_page(GFP_HIGHUSER|__GFP_PGCACHE);
 			if (!new_page)
 				break;		/* Out of memory */
 		}
mm/vmscan.c +41 -0

53 lines not on this page · raw message