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

     1  // Package apc: API control messages and constants
     2  /*
     3   * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package apc
     6  
     7  import (
     8  	"os"
     9  
    10  	"github.com/NVIDIA/aistore/cmn/debug"
    11  	"github.com/NVIDIA/aistore/sys"
    12  )
    13  
    14  type MemCPUInfo struct {
    15  	MemUsed    uint64      `json:"mem_used"`
    16  	MemAvail   uint64      `json:"mem_avail"`
    17  	PctMemUsed float64     `json:"pct_mem_used"`
    18  	PctCPUUsed float64     `json:"pct_cpu_used"`
    19  	LoadAvg    sys.LoadAvg `json:"load_avg"`
    20  }
    21  
    22  func GetMemCPU() MemCPUInfo {
    23  	var (
    24  		mem sys.MemStat
    25  		err error
    26  	)
    27  	err = mem.Get()
    28  	debug.AssertNoErr(err)
    29  
    30  	proc, err := sys.ProcessStats(os.Getpid())
    31  	debug.AssertNoErr(err)
    32  
    33  	load, err := sys.LoadAverage()
    34  	debug.AssertNoErr(err)
    35  
    36  	return MemCPUInfo{
    37  		MemAvail:   mem.ActualFree,
    38  		MemUsed:    proc.Mem.Resident,
    39  		PctMemUsed: float64(proc.Mem.Resident) * 100 / float64(mem.Total),
    40  		PctCPUUsed: proc.CPU.Percent,
    41  		LoadAvg:    load,
    42  	}
    43  }