Class: OS

Inherits:
Object
  • Object
show all
Defined in:
lib/os.rb

Overview

a set of friendly files for determining your Ruby runtime treats cygwin as linux also treats IronRuby on mono as…linux

Defined Under Namespace

Classes: Underlying

Class Method Summary collapse

Class Method Details

.bitsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/os.rb', line 58

def self.bits
  @bits ||= begin
    require 'rbconfig'
    host_cpu = RbConfig::CONFIG['host_cpu']
    if host_cpu =~ /_64$/ # x86_64
      64
    elsif RUBY_PLATFORM == 'java' && ENV_JAVA['sun.arch.data.model'] # "32" or "64" http://www.ruby-forum.com/topic/202173#880613
      ENV_JAVA['sun.arch.data.model'].to_i
    elsif host_cpu == 'i386'
      32
    elsif RbConfig::CONFIG['host_os'] =~ /32$/ # mingw32, mswin32
      32
    else # cygwin only...I think
      if 1.size == 8
        64
      else
        32
      end
    end
  end
end

.cygwin?Boolean

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
158
# File 'lib/os.rb', line 150

def self.cygwin?
  @cygwin = begin
    if RUBY_PLATFORM =~ /-cygwin/
      true
    else
      false
    end
  end
end

.iron_ruby?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
# File 'lib/os.rb', line 48

def self.iron_ruby?
 @iron_ruby ||= begin
   if defined?(RUBY_ENGINE) && (RUBY_ENGINE == 'ironruby')
     true
   else
     false
   end
 end
end

.java?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
# File 'lib/os.rb', line 81

def self.java?
  @java ||= begin
    if RUBY_PLATFORM =~ /java/
      true
    else
      false
    end
  end
end

.mac?Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
# File 'lib/os.rb', line 99

def self.mac?
  @mac = begin
    if RUBY_PLATFORM =~ /darwin/
      true
    else
      false
    end
  end      
end

.posix?Boolean

true for linux, os x, cygwin

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/os.rb', line 22

def self.posix?
  @posix ||=
  begin
    if OS.windows?
      begin
        begin
          # what if we're on interix...
          # untested, of course
          Process.wait fork{}
          true
        rescue NotImplementedError, NoMethodError
          false
        end
      end
    else
      # assume non windows is posix
      true
    end
  end

end

.rss_bytesObject

amount of memory the current process “is using”, in RAM (doesn’t include any swap memory that it may be using, just that in actual RAM) raises ‘unknown’ on jruby currently



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/os.rb', line 112

def self.rss_bytes
  # attempt to do this in a jruby friendly way
  if OS::Underlying.windows?
    # MRI, Java, IronRuby, Cygwin
    if OS.java?
      # no win32ole yet available...
      require 'java'
      mem_bean = java.lang.management.ManagementFactory.memory_mxbean
      mem_bean.heap_memory_usage.used + mem_bean.non_heap_memory_usage.used
    else
      wmi = nil
      begin
        require 'win32ole'
        wmi = WIN32OLE.connect("winmgmts://")
      rescue LoadError, NoMethodError # NoMethod for IronRuby currently [sigh]
        raise 'rss unknown for this platform'
      end        
      processes = wmi.ExecQuery("select * from win32_process where ProcessId = #{Process.pid}")
      memory_used = nil
      # only allow for one...
      for process in processes; raise if memory_used; memory_used = process.WorkingSetSize.to_i; end
      memory_used
    end
  elsif OS.posix? # assume linux I guess...
    kb = `ps -o rss= -p #{Process.pid}`.to_i # in kilobytes
  else
    raise 'unknown rss for this platform'
  end
end

.ruby_binObject



91
92
93
94
95
96
97
# File 'lib/os.rb', line 91

def self.ruby_bin
  @ruby_exe ||= begin
    require 'rbconfig'
    config = RbConfig::CONFIG
    File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT']
  end
end

.windows?Boolean Also known as: doze?

true if on windows [and/or jruby] false if on linux or cygwin

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/os.rb', line 8

def self.windows?
  @windows ||= begin
    if RUBY_PLATFORM =~ /cygwin/ # i386-cygwin
      false
    elsif ENV['OS'] == 'Windows_NT'
      true
    else
      false
    end
  end

end