From 30d80db02d23daa8286ade2e498d65de1e23d006 Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Sun, 6 Feb 2022 18:52:53 +0100 Subject: [PATCH] All an option to limit number of CPU --- pkg/obioptions/options.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/pkg/obioptions/options.go b/pkg/obioptions/options.go index 685ed58..bba7f6d 100644 --- a/pkg/obioptions/options.go +++ b/pkg/obioptions/options.go @@ -2,6 +2,7 @@ package obioptions import ( "fmt" + "log" "os" "runtime" @@ -10,6 +11,7 @@ import ( var _Debug = false var _ParallelWorkers = runtime.NumCPU() - 1 +var _MaxAllowedCPU = runtime.NumCPU() var _BufferSize = 1 var _BatchSize = 5000 @@ -20,7 +22,11 @@ func GenerateOptionParser(optionset ...func(*getoptions.GetOpt)) ArgumentParser options.Bool("help", false, options.Alias("h", "?")) options.BoolVar(&_Debug, "debug", false) - options.IntVar(&_ParallelWorkers, "workers", runtime.NumCPU()-1, + options.IntVar(&_ParallelWorkers, "workers", _ParallelWorkers, + options.Alias("w"), + options.Description("Number of parallele threads computing the result")) + + options.IntVar(&_MaxAllowedCPU, "max-cpu", _MaxAllowedCPU, options.Alias("w"), options.Description("Number of parallele threads computing the result")) @@ -32,6 +38,12 @@ func GenerateOptionParser(optionset ...func(*getoptions.GetOpt)) ArgumentParser remaining, err := options.Parse(args[1:]) + // Setup the maximum number of CPU usable by the program + runtime.GOMAXPROCS(_MaxAllowedCPU) + if options.Called("max-cpu") { + log.Printf("CPU number limited to %d", _MaxAllowedCPU) + } + if options.Called("help") { fmt.Fprint(os.Stderr, options.Help()) os.Exit(1) @@ -51,6 +63,12 @@ func CLIParallelWorkers() int { return _ParallelWorkers } +// CLIParallelWorkers returns the number of parallel workers requested by +// the command line option --workers|-w. +func CLIMaxCPU() int { + return _MaxAllowedCPU +} + // CLIBufferSize returns the expeted channel buffer size for obitools func CLIBufferSize() int { return _BufferSize