forked from kahing/goofys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgroup.go
100 lines (84 loc) · 2.69 KB
/
cgroup.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
93
94
95
96
97
98
99
100
// Copyright 2019 Ka-Hing Cheung
//
// 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 internal
import (
"errors"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
)
const CGROUP_PATH = "/proc/self/cgroup"
const CGROUP_FOLDER_PREFIX = "/sys/fs/cgroup/memory"
const MEM_LIMIT_FILE_SUFFIX = "/memory.limit_in_bytes"
const MEM_USAGE_FILE_SUFFIX = "/memory.usage_in_bytes"
func getCgroupAvailableMem() (retVal uint64, err error) {
//get the memory cgroup for self and send limit - usage for the cgroup
data, err := ioutil.ReadFile(CGROUP_PATH)
if err != nil {
log.Debugf("Unable to read file %s error: %s", CGROUP_PATH, err)
return 0, err
}
path, err := getMemoryCgroupPath(string(data))
if err != nil {
log.Debugf("Unable to get memory cgroup path")
return 0, err
}
log.Debugf("the memory cgroup path for the current process is %v", path)
mem_limit, err := readFileAndGetValue(filepath.Join(CGROUP_FOLDER_PREFIX, path, MEM_LIMIT_FILE_SUFFIX))
if err != nil {
log.Debugf("Unable to get memory limit from cgroup error: %v", err)
return 0, err
}
mem_usage, err := readFileAndGetValue(filepath.Join(CGROUP_FOLDER_PREFIX, path, MEM_USAGE_FILE_SUFFIX))
if err != nil {
log.Debugf("Unable to get memory usage from cgroup error: %v", err)
return 0, err
}
return (mem_limit - mem_usage), nil
}
func getMemoryCgroupPath(data string) (string, error) {
/*
Content of /proc/self/cgroup
11:hugetlb:/
10:memory:/user.slice
9:cpuset:/
8:blkio:/user.slice
7:perf_event:/
6:net_prio,net_cls:/
5:cpuacct,cpu:/user.slice
4:devices:/user.slice
3:freezer:/
2:pids:/
1:name=systemd:/user.slice/user-1000.slice/session-1759.scope
*/
dataArray := strings.Split(data, "\n")
for index := range dataArray {
kvArray := strings.Split(dataArray[index], ":")
if len(kvArray) == 3 {
if kvArray[1] == "memory" {
return kvArray[2], nil
}
}
}
return "", errors.New("Unable to get memory cgroup path")
}
func readFileAndGetValue(path string) (uint64, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
log.Debugf("Unable to read file %v error: %v", path, err)
return 0, err
}
return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
}