github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/sys/proc_linux.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 "strconv" 10 "strings" 11 12 "github.com/NVIDIA/aistore/cmn/cos" 13 ) 14 15 const ticks = 100 // C.sysconf(C._SC_CLK_TCK) 16 17 func procMem(pid int) (ProcMemStats, error) { 18 mem := ProcMemStats{} 19 20 procPath := fmt.Sprintf(hostProcessStatMemPath, pid) 21 line, err := cos.ReadOneLine(procPath) 22 if err != nil { 23 return mem, err 24 } 25 26 fields := strings.Fields(line) 27 val, err := strconv.ParseUint(fields[0], 10, 64) 28 if err != nil { 29 return mem, err 30 } 31 mem.Size = val << 12 32 val, err = strconv.ParseUint(fields[1], 10, 64) 33 if err != nil { 34 return mem, err 35 } 36 mem.Resident = val << 12 37 val, err = strconv.ParseUint(fields[2], 10, 64) 38 if err != nil { 39 return mem, err 40 } 41 mem.Share = val << 12 42 43 return mem, err 44 } 45 46 func procCPU(pid int) (ProcCPUStats, error) { 47 cpu := ProcCPUStats{} 48 49 procPath := fmt.Sprintf(hostProcessStatCPUPath, pid) 50 line, err := cos.ReadOneLine(procPath) 51 if err != nil { 52 return cpu, err 53 } 54 55 fields := strings.Fields(line) 56 user, err := strconv.ParseUint(fields[13], 10, 64) 57 if err != nil { 58 return cpu, err 59 } 60 sys, err := strconv.ParseUint(fields[14], 10, 64) 61 if err != nil { 62 return cpu, err 63 } 64 65 // convert to milliseconds 66 cpu.User = user * (1000 / ticks) 67 cpu.System = sys * (1000 / ticks) 68 cpu.Total = cpu.User + cpu.System 69 70 return cpu, nil 71 }