2023-11-14 01:20:34 +00:00
|
|
|
//go:build darwin
|
|
|
|
|
2024-10-17 00:45:00 +00:00
|
|
|
package discover
|
2024-03-07 00:53:51 +00:00
|
|
|
|
2024-02-25 23:16:45 +00:00
|
|
|
/*
|
|
|
|
#cgo CFLAGS: -x objective-c
|
|
|
|
#cgo LDFLAGS: -framework Foundation -framework CoreGraphics -framework Metal
|
|
|
|
#include "gpu_info_darwin.h"
|
|
|
|
*/
|
2023-11-29 19:00:37 +00:00
|
|
|
import "C"
|
2024-08-01 21:52:15 +00:00
|
|
|
|
2023-11-14 01:20:34 +00:00
|
|
|
import (
|
2024-10-15 18:36:08 +00:00
|
|
|
"log/slog"
|
2023-12-19 21:32:24 +00:00
|
|
|
"runtime"
|
2024-10-15 18:36:08 +00:00
|
|
|
"syscall"
|
2024-05-01 15:46:03 +00:00
|
|
|
|
|
|
|
"github.com/ollama/ollama/format"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-05-10 16:15:28 +00:00
|
|
|
metalMinimumMemory = 512 * format.MebiByte
|
2023-11-14 01:20:34 +00:00
|
|
|
)
|
|
|
|
|
2024-03-30 16:50:05 +00:00
|
|
|
func GetGPUInfo() GpuInfoList {
|
|
|
|
mem, _ := GetCPUMem()
|
2024-01-10 04:29:58 +00:00
|
|
|
if runtime.GOARCH == "amd64" {
|
2024-03-30 16:50:05 +00:00
|
|
|
return []GpuInfo{
|
|
|
|
{
|
|
|
|
Library: "cpu",
|
2024-05-31 04:54:07 +00:00
|
|
|
Variant: GetCPUCapability().String(),
|
2024-03-30 16:50:05 +00:00
|
|
|
memInfo: mem,
|
|
|
|
},
|
2024-01-10 04:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-30 16:50:05 +00:00
|
|
|
info := GpuInfo{
|
2024-01-10 04:29:58 +00:00
|
|
|
Library: "metal",
|
2024-03-30 16:50:05 +00:00
|
|
|
ID: "0",
|
2023-12-22 23:43:31 +00:00
|
|
|
}
|
2024-03-30 16:50:05 +00:00
|
|
|
info.TotalMemory = uint64(C.getRecommendedMaxVRAM())
|
|
|
|
|
|
|
|
// TODO is there a way to gather actual allocated video memory? (currentAllocatedSize doesn't work)
|
|
|
|
info.FreeMemory = info.TotalMemory
|
|
|
|
|
2024-05-01 15:46:03 +00:00
|
|
|
info.MinimumMemory = metalMinimumMemory
|
2024-03-30 16:50:05 +00:00
|
|
|
return []GpuInfo{info}
|
2023-12-22 23:43:31 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 02:09:23 +00:00
|
|
|
func GetCPUInfo() GpuInfoList {
|
|
|
|
mem, _ := GetCPUMem()
|
|
|
|
return []GpuInfo{
|
|
|
|
{
|
|
|
|
Library: "cpu",
|
2024-05-31 04:54:07 +00:00
|
|
|
Variant: GetCPUCapability().String(),
|
2024-06-04 02:09:23 +00:00
|
|
|
memInfo: mem,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-30 16:50:05 +00:00
|
|
|
func GetCPUMem() (memInfo, error) {
|
2023-12-22 23:43:31 +00:00
|
|
|
return memInfo{
|
2024-04-16 18:22:38 +00:00
|
|
|
TotalMemory: uint64(C.getPhysicalMemory()),
|
2024-07-06 23:35:04 +00:00
|
|
|
FreeMemory: uint64(C.getFreeMemory()),
|
2024-07-11 23:42:57 +00:00
|
|
|
// FreeSwap omitted as Darwin uses dynamic paging
|
2023-12-22 23:43:31 +00:00
|
|
|
}, nil
|
2023-11-29 19:00:37 +00:00
|
|
|
}
|
2024-03-30 16:50:05 +00:00
|
|
|
|
|
|
|
func (l GpuInfoList) GetVisibleDevicesEnv() (string, string) {
|
|
|
|
// No-op on darwin
|
|
|
|
return "", ""
|
|
|
|
}
|
2024-10-14 23:26:45 +00:00
|
|
|
|
|
|
|
func GetSystemInfo() SystemInfo {
|
|
|
|
mem, _ := GetCPUMem()
|
2024-10-15 18:36:08 +00:00
|
|
|
query := "hw.perflevel0.physicalcpu"
|
|
|
|
perfCores, err := syscall.SysctlUint32(query)
|
|
|
|
if err != nil {
|
|
|
|
slog.Warn("failed to discover physical CPU details", "query", query, "error", err)
|
|
|
|
}
|
|
|
|
query = "hw.perflevel1.physicalcpu"
|
|
|
|
efficiencyCores, _ := syscall.SysctlUint32(query) // On x86 xeon this wont return data
|
|
|
|
|
|
|
|
// Determine thread count
|
|
|
|
query = "hw.logicalcpu"
|
|
|
|
logicalCores, _ := syscall.SysctlUint32(query)
|
|
|
|
|
2024-10-14 23:26:45 +00:00
|
|
|
return SystemInfo{
|
|
|
|
System: CPUInfo{
|
|
|
|
GpuInfo: GpuInfo{
|
|
|
|
memInfo: mem,
|
|
|
|
},
|
2024-10-15 18:36:08 +00:00
|
|
|
CPUs: []CPU{
|
|
|
|
{
|
|
|
|
CoreCount: int(perfCores + efficiencyCores),
|
|
|
|
EfficiencyCoreCount: int(efficiencyCores),
|
|
|
|
ThreadCount: int(logicalCores),
|
|
|
|
},
|
|
|
|
},
|
2024-10-14 23:26:45 +00:00
|
|
|
},
|
|
|
|
GPUs: GetGPUInfo(),
|
|
|
|
}
|
|
|
|
}
|