Skip to content
Browse files

Add getrusage().

  • Loading branch information...
1 parent b640c65 commit 3d515cae92f46f5b7a12f215d9c38552fb5f0f5e Al Tobey committed Apr 5, 2012
Showing with 43 additions and 2 deletions.
  1. +18 −2 lib/ffi/libc/libc.rb
  2. +25 −0 lib/ffi/libc/rusage.rb
View
20 lib/ffi/libc/libc.rb
@@ -2,6 +2,7 @@
require 'ffi/libc/timeval'
require 'ffi/libc/timezone'
require 'ffi/libc/ifaddrs'
+require 'ffi/libc/rusage'
require 'ffi'
@@ -82,15 +83,15 @@ def self.raise_error(error=self.errno)
attach_function :strrchr, [:buffer_in, :int], :pointer
attach_function :strstr, [:buffer_in, :string], :pointer
attach_function :strerror, [:int], :string
-
+
begin
attach_variable :stdin, :pointer
attach_variable :stdout, :pointer
attach_variable :stderr, :pointer
rescue FFI::NotFoundError
# stdin, stdout, stderr are not available on OSX
end
-
+
attach_function :fopen, [:string, :string], :FILE
attach_function :fdopen, [:int, :string], :FILE
attach_function :freopen, [:string, :string, :FILE], :FILE
@@ -153,5 +154,20 @@ def self.each_ifaddrs
freeifaddrs(ptr.get_pointer(0))
end
+
+ # /usr/include/bits/resource.h on Linux
+ # /usr/include/sys/resource.h on Darwin
+ RUSAGE_SELF = 0
+ RUSAGE_CHILDREN = -1
+ RUSAGE_THREAD = 1 # Linux/glibc only
+
+ attach_function :getrusage, [:int, :pointer], :int
+
+ def self.getrusage(who=RUSAGE_SELF)
+ ru = RUsage.new
+ ret = getrusage(who, ru)
+ raise_error(ret) unless ret == 0
+ ru
+ end
end
end
View
25 lib/ffi/libc/rusage.rb
@@ -0,0 +1,25 @@
+require 'ffi/libc/types'
+require 'ffi/libc/timeval'
+
+module FFI
+ module LibC
+ class RUsage < FFI::Struct
+ layout :ru_utime, Timeval,
+ :ru_stime, Timeval,
+ :ru_maxrss, :long,
+ :ru_ixrss, :long,
+ :ru_idrss, :long,
+ :ru_isrss, :long,
+ :ru_minflt, :long,
+ :ru_majflt, :long,
+ :ru_nswap, :long,
+ :ru_inblock, :long,
+ :ru_oublock, :long,
+ :ru_msgsnd, :long,
+ :ru_msgrcv, :long,
+ :ru_nsignals, :long,
+ :ru_nvcsw, :long,
+ :ru_nivcsw, :long,
+ end
+ end
+end

0 comments on commit 3d515ca

Please sign in to comment.
Something went wrong with that request. Please try again.