forked from cloudfoundry/java-buildpack-memory-calculator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
92 lines (76 loc) · 3.28 KB
/
main.go
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
/*
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"fmt"
"os"
"strings"
"github.com/instana/java-buildpack-memory-calculator/v4/calculator"
"github.com/instana/java-buildpack-memory-calculator/v4/flags"
flag "github.com/spf13/pflag"
)
func main() {
d := flags.DefaultDetectMemoryLimits
h := flags.DefaultHeadRoom
j := flags.DefaultJVMOptions
l := flags.DefaultLoadedClassCount
t := flags.DefaultThreadCount
m := flags.DefaultTotalMemory
r := flags.DefaultDirectMemoryToHeapRatio
y := flags.DefaultHeapYoungGenerationRatio
c := calculator.Calculator{HeadRoom: &h, JvmOptions: &j, LoadedClassCount: &l, ThreadCount: &t, DetectMemoryLimits: &d, TotalMemory: &m, DirectMemoryToHeapRatio: &r, HeapYoungGenerationRatio: &y}
flag.Var(c.HeadRoom, flags.FlagHeadRoom, "percentage of total memory available which will be left unallocated to cover JVM overhead")
flag.Var(c.JvmOptions, flags.FlagJVMOptions, "JVM options, typically JAVA_OPTS")
flag.Var(c.LoadedClassCount, flags.FlagLoadedClassCount, "the number of classes that will be loaded when the application is running; optional as long as '-XX:MaxMetaspaceSize' is provided via '--jvm-options'")
flag.Var(c.ThreadCount, flags.FlagThreadCount, "the number of user threads")
flag.Var(c.DirectMemoryToHeapRatio, flags.FlagDirectMemoryToHeapRatio, "ratio of direct memory to heap, expressed as a float between 0 and 1, e.g., 0.3")
flag.Var(c.HeapYoungGenerationRatio, flags.FlagHeapYoungGenerationRatio, "ratio of heap memory to reserve to the young generation, expressed as a float between 0 and 1, e.g., 0.3")
flag.Var(c.TotalMemory, flags.FlagTotalMemory, "total memory available to the application, typically expressed with size classification (B, K, M, G, T); optional if '--detect-memory-limits=true' is used")
flag.BoolVar((*bool)(c.DetectMemoryLimits), flags.FlagDetectMemoryLimits, false, "[Experimental; Linux only] automatically detect total memory available to the application based on cgroups; this flag is incompatible with '--total-memory'")
flag.Parse()
if !validate(c.HeadRoom, c.JvmOptions, c.LoadedClassCount, c.ThreadCount, c.TotalMemory) {
_, _ = fmt.Fprintln(os.Stderr, "")
flag.Usage()
os.Exit(1)
}
o, err := c.Calculate()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, err.Error())
os.Exit(2)
}
s := make([]string, len(o))
for i, t := range o {
s[i] = t.String()
}
fmt.Println(output(o))
}
func output(o []fmt.Stringer) string {
s := make([]string, len(o))
for i, t := range o {
s[i] = t.String()
}
return strings.Join(s, " ")
}
func validate(vs ...flags.Validatable) bool {
valid := true
for _, v := range vs {
if err := v.Validate(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
valid = false
}
}
return valid
}