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.
This commit is contained in:
Alexander Motin 2019-06-18 21:05:10 +00:00
parent cfdcc8c7fa
commit 5c32e9fcb2
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=349178
4 changed files with 48 additions and 37 deletions

View File

@ -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");

View File

@ -1422,7 +1422,6 @@ static void __elfN(puthdr)(struct thread *, void *, size_t, int, size_t);
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, int len)
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 *sizep)
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 *sizep)
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, size_t *sizep)
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);

View File

@ -341,6 +341,21 @@ sbuf_setpos(struct sbuf *s, ssize_t pos)
return (0);
}
/*
* 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.

View File

@ -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 *);