shirou / gopsutil

psutil for golang
Other
10.47k stars 1.58k forks source link

GPU Stats #1528

Closed SonnyWalkman closed 8 months ago

SonnyWalkman commented 11 months ago

Hello Shirou, Is there appetite to include GPU statistics? Most systems have some form of GPU built into silicon. I'm using NVIDA RTX3050 and currently manipulating nvidia-smi to get % from GPU0? To include GPU to your suite make it far more universal. passing in GPU number and retuning % however, a value for encoding and decoding is what I need which is possibly not that easy to get. Windows Task view or HTOP on Linux seems return GPU maybe a API call? Below is what I'm working with however there could be far better approach than exec call.

func GetGPUUsage() (string, error) {
    cmd := exec.Command("nvidia-smi")
    var out bytes.Buffer
    cmd.Stdout = &out

    if err := cmd.Run(); err != nil {
        return "", err
    }

    // Get the string representation of the command's output
    output := out.String()

    // Regular expression to find the GPU Utilization
    re := regexp.MustCompile(`\|\s+N\/A\s+\d{1,2}C\s+\w+\s+\d{1,2}W \/  \d{1,3}W \|\s+\d{1,4}MiB \/  \d{1,4}MiB \|\s+(\d{1,3})%`)
    match := re.FindStringSubmatch(output)
    if len(match) < 2 {
        return "", fmt.Errorf("unexpected nvidia-smi output: %s", output)
    }

    gpuUsage := match[1]
    return gpuUsage, nil
}
Lomanic commented 8 months ago

This is not a small feat with so many hardware vendors, multiplied by the number of platforms that gopsutil aims to support, with its tiny team (basically a one-man show today as I don't find energy anymore to work on it).

Closing. Duplicate of #598.