Skip to content

Commit

Permalink
module loader: Utilize CPU better (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Feb 3, 2021
1 parent 18e794b commit 4e3362b
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions internal/terraform/module/module_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ type moduleLoader struct {
}

func newModuleLoader() *moduleLoader {
nonPrioParallelism := 2 * runtime.NumCPU()
prioParallelism := 1 * runtime.NumCPU()

p := loaderParallelism(runtime.NumCPU())
plc, lc := int64(0), int64(0)
ml := &moduleLoader{
queue: newModuleOpsQueue(),
logger: defaultLogger,
nonPrioParallelism: int64(nonPrioParallelism),
prioParallelism: int64(prioParallelism),
nonPrioParallelism: p.NonPriority,
prioParallelism: p.Priority,
opsToDispatch: make(chan ModuleOperation, 1),
loadingCount: &lc,
prioLoadingCount: &plc,
Expand All @@ -40,6 +38,31 @@ func newModuleLoader() *moduleLoader {
return ml
}

type parallelism struct {
NonPriority, Priority int64
}

func loaderParallelism(cpu int) parallelism {
// Cap utilization for powerful machines
if cpu >= 4 {
return parallelism{
NonPriority: int64(3),
Priority: int64(1),
}
}
if cpu == 3 {
return parallelism{
NonPriority: int64(2),
Priority: int64(1),
}
}

return parallelism{
NonPriority: 1,
Priority: 1,
}
}

func (ml *moduleLoader) SetLogger(logger *log.Logger) {
ml.logger = logger
}
Expand Down

0 comments on commit 4e3362b

Please sign in to comment.