2022-01-13 23:27:39 +01:00
|
|
|
package obioptions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-01-14 23:11:36 +01:00
|
|
|
"runtime"
|
2022-01-13 23:27:39 +01:00
|
|
|
|
2022-02-24 12:14:52 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2022-01-13 23:27:39 +01:00
|
|
|
"github.com/DavidGamba/go-getoptions"
|
2023-03-28 20:07:26 +07:00
|
|
|
|
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
2022-01-13 23:27:39 +01:00
|
|
|
)
|
|
|
|
|
2022-01-14 23:11:36 +01:00
|
|
|
var _Debug = false
|
2023-03-31 10:53:53 +02:00
|
|
|
var _WorkerPerCore = 2
|
2022-02-06 18:52:53 +01:00
|
|
|
var _MaxAllowedCPU = runtime.NumCPU()
|
2022-01-14 23:11:36 +01:00
|
|
|
var _BatchSize = 5000
|
2023-03-28 20:07:26 +07:00
|
|
|
var _Pprof = false
|
2022-01-13 23:27:39 +01:00
|
|
|
|
2023-03-07 11:12:13 +07:00
|
|
|
type ArgumentParser func([]string) (*getoptions.GetOpt, []string)
|
2022-01-13 23:27:39 +01:00
|
|
|
|
|
|
|
func GenerateOptionParser(optionset ...func(*getoptions.GetOpt)) ArgumentParser {
|
2023-02-23 23:35:58 +01:00
|
|
|
|
2022-01-13 23:27:39 +01:00
|
|
|
options := getoptions.New()
|
2023-02-23 23:35:58 +01:00
|
|
|
options.SetMode(getoptions.Bundling)
|
|
|
|
options.SetUnknownMode(getoptions.Fail)
|
2022-01-13 23:27:39 +01:00
|
|
|
options.Bool("help", false, options.Alias("h", "?"))
|
2022-01-14 23:11:36 +01:00
|
|
|
options.BoolVar(&_Debug, "debug", false)
|
2023-03-28 20:07:26 +07:00
|
|
|
options.BoolVar(&_Pprof, "pprof", false)
|
2022-01-14 23:11:36 +01:00
|
|
|
|
2023-03-31 10:53:53 +02:00
|
|
|
// options.IntVar(&_ParallelWorkers, "workers", _ParallelWorkers,
|
|
|
|
// options.Alias("w"),
|
|
|
|
// options.Description("Number of parallele threads computing the result"))
|
2022-02-06 18:52:53 +01:00
|
|
|
|
|
|
|
options.IntVar(&_MaxAllowedCPU, "max-cpu", _MaxAllowedCPU,
|
2023-02-23 23:35:58 +01:00
|
|
|
options.GetEnv("OBIMAXCPU"),
|
2022-01-14 23:11:36 +01:00
|
|
|
options.Description("Number of parallele threads computing the result"))
|
2022-01-13 23:27:39 +01:00
|
|
|
|
|
|
|
for _, o := range optionset {
|
|
|
|
o(options)
|
|
|
|
}
|
|
|
|
|
2023-03-07 11:12:13 +07:00
|
|
|
return func(args []string) (*getoptions.GetOpt, []string) {
|
2022-01-13 23:27:39 +01:00
|
|
|
|
|
|
|
remaining, err := options.Parse(args[1:])
|
|
|
|
|
2023-03-21 22:01:20 +07:00
|
|
|
if options.Called("help") {
|
|
|
|
fmt.Fprint(os.Stderr, options.Help())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.SetLevel(log.InfoLevel)
|
|
|
|
if options.Called("debug") {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
log.Debugln("Switch to debug level logging")
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:07:26 +07:00
|
|
|
if options.Called("pprof") {
|
|
|
|
go http.ListenAndServe("localhost:8080", nil)
|
|
|
|
log.Infoln("Start a pprof server at address http://localhost:8080/debug/pprof")
|
|
|
|
}
|
|
|
|
|
2023-03-21 22:01:20 +07:00
|
|
|
// Handle user errors
|
2023-03-07 11:12:13 +07:00
|
|
|
if err != nil {
|
2023-03-21 22:01:20 +07:00
|
|
|
fmt.Fprintf(os.Stderr, "ERROR: %s\n\n", err)
|
|
|
|
fmt.Fprint(os.Stderr, options.Help(getoptions.HelpSynopsis))
|
|
|
|
os.Exit(1)
|
2023-03-07 11:12:13 +07:00
|
|
|
}
|
|
|
|
|
2022-02-06 18:52:53 +01:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2022-02-24 12:14:52 +01:00
|
|
|
if options.Called("no-singleton") {
|
|
|
|
log.Printf("No singleton option set")
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:53:53 +02:00
|
|
|
log.Printf("Number of workers set %d", CLIParallelWorkers())
|
2023-03-07 11:12:13 +07:00
|
|
|
return options, remaining
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 23:11:36 +01:00
|
|
|
// Predicate indicating if the debug mode is activated.
|
2022-02-01 17:31:28 +01:00
|
|
|
func CLIIsDebugMode() bool {
|
2022-01-14 23:11:36 +01:00
|
|
|
return _Debug
|
|
|
|
}
|
|
|
|
|
2022-02-01 17:31:28 +01:00
|
|
|
// CLIParallelWorkers returns the number of parallel workers requested by
|
2022-01-14 23:11:36 +01:00
|
|
|
// the command line option --workers|-w.
|
2022-02-01 17:31:28 +01:00
|
|
|
func CLIParallelWorkers() int {
|
2023-03-31 10:53:53 +02:00
|
|
|
return _MaxAllowedCPU * _WorkerPerCore
|
2022-01-14 23:11:36 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 18:52:53 +01:00
|
|
|
// CLIParallelWorkers returns the number of parallel workers requested by
|
|
|
|
// the command line option --workers|-w.
|
|
|
|
func CLIMaxCPU() int {
|
|
|
|
return _MaxAllowedCPU
|
|
|
|
}
|
|
|
|
|
2022-02-01 17:31:28 +01:00
|
|
|
// CLIBatchSize returns the expeted size of the sequence batches
|
|
|
|
func CLIBatchSize() int {
|
2022-01-14 23:11:36 +01:00
|
|
|
return _BatchSize
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
|
|
|
|
2022-01-14 23:11:36 +01:00
|
|
|
// DebugOn sets the debug mode on.
|
2022-01-13 23:27:39 +01:00
|
|
|
func DebugOn() {
|
2022-01-14 23:11:36 +01:00
|
|
|
_Debug = true
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
|
|
|
|
2022-01-14 23:11:36 +01:00
|
|
|
// DebugOff sets the debug mode off.
|
2022-01-13 23:27:39 +01:00
|
|
|
func DebugOff() {
|
2022-01-14 23:11:36 +01:00
|
|
|
_Debug = false
|
2022-01-13 23:27:39 +01:00
|
|
|
}
|
2023-03-31 10:53:53 +02:00
|
|
|
|
|
|
|
func SetWorkerPerCore(n int) {
|
|
|
|
_WorkerPerCore = n
|
|
|
|
}
|
|
|
|
|
|
|
|
func WorkerPerCore() int {
|
|
|
|
return _WorkerPerCore
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetBatchSize(n int) {
|
|
|
|
_BatchSize = n
|
|
|
|
}
|