Created
July 9, 2011 14:47
-
-
Save mikbe/1073628 to your computer and use it in GitHub Desktop.
FFI testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "ffi" | |
class TimeValue < FFI::Struct | |
layout :seconds, :int, | |
:microseconds, :int | |
end | |
#struct task_basic_info | |
#{ | |
# integer_t suspend_count; | |
# vm_size_t virtual_size; | |
# vm_size_t resident_size; | |
# time_value_t user_time; | |
# time_value_t system_time; | |
# policy_t policy; | |
#}; | |
#struct task_basic_info_64 { | |
# integer_t suspend_count; /* suspend count for task */ | |
# mach_vm_size_t virtual_size; /* virtual memory size (bytes) */ | |
# mach_vm_size_t resident_size; /* resident memory size (bytes) */ | |
# time_value_t user_time; /* total user run time for | |
# terminated threads */ | |
# time_value_t system_time; /* total system run time for | |
# terminated threads */ | |
# policy_t policy; /* default policy for new threads */ | |
#}; | |
class TaskBasicInfo < FFI::Struct | |
layout :suspend_count, :int, | |
:virtual_size, :uint64, | |
:resident_size, :uint64, | |
:user_time, TimeValue, | |
:system_time, TimeValue, | |
:policy, :int | |
end | |
# class TaskBasicInfo < FFI::Struct | |
# layout( | |
# :suspend_count, integer_t, | |
# :virtual_size, vm_size_t, | |
# :resident_size, vm_size_t, | |
# :user_time, time_value_t, | |
# :system_time, time_value_t, | |
# :policy, policy_t | |
# ) | |
# end | |
TASK_BASIC_INFO = 5 | |
module LibSystem | |
extend FFI::Library | |
lib = ffi_lib '/usr/lib/libSystem.dylib' | |
attach_function :mach_task_self, [], :uint64 | |
attach_function :task_for_pid, [:uint64, :uint, :pointer], :uint | |
#attach_function :task_info, [:uint, :int, :pointer, :pointer], :uint | |
attach_function :task_info, [:uint64, :uint, :pointer, :pointer], :uint | |
end | |
module Foo | |
extend FFI::Library | |
lib = ffi_lib '/Users/bethanym/development/mikbe/rbmu/CPUThrottle/foo.dylib' | |
attach_function :mach_task_self, [], :uint | |
attach_function :sample, [:int32, :pointer, :pointer, :pointer], :int | |
end | |
user_time = FFI::MemoryPointer.new(8, 1, true) | |
system_time = FFI::MemoryPointer.new(8, 1, true) | |
percent = FFI::MemoryPointer.new(8, 1, true) | |
puts me = Foo.mach_task_self | |
puts Foo.sample(me, user_time, system_time, percent) | |
#KB64 = 64 * 1024 * 1024 | |
#PID = 14433 | |
#task_info_out = FFI::MemoryPointer.new TaskBasicInfo.size, 2, true | |
#task_info_out_count = FFI::MemoryPointer.new(KB64.size, 1, true) | |
#task_pointer = FFI::MemoryPointer.new(KB64.size, 1, true) | |
#puts "this task" | |
#puts (this_task = LibSystem.mach_task_self) | |
#puts "task for pid: (5 == failure)" | |
#puts LibSystem.task_for_pid(this_task, PID, task_pointer) | |
#puts task = task_pointer.get_uint(0) | |
#puts "Get task info: (should be 0)" | |
#puts LibSystem.task_info this_task, TASK_BASIC_INFO, task_info_out, task_info_out_count | |
#task_info = TaskBasicInfo.new task_info_out | |
#puts task_info[:resident_size] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment