github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/sys/mem.go (about)

     1  // Package sys provides methods to read system information
     2  /*
     3   * Copyright (c) 2018-2021, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package sys
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/NVIDIA/aistore/cmn/cos"
    11  )
    12  
    13  // Memory stats for the host OS or for container, depending on where the app is running.
    14  // For the host, returns an error if memory cannot be read. If the function fails to read
    15  // container's stats, it returns host memory. Swap stats, however, are _always_ host stats.
    16  
    17  type MemStat struct {
    18  	Total      uint64
    19  	Used       uint64
    20  	Free       uint64
    21  	BuffCache  uint64
    22  	ActualFree uint64
    23  	ActualUsed uint64
    24  	SwapTotal  uint64
    25  	SwapFree   uint64
    26  	SwapUsed   uint64
    27  }
    28  
    29  func (mem *MemStat) Get() error {
    30  	if !containerized {
    31  		return mem.host()
    32  	}
    33  	return mem.container()
    34  }
    35  
    36  func (mem *MemStat) String() string {
    37  	var (
    38  		used      = cos.ToSizeIEC(int64(mem.Used), 0)
    39  		free      = cos.ToSizeIEC(int64(mem.Free), 0)
    40  		buffcache = cos.ToSizeIEC(int64(mem.BuffCache), 0)
    41  		actfree   = cos.ToSizeIEC(int64(mem.ActualFree), 0)
    42  	)
    43  	return fmt.Sprintf("used %s, free %s, buffcache %s, actfree %s", used, free, buffcache, actfree)
    44  }