2021-02-04 17:10:10 +03:00
|
|
|
package metrics
|
|
|
|
|
2021-03-17 18:48:29 +03:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetPageCacheRSSFromSmapsFailure(t *testing.T) {
|
|
|
|
f := func(s string) {
|
|
|
|
t.Helper()
|
|
|
|
bb := bytes.NewBufferString(s)
|
|
|
|
_, _, err := getRSSStatsFromSmaps(bb)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("foobar")
|
|
|
|
|
|
|
|
// Invalid unit for Rss
|
|
|
|
f(`7ffcdf335000-7ffcdf337000 r-xp 00000000 00:00 0 [vdso]
|
|
|
|
Size: 80 kB
|
|
|
|
KernelPageSize: 4 kB
|
|
|
|
MMUPageSize: 4 kB
|
|
|
|
Rss: 12 MB
|
|
|
|
Pss: 0 kB
|
|
|
|
Shared_Clean: 4 kB
|
|
|
|
Shared_Dirty: 0 kB
|
|
|
|
Private_Clean: 0 kB
|
|
|
|
Private_Dirty: 0 kB
|
|
|
|
Referenced: 4 kB
|
|
|
|
Anonymous: 0 kB
|
|
|
|
LazyFree: 0 kB
|
|
|
|
AnonHugePages: 0 kB
|
|
|
|
ShmemPmdMapped: 0 kB
|
|
|
|
Shared_Hugetlb: 0 kB
|
|
|
|
Private_Hugetlb: 0 kB
|
|
|
|
Swap: 0 kB
|
|
|
|
SwapPss: 0 kB
|
|
|
|
Locked: 0 kB
|
|
|
|
VmFlags: rd ex mr mw me de sd
|
|
|
|
`)
|
|
|
|
|
|
|
|
// Invalid unit for Anonymous
|
|
|
|
f(`7ffcdf335000-7ffcdf337000 r-xp 00000000 00:00 0 [vdso]
|
|
|
|
Size: 80 kB
|
|
|
|
KernelPageSize: 4 kB
|
|
|
|
MMUPageSize: 4 kB
|
|
|
|
Rss: 12 kB
|
|
|
|
Pss: 0 kB
|
|
|
|
Shared_Clean: 4 kB
|
|
|
|
Shared_Dirty: 0 kB
|
|
|
|
Private_Clean: 0 kB
|
|
|
|
Private_Dirty: 0 kB
|
|
|
|
Referenced: 4 kB
|
|
|
|
Anonymous: 5 MB
|
|
|
|
LazyFree: 0 kB
|
|
|
|
AnonHugePages: 0 kB
|
|
|
|
ShmemPmdMapped: 0 kB
|
|
|
|
Shared_Hugetlb: 0 kB
|
|
|
|
Private_Hugetlb: 0 kB
|
|
|
|
Swap: 0 kB
|
|
|
|
SwapPss: 0 kB
|
|
|
|
Locked: 0 kB
|
|
|
|
VmFlags: rd ex mr mw me de sd
|
|
|
|
`)
|
|
|
|
|
|
|
|
// Invalid size for Rss
|
|
|
|
f(`7ffcdf335000-7ffcdf337000 r-xp 00000000 00:00 0 [vdso]
|
|
|
|
Size: 80 kB
|
|
|
|
KernelPageSize: 4 kB
|
|
|
|
MMUPageSize: 4 kB
|
|
|
|
Rss: 1.2 kB
|
|
|
|
Pss: 0 kB
|
|
|
|
Shared_Clean: 4 kB
|
|
|
|
Shared_Dirty: 0 kB
|
|
|
|
Private_Clean: 0 kB
|
|
|
|
Private_Dirty: 0 kB
|
|
|
|
Referenced: 4 kB
|
|
|
|
Anonymous: 0 kB
|
|
|
|
LazyFree: 0 kB
|
|
|
|
AnonHugePages: 0 kB
|
|
|
|
ShmemPmdMapped: 0 kB
|
|
|
|
Shared_Hugetlb: 0 kB
|
|
|
|
Private_Hugetlb: 0 kB
|
|
|
|
Swap: 0 kB
|
|
|
|
SwapPss: 0 kB
|
|
|
|
Locked: 0 kB
|
|
|
|
VmFlags: rd ex mr mw me de sd
|
|
|
|
`)
|
|
|
|
|
|
|
|
// Too big size for Rss
|
|
|
|
f(`7ffcdf335000-7ffcdf337000 r-xp 00000000 00:00 0 [vdso]
|
|
|
|
Size: 80 kB
|
|
|
|
KernelPageSize: 4 kB
|
|
|
|
MMUPageSize: 4 kB
|
|
|
|
Rss: 9999999999999999999 kB
|
|
|
|
Pss: 0 kB
|
|
|
|
Shared_Clean: 4 kB
|
|
|
|
Shared_Dirty: 0 kB
|
|
|
|
Private_Clean: 0 kB
|
|
|
|
Private_Dirty: 0 kB
|
|
|
|
Referenced: 4 kB
|
|
|
|
Anonymous: 0 kB
|
|
|
|
LazyFree: 0 kB
|
|
|
|
AnonHugePages: 0 kB
|
|
|
|
ShmemPmdMapped: 0 kB
|
|
|
|
Shared_Hugetlb: 0 kB
|
|
|
|
Private_Hugetlb: 0 kB
|
|
|
|
Swap: 0 kB
|
|
|
|
SwapPss: 0 kB
|
|
|
|
Locked: 0 kB
|
|
|
|
VmFlags: rd ex mr mw me de sd
|
|
|
|
`)
|
|
|
|
|
|
|
|
// Partial entry
|
|
|
|
f(`7ffcdf335000-7ffcdf337000 r-xp 00000000 00:00 0 [vdso]
|
|
|
|
Size: 80 kB
|
|
|
|
KernelPageSize: 4 kB
|
|
|
|
MMUPageSize: 4 kB
|
|
|
|
Rss: 12 kB
|
|
|
|
Pss: 0 kB
|
|
|
|
Shared_Clean: 4 kB
|
|
|
|
Shared_Dirty: 0 kB
|
|
|
|
Private_Clean: 0 kB
|
|
|
|
Private_Dirty: 0 kB
|
|
|
|
Referenced: 4 kB
|
|
|
|
Anonymous: 0 kB
|
|
|
|
LazyFree: 0 kB
|
|
|
|
`)
|
|
|
|
|
|
|
|
// Partial second entry
|
|
|
|
f(`7ffcdf335000-7ffcdf337000 r-xp 00000000 00:00 0 [vdso]
|
|
|
|
Size: 80 kB
|
|
|
|
KernelPageSize: 4 kB
|
|
|
|
MMUPageSize: 4 kB
|
|
|
|
Rss: 12 kB
|
|
|
|
Pss: 0 kB
|
|
|
|
Shared_Clean: 4 kB
|
|
|
|
Shared_Dirty: 0 kB
|
|
|
|
Private_Clean: 0 kB
|
|
|
|
Private_Dirty: 0 kB
|
|
|
|
Referenced: 4 kB
|
|
|
|
Anonymous: 0 kB
|
|
|
|
LazyFree: 0 kB
|
|
|
|
AnonHugePages: 0 kB
|
|
|
|
ShmemPmdMapped: 0 kB
|
|
|
|
Shared_Hugetlb: 0 kB
|
|
|
|
Private_Hugetlb: 0 kB
|
|
|
|
Swap: 0 kB
|
|
|
|
SwapPss: 0 kB
|
|
|
|
Locked: 0 kB
|
|
|
|
VmFlags: rd ex mr mw me de sd
|
|
|
|
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
|
|
|
|
Size: 1024 kB
|
|
|
|
KernelPageSize: 4 kB
|
|
|
|
MMUPageSize: 4 kB
|
|
|
|
Rss: 120 kB
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPageCacheRSSFromSmapsSuccess(t *testing.T) {
|
|
|
|
s := `7ffcdf335000-7ffcdf337000 r-xp 00000000 00:00 0 [vdso]
|
|
|
|
Size: 80 kB
|
|
|
|
KernelPageSize: 4 kB
|
|
|
|
MMUPageSize: 4 kB
|
|
|
|
Rss: 12 kB
|
|
|
|
Pss: 0 kB
|
|
|
|
Shared_Clean: 4 kB
|
|
|
|
Shared_Dirty: 0 kB
|
|
|
|
Private_Clean: 0 kB
|
|
|
|
Private_Dirty: 0 kB
|
|
|
|
Referenced: 4 kB
|
|
|
|
Anonymous: 0 kB
|
|
|
|
LazyFree: 0 kB
|
|
|
|
AnonHugePages: 0 kB
|
|
|
|
ShmemPmdMapped: 0 kB
|
|
|
|
Shared_Hugetlb: 0 kB
|
|
|
|
Private_Hugetlb: 0 kB
|
|
|
|
Swap: 0 kB
|
|
|
|
SwapPss: 0 kB
|
|
|
|
Locked: 0 kB
|
|
|
|
VmFlags: rd ex mr mw me de sd
|
|
|
|
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
|
|
|
|
Size: 1024 kB
|
|
|
|
KernelPageSize: 4 kB
|
|
|
|
MMUPageSize: 4 kB
|
|
|
|
Rss: 120 kB
|
|
|
|
Pss: 0 kB
|
|
|
|
Shared_Clean: 0 kB
|
|
|
|
Shared_Dirty: 0 kB
|
|
|
|
Private_Clean: 0 kB
|
|
|
|
Private_Dirty: 0 kB
|
|
|
|
Referenced: 0 kB
|
|
|
|
Anonymous: 1024 kB
|
|
|
|
LazyFree: 0 kB
|
|
|
|
AnonHugePages: 0 kB
|
|
|
|
ShmemPmdMapped: 0 kB
|
|
|
|
Shared_Hugetlb: 0 kB
|
|
|
|
Private_Hugetlb: 0 kB
|
|
|
|
Swap: 0 kB
|
|
|
|
SwapPss: 0 kB
|
|
|
|
Locked: 0 kB
|
|
|
|
VmFlags: rd ex
|
|
|
|
`
|
|
|
|
bb := bytes.NewBufferString(s)
|
|
|
|
pageCache, anonymous, err := getRSSStatsFromSmaps(bb)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
expectedPageCache := uint64(12 * 1024)
|
|
|
|
if pageCache != expectedPageCache {
|
|
|
|
t.Fatalf("unexpected page cache rss; got %d; want %d", pageCache, expectedPageCache)
|
|
|
|
}
|
|
|
|
expectedAnonymous := uint64(120 * 1024)
|
|
|
|
if anonymous != expectedAnonymous {
|
|
|
|
t.Fatalf("unexpected anonymous rss; got %d; want %d", anonymous, expectedAnonymous)
|
|
|
|
}
|
|
|
|
}
|
2021-02-04 17:10:10 +03:00
|
|
|
|
2021-02-04 17:28:55 +03:00
|
|
|
func TestGetMaxFilesLimit(t *testing.T) {
|
|
|
|
f := func(want uint64, path string, wantErr bool) {
|
2021-02-04 17:10:10 +03:00
|
|
|
t.Helper()
|
|
|
|
got, err := getMaxFilesLimit(path)
|
|
|
|
if err != nil && !wantErr {
|
2021-02-04 17:28:55 +03:00
|
|
|
t.Fatalf("unexpected error: %v", err)
|
2021-02-04 17:10:10 +03:00
|
|
|
}
|
2021-02-04 17:28:55 +03:00
|
|
|
if got != want {
|
|
|
|
t.Fatalf("unexpected result: %d, want: %d at getMaxFilesLimit", got, want)
|
2021-02-04 17:10:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-02-04 17:28:55 +03:00
|
|
|
f(1024, "testdata/limits", false)
|
|
|
|
f(0, "testdata/bad_path", true)
|
|
|
|
f(0, "testdata/limits_bad", true)
|
2021-02-04 17:10:10 +03:00
|
|
|
}
|
|
|
|
|
2021-02-04 17:28:55 +03:00
|
|
|
func TestGetOpenFDsCount(t *testing.T) {
|
|
|
|
f := func(want uint64, path string, wantErr bool) {
|
2021-02-04 17:10:10 +03:00
|
|
|
t.Helper()
|
|
|
|
got, err := getOpenFDsCount(path)
|
2021-02-04 17:28:55 +03:00
|
|
|
if (err != nil && !wantErr) || (err == nil && wantErr) {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
2021-02-04 17:10:10 +03:00
|
|
|
}
|
2021-02-04 17:28:55 +03:00
|
|
|
if got != want {
|
|
|
|
t.Fatalf("unexpected result: %d, want: %d at getOpenFDsCount", got, want)
|
2021-02-04 17:10:10 +03:00
|
|
|
}
|
|
|
|
}
|
2021-02-04 17:28:55 +03:00
|
|
|
f(5, "testdata/fd/", false)
|
|
|
|
f(0, "testdata/fd/0", true)
|
|
|
|
f(0, "testdata/limits", true)
|
2021-02-04 17:10:10 +03:00
|
|
|
}
|