diff --git a/process/process_bsd.go b/process/process_bsd.go index 263829ffa..1f1e7b323 100644 --- a/process/process_bsd.go +++ b/process/process_bsd.go @@ -7,12 +7,29 @@ import ( "bytes" "context" "encoding/binary" + "encoding/json" "github.com/shirou/gopsutil/v3/cpu" "github.com/shirou/gopsutil/v3/internal/common" ) -type MemoryInfoExStat struct{} +type MemoryInfoExStat struct { + RSS uint64 `json:"rss"` // bytes + VMS uint64 `json:"vms"` // bytes + HWM uint64 `json:"hwm"` // bytes + Data uint64 `json:"data"` // bytes + Stack uint64 `json:"stack"` // bytes + Locked uint64 `json:"locked"` // bytes + Swap uint64 `json:"swap"` // bytes + Footprint uint64 `json:"footprint"` // bytes + Wired uint64 `json:"wired"` // bytes + Resident uint64 `json:"resident"` // bytes +} + +func (m MemoryInfoExStat) String() string { + s, _ := json.Marshal(m) + return string(s) +} type MemoryMapsStat struct{} @@ -44,9 +61,9 @@ func (p *Process) CPUAffinityWithContext(ctx context.Context) ([]int32, error) { return nil, common.ErrNotImplementedError } -func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error) { - return nil, common.ErrNotImplementedError -} +// func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error) { +// return nil, common.ErrNotImplementedError +// } func (p *Process) PageFaultsWithContext(ctx context.Context) (*PageFaultsStat, error) { return nil, common.ErrNotImplementedError diff --git a/process/process_darwin_cgo.go b/process/process_darwin_cgo.go index 858f08e7a..5db90ce7b 100644 --- a/process/process_darwin_cgo.go +++ b/process/process_darwin_cgo.go @@ -10,6 +10,7 @@ package process // #include // #include // #include +// #include import "C" import ( @@ -220,3 +221,30 @@ func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, e } return ret, nil } + +func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error) { + const tiSize = C.sizeof_struct_proc_taskinfo + ti := (*C.struct_proc_taskinfo)(C.malloc(tiSize)) + defer C.free(unsafe.Pointer(ti)) + + _, err := C.proc_pidinfo(C.int(p.Pid), C.PROC_PIDTASKINFO, 0, unsafe.Pointer(ti), tiSize) + if err != nil { + return nil, err + } + + var usage C.struct_rusage_info_v2 + + _, err = C.proc_pid_rusage(C.int(p.Pid), C.RUSAGE_INFO_V2, (*C.rusage_info_t)(unsafe.Pointer(&usage))) + if err != nil { + return nil, err + } + + ret := &MemoryInfoExStat{ + VMS: uint64(ti.pti_virtual_size), + Swap: uint64(ti.pti_pageins), + Footprint: uint64(usage.ri_phys_footprint), + Wired: uint64(usage.ri_wired_size), + Resident: uint64(usage.ri_resident_size), + } + return ret, nil +}