How to know the OS architecture (32 or 64 bits)?

Hi, is there a reliable way under Ruby to know the OS architecture (32
or 64
bits)?

I’ve just found RUBY_PLATFORM constant which returns “x86_64-linux”
under 64
bits, however it doesn’t send very reliable for me.

I need a way working under Linux and BSD. Thanks for any suggestion.

On Jan 14, 2010, at 6:12 PM, Iñaki Baz C. wrote:


Iñaki Baz C. [email protected]

You can use Fixnum#size to get number of bytes for a Fixnum and
multiply by 8 bits/byte:

irb(main):001:0> 1.size * 8
=> 64
irb(main):002:0> RUBY_PLATFORM
=> “x86_64-linux”

irb> 1.size * 8
=> 32
irb> RUBY_PLATFORM
=> “universal-darwin9.0”

I think it’s reliable. (Although I guess there could be a 32-bit ruby
running on a 64-bit platform.)

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

On 2010-01-14, Iñaki Baz C. [email protected] wrote:

Hi, is there a reliable way under Ruby to know the OS architecture (32 or 64
bits)?

Probably not.

Could you explain what you’re trying to do? Without knowing why you
think
you need to know this, it’s hard to give you good advice. I couldn’t
tell
you off the top of my head whether the machine I’m working on right now
is
32-bit or 64-bit. I’ve been doing software development on it for two
years
and I’ve never needed to know.

-s

El Viernes, 15 de Enero de 2010, Seebs
escribió:> and I’ve never needed to know.
The application I’m developing uses Posix Queue Messages thanks to
posix_mq
gem:
posix_mq - POSIX message queues for Ruby

When the app runs it tries to create a posix mqueue with maxmsg=5000 and
msgsize=1024. The user running the application could have not
permissions to
create such posix mqueue due to system limits (“ulimit -q”).

In that case the creation of the posix mqueue raises a Errno::ENOMEM
and I
want to tell the user the exact amount of bytes required.

The algorimth to know such amount of required bytes is:

queue.attr.mq_maxmsg * sizeof(struct msg_msg *) +
queue.attr.mq_maxmsg * queue.attr.mq_msgsize

In 32 bits sizeof(struct msg_msg *) is 4 bytes while in 64 it’s 8 bytes,
so
the total ammount of bytes changes. This means that “ulimit -q” must be
different depending on the system architecture (32/64 bits).

El Viernes, 15 de Enero de 2010, Rob B.
escribió:>

irb> RUBY_PLATFORM
=> “universal-darwin9.0”

It’s really good!
Thanks a lot.

On 1/15/2010 9:36 AM, Walton H. wrote:

=> true
irb(main):005:0>

Hmm… it does not appear to deal with 32-bit ruby running on a 64 bit
system though.
On my Windows 7 x64 (with 32-bit ruby):
irb(main):005:0> OS.bits
=> 32
irb(main):006:0> 1.size
=> 4
irb(main):007:0>

On 1/14/2010 4:12 PM, Iñaki Baz C. wrote:

Hi, is there a reliable way under Ruby to know the OS architecture (32 or 64
bits)?

I’ve just found RUBY_PLATFORM constant which returns “x86_64-linux” under 64
bits, however it doesn’t send very reliable for me.

I need a way working under Linux and BSD. Thanks for any suggestion.

I can’t vouch for how accurate it is, but an OS gem was recently
announced on this list.
gem install os

irb(main):001:0> require ‘os’
=> true
irb(main):002:0> OS.bits
=> 64
irb(main):004:0> OS.posix?
=> true
irb(main):005:0>

On Sat, 16 Jan 2010, Walton H. wrote:

I need a way working under Linux and BSD. Thanks for any suggestion.
irb(main):004:0> OS.posix?
=> 4
irb(main):007:0>

Correct me if I’m wrong, but don’t 32bit apps running in a 64bit
architecture run in a special space which mimics 32 bits? If that’s the
case, then I’d think the behaviour was as expected.

Matt

El Viernes, 15 de Enero de 2010, Walton H. escribió:

I need a way working under Linux and BSD. Thanks for any suggestion.
=> true
irb(main):005:0>

Hmm… it does not appear to deal with 32-bit ruby running on a 64 bit
system though.
On my Windows 7 x64 (with 32-bit ruby):
irb(main):005:0> OS.bits
=> 32
irb(main):006:0> 1.size
=> 4
irb(main):007:0>

Interesting, I’ll take a look to its implementation. However in my case
I will
never run 32 bits Ruby over a 64 bits SO.

El Viernes, 15 de Enero de 2010, Walton H. escribió:

I need a way working under Linux and BSD. Thanks for any suggestion.
=> true
irb(main):005:0>

Hmm… it does not appear to deal with 32-bit ruby running on a 64 bit
system though.
On my Windows 7 x64 (with 32-bit ruby):
irb(main):005:0> OS.bits
=> 32
irb(main):006:0> 1.size
=> 4
irb(main):007:0>

Note that to know the bits it uses “rbconfig” gem, and them:

def self.bits
@bits ||= begin
require ‘rbconfig’
host_os = RbConfig::CONFIG[‘host_os’]
if host_os =~ /32/
32
else
if host_os =~ /64/
64
else # cygwin…
if (1<<32).class == Fixnum
64
else
32
end
end
end
end
end

In my server RbConfig::CONFIG[‘host_os’] = “linux-gnu” so finally it
ends
doing:

      if (1<<32).class == Fixnum
        64
      else
        32
      end

Which is basically the same as doing

if 1.size == 8
64
else
32
end

