-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
380 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
//go:build darwin && arm64 | ||
|
||
package cpu | ||
|
||
import ( | ||
"fmt" | ||
"unsafe" | ||
|
||
"github.com/shirou/gopsutil/v4/internal/common" | ||
) | ||
|
||
// https://github.com/shoenig/go-m1cpu/blob/v0.1.6/cpu.go | ||
func getFrequency() (float64, error) { | ||
ioKit, err := common.NewLibrary(common.IOKit) | ||
if err != nil { | ||
return 0, err | ||
} | ||
defer ioKit.Close() | ||
|
||
coreFoundation, err := common.NewLibrary(common.CoreFoundation) | ||
if err != nil { | ||
return 0.0, err | ||
} | ||
defer coreFoundation.Close() | ||
|
||
ioServiceMatching := common.GetFunc[common.IOServiceMatchingFunc](ioKit, common.IOServiceMatchingSym) | ||
ioServiceGetMatchingServices := common.GetFunc[common.IOServiceGetMatchingServicesFunc](ioKit, common.IOServiceGetMatchingServicesSym) | ||
ioIteratorNext := common.GetFunc[common.IOIteratorNextFunc](ioKit, common.IOIteratorNextSym) | ||
ioRegistryEntryGetName := common.GetFunc[common.IORegistryEntryGetNameFunc](ioKit, common.IORegistryEntryGetNameSym) | ||
ioRegistryEntryCreateCFProperty := common.GetFunc[common.IORegistryEntryCreateCFPropertyFunc](ioKit, common.IORegistryEntryCreateCFPropertySym) | ||
ioObjectRelease := common.GetFunc[common.IOObjectReleaseFunc](ioKit, common.IOObjectReleaseSym) | ||
|
||
cfStringCreateWithCString := common.GetFunc[common.CFStringCreateWithCStringFunc](coreFoundation, common.CFStringCreateWithCStringSym) | ||
cfDataGetLength := common.GetFunc[common.CFDataGetLengthFunc](coreFoundation, common.CFDataGetLengthSym) | ||
cfDataGetBytePtr := common.GetFunc[common.CFDataGetBytePtrFunc](coreFoundation, common.CFDataGetBytePtrSym) | ||
cfRelease := common.GetFunc[common.CFReleaseFunc](coreFoundation, common.CFReleaseSym) | ||
|
||
matching := ioServiceMatching("AppleARMIODevice") | ||
|
||
var iterator uint32 | ||
if status := ioServiceGetMatchingServices(common.KIOMainPortDefault, uintptr(matching), &iterator); status != common.KERN_SUCCESS { | ||
return 0.0, fmt.Errorf("IOServiceGetMatchingServices error=%d", status) | ||
} | ||
|
||
pCorekey := cfStringCreateWithCString(common.KCFAllocatorDefault, "voltage-states5-sram", common.KCFStringEncodingUTF8) | ||
defer cfRelease(uintptr(pCorekey)) | ||
|
||
var pCoreHz uint32 | ||
for { | ||
service := ioIteratorNext(iterator) | ||
if !(service > 0) { | ||
break | ||
} | ||
|
||
buf := make([]byte, 512) | ||
ioRegistryEntryGetName(service, &buf[0]) | ||
|
||
if common.GoString(&buf[0]) == "pmgr" { | ||
pCoreRef := ioRegistryEntryCreateCFProperty(service, uintptr(pCorekey), common.KCFAllocatorDefault, common.KNilOptions) | ||
length := cfDataGetLength(uintptr(pCoreRef)) | ||
data := cfDataGetBytePtr(uintptr(pCoreRef)) | ||
|
||
// composite uint32 from the byte array | ||
buf := unsafe.Slice((*byte)(data), length) | ||
b1 := buf[length-5] | ||
b2 := buf[length-6] | ||
b3 := buf[length-7] | ||
b4 := buf[length-8] | ||
|
||
// combine the bytes into a uint32 value | ||
pCoreHz = uint32(b1)<<24 | uint32(b2)<<16 | uint32(b3)<<8 | uint32(b4) | ||
ioObjectRelease(service) | ||
break | ||
} | ||
|
||
ioObjectRelease(service) | ||
} | ||
|
||
return float64(pCoreHz / 1_000_000), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
//go:build darwin && !arm64 | ||
|
||
package cpu | ||
|
||
import "golang.org/x/sys/unix" | ||
|
||
func getFrequency() (float64, error) { | ||
// Use the rated frequency of the CPU. This is a static value and does not | ||
// account for low power or Turbo Boost modes. | ||
cpuFrequency, err := unix.SysctlUint64("hw.cpufrequency") | ||
return float64(cpuFrequency) / 1000000.0, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.