win: revert CPU discovery logic to 0.12.3 (#12969)

The behavior change in 0.12.4 is the most likely the root cause of hangs some
users are seeing.  This reverts to the 0.12.3 code, with some added trace
logging.
This commit is contained in:
Daniel Hiltgen
2025-11-05 10:32:38 -08:00
committed by GitHub
parent 8bbc7395db
commit 97e05d2a6b

View File

@@ -5,6 +5,8 @@ import (
"log/slog" "log/slog"
"syscall" "syscall"
"unsafe" "unsafe"
"github.com/ollama/ollama/logutil"
) )
type MEMORYSTATUSEX struct { type MEMORYSTATUSEX struct {
@@ -99,22 +101,29 @@ func (pkg *winPackage) IsMember(target *GROUP_AFFINITY) bool {
} }
func getLogicalProcessorInformationEx() ([]byte, error) { func getLogicalProcessorInformationEx() ([]byte, error) {
buf := make([]byte, 1024) buf := make([]byte, 1)
bufSize := len(buf) bufSize := len(buf)
var err error ret, _, err := GetLogicalProcessorInformationEx.Call(
for range 3 { uintptr(RelationAll),
var ret uintptr uintptr(unsafe.Pointer(&buf[0])),
uintptr(unsafe.Pointer(&bufSize)),
)
if ret != 0 {
logutil.Trace("failed to retrieve CPU payload size", "ret", ret, "size", bufSize, "error", err)
return nil, fmt.Errorf("failed to determine size info ret:%d %w", ret, err)
}
buf = make([]byte, bufSize)
ret, _, err = GetLogicalProcessorInformationEx.Call( ret, _, err = GetLogicalProcessorInformationEx.Call(
uintptr(RelationAll), uintptr(RelationAll),
uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&buf[0])),
uintptr(unsafe.Pointer(&bufSize)), uintptr(unsafe.Pointer(&bufSize)),
) )
if ret == 1 && bufSize <= len(buf) { if ret == 0 {
logutil.Trace("failed to retrieve CPU information", "ret", ret, "size", len(buf), "new_size", bufSize, "error", err)
return nil, fmt.Errorf("failed to gather processor information ret:%d buflen:%d %w", ret, bufSize, err)
}
return buf, nil return buf, nil
}
buf = make([]byte, bufSize)
}
return nil, fmt.Errorf("unable to determine CPU details: %w", err)
} }
func processSystemLogicalProcessorInforationList(buf []byte) []*winPackage { func processSystemLogicalProcessorInforationList(buf []byte) []*winPackage {