Module Name:    src
Committed By:   rmind
Date:           Sat Apr 23 18:57:28 UTC 2011

Modified Files:
        src/sys/kern: kern_descrip.c
        src/sys/sys: file.h

Log Message:
- Sprinkle __cacheline_aligned and __read_mostly in file descriptor code.
- While here, remove trailing whitespaces, KNF.


To generate a diff of this commit:
cvs rdiff -u -r1.212 -r1.213 src/sys/kern/kern_descrip.c
cvs rdiff -u -r1.72 -r1.73 src/sys/sys/file.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/kern_descrip.c
diff -u src/sys/kern/kern_descrip.c:1.212 src/sys/kern/kern_descrip.c:1.213
--- src/sys/kern/kern_descrip.c:1.212	Sun Apr 10 15:45:33 2011
+++ src/sys/kern/kern_descrip.c	Sat Apr 23 18:57:27 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_descrip.c,v 1.212 2011/04/10 15:45:33 christos Exp $	*/
+/*	$NetBSD: kern_descrip.c,v 1.213 2011/04/23 18:57:27 rmind Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.212 2011/04/10 15:45:33 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.213 2011/04/23 18:57:27 rmind Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -97,6 +97,17 @@
 #include <sys/sysctl.h>
 #include <sys/ktrace.h>
 
+/*
+ * A list (head) of open files, counter, and lock protecting them.
+ */
+struct filelist		filehead	__cacheline_aligned;
+static u_int		nfiles		__cacheline_aligned;
+kmutex_t		filelist_lock	__cacheline_aligned;
+
+static pool_cache_t	filedesc_cache	__read_mostly;
+static pool_cache_t	file_cache	__read_mostly;
+static pool_cache_t	fdfile_cache	__read_mostly;
+
 static int	file_ctor(void *, void *, int);
 static void	file_dtor(void *, void *);
 static int	fdfile_ctor(void *, void *, int);
@@ -110,14 +121,6 @@
 static void fill_file(struct kinfo_file *, const file_t *, const fdfile_t *,
 		      int, pid_t);
 
-kmutex_t	filelist_lock;	/* lock on filehead */
-struct filelist	filehead;	/* head of list of open files */
-u_int		nfiles;		/* actual number of open files */
-
-static pool_cache_t filedesc_cache;
-static pool_cache_t file_cache;
-static pool_cache_t fdfile_cache;
-
 const struct cdevsw filedesc_cdevsw = {
 	filedescopen, noclose, noread, nowrite, noioctl,
 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER | D_MPSAFE,
@@ -206,9 +209,7 @@
 			KASSERT(fd_isused(fdp, fd));
 		}
 	}
-#else	/* DEBUG */
-	/* nothing */
-#endif	/* DEBUG */
+#endif
 }
 
 static int
@@ -240,7 +241,7 @@
 		off++;
 	}
 
-	return (-1);
+	return -1;
 
  found:
 	return (off << NDENTRYSHIFT) + ffs(~sub) - 1;
@@ -263,7 +264,7 @@
 		off--;
 
 	if (off < 0)
-		return (-1);
+		return -1;
 
 	i = ((off + 1) << NDENTRYSHIFT) - 1;
 	if (i >= last)
@@ -273,7 +274,7 @@
 	while (i > 0 && (ff[i] == NULL || !ff[i]->ff_allocated))
 		i--;
 
-	return (i);
+	return i;
 }
 
 static inline void
@@ -288,9 +289,9 @@
 	KASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) == 0);
 	KASSERT(ff != NULL);
 	KASSERT(ff->ff_file == NULL);
-   	KASSERT(!ff->ff_allocated);
+	KASSERT(!ff->ff_allocated);
 
