mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-12-20 00:50:06 +08:00
chore: simplify GetMemoryInfo in darwin
Some checks are pending
Test / test (1.20, macos-13) (push) Waiting to run
Test / test (1.20, macos-latest) (push) Waiting to run
Test / test (1.20, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.20, ubuntu-latest) (push) Waiting to run
Test / test (1.20, windows-latest) (push) Waiting to run
Test / test (1.21, macos-13) (push) Waiting to run
Test / test (1.21, macos-latest) (push) Waiting to run
Test / test (1.21, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.21, ubuntu-latest) (push) Waiting to run
Test / test (1.21, windows-latest) (push) Waiting to run
Test / test (1.22, macos-13) (push) Waiting to run
Test / test (1.22, macos-latest) (push) Waiting to run
Test / test (1.22, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.22, ubuntu-latest) (push) Waiting to run
Test / test (1.22, windows-latest) (push) Waiting to run
Test / test (1.23, macos-13) (push) Waiting to run
Test / test (1.23, macos-latest) (push) Waiting to run
Test / test (1.23, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.23, ubuntu-latest) (push) Waiting to run
Test / test (1.23, windows-latest) (push) Waiting to run
Test / test (1.24, macos-13) (push) Waiting to run
Test / test (1.24, macos-latest) (push) Waiting to run
Test / test (1.24, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.24, ubuntu-latest) (push) Waiting to run
Test / test (1.24, windows-latest) (push) Waiting to run
Test / test (1.25, macos-13) (push) Waiting to run
Test / test (1.25, macos-latest) (push) Waiting to run
Test / test (1.25, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.25, ubuntu-latest) (push) Waiting to run
Test / test (1.25, windows-latest) (push) Waiting to run
Trigger CMFA Update / trigger-CMFA-update (push) Waiting to run
Some checks are pending
Test / test (1.20, macos-13) (push) Waiting to run
Test / test (1.20, macos-latest) (push) Waiting to run
Test / test (1.20, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.20, ubuntu-latest) (push) Waiting to run
Test / test (1.20, windows-latest) (push) Waiting to run
Test / test (1.21, macos-13) (push) Waiting to run
Test / test (1.21, macos-latest) (push) Waiting to run
Test / test (1.21, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.21, ubuntu-latest) (push) Waiting to run
Test / test (1.21, windows-latest) (push) Waiting to run
Test / test (1.22, macos-13) (push) Waiting to run
Test / test (1.22, macos-latest) (push) Waiting to run
Test / test (1.22, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.22, ubuntu-latest) (push) Waiting to run
Test / test (1.22, windows-latest) (push) Waiting to run
Test / test (1.23, macos-13) (push) Waiting to run
Test / test (1.23, macos-latest) (push) Waiting to run
Test / test (1.23, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.23, ubuntu-latest) (push) Waiting to run
Test / test (1.23, windows-latest) (push) Waiting to run
Test / test (1.24, macos-13) (push) Waiting to run
Test / test (1.24, macos-latest) (push) Waiting to run
Test / test (1.24, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.24, ubuntu-latest) (push) Waiting to run
Test / test (1.24, windows-latest) (push) Waiting to run
Test / test (1.25, macos-13) (push) Waiting to run
Test / test (1.25, macos-latest) (push) Waiting to run
Test / test (1.25, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.25, ubuntu-latest) (push) Waiting to run
Test / test (1.25, windows-latest) (push) Waiting to run
Trigger CMFA Update / trigger-CMFA-update (push) Waiting to run
This commit is contained in:
parent
29eaa4d699
commit
57b527d54a
@ -6,9 +6,7 @@ import (
|
|||||||
"github.com/ebitengine/purego"
|
"github.com/ebitengine/purego"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const PROC_PIDTASKINFO = 4
|
||||||
PROC_PIDTASKINFO = 4
|
|
||||||
)
|
|
||||||
|
|
||||||
type ProcTaskInfo struct {
|
type ProcTaskInfo struct {
|
||||||
Virtual_size uint64
|
Virtual_size uint64
|
||||||
@ -31,89 +29,24 @@ type ProcTaskInfo struct {
|
|||||||
Priority int32
|
Priority int32
|
||||||
}
|
}
|
||||||
|
|
||||||
// Library represents a dynamic library loaded by purego.
|
const System = "/usr/lib/libSystem.B.dylib"
|
||||||
type Library struct {
|
|
||||||
addr uintptr
|
|
||||||
path string
|
|
||||||
close func()
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewLibrary(path string) (*Library, error) {
|
type ProcPidInfoFunc func(pid, flavor int32, arg uint64, buffer uintptr, bufferSize int32) int32
|
||||||
lib, err := purego.Dlopen(path, purego.RTLD_LAZY|purego.RTLD_GLOBAL)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
closeFunc := func() {
|
const ProcPidInfoSym = "proc_pidinfo"
|
||||||
purego.Dlclose(lib)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Library{
|
|
||||||
addr: lib,
|
|
||||||
path: path,
|
|
||||||
close: closeFunc,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lib *Library) Dlsym(symbol string) (uintptr, error) {
|
|
||||||
return purego.Dlsym(lib.addr, symbol)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetFunc[T any](lib *Library, symbol string) T {
|
|
||||||
var fptr T
|
|
||||||
purego.RegisterLibFunc(&fptr, lib.addr, symbol)
|
|
||||||
return fptr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lib *Library) Close() {
|
|
||||||
lib.close()
|
|
||||||
}
|
|
||||||
|
|
||||||
// library paths
|
|
||||||
const (
|
|
||||||
System = "/usr/lib/libSystem.B.dylib"
|
|
||||||
)
|
|
||||||
|
|
||||||
// System functions and symbols.
|
|
||||||
type (
|
|
||||||
ProcPidInfoFunc func(pid, flavor int32, arg uint64, buffer uintptr, bufferSize int32) int32
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
ProcPidInfoSym = "proc_pidinfo"
|
|
||||||
)
|
|
||||||
|
|
||||||
type dlFuncs struct {
|
|
||||||
lib *Library
|
|
||||||
|
|
||||||
procPidInfo ProcPidInfoFunc
|
|
||||||
}
|
|
||||||
|
|
||||||
func loadProcFuncs() (*dlFuncs, error) {
|
|
||||||
lib, err := NewLibrary(System)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &dlFuncs{
|
|
||||||
lib: lib,
|
|
||||||
procPidInfo: GetFunc[ProcPidInfoFunc](lib, ProcPidInfoSym),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *dlFuncs) Close() {
|
|
||||||
f.lib.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetMemoryInfo(pid int32) (*MemoryInfoStat, error) {
|
func GetMemoryInfo(pid int32) (*MemoryInfoStat, error) {
|
||||||
funcs, err := loadProcFuncs()
|
lib, err := purego.Dlopen(System, purego.RTLD_LAZY|purego.RTLD_GLOBAL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer funcs.Close()
|
defer purego.Dlclose(lib)
|
||||||
|
|
||||||
|
var procPidInfo ProcPidInfoFunc
|
||||||
|
purego.RegisterLibFunc(&procPidInfo, lib, ProcPidInfoSym)
|
||||||
|
|
||||||
var ti ProcTaskInfo
|
var ti ProcTaskInfo
|
||||||
funcs.procPidInfo(pid, PROC_PIDTASKINFO, 0, uintptr(unsafe.Pointer(&ti)), int32(unsafe.Sizeof(ti)))
|
procPidInfo(pid, PROC_PIDTASKINFO, 0, uintptr(unsafe.Pointer(&ti)), int32(unsafe.Sizeof(ti)))
|
||||||
|
|
||||||
ret := &MemoryInfoStat{
|
ret := &MemoryInfoStat{
|
||||||
RSS: uint64(ti.Resident_size),
|
RSS: uint64(ti.Resident_size),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user