summaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
authorArne Juul <arnej@yahooinc.com>2022-11-09 11:00:23 +0000
committerArne Juul <arnej@yahooinc.com>2022-11-09 11:02:34 +0000
commit5865995cb03a5f077967c966fb65e87ea2191ad9 (patch)
treec2cf11b971964d1d91d7cc49f73f52e2ea590902 /client
parent29043ae1e3e79ca3587d2311d0276dba0979a406 (diff)
write more unit tests
Diffstat (limited to 'client')
-rw-r--r--client/go/jvm/mem_avail.go45
-rw-r--r--client/go/jvm/mem_avail_test.go42
-rw-r--r--client/go/jvm/mem_options_test.go16
-rw-r--r--client/go/jvm/mock-cg2/a/proc/self/cgroup1
-rw-r--r--client/go/jvm/mock-cg2/a/sys/fs/cgroup/cgroup.controllers1
-rw-r--r--client/go/jvm/mock-cg2/a/sys/fs/cgroup/memory.max1
-rw-r--r--client/go/jvm/mock-cg2/a/sys/fs/cgroup/system.slice/memory.max1
-rw-r--r--client/go/jvm/mock-cg2/a/sys/fs/cgroup/system.slice/sshd.service/memory.max1
-rw-r--r--client/go/jvm/mock-cg2/b/proc/self/cgroup1
-rw-r--r--client/go/jvm/mock-cg2/b/sys/fs/cgroup/cgroup.controllers1
-rw-r--r--client/go/jvm/mock-cg2/b/sys/fs/cgroup/init.scope/memory.max1
-rw-r--r--client/go/jvm/mock-cg2/b/sys/fs/cgroup/init.scope/sshd.service/memory.max1
-rw-r--r--client/go/jvm/mock-cg2/b/sys/fs/cgroup/memory.max1
-rw-r--r--client/go/jvm/mock-cg2/c/proc/self/cgroup1
-rw-r--r--client/go/jvm/mock-cg2/c/sys/fs/cgroup/cgroup.controllers1
-rw-r--r--client/go/jvm/mock-cg2/c/sys/fs/cgroup/memory.max1
-rw-r--r--client/go/jvm/mock-cg2/c/sys/fs/cgroup/system.slice/memory.max1
-rw-r--r--client/go/jvm/mock-cg2/c/sys/fs/cgroup/system.slice/vespa.service/memory.max1
18 files changed, 86 insertions, 32 deletions
diff --git a/client/go/jvm/mem_avail.go b/client/go/jvm/mem_avail.go
index 78627a9b7bb..579b5aa8049 100644
--- a/client/go/jvm/mem_avail.go
+++ b/client/go/jvm/mem_avail.go
@@ -38,26 +38,42 @@ func parentDir(dir string) string {
return dir[:lastSlash]
}
-func vespa_cg2get(filename string) (output string, err error) {
- _, err = os.Stat("/sys/fs/cgroup/cgroup.controllers")
+func readLineFrom(filename string) (string, error) {
+ content, err := os.ReadFile(filename)
+ s := string(content)
+ if err != nil {
+ return s, err
+ }
+ s = strings.TrimSuffix(s, "\n")
+ if strings.Contains(s, "\n") {
+ return s, fmt.Errorf("unexpected multiple lines in file %s", filename)
+ }
+ return s, nil
+}
+
+func vespa_cg2get(limitname string) (output string, err error) {
+ return vespa_cg2get_impl("", limitname)
+}
+func vespa_cg2get_impl(rootdir, limitname string) (output string, err error) {
+ _, err = os.Stat(rootdir + "/sys/fs/cgroup/cgroup.controllers")
if err != nil {
trace.Trace("no cgroups:", err)
return
}
- cgroup_content, err := os.ReadFile("/proc/self/cgroup")
+ cgroup_content, err := readLineFrom(rootdir + "/proc/self/cgroup")
if err != nil {
trace.Trace("no cgroup for self:", err)
return
}
min_value := "max"
- slice := strings.TrimPrefix(string(cgroup_content), "0::")
- slice = strings.TrimSuffix(slice, "\n")
- for strings.HasPrefix(slice, "/") {
- path := fmt.Sprintf("/sys/fs/cgroup%s/%s", slice, filename)
- fileContents, err := os.ReadFile(path)
+ path := rootdir + "/sys/fs/cgroup"
+ slice := strings.TrimPrefix(cgroup_content, "0::")
+ dirNames := strings.Split(slice, "/")
+ for _, dirName := range dirNames {
+ path = path + dirName + "/"
+ value, err := readLineFrom(path + limitname)
+ trace.Debug("read from", path+limitname, "=>", value)
if err == nil {
- value := strings.TrimSuffix(string(fileContents), "\n")
- trace.Debug("read from", path, "=>", value)
if value == "max" {
// nop
} else if min_value == "max" {
@@ -68,9 +84,8 @@ func vespa_cg2get(filename string) (output string, err error) {
min_value = value
}
}
- slice = parentDir(slice)
}
- trace.Trace("min_value:", min_value)
+ trace.Trace("min_value of", limitname, "for cgroups v2:", min_value)
return min_value, nil
}
@@ -87,9 +102,7 @@ func getAvailableMemory() AmountOfMemory {
available_cgroup := KiloBytesOfMemory(1 << 31)
cggetOutput, err := backticks.Run("cgget", "-nv", "-r", "memory.limit_in_bytes", "/")
if err != nil {
- if strings.Contains(cggetOutput, "Cgroup is not mounted") {
- cggetOutput, err = vespa_cg2get("memory.max")
- }
+ cggetOutput, err = vespa_cg2get("memory.max")
}
cggetOutput = strings.TrimSpace(cggetOutput)
if err != nil {
@@ -98,7 +111,7 @@ func getAvailableMemory() AmountOfMemory {
if err == nil && cggetOutput != "max" {
numBytes, err := strconv.ParseInt(cggetOutput, 10, 64)
if err == nil && numBytes > (1<<28) {
- available_cgroup = AmountOfMemory{numBytes: numBytes}
+ available_cgroup = BytesOfMemory(numBytes)
} else {
trace.Warning("unexpected 'cgget' output:", cggetOutput)
}
diff --git a/client/go/jvm/mem_avail_test.go b/client/go/jvm/mem_avail_test.go
new file mode 100644
index 00000000000..34366759523
--- /dev/null
+++ b/client/go/jvm/mem_avail_test.go
@@ -0,0 +1,42 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package jvm
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/vespa-engine/vespa/client/go/trace"
+)
+
+func TestCg2Get(t *testing.T) {
+ trace.AdjustVerbosity(2)
+ const MM = "memory.max"
+ res, err := vespa_cg2get(MM)
+
+ res, err = vespa_cg2get_impl("mock-cg2/a", MM)
+ assert.Nil(t, err)
+ assert.Equal(t, "123", res)
+
+ res, err = vespa_cg2get_impl("mock-cg2/b", MM)
+ assert.Nil(t, err)
+ assert.Equal(t, "67430985728", res)
+
+ res, err = vespa_cg2get_impl("mock-cg2/c", MM)
+ assert.Nil(t, err)
+ assert.Equal(t, "9663676416", res)
+}
+
+func TestParseFree(t *testing.T) {
+ res := parseFree(`
+ total used free shared buff/cache available
+Mem: 19986 656 3157 218 16172 18832
+Swap: 2047 320 1727
+`)
+ assert.Equal(t, MegaBytesOfMemory(19986), res)
+}
+
+func TestGetAvail(t *testing.T) {
+ trace.AdjustVerbosity(0)
+ available := getAvailableMemory()
+ assert.True(t, available.ToMB() >= 0)
+}
diff --git a/client/go/jvm/mem_options_test.go b/client/go/jvm/mem_options_test.go
index d7bb05000ca..c15143d4758 100644
--- a/client/go/jvm/mem_options_test.go
+++ b/client/go/jvm/mem_options_test.go
@@ -5,7 +5,6 @@ import (
"testing"
"github.com/stretchr/testify/assert"
- "github.com/vespa-engine/vespa/client/go/trace"
)
func TestAdjustment(t *testing.T) {
@@ -18,18 +17,3 @@ func TestAdjustment(t *testing.T) {
adj := adjustAvailableMemory(MegaBytesOfMemory(31024)).ToMB()
assert.Equal(t, 30000, int(adj))
}
-
-func TestParseFree(t *testing.T) {
- res := parseFree(`
- total used free shared buff/cache available
-Mem: 19986 656 3157 218 16172 18832
-Swap: 2047 320 1727
-`)
- assert.Equal(t, MegaBytesOfMemory(19986), res)
-}
-
-func TestGetAvail(t *testing.T) {
- trace.AdjustVerbosity(0)
- available := getAvailableMemory()
- assert.True(t, available.ToMB() >= 0)
-}
diff --git a/client/go/jvm/mock-cg2/a/proc/self/cgroup b/client/go/jvm/mock-cg2/a/proc/self/cgroup
new file mode 100644
index 00000000000..2cc2de315da
--- /dev/null
+++ b/client/go/jvm/mock-cg2/a/proc/self/cgroup
@@ -0,0 +1 @@
+0::/system.slice/sshd.service
diff --git a/client/go/jvm/mock-cg2/a/sys/fs/cgroup/cgroup.controllers b/client/go/jvm/mock-cg2/a/sys/fs/cgroup/cgroup.controllers
new file mode 100644
index 00000000000..acb4d263e65
--- /dev/null
+++ b/client/go/jvm/mock-cg2/a/sys/fs/cgroup/cgroup.controllers
@@ -0,0 +1 @@
+cpuset cpu io memory hugetlb pids rdma
diff --git a/client/go/jvm/mock-cg2/a/sys/fs/cgroup/memory.max b/client/go/jvm/mock-cg2/a/sys/fs/cgroup/memory.max
new file mode 100644
index 00000000000..355295a05af
--- /dev/null
+++ b/client/go/jvm/mock-cg2/a/sys/fs/cgroup/memory.max
@@ -0,0 +1 @@
+max
diff --git a/client/go/jvm/mock-cg2/a/sys/fs/cgroup/system.slice/memory.max b/client/go/jvm/mock-cg2/a/sys/fs/cgroup/system.slice/memory.max
new file mode 100644
index 00000000000..190a18037c6
--- /dev/null
+++ b/client/go/jvm/mock-cg2/a/sys/fs/cgroup/system.slice/memory.max
@@ -0,0 +1 @@
+123
diff --git a/client/go/jvm/mock-cg2/a/sys/fs/cgroup/system.slice/sshd.service/memory.max b/client/go/jvm/mock-cg2/a/sys/fs/cgroup/system.slice/sshd.service/memory.max
new file mode 100644
index 00000000000..355295a05af
--- /dev/null
+++ b/client/go/jvm/mock-cg2/a/sys/fs/cgroup/system.slice/sshd.service/memory.max
@@ -0,0 +1 @@
+max
diff --git a/client/go/jvm/mock-cg2/b/proc/self/cgroup b/client/go/jvm/mock-cg2/b/proc/self/cgroup
new file mode 100644
index 00000000000..8dc7ddcf52f
--- /dev/null
+++ b/client/go/jvm/mock-cg2/b/proc/self/cgroup
@@ -0,0 +1 @@
+0::/init.scope
diff --git a/client/go/jvm/mock-cg2/b/sys/fs/cgroup/cgroup.controllers b/client/go/jvm/mock-cg2/b/sys/fs/cgroup/cgroup.controllers
new file mode 100644
index 00000000000..acb4d263e65
--- /dev/null
+++ b/client/go/jvm/mock-cg2/b/sys/fs/cgroup/cgroup.controllers
@@ -0,0 +1 @@
+cpuset cpu io memory hugetlb pids rdma
diff --git a/client/go/jvm/mock-cg2/b/sys/fs/cgroup/init.scope/memory.max b/client/go/jvm/mock-cg2/b/sys/fs/cgroup/init.scope/memory.max
new file mode 100644
index 00000000000..355295a05af
--- /dev/null
+++ b/client/go/jvm/mock-cg2/b/sys/fs/cgroup/init.scope/memory.max
@@ -0,0 +1 @@
+max
diff --git a/client/go/jvm/mock-cg2/b/sys/fs/cgroup/init.scope/sshd.service/memory.max b/client/go/jvm/mock-cg2/b/sys/fs/cgroup/init.scope/sshd.service/memory.max
new file mode 100644
index 00000000000..355295a05af
--- /dev/null
+++ b/client/go/jvm/mock-cg2/b/sys/fs/cgroup/init.scope/sshd.service/memory.max
@@ -0,0 +1 @@
+max
diff --git a/client/go/jvm/mock-cg2/b/sys/fs/cgroup/memory.max b/client/go/jvm/mock-cg2/b/sys/fs/cgroup/memory.max
new file mode 100644
index 00000000000..ca65b2abde1
--- /dev/null
+++ b/client/go/jvm/mock-cg2/b/sys/fs/cgroup/memory.max
@@ -0,0 +1 @@
+67430985728
diff --git a/client/go/jvm/mock-cg2/c/proc/self/cgroup b/client/go/jvm/mock-cg2/c/proc/self/cgroup
new file mode 100644
index 00000000000..c76235307d0
--- /dev/null
+++ b/client/go/jvm/mock-cg2/c/proc/self/cgroup
@@ -0,0 +1 @@
+0::/system.slice/vespa.service
diff --git a/client/go/jvm/mock-cg2/c/sys/fs/cgroup/cgroup.controllers b/client/go/jvm/mock-cg2/c/sys/fs/cgroup/cgroup.controllers
new file mode 100644
index 00000000000..acb4d263e65
--- /dev/null
+++ b/client/go/jvm/mock-cg2/c/sys/fs/cgroup/cgroup.controllers
@@ -0,0 +1 @@
+cpuset cpu io memory hugetlb pids rdma
diff --git a/client/go/jvm/mock-cg2/c/sys/fs/cgroup/memory.max b/client/go/jvm/mock-cg2/c/sys/fs/cgroup/memory.max
new file mode 100644
index 00000000000..ca65b2abde1
--- /dev/null
+++ b/client/go/jvm/mock-cg2/c/sys/fs/cgroup/memory.max
@@ -0,0 +1 @@
+67430985728
diff --git a/client/go/jvm/mock-cg2/c/sys/fs/cgroup/system.slice/memory.max b/client/go/jvm/mock-cg2/c/sys/fs/cgroup/system.slice/memory.max
new file mode 100644
index 00000000000..355295a05af
--- /dev/null
+++ b/client/go/jvm/mock-cg2/c/sys/fs/cgroup/system.slice/memory.max
@@ -0,0 +1 @@
+max
diff --git a/client/go/jvm/mock-cg2/c/sys/fs/cgroup/system.slice/vespa.service/memory.max b/client/go/jvm/mock-cg2/c/sys/fs/cgroup/system.slice/vespa.service/memory.max
new file mode 100644
index 00000000000..8061635036a
--- /dev/null
+++ b/client/go/jvm/mock-cg2/c/sys/fs/cgroup/system.slice/vespa.service/memory.max
@@ -0,0 +1 @@
+9663676416