github.com/PandaGoAdmin/utils@v0.0.0-20211208134815-d5461603a00f/os_darwin_nocgo.go (about)

     1  // +build darwin
     2  // +build !cgo
     3  
     4  package kgo
     5  
     6  import (
     7  	"fmt"
     8  	"os/exec"
     9  	"strings"
    10  )
    11  
    12  // CpuUsage 获取CPU使用率(darwin系统必须使用cgo),单位jiffies(节拍数).
    13  // user为用户态(用户进程)的运行时间,
    14  // idle为空闲时间,
    15  // total为累计时间.
    16  func (ko *LkkOS) CpuUsage() (user, idle, total uint64) {
    17  	//CPU counters for darwin is unavailable without cgo
    18  	return
    19  }
    20  
    21  // getProcessPathByPid 根据PID获取进程的执行路径.
    22  func getProcessPathByPid(pid int) (res string) {
    23  	lsof, err := exec.LookPath("lsof")
    24  	if err != nil {
    25  		return
    26  	}
    27  	command := fmt.Sprintf("%s -p %d -Fpfn", lsof, pid)
    28  	_, out, _ = KOS.System(command)
    29  	txtFound := 0
    30  	lines := strings.Split(string(out), "\n")
    31  	for i := 1; i < len(lines); i++ {
    32  		if lines[i] == "ftxt" {
    33  			txtFound++
    34  			if txtFound == 2 {
    35  				return lines[i-1][1:], nil
    36  			}
    37  		}
    38  	}
    39  
    40  	return
    41  }