Skip to content

Commit

Permalink
Adaptive database cache size
Browse files Browse the repository at this point in the history
  • Loading branch information
calmh committed Apr 30, 2015
1 parent c9da823 commit 8343364
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions cmd/syncthing/main.go
Expand Up @@ -518,10 +518,9 @@ func syncthingMain() {
}

dbFile := locations[locDatabase]
dbOpts := &opt.Options{OpenFilesCacheCapacity: 100}
ldb, err := leveldb.OpenFile(dbFile, dbOpts)
ldb, err := leveldb.OpenFile(dbFile, dbOpts())
if err != nil && errors.IsCorrupted(err) {
ldb, err = leveldb.RecoverFile(dbFile, dbOpts)
ldb, err = leveldb.RecoverFile(dbFile, dbOpts())
}
if err != nil {
l.Fatalln("Cannot open database:", err, "- Is another copy of Syncthing already running?")
Expand Down Expand Up @@ -665,6 +664,37 @@ func syncthingMain() {
os.Exit(code)
}

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.
// 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
} 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)
}
l.Infoln("Database block cache capacity", blockCacheCapacity/1024, "KiB")
}

return &opt.Options{
OpenFilesCacheCapacity: 100,
BlockCacheCapacity: blockCacheCapacity,
WriteBuffer: blockCacheCapacity / 2,
}
}

func startAuditing(mainSvc *suture.Supervisor) {
auditFile := timestampedLoc(locAuditLog)
fd, err := os.OpenFile(auditFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
Expand Down

0 comments on commit 8343364

Please sign in to comment.