From 0b490027b11e779c312912484554d260ab9f9710 Mon Sep 17 00:00:00 2001
From: William Brown <firstyear@redhat.com>
Date: Mon, 9 Jan 2017 12:59:22 +1000
Subject: [PATCH] Ticket 49028 - Autosize database cache by default.

Bug Description:  We ship Directory Server with very conservative defaults.
These are often not fit for production, yet many people run with them and
complain about the performance of Directory Server.

Fix Description:  Instead of relying on the admin to tune their system, by
defaulting to the autotuning newly installed systems will get the "best possible"
experience. Please see:

http://www.port389.org/docs/389ds/design/autotuning.html

https://fedorahosted.org/389/ticket/49028

Author: wibrown

Review by: ???
---
 ldap/servers/slapd/back-ldbm/ldbm_config.c |  8 ++++----
 ldap/servers/slapd/back-ldbm/start.c       | 14 ++++++++++----
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/ldap/servers/slapd/back-ldbm/ldbm_config.c b/ldap/servers/slapd/back-ldbm/ldbm_config.c
index bf1b9c7..e603a79 100644
--- a/ldap/servers/slapd/back-ldbm/ldbm_config.c
+++ b/ldap/servers/slapd/back-ldbm/ldbm_config.c
@@ -436,13 +436,13 @@ static int ldbm_config_dbcachesize_set(void *arg, void *value, char *errorbuf, i
         if (CONFIG_PHASE_RUNNING == phase) {
             li->li_new_dbcachesize = val;
             slapi_log_err(SLAPI_LOG_NOTICE, "ldbm_config_dbcachesize_set",
-            	"New db cache size will not take affect until the server is restarted\n");
+                "New db cache size will not take affect until the server is restarted\n");
         } else {
             li->li_new_dbcachesize = val;
             li->li_dbcachesize = val;
         }
     }
-    
+
     return retval;
 }
 
@@ -1535,8 +1535,8 @@ static config_info ldbm_config[] = {
     {CONFIG_DB_DEBUG_CHECKPOINTING, CONFIG_TYPE_ONOFF, "off", &ldbm_config_db_debug_checkpointing_get, &ldbm_config_db_debug_checkpointing_set, 0},
     {CONFIG_DB_HOME_DIRECTORY, CONFIG_TYPE_STRING, "", &ldbm_config_db_home_directory_get, &ldbm_config_db_home_directory_set, 0},
     {CONFIG_IMPORT_CACHE_AUTOSIZE, CONFIG_TYPE_INT, "-1", &ldbm_config_import_cache_autosize_get, &ldbm_config_import_cache_autosize_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
-    {CONFIG_CACHE_AUTOSIZE, CONFIG_TYPE_INT, "0", &ldbm_config_cache_autosize_get, &ldbm_config_cache_autosize_set, 0},
-    {CONFIG_CACHE_AUTOSIZE_SPLIT, CONFIG_TYPE_INT, "50", &ldbm_config_cache_autosize_split_get, &ldbm_config_cache_autosize_split_set, 0},
+    {CONFIG_CACHE_AUTOSIZE, CONFIG_TYPE_INT, "10", &ldbm_config_cache_autosize_get, &ldbm_config_cache_autosize_set, 0},
+    {CONFIG_CACHE_AUTOSIZE_SPLIT, CONFIG_TYPE_INT, "40", &ldbm_config_cache_autosize_split_get, &ldbm_config_cache_autosize_split_set, 0},
     {CONFIG_IMPORT_CACHESIZE, CONFIG_TYPE_SIZE_T, "20000000", &ldbm_config_import_cachesize_get, &ldbm_config_import_cachesize_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
     {CONFIG_IDL_SWITCH, CONFIG_TYPE_STRING, "new", &ldbm_config_idl_get_idl_new, &ldbm_config_idl_set_tune, CONFIG_FLAG_ALWAYS_SHOW},
     {CONFIG_IDL_UPDATE, CONFIG_TYPE_ONOFF, "on", &ldbm_config_idl_get_update, &ldbm_config_idl_set_update, 0},
diff --git a/ldap/servers/slapd/back-ldbm/start.c b/ldap/servers/slapd/back-ldbm/start.c
index 836e67f..6ffe995 100644
--- a/ldap/servers/slapd/back-ldbm/start.c
+++ b/ldap/servers/slapd/back-ldbm/start.c
@@ -195,6 +195,12 @@ ldbm_back_start( Slapi_PBlock *pb )
               /* It's valid, lets divide it up and set according to user prefs */
               zone_pages = zone_size / pagesize;
               db_pages = (li->li_cache_autosize_split * zone_pages) / 100;
+
+              /* Cap the DB size at 512MB, as this doesn't help perf much more (lkrispen's advice) */
+              if ((db_pages * pagesize) > (512 * MEGABYTE)) {
+                  db_pages = (512 * MEGABYTE) / pagesize;
+              }
+
               entry_pages = (zone_pages - db_pages) / objset_size(li->li_instance_set);
               /* We update this for the is-sane check below. */
               total_cache_size = (zone_pages - db_pages) * pagesize;
@@ -207,7 +213,7 @@ ldbm_back_start( Slapi_PBlock *pb )
                 "each entry cache (%d total): %luk\n",
                 db_pages*(pagesize/1024), objset_size(li->li_instance_set),
                 entry_pages*(pagesize/1024));
-    
+
               /* libdb allocates 1.25x the amount we tell it to,
                * but only for values < 500Meg 
                * For the larger memory, the overhead is relatively small. */
@@ -218,7 +224,7 @@ ldbm_back_start( Slapi_PBlock *pb )
               sprintf(s, "%lu", cache_size_to_configure);
               ldbm_config_internal_set(li, CONFIG_DBCACHESIZE, s);
               li->li_cache_autosize_ec = (unsigned long)entry_pages * pagesize;
-    
+
               for (inst_obj = objset_first_obj(li->li_instance_set); inst_obj;
                   inst_obj = objset_next_obj(li->li_instance_set, inst_obj)) {
                   inst = (ldbm_instance *)object_get_data(inst_obj);
@@ -230,7 +236,7 @@ ldbm_back_start( Slapi_PBlock *pb )
                   /* Trac 48831 exists to allow this to be auto-sized too ... */
                   total_cache_size += (PRUint64)cache_get_max_size(&(inst->inst_dncache));
               }
-          }    
+          }
           /* autosizing importCache */
           if (li->li_import_cache_autosize > 0) {
               /* For some reason, -1 means 50 ... */
@@ -249,7 +255,7 @@ ldbm_back_start( Slapi_PBlock *pb )
               import_pages = import_size / pagesize;
               slapi_log_err(SLAPI_LOG_NOTICE, "ldbm_back_start", "cache autosizing: import cache: %luk\n",
                 import_pages*(pagesize/1024));
-    
+
               sprintf(s, "%lu", (unsigned long)(import_pages * pagesize));
               ldbm_config_internal_set(li, CONFIG_IMPORT_CACHESIZE, s);
           }
-- 
1.8.3.1

