Config: Detect physical CPU cores to limit number of workers

This commit is contained in:
Michael Mayer 2021-01-09 12:18:59 +01:00
parent 507d0957eb
commit dcaf22b329
5 changed files with 18 additions and 7 deletions

1
go.mod
View file

@ -29,6 +29,7 @@ require (
github.com/json-iterator/go v1.1.10 // indirect
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
github.com/karrick/godirwalk v1.16.1
github.com/klauspost/cpuid/v2 v2.0.3
github.com/leandro-lugaresi/hub v1.1.1
github.com/leonelquinteros/gotext v1.4.0
github.com/lib/pq v1.3.0 // indirect

3
go.sum
View file

@ -191,6 +191,9 @@ github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALr
github.com/karrick/godirwalk v1.16.1 h1:DynhcF+bztK8gooS0+NDJFrdNZjJ3gzVzC545UNA9iw=
github.com/karrick/godirwalk v1.16.1/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/cpuid v1.3.1 h1:5JNjFYYQrZeKRJ0734q51WCEEn2huer72Dc7K+R/b6s=
github.com/klauspost/cpuid/v2 v2.0.3 h1:DNljyrHyxlkk8139OXIAAauCwV8eQGDD6Z8YqnDXdZw=
github.com/klauspost/cpuid/v2 v2.0.3/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

View file

@ -55,7 +55,7 @@ func configAction(ctx *cli.Context) error {
fmt.Printf("%-25s %s\n", "img-path", conf.ImgPath())
fmt.Printf("%-25s %s\n", "templates-path", conf.TemplatesPath())
// Workers.
// Background workers.
fmt.Printf("%-25s %d\n", "workers", conf.Workers())
fmt.Printf("%-25s %d\n", "wakeup-interval", conf.WakeupInterval()/time.Second)
fmt.Printf("%-25s %d\n", "auto-index", conf.AutoIndex()/time.Second)

View file

@ -20,6 +20,7 @@ import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/klauspost/cpuid/v2"
"github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/hub"
"github.com/photoprism/photoprism/internal/hub/places"
@ -133,6 +134,10 @@ func (c *Config) Init() error {
fs.IgnoreCase()
}
if cpuName := cpuid.CPU.BrandName; cpuName != "" {
log.Debugf("config: running on %s", txt.Quote(cpuid.CPU.BrandName))
}
c.initSettings()
c.initHub()
@ -328,19 +333,19 @@ func (c *Config) Shutdown() {
// Workers returns the number of workers e.g. for indexing files.
func (c *Config) Workers() int {
numCPU := runtime.NumCPU()
cores := cpuid.CPU.PhysicalCores
// Limit number of workers when using SQLite to avoid database locking issues.
if c.DatabaseDriver() == SQLite && numCPU >= 8 && c.options.Workers <= 0 {
if c.DatabaseDriver() == SQLite && cores >= 8 && c.options.Workers <= 0 || c.options.Workers > 4 {
return 4
}
if c.options.Workers > 0 && c.options.Workers <= numCPU {
if c.options.Workers > 0 && c.options.Workers <= runtime.NumCPU() {
return c.options.Workers
}
if numCPU > 1 {
return numCPU / 2
if cores > 1 {
return cores / 2
}
return 1

View file

@ -1,6 +1,7 @@
package config
import (
"github.com/klauspost/cpuid/v2"
"github.com/urfave/cli"
)
@ -95,8 +96,9 @@ var GlobalFlags = []cli.Flag{
},
cli.IntFlag{
Name: "workers, w",
Usage: "`LIMIT` the number of indexing workers to reduce system load",
Usage: "adjusts `MAX` number of indexing workers",
EnvVar: "PHOTOPRISM_WORKERS",
Value: cpuid.CPU.PhysicalCores / 2,
},
cli.IntFlag{
Name: "wakeup-interval",