Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for CPU/CPU Core entity types and queries #48

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions pkg/dcgm/cpu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package dcgm

/*
#include "dcgm_agent.h"
#include "dcgm_structs.h"
*/
import "C"
import (
"fmt"
"unsafe"
)

/*
*See dcgm_structs.h
* DCGM_CPU_CORE_BITMASK_COUNT_V1 (DCGM_MAX_NUM_CPU_CORES / sizeof(uint64_t) / CHAR_BIT)
* or
* 1024 / 8 / 8
*/

const (
MAX_NUM_CPU_CORES uint = C.DCGM_MAX_NUM_CPU_CORES
MAX_NUM_CPUS uint = C.DCGM_MAX_NUM_CPUS
CHAR_BIT uint = C.CHAR_BIT
MAX_CPU_CORE_BITMASK_COUNT uint = 1024 / 8 / 8
)

type CpuHierarchyCpu_v1 struct {
CpuId uint
OwnedCores []uint64
}

type CpuHierarchy_v1 struct {
Version uint
NumCpus uint
Cpus [MAX_NUM_CPUS]CpuHierarchyCpu_v1
}

func GetCpuHierarchy() (hierarchy CpuHierarchy_v1, err error) {
var c_hierarchy C.dcgmCpuHierarchy_v1
c_hierarchy.version = C.dcgmCpuHierarchy_version1
ptr_hierarchy := (*C.dcgmCpuHierarchy_v1)(unsafe.Pointer(&c_hierarchy))
result := C.dcgmGetCpuHierarchy(handle.handle, ptr_hierarchy)

if err = errorString(result); err != nil {
return toCpuHierarchy(c_hierarchy), fmt.Errorf("Error retrieving DCGM MIG hierarchy: %s", err)
}

return toCpuHierarchy(c_hierarchy), nil
}

func toCpuHierarchy(c_hierarchy C.dcgmCpuHierarchy_v1) CpuHierarchy_v1 {
var hierarchy CpuHierarchy_v1
hierarchy.Version = uint(c_hierarchy.version)
hierarchy.NumCpus = uint(c_hierarchy.numCpus)
for i := uint(0); i < hierarchy.NumCpus; i++ {
bits := make([]uint64, MAX_CPU_CORE_BITMASK_COUNT)

for j := uint(0); j < MAX_CPU_CORE_BITMASK_COUNT; j++ {
bits[j] = uint64(c_hierarchy.cpus[i].ownedCores.bitmask[j])
}

hierarchy.Cpus[i] = CpuHierarchyCpu_v1{
CpuId: uint(c_hierarchy.cpus[i].cpuId),
OwnedCores: bits,
}
}

return hierarchy
}
6 changes: 6 additions & 0 deletions pkg/dcgm/mig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const (
FE_GPU_I
FE_GPU_CI
FE_LINK
FE_CPU
FE_CPU_CORE
FE_COUNT
)

Expand All @@ -37,6 +39,10 @@ func (e Field_Entity_Group) String() string {
return "GPU Compute Instance"
case FE_LINK:
return "NvLink"
case FE_CPU:
return "CPU"
case FE_CPU_CORE:
return "CPU Core"
}
return "unknown"
}
Expand Down
28 changes: 28 additions & 0 deletions tests/dcgm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import (
"github.com/NVIDIA/go-dcgm/pkg/dcgm"
"math"
"os"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -42,6 +43,33 @@ func BenchmarkDeviceCount1(b *testing.B) {
dcgm.Shutdown()
}

func TestCpuQuery(t *testing.T) {
os.Setenv("DCGM_SKIP_SYSMON_HARDWARE_CHECK", "1")
cleanup, err := dcgm.Init(dcgm.Embedded)
nvvfedorov marked this conversation as resolved.
Show resolved Hide resolved
check(err, t)
defer cleanup()

hierarchy, err := dcgm.GetCpuHierarchy()
check(err, t)

if hierarchy.NumCpus == 0 {
t.Errorf("Found no CPUs")
}

for i := uint(0); i < hierarchy.NumCpus; i++ {
var coresFound = false
for j := uint(0); j < dcgm.MAX_CPU_CORE_BITMASK_COUNT; j++ {
if hierarchy.Cpus[i].OwnedCores[j] != 0 {
coresFound = true
}
}

if coresFound == false {
t.Errorf("Cpu %d has no cores", i)
}
}
}

func TestDeviceInfo(t *testing.T) {
cleanup, err := dcgm.Init(dcgm.Embedded)
check(err, t)
Expand Down
Loading