-   	ff->ff_allocated = 1;
+	ff->ff_allocated = 1;
 	fdp->fd_lomap[off] |= 1 << (fd & NDENTRYMASK);
 	if (__predict_false(fdp->fd_lomap[off] == ~0)) {
 		KASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
@@ -322,7 +323,7 @@
 	 */
 	KASSERT(ff != NULL);
 	KASSERT(ff->ff_file == NULL);
-   	KASSERT(ff->ff_allocated);
+	KASSERT(ff->ff_allocated);
 
 	if (fd < fdp->fd_freefile) {
 		fdp->fd_freefile = fd;
@@ -709,11 +710,9 @@
 int
 fd_dup(file_t *fp, int minfd, int *newp, bool exclose)
 {
-	proc_t *p;
+	proc_t *p = curproc;
 	int error;
 
-	p = curproc;
-
 	while ((error = fd_alloc(p, minfd, newp)) != 0) {
 		if (error != ENOSPC) {
 			return error;
@@ -732,12 +731,10 @@
 int
 fd_dup2(file_t *fp, unsigned new)
 {
-	filedesc_t *fdp;
+	filedesc_t *fdp = curlwp->l_fd;
 	fdfile_t *ff;
 	fdtab_t *dt;
 
-	fdp = curlwp->l_fd;
-
 	/*
 	 * Ensure there are enough slots in the descriptor table,
 	 * and allocate an fdfile_t up front in case we need it.
@@ -772,7 +769,7 @@
 		KASSERT(new >= NDFDFILE);
 		dt->dt_ff[new] = ff;
 		ff = NULL;
-	}		
+	}
 	fd_used(fdp, new);
 	mutex_exit(&fdp->fd_lock);
 
@@ -807,8 +804,8 @@
 	mutex_exit(&fp->f_lock);
 
 	/* We held the last reference - release locks, close and free. */
-        if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
-        	lf.l_whence = SEEK_SET;
+	if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
+		lf.l_whence = SEEK_SET;
 		lf.l_start = 0;
 		lf.l_len = 0;
 		lf.l_type = F_UNLCK;
@@ -832,15 +829,13 @@
 int
 fd_alloc(proc_t *p, int want, int *result)
 {
-	filedesc_t *fdp;
+	filedesc_t *fdp = p->p_fd;
 	int i, lim, last, error;
 	u_int off, new;
 	fdtab_t *dt;
 
 	KASSERT(p == curproc || p == &proc0);
 
-	fdp = p->p_fd;
-
 	/*
 	 * Search for a free descriptor starting at the higher
 	 * of want or fd_freefile.
@@ -1070,13 +1065,11 @@
 int
 fd_allocfile(file_t **resultfp, int *resultfd)
 {
+	proc_t *p = curproc;
 	kauth_cred_t cred;
 	file_t *fp;
-	proc_t *p;
 	int error;
 
-	p = curproc;
-
 	while ((error = fd_alloc(p, 0, resultfd)) != 0) {
 		if (error != ENOSPC) {
 			return error;
@@ -1243,9 +1236,8 @@
 {
 	file_t *fp;
 
-	fp = kmem_alloc(sizeof(*fp), KM_SLEEP);
+	fp = kmem_zalloc(sizeof(*fp), KM_SLEEP);
 	if (fp != NULL) {
-		memset(fp, 0, sizeof(*fp));
 		mutex_init(&fp->f_lock, MUTEX_DEFAULT, IPL_NONE);
 	}
 	return fp;
@@ -1458,8 +1450,9 @@
 		}
 		if (__predict_false(fp->f_type == DTYPE_KQUEUE)) {
 			/* kqueue descriptors cannot be copied. */
-                       if (i < newfdp->fd_freefile)
-                               newfdp->fd_freefile = i;
+			if (i < newfdp->fd_freefile) {
+				newfdp->fd_freefile = i;
+			}
 			continue;
 		}
 		/* It's active: add a reference to the file. */
@@ -1494,8 +1487,8 @@
 	newfdp->fd_lastfile = newlast;
 	fd_checkmaps(newfdp);
 	mutex_exit(&fdp->fd_lock);
-	
-	return (newfdp);
+
+	return newfdp;
 }
 
 /*
@@ -1781,6 +1774,7 @@
 {
 	filedesc_t *fdp = l->l_fd;
 	fdfile_t *ff = fdp->fd_dt->dt_ff[fd];
+
 	ff->ff_exclose = exclose;
 	if (exclose)
 		fdp->fd_exclose = true;
@@ -1803,7 +1797,7 @@
 		*(int *)data = pgid;
 		break;
 	}
-	return (0);
+	return 0;
 }
 
 /*
@@ -1949,7 +1943,6 @@
 		u_int i;
 
 		mutex_enter(&fd->fd_lock);
-
 		dt = fd->fd_dt;
 		for (i = 0; i < dt->dt_nfiles; i++) {
 			struct file *fp;
@@ -1958,14 +1951,11 @@
 			if ((ff = dt->dt_ff[i]) == NULL) {
 				continue;
 			}
-
 			if ((fp = ff->ff_file) == NULL) {
 				continue;
 			}
-
 			fp->f_marker = 0;
 		}
-
 		mutex_exit(&fd->fd_lock);
 	}
 }
@@ -1991,7 +1981,7 @@
 		 */
 		*oldlenp = sizeof(filehead) + (nfiles + 10) *
 		    sizeof(struct file);
-		return (0);
+		return 0;
 	}
 
 	/*
@@ -1999,12 +1989,12 @@
 	 */
 	if (buflen < sizeof(filehead)) {
 		*oldlenp = 0;
-		return (0);
+		return 0;
 	}
 	sysctl_unlock();
 	error = sysctl_copyout(l, &filehead, where, sizeof(filehead));
 	if (error) {
-	 	sysctl_relock();
+		sysctl_relock();
 		return error;
 	}
 	buflen -= sizeof(filehead);
@@ -2071,7 +2061,7 @@
 			/* Check that we have enough space. */
 			if (buflen < sizeof(struct file)) {
 				*oldlenp = where - start;
-			    	mutex_exit(&fp->f_lock);
+				mutex_exit(&fp->f_lock);
 				error = ENOMEM;
 				break;
 			}
@@ -2110,8 +2100,8 @@
 	mutex_exit(&sysctl_file_marker_lock);
 
 	*oldlenp = where - start;
- 	sysctl_relock();
-	return (error);
+	sysctl_relock();
+	return error;
 }
 
 /*
@@ -2132,10 +2122,10 @@
 	fdtab_t *dt;
 
 	if (namelen == 1 && name[0] == CTL_QUERY)
-		return (sysctl_query(SYSCTLFN_CALL(rnode)));
+		return sysctl_query(SYSCTLFN_CALL(rnode));
 
 	if (namelen != 4)
-		return (EINVAL);
+		return EINVAL;
 
 	error = 0;
 	dp = oldp;
@@ -2148,7 +2138,7 @@
 	needed = 0;
 
 	if (elem_size < 1 || elem_count < 0)
-		return (EINVAL);
+		return EINVAL;
 
 	switch (op) {
 	case KERN_FILE_BYFILE:
@@ -2165,7 +2155,7 @@
 
 		if ((op == KERN_FILE_BYPID) && (arg < -1))
 			/* -1 means all processes */
-			return (EINVAL);
+			return EINVAL;
 
 		sysctl_unlock();
 		if (op == KERN_FILE_BYFILE)
@@ -2261,14 +2251,14 @@
 		sysctl_relock();
 		break;
 	default:
-		return (EINVAL);
+		return EINVAL;
 	}
 
 	if (oldp == NULL)
 		needed += KERN_FILESLOP * elem_size;
 	*oldlenp = needed;
 
-	return (error);
+	return error;
 }
 
 static void

Index: src/sys/sys/file.h
diff -u src/sys/sys/file.h:1.72 src/sys/sys/file.h:1.73
--- src/sys/sys/file.h:1.72	Mon Apr 11 22:31:43 2011
+++ src/sys/sys/file.h	Sat Apr 23 18:57:28 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: file.h,v 1.72 2011/04/11 22:31:43 rmind Exp $	*/
+/*	$NetBSD: file.h,v 1.73 2011/04/23 18:57:28 rmind Exp $	*/
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -143,7 +143,6 @@
 LIST_HEAD(filelist, file);
 extern struct filelist	filehead;	/* head of list of open files */
 extern u_int		maxfiles;	/* kernel limit on # of open files */
-extern u_int		nfiles;		/* actual number of open files */
 
 extern const struct fileops vnops;	/* vnode operations for files */
 

Reply via email to