Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from mach task_info to proc_pidinfo #42

Merged
merged 3 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/get_process_mem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def linux?

def bytes
memory = linux_status_memory if linux?
memory ||= darwin_memory if RUNS_ON_DARWIN && Process.pid == pid
memory ||= darwin_memory if RUNS_ON_DARWIN
memory ||= ps_memory
end

Expand Down Expand Up @@ -119,6 +119,8 @@ def ps_memory
end

def darwin_memory
Darwin.resident_size
Darwin.resident_size(pid)
rescue Errno::EPERM
nil
end
end
68 changes: 39 additions & 29 deletions lib/get_process_mem/darwin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,59 @@
class GetProcessMem
class Darwin
extend FFI::Library
ffi_lib 'c'
attach_function :mach_task_self, [], :__darwin_mach_port_t
attach_function :task_info,
[
:__darwin_mach_port_t,
:int, # return selector
:pointer, #pointer to task info
:pointer, #pointer to int (size of structure / bytes filled out)
],
:int
ffi_lib 'proc'

class IntPtr < FFI::Struct
layout :value, :int
end

class TaskInfo < FFI::Struct
layout :suspend_count, :int32,
:virtual_size, :uint64,
:resident_size, :uint64,
:user_time, :uint64,
:system_time, :uint64,
:policy, :int32
layout :pti_virtual_size, :uint64,
:pti_resident_size, :uint64,
:pti_total_user, :uint64,
:pti_total_system, :uint64,
:pti_threads_user, :uint64,
:pti_threads_system, :uint64,
:pti_policy, :int32,
:pti_faults, :int32,
:pti_pageins, :int32,
:pti_cow_faults, :int32,
:pti_messages_sent, :int32,
:pti_messages_received, :int32,
:pti_syscalls_mach, :int32,
:pti_syscalls_unix, :int32,
:pti_csw, :int32,
:pti_threadnum, :int32,
:pti_numrunning, :int32,
:pti_priority, :int32

end

MACH_TASK_BASIC_INFO = 20
MACH_TASK_BASIC_INFO_COUNT = TaskInfo.size / FFI.type_size(:uint)

attach_function :proc_pidinfo,
[
:int, #pid
:int, # flavour
:uint64, #arg, not needed for this selector
TaskInfo.by_ref, #output buffer
:int, #size of buffer
],
:int


PROC_PIDTASKINFO = 4 #from sys/proc_info.h

class << self
def resident_size
mach_task_info[:resident_size]
def resident_size(pid)
get_proc_pidinfo(pid)[:pti_resident_size]
end

private

def mach_task_info
def get_proc_pidinfo(pid)
data = TaskInfo.new
out_count = IntPtr.new
out_count[:value] = MACH_TASK_BASIC_INFO_COUNT
result = task_info(mach_task_self, MACH_TASK_BASIC_INFO, data, out_count)
if result == 0
result = proc_pidinfo(pid, PROC_PIDTASKINFO, 0, data, TaskInfo.size)
if result == TaskInfo.size
data
else
raise "task_info returned #{result}"
raise SystemCallError.new("proc_pidinfo returned #{result}", FFI.errno);
end
end
end
Expand Down