Run of fresh-releases for ruby-get-process-mem

Merge these changes:

git pull https://janitor.debian.net/git/ruby-get-process-mem fresh-releases/main
git pull https://janitor.debian.net/git/ruby-get-process-mem fresh-releases/pristine-tar
git pull https://janitor.debian.net/git/ruby-get-process-mem fresh-releases/upstream

Summary

Merged new upstream version: 0.2.7 (was: 0.2.5).

Diff

Branch: main

Diff is too long (more than 200 lines). Download the raw diff.

Branch: pristine-tar

diff --git a/ruby-get-process-mem_0.2.7.orig.tar.gz.delta b/ruby-get-process-mem_0.2.7.orig.tar.gz.delta
new file mode 100644
index 0000000..c6fb189
Binary files /dev/null and b/ruby-get-process-mem_0.2.7.orig.tar.gz.delta differ
diff --git a/ruby-get-process-mem_0.2.7.orig.tar.gz.id b/ruby-get-process-mem_0.2.7.orig.tar.gz.id
new file mode 100644
index 0000000..e9abddb
--- /dev/null
+++ b/ruby-get-process-mem_0.2.7.orig.tar.gz.id
@@ -0,0 +1 @@
+39016792b6937fafdbc12e8edc2fba19c70b3d58

Branch: upstream

Tag: upstream/0.2.7
diff --git a/.travis.yml b/.travis.yml
index c11d9fa..1748b52 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -15,6 +15,8 @@ rvm:
 
 matrix:
   allow_failures:
+    - rvm: 2.2
+      if: os = osx
     - rvm: ruby-head
     - rvm: jruby-head
     - rvm: truffleruby
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 02e9f6d..d03f3c0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,4 +1,12 @@
-## Master - unreleased
+## HEAD (unreleased)
+
+## 0.2.7
+
+- Native (faster) support for returning memory from different PIDs on mac (https://github.com/schneems/get_process_mem/pull/42)
+
+## 0.2.6
+
+- Support returning memory from different PIDs on mac (https://github.com/schneems/get_process_mem/pull/41)
 
 ## 0.2.5
 
diff --git a/lib/get_process_mem.rb b/lib/get_process_mem.rb
index e781209..67aaf25 100644
--- a/lib/get_process_mem.rb
+++ b/lib/get_process_mem.rb
@@ -119,6 +119,8 @@ class GetProcessMem
   end
 
   def darwin_memory
-    Darwin.resident_size
+    Darwin.resident_size(pid)
+  rescue Errno::EPERM
+    nil
   end
 end
diff --git a/lib/get_process_mem/darwin.rb b/lib/get_process_mem/darwin.rb
index 2fd98cb..111f734 100644
--- a/lib/get_process_mem/darwin.rb
+++ b/lib/get_process_mem/darwin.rb
@@ -3,49 +3,59 @@ require 'ffi'
 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
diff --git a/lib/get_process_mem/version.rb b/lib/get_process_mem/version.rb
index 018d81f..91be889 100644
--- a/lib/get_process_mem/version.rb
+++ b/lib/get_process_mem/version.rb
@@ -1,3 +1,3 @@
 class GetProcessMem
-  VERSION = "0.2.5"
+  VERSION = "0.2.7"
 end
diff --git a/test/get_process_mem_test.rb b/test/get_process_mem_test.rb
index 8f39297..f7b831f 100644
--- a/test/get_process_mem_test.rb
+++ b/test/get_process_mem_test.rb
@@ -1,11 +1,20 @@
 require 'test_helper'
 
 class GetProcessMemTest < Test::Unit::TestCase
-
   def setup
     @mem = GetProcessMem.new
   end
 
+  def test_different_pid_returns_different_memory
+    pid = Process.spawn("tail -f Gemfile")
+
+    other_mem = GetProcessMem.new(pid)
+    assert @mem.kb > other_mem.kb
+  ensure
+    Process.kill('TERM', pid) if pid
+    Process.wait(pid) if pid
+  end
+
   def test_seems_to_work
     assert @mem.kb    > 0
     assert @mem.mb    > 0

Publish Blockers

  • ☑ run was successful
  • ☑ package has not been removed from the archive
  • ☑ command has not changed
  • ☒ not yet reviewed, but review needed
  • ☑ 0 publish attempts so far.not currently attempting to back off
  • ☑ proposals not currently being rate-limited for maintainer pkg-ruby-extras-maintainers@lists.alioth.debian.org
  • ☑ change set bc45949f-b73c-4e72-97fb-d65ffcb65ca1 is ready

Resulting package

The resulting binary packages can be installed (if you have the apt repository enabled) by running one of:

apt install -t fresh-releases ruby-get-process-mem

Lintian Result

Full worker log Full build log