mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-12-21 17:50:06 +08:00
fix: data race on darwin
This commit is contained in:
parent
0992ee8adf
commit
92ecdfcc00
@ -1,5 +1,5 @@
|
|||||||
// Package memory return MemoryInfoStat
|
// Package memory return MemoryInfoStat
|
||||||
// modify from https://github.com/shirou/gopsutil/tree/v4.25.1/process
|
// modify from https://github.com/shirou/gopsutil/tree/v4.25.8/process
|
||||||
package memory
|
package memory
|
||||||
|
|
||||||
type MemoryInfoStat struct {
|
type MemoryInfoStat struct {
|
||||||
|
|||||||
@ -83,30 +83,37 @@ const (
|
|||||||
ProcPidInfoSym = "proc_pidinfo"
|
ProcPidInfoSym = "proc_pidinfo"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
type dlFuncs struct {
|
||||||
procPidInfo ProcPidInfoFunc
|
lib *Library
|
||||||
)
|
|
||||||
|
|
||||||
func registerFuncs() (*Library, error) {
|
procPidInfo ProcPidInfoFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadProcFuncs() (*dlFuncs, error) {
|
||||||
lib, err := NewLibrary(System)
|
lib, err := NewLibrary(System)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
procPidInfo = GetFunc[ProcPidInfoFunc](lib, ProcPidInfoSym)
|
return &dlFuncs{
|
||||||
|
lib: lib,
|
||||||
|
procPidInfo: GetFunc[ProcPidInfoFunc](lib, ProcPidInfoSym),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
return lib, nil
|
func (f *dlFuncs) Close() {
|
||||||
|
f.lib.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMemoryInfo(pid int32) (*MemoryInfoStat, error) {
|
func GetMemoryInfo(pid int32) (*MemoryInfoStat, error) {
|
||||||
lib, err := registerFuncs()
|
funcs, err := loadProcFuncs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer lib.Close()
|
defer funcs.Close()
|
||||||
|
|
||||||
var ti ProcTaskInfo
|
var ti ProcTaskInfo
|
||||||
procPidInfo(pid, PROC_PIDTASKINFO, 0, uintptr(unsafe.Pointer(&ti)), int32(unsafe.Sizeof(ti)))
|
funcs.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