From 3fc4336e58ac0a0ec80fd4daba0792b23851d274 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Mon, 11 Jan 2021 13:00:46 +0100 Subject: [PATCH] Config: Fix detection of CPU cores to limit number of workers --- internal/config/config.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index c45b1061d..cc5c1973f 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -333,17 +333,27 @@ func (c *Config) Shutdown() { // Workers returns the number of workers e.g. for indexing files. func (c *Config) Workers() int { - cores := cpuid.CPU.PhysicalCores + // NumCPU returns the number of logical CPU cores. + cores := runtime.NumCPU() + + // Limit to physical cores to avoid high load on HT capable CPUs. + if cores > cpuid.CPU.PhysicalCores { + cores = cpuid.CPU.PhysicalCores + } // Limit number of workers when using SQLite to avoid database locking issues. - if c.DatabaseDriver() == SQLite && cores >= 8 && c.options.Workers <= 0 || c.options.Workers > 4 { + if c.DatabaseDriver() == SQLite && (cores >= 8 && c.options.Workers <= 0 || c.options.Workers > 4) { return 4 } - if c.options.Workers > 0 && c.options.Workers <= runtime.NumCPU() { + // Return explicit value if set and not too large. + if c.options.Workers > runtime.NumCPU() { + return runtime.NumCPU() + } else if c.options.Workers > 0 { return c.options.Workers } + // Use half the available cores by default. if cores > 1 { return cores / 2 }