From c2f2d8771f3b75db4b8042f64a066bafbe134ed4 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 11 May 2015 09:01:09 +0200 Subject: [PATCH] Tweak the database block cache size, and add config for it --- cmd/syncthing/main.go | 25 ++++++++++---------- internal/config/config.go | 1 + internal/config/config_test.go | 2 ++ internal/config/testdata/overridenvalues.xml | 1 + 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 7e54a2072..be01254e7 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -715,22 +715,21 @@ func syncthingMain() { } func dbOpts() *opt.Options { - // Calculate a sutiable database block cache capacity. We start at the - // default of 8 MiB and use larger values for machines with more memory. + // Calculate a sutiable database block cache capacity. Default is 8 MiB. // In reality, the database will use twice the amount we calculate here, // as it also has two write buffers each sized at half the block cache. - blockCacheCapacity := 8 << 20 - if bytes, err := memorySize(); err == nil { - if bytes > 74<<30 { - // At 74 GiB of RAM, we hit a 256 MiB block cache (per the - // calculations below). There's probably no point in growing the - // cache beyond this point. - blockCacheCapacity = 256 << 20 - } else if bytes > 8<<30 { - // Slowly grow from 128 MiB at 8 GiB of RAM up to 256 MiB for a - // ~74 GiB RAM machine - blockCacheCapacity = int(bytes/512) + 128 - 16 + + if v := cfg.Options().DatabaseBlockCacheMiB; v != 0 { + // Use the value from the config, if it's set. + blockCacheCapacity = v << 20 + } else if bytes, err := memorySize(); err == nil { + // We start at the default of 8 MiB and use larger values for machines + // with more memory. + + if bytes > 8<<30 { + // Cap the cache at 128 MB when we reach 8 GiB of RAM + blockCacheCapacity = 128 << 20 } else if bytes > 512<<20 { // Grow from 8 MiB at start to 128 MiB of cache at 8 GiB of RAM. blockCacheCapacity = int(bytes / 64) diff --git a/internal/config/config.go b/internal/config/config.go index 3b2eea8a6..5da30ad0d 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -240,6 +240,7 @@ type OptionsConfiguration struct { ProgressUpdateIntervalS int `xml:"progressUpdateIntervalS" json:"progressUpdateIntervalS" default:"5"` SymlinksEnabled bool `xml:"symlinksEnabled" json:"symlinksEnabled" default:"true"` LimitBandwidthInLan bool `xml:"limitBandwidthInLan" json:"limitBandwidthInLan" default:"false"` + DatabaseBlockCacheMiB int `xml:"databaseBlockCacheMiB" json:"databaseBlockCacheMiB" default:"0"` } func (orig OptionsConfiguration) Copy() OptionsConfiguration { diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 4e02897dd..1192ab827 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -52,6 +52,7 @@ func TestDefaultValues(t *testing.T) { ProgressUpdateIntervalS: 5, SymlinksEnabled: true, LimitBandwidthInLan: false, + DatabaseBlockCacheMiB: 0, } cfg := New(device1) @@ -158,6 +159,7 @@ func TestOverriddenValues(t *testing.T) { ProgressUpdateIntervalS: 10, SymlinksEnabled: false, LimitBandwidthInLan: true, + DatabaseBlockCacheMiB: 42, } cfg, err := Load("testdata/overridenvalues.xml", device1) diff --git a/internal/config/testdata/overridenvalues.xml b/internal/config/testdata/overridenvalues.xml index 3dac47b78..d0b9d9677 100755 --- a/internal/config/testdata/overridenvalues.xml +++ b/internal/config/testdata/overridenvalues.xml @@ -23,5 +23,6 @@ 10 false true + 42