aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/admin/jvm/memory.go
blob: 8a51b1087ff103ea67424ec8048ba855550d44f7 (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
// 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"
)

const (
	_ = 1 << (10 * iota)
	PowerOfTwo10
	PowerOfTwo20
	PowerOfTwo30
)

type AmountOfMemory struct {
	numBytes int64
}

func BytesOfMemory(v int64) AmountOfMemory {
	return AmountOfMemory{numBytes: v}
}
func KiloBytesOfMemory(v int64) AmountOfMemory {
	return BytesOfMemory(v * PowerOfTwo10)
}
func MegaBytesOfMemory(v int) AmountOfMemory {
	return BytesOfMemory(int64(v) * PowerOfTwo20)
}
func GigaBytesOfMemory(v int) AmountOfMemory {
	return BytesOfMemory(int64(v) * PowerOfTwo30)
}

func (v AmountOfMemory) ToBytes() int64 {
	return v.numBytes
}
func (v AmountOfMemory) ToKB() int64 {
	return v.numBytes / PowerOfTwo10
}
func (v AmountOfMemory) ToMB() int {
	return int(v.numBytes / PowerOfTwo20)
}
func (v AmountOfMemory) ToGB() int {
	return int(v.numBytes / PowerOfTwo30)
}
func (v AmountOfMemory) AsJvmSpec() string {
	val := v.ToKB()
	suffix := "k"
	if val%PowerOfTwo10 == 0 {
		val = val / PowerOfTwo10
		suffix = "m"
		if val%PowerOfTwo10 == 0 {
			val = val / PowerOfTwo10
			suffix = "g"
		}
	}
	return fmt.Sprintf("%d%s", val, suffix)
}

func (v AmountOfMemory) String() string {
	val := v.numBytes
	idx := 0
	suffix := [9]string{"bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
	for val > 0 && (val%PowerOfTwo10 == 0) {
		val = val / PowerOfTwo10
		idx++
	}
	return fmt.Sprintf("{%d %s}", val, suffix[idx])
}

func ParseJvmMemorySpec(spec string) (result AmountOfMemory, err error) {
	result = BytesOfMemory(0)
	var n int
	var val int64
	var suffix rune
	n, err = fmt.Sscanf(spec, "%d%c", &val, &suffix)
	if n == 2 && err == nil {
		switch suffix {
		case 'k', 'K':
			result = KiloBytesOfMemory(val)
		case 'm', 'M':
			result = MegaBytesOfMemory(int(val))
		case 'g', 'G':
			result = GigaBytesOfMemory(int(val))
		default:
			err = fmt.Errorf("Unknown suffix in JVM memory spec '%s'", spec)
		}
	}
	return
}