summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorschwarze <schwarze@openbsd.org>2014-04-11 15:45:39 +0000
committerschwarze <schwarze@openbsd.org>2014-04-11 15:45:39 +0000
commit1d77badb402de9e47dda0f4347a04b3a7f27040b (patch)
tree6b2b09a9cd41764144608180b59f1aed9251dbc5
parentInterfaces are associated to rdomains, make it clear by renaming (diff)
downloadwireguard-openbsd-1d77badb402de9e47dda0f4347a04b3a7f27040b.tar.xz
wireguard-openbsd-1d77badb402de9e47dda0f4347a04b3a7f27040b.zip
Further apropos(1) speed optimization was trickier than anticipated.
Contrary to what i initially thought, almost all time is now spent inside sqlite3(3) routines, and i found no easy way calling less of them. However, sqlite(3) spends substantial time in malloc(3), and even more (twice that) in its immediate malloc wrapper, sqlite3MemMalloc(), keeping track of all individual malloc chunk sizes. Typically about 90% of the malloced memory is used for purposes of the pagecache. By providing an mmap(3) MAP_ANON SQLITE_CONFIG_PAGECACHE, execution time decreases by 20-25% for simple (Nd and/or Nm) queries, 10-20% for non-NAME queries, and even apropos(1) resident memory size as reported by top(1) decreases by 20% for simple and by 60% for non-NAME queries. The new function, mansearch_setup(), spends no measurable time. The pagesize chosen is optimal: * Substantially smaller pages yield no gain at all. * Larger pages provide no additional benefit and just waste memory. The chosen number of pages in the cache is a compromise: * For simple queries, a handful of pages would suffice to get the full speed effect, at an apropos(1) resident memory size of about 2.0 MB. * For non-NAME queries, a large pagecache with 2k pages (2.5 MB) might gain a few more percent in speed, but at the expense of doubling the apropos(1) resident memory size for *all* queries. * The chosen number of 256 pages (330 kB) allows nearly full speed gain for all queries at the price of a 15% resident memory size increase.
-rw-r--r--usr.bin/mandoc/apropos.c4
-rw-r--r--usr.bin/mandoc/mansearch.c50
-rw-r--r--usr.bin/mandoc/mansearch.h3
3 files changed, 54 insertions, 3 deletions
diff --git a/usr.bin/mandoc/apropos.c b/usr.bin/mandoc/apropos.c
index 80f3a5a94cb..ea28fb277a2 100644
--- a/usr.bin/mandoc/apropos.c
+++ b/usr.bin/mandoc/apropos.c
@@ -1,4 +1,4 @@
-/* $Id: apropos.c,v 1.20 2014/01/06 03:02:39 schwarze Exp $ */
+/* $Id: apropos.c,v 1.21 2014/04/11 15:45:39 schwarze Exp $ */
/*
* Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2013 Ingo Schwarze <schwarze@openbsd.org>
@@ -92,6 +92,7 @@ apropos(int argc, char *argv[])
search.flags = whatis ? MANSEARCH_WHATIS : 0;
manpath_parse(&paths, conf_file, defpaths, auxpaths);
+ mansearch_setup(1);
ch = mansearch(&search, &paths, argc, argv, outkey, &res, &sz);
manpath_free(&paths);
@@ -107,6 +108,7 @@ apropos(int argc, char *argv[])
}
free(res);
+ mansearch_setup(0);
return(sz ? EXIT_SUCCESS : EXIT_FAILURE);
usage:
fprintf(stderr, "usage: %s [-C file] [-M path] [-m path] "
diff --git a/usr.bin/mandoc/mansearch.c b/usr.bin/mandoc/mansearch.c
index d9c0382ed36..930de9b6344 100644
--- a/usr.bin/mandoc/mansearch.c
+++ b/usr.bin/mandoc/mansearch.c
@@ -1,4 +1,4 @@
-/* $Id: mansearch.c,v 1.18 2014/04/10 02:45:04 schwarze Exp $ */
+/* $Id: mansearch.c,v 1.19 2014/04/11 15:45:39 schwarze Exp $ */
/*
* Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
@@ -15,6 +15,7 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#include <sys/mman.h>
#include <assert.h>
#include <fcntl.h>
#include <getopt.h>
@@ -93,6 +94,53 @@ static void sql_regexp(sqlite3_context *context,
static char *sql_statement(const struct expr *);
int
+mansearch_setup(int start)
+{
+ static void *pagecache;
+ int c;
+
+#define PC_PAGESIZE 1280
+#define PC_NUMPAGES 256
+
+ if (start) {
+ if (NULL != pagecache) {
+ fprintf(stderr, "pagecache already enabled\n");
+ return((int)MANDOCLEVEL_BADARG);
+ }
+
+ pagecache = mmap(NULL, PC_PAGESIZE * PC_NUMPAGES,
+ PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
+
+ if (MAP_FAILED == pagecache) {
+ perror("mmap");
+ pagecache = NULL;
+ return((int)MANDOCLEVEL_SYSERR);
+ }
+
+ c = sqlite3_config(SQLITE_CONFIG_PAGECACHE,
+ pagecache, PC_PAGESIZE, PC_NUMPAGES);
+
+ if (SQLITE_OK == c)
+ return((int)MANDOCLEVEL_OK);
+
+ fprintf(stderr, "pagecache: %s\n", sqlite3_errstr(c));
+
+ } else if (NULL == pagecache) {
+ fprintf(stderr, "pagecache missing\n");
+ return((int)MANDOCLEVEL_BADARG);
+ }
+
+ if (-1 == munmap(pagecache, PC_PAGESIZE * PC_NUMPAGES)) {
+ perror("munmap");
+ pagecache = NULL;
+ return((int)MANDOCLEVEL_SYSERR);
+ }
+
+ pagecache = NULL;
+ return((int)MANDOCLEVEL_OK);
+}
+
+int
mansearch(const struct mansearch *search,
const struct manpaths *paths,
int argc, char *argv[],
diff --git a/usr.bin/mandoc/mansearch.h b/usr.bin/mandoc/mansearch.h
index a12ed0998e1..9ec5d327276 100644
--- a/usr.bin/mandoc/mansearch.h
+++ b/usr.bin/mandoc/mansearch.h
@@ -1,4 +1,4 @@
-/* $Id: mansearch.h,v 1.8 2014/04/10 02:45:04 schwarze Exp $ */
+/* $Id: mansearch.h,v 1.9 2014/04/11 15:45:39 schwarze Exp $ */
/*
* Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
@@ -85,6 +85,7 @@ struct mansearch {
#define MANSEARCH_WHATIS 0x01 /* whatis mode: equality, no key */
};
+int mansearch_setup(int);
int mansearch(const struct mansearch *cfg, /* options */
const struct manpaths *paths, /* manpaths */
int argc, /* size of argv */