svn commit: r349823 - in stable/12/sys: geom kern sys

Alexander Motin mav at FreeBSD.org
Sun Jul 7 18:44:53 UTC 2019


Author: mav
Date: Sun Jul  7 18:44:51 2019
New Revision: 349823
URL: https://svnweb.freebsd.org/changeset/base/349823

Log:
  MFC r349178: Optimize kern.geom.conf* sysctls.
  
  On large systems those sysctls may generate megabytes of output.  Before
  this change sbuf(9) code was resizing buffer by 4KB each time many times,
  generating tons of TLB shootdowns.  Unfortunately in this case existing
  sbuf_new_for_sysctl() mechanism, supposed to help with this issue, is not
  applicable, since all the sbuf writes are done in different kernel thread.
  
  This change improves situation in two ways:
   - on first sysctl call, not providing any output buffer, it sets special
  sbuf drain function, just counting the data and so not needing big buffer;
   - on second sysctl call it uses as initial buffer size value saved on
  previous call, so that in most cases there will be no reallocation, unless
  GEOM topology changed significantly.

Modified:
  stable/12/sys/geom/geom_kern.c
  stable/12/sys/kern/imgact_elf.c
  stable/12/sys/kern/subr_sbuf.c
  stable/12/sys/sys/sbuf.h
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/sys/geom/geom_kern.c
==============================================================================
--- stable/12/sys/geom/geom_kern.c	Sun Jul  7 18:42:25 2019	(r349822)
+++ stable/12/sys/geom/geom_kern.c	Sun Jul  7 18:44:51 2019	(r349823)
@@ -157,42 +157,51 @@ g_init(void)
 }
 
 static int
-sysctl_kern_geom_conftxt(SYSCTL_HANDLER_ARGS)
+sysctl_kern_geom_confany(struct sysctl_req *req, g_event_t *func, size_t *hint)
 {
-	int error;
+	size_t len = 0;
+	int error = 0;
 	struct sbuf *sb;
 
-	sb = sbuf_new_auto();
-	g_waitfor_event(g_conftxt, sb, M_WAITOK, NULL);
-	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
+	if (req->oldptr == NULL) {
+		sb = sbuf_new(NULL, NULL, PAGE_SIZE, SBUF_FIXEDLEN |
+		    SBUF_INCLUDENUL);
+		sbuf_set_drain(sb, sbuf_count_drain, &len);
+		g_waitfor_event(func, sb, M_WAITOK, NULL);
+		req->oldidx = *hint = len;
+	} else {
+		sb = sbuf_new(NULL, NULL, *hint, SBUF_AUTOEXTEND |
+		    SBUF_INCLUDENUL);
+		g_waitfor_event(func, sb, M_WAITOK, NULL);
+		*hint = sbuf_len(sb);
+		error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
+	}
 	sbuf_delete(sb);
 	return error;
 }
+
+static int
+sysctl_kern_geom_conftxt(SYSCTL_HANDLER_ARGS)
+{
+	static size_t hint = PAGE_SIZE;
+
+	return (sysctl_kern_geom_confany(req, g_conftxt, &hint));
+}
  
 static int
 sysctl_kern_geom_confdot(SYSCTL_HANDLER_ARGS)
 {
-	int error;
-	struct sbuf *sb;
+	static size_t hint = PAGE_SIZE;
 
-	sb = sbuf_new_auto();
-	g_waitfor_event(g_confdot, sb, M_WAITOK, NULL);
-	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
-	sbuf_delete(sb);
-	return error;
+	return (sysctl_kern_geom_confany(req, g_confdot, &hint));
 }
- 
+
 static int
 sysctl_kern_geom_confxml(SYSCTL_HANDLER_ARGS)
 {
-	int error;
-	struct sbuf *sb;
+	static size_t hint = PAGE_SIZE;
 
-	sb = sbuf_new_auto();
-	g_waitfor_event(g_confxml, sb, M_WAITOK, NULL);
-	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
-	sbuf_delete(sb);
-	return error;
+	return (sysctl_kern_geom_confany(req, g_confxml, &hint));
 }
 
 SYSCTL_NODE(_kern, OID_AUTO, geom, CTLFLAG_RW, 0, "GEOMetry management");

