/tools/jruby-1.5.1/lib/ruby/site_ruby/shared/ffi/times.rb

https://github.com/mingle/oauth2_provider · Ruby · 36 lines · 33 code · 3 blank · 0 comment · 3 complexity · 00e1403ae662f4e2dad53aeb42dfe602 MD5 · raw file

  1. require 'ffi'
  2. require 'java'
  3. module Process
  4. class Foreign
  5. SC_CLK_TCK = com.kenai.constantine.platform.Sysconf::_SC_CLK_TCK.value
  6. extend FFI::Library
  7. ffi_lib FFI::Library::LIBC
  8. class Times < FFI::Struct
  9. layout \
  10. :utime => :long,
  11. :stime => :long,
  12. :cutime => :long,
  13. :cstime => :long
  14. end
  15. attach_function :times, [ :buffer_out ], :long
  16. attach_function :sysconf, [ :int ], :long
  17. Tms = Struct.new("Foreign::Tms", :utime, :stime, :cutime, :cstime)
  18. end
  19. def self.times
  20. hz = Foreign.sysconf(Foreign::SC_CLK_TCK).to_f
  21. t = Foreign::Times.alloc_out(false)
  22. Foreign.times(t.pointer)
  23. Foreign::Tms.new(t[:utime] / hz, t[:stime] / hz, t[:cutime] / hz, t[:cstime] / hz)
  24. end
  25. end
  26. if $0 == __FILE__
  27. while true
  28. 10.times { system("ls -l / > /dev/null") }
  29. t = Process.times
  30. puts "utime=#{t.utime} stime=#{t.stime} cutime=#{t.cutime} cstime=#{t.cstime}"
  31. sleep 1
  32. end
  33. end