aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/admin/jvm/options.go
blob: a788fb0cca95a4f2a3f2f30ca02d8d37609eca94 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Author: arnej

package jvm

import (
	"fmt"
	"strconv"
	"strings"

	"github.com/vespa-engine/vespa/client/go/internal/admin/defaults"
	"github.com/vespa-engine/vespa/client/go/internal/admin/trace"
	"github.com/vespa-engine/vespa/client/go/internal/util"
	"github.com/vespa-engine/vespa/client/go/internal/vespa"
)

type Options struct {
	container   Container
	classPath   []string
	jvmArgs     util.ArrayList[string]
	mainClass   string
	jarWithDeps string
	fixSpec     util.FixSpec
}

func NewOptions(c Container) *Options {
	vespaUid, vespaGid := vespa.FindVespaUidAndGid()
	fixSpec := util.FixSpec{
		UserId:   vespaUid,
		GroupId:  vespaGid,
		DirMode:  0755,
		FileMode: 0644,
	}
	return &Options{
		container:   c,
		classPath:   make([]string, 0, 10),
		jvmArgs:     make([]string, 0, 100),
		jarWithDeps: DEFAULT_JAR_WITH_DEPS,
		mainClass:   DEFAULT_MAIN_CLASS,
		fixSpec:     fixSpec,
	}
}

func (opts *Options) AddOption(arg string) {
	if opts.jvmArgs.Contains(arg) {
		return
	}
	opts.AppendOption(arg)
}

func (opts *Options) AppendOption(arg string) {
	trace.Trace("append JVM option:", arg)
	opts.jvmArgs = append(opts.jvmArgs, arg)
}

func (opts *Options) ClassPath() string {
	cp := defaults.UnderVespaHome(opts.jarWithDeps)
	for _, x := range opts.classPath {
		cp = fmt.Sprintf("%s:%s", cp, x)
	}
	trace.Trace("computed classpath:", cp)
	return cp
}

func (opts *Options) Args() []string {
	args := opts.jvmArgs
	args = append(args, "-cp")
	args = append(args, opts.ClassPath())
	args = append(args, opts.mainClass)
	args = append(args, opts.container.ArgForMain())
	return args
}

func (opts *Options) AddJvmArgsFromString(args string) {
	for _, x := range strings.Fields(args) {
		opts.AppendOption(x)
	}
}

func (opts *Options) ConfigureCpuCount(cnt int) {
	if cnt <= 0 {
		out, err := util.BackTicksForwardStderr.Run("nproc", "--all")
		if err != nil {
			trace.Trace("failed nproc:", err)
		} else {
			cnt, err = strconv.Atoi(strings.TrimSpace(out))
			if err != nil {
				trace.Trace("bad nproc output:", strings.TrimSpace(out))
				cnt = 0
			} else {
				trace.Trace("CpuCount: using", cnt, "from nproc --all")
			}
		}
	}
	if cnt > 0 {
		opts.AddOption(fmt.Sprintf("-XX:ActiveProcessorCount=%d", cnt))
	}
}