Modified: stable/12/sys/kern/imgact_elf.c
==============================================================================
--- stable/12/sys/kern/imgact_elf.c	Sun Jul  7 18:42:25 2019	(r349822)
+++ stable/12/sys/kern/imgact_elf.c	Sun Jul  7 18:44:51 2019	(r349823)
@@ -1423,7 +1423,6 @@ static void __elfN(puthdr)(struct thread *, void *, si
 static void __elfN(putnote)(struct note_info *, struct sbuf *);
 static size_t register_note(struct note_info_list *, int, outfunc_t, void *);
 static int sbuf_drain_core_output(void *, const char *, int);
-static int sbuf_drain_count(void *arg, const char *data, int len);
 
 static void __elfN(note_fpregset)(void *, struct sbuf *, size_t *);
 static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *);
@@ -1553,19 +1552,6 @@ sbuf_drain_core_output(void *arg, const char *data, in
 	return (len);
 }
 
-/*
- * Drain into a counter.
- */
-static int
-sbuf_drain_count(void *arg, const char *data __unused, int len)
-{
-	size_t *sizep;
-
-	sizep = (size_t *)arg;
-	*sizep += len;
-	return (len);
-}
-
 int
 __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags)
 {
@@ -2342,7 +2328,7 @@ note_procstat_files(void *arg, struct sbuf *sb, size_t
 	if (sb == NULL) {
 		size = 0;
 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
-		sbuf_set_drain(sb, sbuf_drain_count, &size);
+		sbuf_set_drain(sb, sbuf_count_drain, &size);
 		sbuf_bcat(sb, &structsize, sizeof(structsize));
 		PROC_LOCK(p);
 		kern_proc_filedesc_out(p, sb, -1, filedesc_flags);
@@ -2393,7 +2379,7 @@ note_procstat_vmmap(void *arg, struct sbuf *sb, size_t
 	if (sb == NULL) {
 		size = 0;
 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
-		sbuf_set_drain(sb, sbuf_drain_count, &size);
+		sbuf_set_drain(sb, sbuf_count_drain, &size);
 		sbuf_bcat(sb, &structsize, sizeof(structsize));
 		PROC_LOCK(p);
 		kern_proc_vmmap_out(p, sb, -1, vmmap_flags);
@@ -2521,7 +2507,7 @@ __elfN(note_procstat_auxv)(void *arg, struct sbuf *sb,
 	if (sb == NULL) {
 		size = 0;
 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
-		sbuf_set_drain(sb, sbuf_drain_count, &size);
+		sbuf_set_drain(sb, sbuf_count_drain, &size);
 		sbuf_bcat(sb, &structsize, sizeof(structsize));
 		PHOLD(p);
 		proc_getauxv(curthread, p, sb);

Modified: stable/12/sys/kern/subr_sbuf.c
==============================================================================
--- stable/12/sys/kern/subr_sbuf.c	Sun Jul  7 18:42:25 2019	(r349822)
+++ stable/12/sys/kern/subr_sbuf.c	Sun Jul  7 18:44:51 2019	(r349823)
@@ -342,6 +342,21 @@ sbuf_setpos(struct sbuf *s, ssize_t pos)
 }
 
 /*
+ * Drain into a counter.  Counts amount of data without prodicing output.
+ * Useful for cases like sysctl, where user may first request only size.
+ * This allows to avoid pointless allocation/freeing of large buffers.
+ */
+int
+sbuf_count_drain(void *arg, const char *data __unused, int len)
+{
+	size_t *sizep;
+
+	sizep = (size_t *)arg;
+	*sizep += len;
+	return (len);
+}
+
+/*
  * Set up a drain function and argument on an sbuf to flush data to
  * when the sbuf buffer overflows.
  */

Modified: stable/12/sys/sys/sbuf.h
==============================================================================
--- stable/12/sys/sys/sbuf.h	Sun Jul  7 18:42:25 2019	(r349822)
+++ stable/12/sys/sys/sbuf.h	Sun Jul  7 18:44:51 2019	(r349823)
@@ -103,6 +103,7 @@ void		 sbuf_start_section(struct sbuf *, ssize_t *);
 ssize_t		 sbuf_end_section(struct sbuf *, ssize_t, size_t, int);
 void		 sbuf_hexdump(struct sbuf *, const void *, int, const char *,
 		     int);
+int		 sbuf_count_drain(void *arg, const char *data, int len);
 void		 sbuf_putbuf(struct sbuf *);
 
 #ifdef _KERNEL


More information about the svn-src-stable-12 mailing list