Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 18 Jun 2019 21:05:11 +0000 (UTC)
From:      Alexander Motin <mav@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r349178 - in head/sys: geom kern sys
Message-ID:  <201906182105.x5IL5Bgk003827@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: mav
Date: Tue Jun 18 21:05:10 2019
New Revision: 349178
URL: https://svnweb.freebsd.org/changeset/base/349178

Log:
  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.
  
  MFC after:	1 week
  Sponsored by:	iXsystems, Inc.

Modified:
  head/sys/geom/geom_kern.c
  head/sys/kern/imgact_elf.c
  head/sys/kern/subr_sbuf.c
  head/sys/sys/sbuf.h

Modified: head/sys/geom/geom_kern.c
==============================================================================
--- head/sys/geom/geom_kern.c	Tue Jun 18 21:02:40 2019	(r349177)
+++ head/sys/geom/geom_kern.c	Tue Jun 18 21:05:10 2019	(r349178)
@@ -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: head/sys/kern/imgact_elf.c
==============================================================================
--- head/sys/kern/imgact_elf.c	Tue Jun 18 21:02:40 2019	(r349177)
+++ head/sys/kern/imgact_elf.c	Tue Jun 18 21:05:10 2019	(r349178)
@@ -1422,7 +1422,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 *);
@@ -1552,19 +1551,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)
 {
@@ -2341,7 +2327,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);
@@ -2392,7 +2378,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);
@@ -2520,7 +2506,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: head/sys/kern/subr_sbuf.c
==============================================================================
--- head/sys/kern/subr_sbuf.c	Tue Jun 18 21:02:40 2019	(r349177)
+++ head/sys/kern/subr_sbuf.c	Tue Jun 18 21:05:10 2019	(r349178)
@@ -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: head/sys/sys/sbuf.h
==============================================================================
--- head/sys/sys/sbuf.h	Tue Jun 18 21:02:40 2019	(r349177)
+++ head/sys/sys/sbuf.h	Tue Jun 18 21:05:10 2019	(r349178)
@@ -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);
 int		 sbuf_printf_drain(void *arg, const char *data, int len);
 void		 sbuf_putbuf(struct sbuf *);
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201906182105.x5IL5Bgk003827>