On 2010-01-15, Iñaki Baz C. [email protected] wrote:

When the app runs it tries to create a posix mqueue with maxmsg=5000 and
msgsize=1024. The user running the application could have not permissions to
create such posix mqueue due to system limits (“ulimit -q”).

Ahh.

The algorimth to know such amount of required bytes is:

queue.attr.mq_maxmsg * sizeof(struct msg_msg *) +
queue.attr.mq_maxmsg * queue.attr.mq_msgsize

In 32 bits sizeof(struct msg_msg *) is 4 bytes while in 64 it’s 8 bytes, so
the total ammount of bytes changes. This means that “ulimit -q” must be
different depending on the system architecture (32/64 bits).

Ahh, yes. Although to be picky, you’re now into one of the areas where,
if
you ever end up on stranger hardware, the answer may be ill-defined; it
appears you care specifically about pointer sizes. There are some
systems
where “pointer size” is not the same as “integer size”, and so on… So
you could get some surprises.

I suspect Ruby tries to get a 64-bit fixnum if it can, so you’re
probably
set.

-s

El Viernes, 15 de Enero de 2010, Seebs
escribió:

you ever end up on stranger hardware, the answer may be ill-defined; it
appears you care specifically about pointer sizes. There are some systems
where “pointer size” is not the same as “integer size”, and so on… So
you could get some surprises.

Yes, for now I use 1.size to determine if I’m under 32 or 64 bits but
it’s
just a hack. Most probably I will code a small C extension method with
returns
the exact value of sizeof(struct msg_msg *). Then the calculated valued
would
be exact for any kinf of strange architecture (I hope).

Regards.

El Viernes, 15 de Enero de 2010, Iñaki Baz C. escribió:

Note that to know the bits it uses “rbconfig” gem, and them:

Well, “rbconfig” is not a gem but a Ruby built in library.

if 1.size == 8
64
else
32
end

Definitively I don’t like “os” gem at all. It could use
RbConfig::CONFIG[‘host_cpu’] rather than the not reliable
RbConfig::CONFIG[‘host_os’]:

a) 32 bits host:

RbConfig::CONFIG[‘host_os’] => “linux-gnu”
RbConfig::CONFIG[‘host_cpu’] => “i486”

b) 64 bits host:

RbConfig::CONFIG[‘host_os’] => “linux-gnu”
RbConfig::CONFIG[‘host_cpu’] => “x86_64”

On 2010-01-15, Iñaki Baz C. [email protected] wrote:

Most probably I will code a small C extension method with returns
the exact value of sizeof(struct msg_msg *). Then the calculated valued would
be exact for any kinf of strange architecture (I hope).

That seems like it should work.

-s

On 15.01.2010 20:56, Walton H. wrote:

My main concern with that though:
Would RbConfig::CONFIG[‘host_cpu’] return “x86_64” if I’m running 32-bit
Linux on a 64-bit CPU?

No. If the OS isn’t 64 bits itself, it’ll run on an x86_64 architecture,
but the CPU is in 32 bit mode. The only thing you can really test, is
the bit-ness of the OS, not the CPU.

After all, how should a 32 bit OS deal with a 64 bit memory address?

On Jan 15, 1:40 pm, Walton H. [email protected] wrote:

under 64
irb(main):002:0> OS.bits
irb(main):006:0> 1.size
=> 4
irb(main):007:0>

No matter how many bits the OS has, as long the compiled interpreter
is 32 bits, the returned values is going to be 32 bits.

Windows can run 32bits applications along with 64bits ones, but that
doesn’t mean you can access 64bits address space or tools from 32bits
applications.

On 1/15/2010 9:59 AM, Iñaki Baz C. wrote:

In my server RbConfig::CONFIG[‘host_os’] = “linux-gnu” so finally it ends
if 1.size == 8

submit a bug! GitHub - rdp/os: The OS gem allows for some easy telling if you’re on windows or not. OS.windows? as well as some other helper utilities

My main concern with that though:
Would RbConfig::CONFIG[‘host_cpu’] return “x86_64” if I’m running 32-bit
Linux on a 64-bit CPU?

On 1/15/2010 1:10 PM, Luis L. wrote:

(32 or 64

=> true

Ah! But this tool doesn’t claim to tell me about my address space or
runtime enviornment, but rather my OS.

Just because I’m in a 32-bit app, doesn’t mean I may not care that I’m
on a 64-bit os.

On 1/15/2010 1:07 PM, Phillip G. wrote:

After all, how should a 32 bit OS deal with a 64 bit memory address?


Phillip G.

Makes sense, but I don’t know enough about low-level OS/hardware to be
sure that there isn’t a CPU flag exposed that would still allow for an
app or the OS to determine the bittedness of the CPU. So I asked and
was answered :slight_smile:

My main concern with that though:
Would RbConfig::CONFIG[‘host_cpu’] return “x86_64” if I’m running 32-bit
Linux on a 64-bit CPU?

At least within a 32-bit OS (in a VM) it appears to be i686-linux so I
think we’re safe there.

Also, thanks for the hint on 1.size I didn’t know that one–it’s
integrated now [v 0.6.1]. That wouldn’t work for jruby (which always
returns 8), but should work fine for MRI, and I think we handle jruby
ok.

I also added a .mac? method–if anybody on a mac could try it out
[and/or tell me what the RUBY_PLATFORM is for OS X and OS X 64 bit] then
I could actually test it.

Re: OS.bits on a 32 within a 64…anybody know how you can tell that
you’re on 64 bit running a 32 bit ruby, on windows [or linux]?

Thanks.
-r