File: times.rb

package info (click to toggle)
jruby 1.5.1-1%2Bdeb6u1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze-lts
  • size: 47,024 kB
  • ctags: 74,144
  • sloc: ruby: 398,155; java: 169,506; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (36 lines) | stat: -rw-r--r-- 967 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
require 'ffi'
require 'java'

module Process
  class Foreign
    SC_CLK_TCK = com.kenai.constantine.platform.Sysconf::_SC_CLK_TCK.value

    extend FFI::Library
    ffi_lib FFI::Library::LIBC
    class Times < FFI::Struct
      layout \
        :utime => :long, 
        :stime => :long,
        :cutime => :long,
        :cstime => :long
    end
    attach_function :times, [ :buffer_out ], :long
    attach_function :sysconf, [ :int ], :long
    Tms = Struct.new("Foreign::Tms", :utime, :stime, :cutime, :cstime)    
  end
  def self.times
    hz = Foreign.sysconf(Foreign::SC_CLK_TCK).to_f
    t = Foreign::Times.alloc_out(false)
    Foreign.times(t.pointer)
    Foreign::Tms.new(t[:utime] / hz, t[:stime] / hz, t[:cutime] / hz, t[:cstime] / hz)
  end
  
end
if $0 == __FILE__
  while true
    10.times { system("ls -l / > /dev/null") }
    t = Process.times
    puts "utime=#{t.utime} stime=#{t.stime} cutime=#{t.cutime} cstime=#{t.cstime}"
    sleep 1
  end
end