github.com/yandex-cloud/geesefs@v0.40.9/internal/cgroup.go (about)

     1  // Copyright 2019 Ka-Hing Cheung
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package internal
    16  
    17  import (
    18  	"errors"
    19  	"io/ioutil"
    20  	"os"
    21  	"path/filepath"
    22  	"strconv"
    23  	"strings"
    24  )
    25  
    26  const CGROUP_PATH = "/proc/self/cgroup"
    27  const CGROUP_FOLDER_PREFIX = "/sys/fs/cgroup/memory"
    28  const MEM_LIMIT_FILE_SUFFIX = "/memory.limit_in_bytes"
    29  const MEM_USAGE_FILE_SUFFIX = "/memory.usage_in_bytes"
    30  
    31  func getCgroupAvailableMem() (retVal uint64, err error) {
    32  	// get the memory cgroup for self and send limit - usage for the cgroup
    33  
    34  	data, err := ioutil.ReadFile(CGROUP_PATH)
    35  	if err != nil {
    36  		log.Debugf("Unable to read file %s error: %s", CGROUP_PATH, err)
    37  		return 0, err
    38  	}
    39  
    40  	path, err := getMemoryCgroupPath(string(data))
    41  	if err != nil {
    42  		return 0, err
    43  	}
    44  
    45  	// newer version of docker mounts the cgroup memory limit/usage files directly under
    46  	// /sys/fs/cgroup/memory/ rather than /sys/fs/cgroup/memory/docker/$container_id/
    47  	if _, err := os.Stat(filepath.Join(CGROUP_FOLDER_PREFIX, path)); os.IsExist(err) {
    48  		path = filepath.Join(CGROUP_FOLDER_PREFIX, path)
    49  	} else {
    50  		path = filepath.Join(CGROUP_FOLDER_PREFIX)
    51  	}
    52  
    53  	mem_limit, err := readFileAndGetValue(filepath.Join(path, MEM_LIMIT_FILE_SUFFIX))
    54  	if err != nil {
    55  		log.Debugf("Unable to get memory limit from cgroup file %v error: %v", path, err)
    56  		return 0, err
    57  	}
    58  
    59  	mem_usage, err := readFileAndGetValue(filepath.Join(path, MEM_USAGE_FILE_SUFFIX))
    60  	if err != nil {
    61  		log.Debugf("Unable to get memory usage from cgroup file %v error: %v", path, err)
    62  		return 0, err
    63  	}
    64  
    65  	return (mem_limit - mem_usage), nil
    66  }
    67  
    68  func getMemoryCgroupPath(data string) (string, error) {
    69  
    70  	/*
    71  	   Content of /proc/self/cgroup
    72  
    73  	   11:hugetlb:/
    74  	   10:memory:/user.slice
    75  	   9:cpuset:/
    76  	   8:blkio:/user.slice
    77  	   7:perf_event:/
    78  	   6:net_prio,net_cls:/
    79  	   5:cpuacct,cpu:/user.slice
    80  	   4:devices:/user.slice
    81  	   3:freezer:/
    82  	   2:pids:/
    83  	   1:name=systemd:/user.slice/user-1000.slice/session-1759.scope
    84  	*/
    85  
    86  	dataArray := strings.Split(data, "\n")
    87  	for index := range dataArray {
    88  		kvArray := strings.Split(dataArray[index], ":")
    89  		if len(kvArray) == 3 {
    90  			if kvArray[1] == "memory" {
    91  				return kvArray[2], nil
    92  			}
    93  		}
    94  	}
    95  
    96  	return "", errors.New("Unable to get memory cgroup path")
    97  }
    98  
    99  func readFileAndGetValue(path string) (uint64, error) {
   100  	data, err := ioutil.ReadFile(path)
   101  	if err != nil {
   102  		log.Debugf("Unable to read file %v error: %v", path, err)
   103  		return 0, err
   104  	}
   105  
   106  	return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
   107  }