mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-04 16:34:52 +03:00
151 lines
3.6 KiB
Go
151 lines
3.6 KiB
Go
//go:build darwin
|
|
|
|
package sys
|
|
|
|
/*
|
|
#cgo LDFLAGS: -framework IOKit -framework CoreFoundation
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
#include <IOKit/IOKitLib.h>
|
|
|
|
static CFDataRef copyPCoreVoltageStates(void) {
|
|
io_iterator_t devices;
|
|
if (IOServiceGetMatchingServices(MACH_PORT_NULL, IOServiceMatching("AppleARMIODevice"), &devices) != KERN_SUCCESS) {
|
|
return NULL;
|
|
}
|
|
CFDataRef states = NULL;
|
|
io_object_t device;
|
|
while (states == NULL && (device = IOIteratorNext(devices)) != 0) {
|
|
CFTypeRef value = IORegistryEntryCreateCFProperty(device, CFSTR("voltage-states5-sram"), kCFAllocatorDefault, 0);
|
|
if (value != NULL && CFGetTypeID(value) == CFDataGetTypeID()) {
|
|
states = value;
|
|
} else if (value != NULL) {
|
|
CFRelease(value);
|
|
}
|
|
IOObjectRelease(device);
|
|
}
|
|
IOObjectRelease(devices);
|
|
return states;
|
|
}
|
|
*/
|
|
import "C"
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
"syscall"
|
|
"unsafe"
|
|
|
|
"github.com/shirou/gopsutil/v4/net"
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
var SIGUSR1 = syscall.SIGUSR1
|
|
|
|
func GetTCPCount() (int, error) {
|
|
stats, err := net.Connections("tcp")
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return len(stats), nil
|
|
}
|
|
|
|
func GetUDPCount() (int, error) {
|
|
stats, err := net.Connections("udp")
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return len(stats), nil
|
|
}
|
|
|
|
// --- CPU Utilization (macOS native) ---
|
|
|
|
// sysctl kern.cp_time returns 5 longs in the BSD CPUSTATES order:
|
|
// user, nice, sys, intr, idle (CP_INTR=3, CP_IDLE=4). gopsutil reads the
|
|
// same layout in cpu_darwin_nocgo.go.
|
|
var (
|
|
cpuMu sync.Mutex
|
|
lastTotals [5]uint64
|
|
hasLastCPUT bool
|
|
)
|
|
|
|
func CPUPercentRaw() (float64, error) {
|
|
raw, err := unix.SysctlRaw("kern.cp_time")
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
// Expect either 5*8 bytes (uint64) or 5*4 bytes (uint32)
|
|
var out [5]uint64
|
|
switch len(raw) {
|
|
case 5 * 8:
|
|
for i := range 5 {
|
|
out[i] = binary.LittleEndian.Uint64(raw[i*8 : (i+1)*8])
|
|
}
|
|
case 5 * 4:
|
|
for i := range 5 {
|
|
out[i] = uint64(binary.LittleEndian.Uint32(raw[i*4 : (i+1)*4]))
|
|
}
|
|
default:
|
|
return 0, fmt.Errorf("unexpected kern.cp_time size: %d", len(raw))
|
|
}
|
|
|
|
cpuMu.Lock()
|
|
defer cpuMu.Unlock()
|
|
|
|
if !hasLastCPUT {
|
|
lastTotals = out
|
|
hasLastCPUT = true
|
|
return 0, nil
|
|
}
|
|
|
|
var deltas [5]uint64
|
|
var totald uint64
|
|
for i := range 5 {
|
|
deltas[i] = out[i] - lastTotals[i]
|
|
totald += deltas[i]
|
|
}
|
|
lastTotals = out
|
|
|
|
if totald == 0 {
|
|
return 0, nil
|
|
}
|
|
idleDelta := deltas[4]
|
|
busy := totald - idleDelta
|
|
pct := float64(busy) / float64(totald) * 100.0
|
|
if pct > 100 {
|
|
pct = 100
|
|
}
|
|
return pct, nil
|
|
}
|
|
|
|
// gopsutil reads the table only from the "pmgr" node; M6 moved it to
|
|
// "pmgr-child", so every AppleARMIODevice is searched for it here.
|
|
func platformCPUSpeedMHz() (float64, error) {
|
|
states := C.copyPCoreVoltageStates()
|
|
if states == 0 {
|
|
return 0, errors.New("no AppleARMIODevice carries voltage-states5-sram")
|
|
}
|
|
defer C.CFRelease(C.CFTypeRef(states))
|
|
return appleSiliconMaxMHz(C.GoBytes(unsafe.Pointer(C.CFDataGetBytePtr(states)), C.int(C.CFDataGetLength(states))))
|
|
}
|
|
|
|
// No core clocks below 100 MHz, so a smaller frequency word is in kHz.
|
|
const appleSiliconKHzCeiling = 100_000_000
|
|
|
|
// appleSiliconMaxMHz reads a voltage-states table of little-endian
|
|
// (frequency, millivolt) pairs: Hz through M3, kHz from M4 on.
|
|
func appleSiliconMaxMHz(states []byte) (float64, error) {
|
|
var fastest uint64
|
|
for pair := 0; pair+8 <= len(states); pair += 8 {
|
|
fastest = max(fastest, uint64(binary.LittleEndian.Uint32(states[pair:])))
|
|
}
|
|
if fastest == 0 {
|
|
return 0, errors.New("voltage-states table holds no frequency")
|
|
}
|
|
if fastest < appleSiliconKHzCeiling {
|
|
fastest *= 1000
|
|
}
|
|
return float64(fastest) / 1_000_000, nil
|
|
}
|