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

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

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

I've just found RUBY_PLATFORM constant which returns "x86_64-linux" under 6=
4=20
bits, however it doesn't send very reliable for me.

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

=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
R

Rob Biedenharn

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

I've just found RUBY_PLATFORM constant which returns "x86_64-linux" =20=
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.

--=20
I=F1aki Baz Castillo <[email protected]>


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

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

irb> 1.size * 8
=3D> 32
irb> RUBY_PLATFORM
=3D> "universal-darwin9.0"

I think it's reliable. (Although I guess there could be a 32-bit ruby =20=

running on a 64-bit platform.)

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
S

Seebs

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
 
I

Iñaki Baz Castillo

El Viernes, 15 de Enero de 2010, Seebs escribi=F3:
=20
Probably not.
=20
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 yea= rs
and I've never needed to know.

The application I'm developing uses Posix Queue Messages thanks to posix_mq=
=20
gem:
http://bogomips.org/ruby_posix_mq/README.html

When the app runs it tries to create a posix mqueue with maxmsg=3D5000 and=
=20
msgsize=3D1024. The user running the application could have not permissions=
to=20
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=
=20
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=
=20
the total ammount of bytes changes. This means that "ulimit -q" must be=20
different depending on the system architecture (32/64 bits).


=2D-=20
I=F1aki Baz Castillo <[email protected]>
 
I

Iñaki Baz Castillo

El Viernes, 15 de Enero de 2010, Rob Biedenharn escribi=F3:
=20
You can use Fixnum#size to get number of bytes for a Fixnum and
multiply by 8 bits/byte:
=20
irb(main):001:0> 1.size * 8
=3D> 64
irb(main):002:0> RUBY_PLATFORM
=3D> "x86_64-linux"
=20
irb> 1.size * 8
=3D> 32
irb> RUBY_PLATFORM
=3D> "universal-darwin9.0"

It's really good!
Thanks a lot.


=2D-=20
I=F1aki Baz Castillo <[email protected]>
 
W

Walton Hoops

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>
 
W

Walton Hoops

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>
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>
 
I

Iñaki Baz Castillo

El Viernes, 15 de Enero de 2010, Walton Hoops escribi=C3=B3:
=20
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
=3D> 32
irb(main):006:0> 1.size
=3D> 4
irb(main):007:0>

Interesting, I'll take a look to its implementation. However in my case I w=
ill=20
never run 32 bits Ruby over a 64 bits SO.=20


=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
I

Iñaki Baz Castillo

El Viernes, 15 de Enero de 2010, Walton Hoops escribi=C3=B3:
=20
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
=3D> 32
irb(main):006:0> 1.size
=3D> 4
irb(main):007:0>

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

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


In my server RbConfig::CONFIG['host_os'] =3D "linux-gnu" so finally it ends=
=20
doing:

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

Which is basically the same as doing

if 1.size =3D=3D 8
64
else
32
end



=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
I

Iñaki Baz Castillo

El Viernes, 15 de Enero de 2010, I=C3=B1aki Baz Castillo escribi=C3=B3:
Note that to know the bits it uses "rbconfig" gem, and them:

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


In my server RbConfig::CONFIG['host_os'] =3D "linux-gnu" so finally it en= ds
doing:
=20
if (1<<32).class =3D=3D Fixnum
64
else
32
end
=20
Which is basically the same as doing
=20
if 1.size =3D=3D 8
64
else
32
end
=20


Definitively I don't like "os" gem at all. It could use=20
RbConfig::CONFIG['host_cpu'] rather than the not reliable=20
RbConfig::CONFIG['host_os']:


a) 32 bits host:

RbConfig::CONFIG['host_os'] =3D> "linux-gnu"
RbConfig::CONFIG['host_cpu'] =3D> "i486"

b) 64 bits host:

RbConfig::CONFIG['host_os'] =3D> "linux-gnu"
RbConfig::CONFIG['host_cpu'] =3D> "x86_64"
=20

=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
S

Seebs

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
 
I

Iñaki Baz Castillo

El Viernes, 15 de Enero de 2010, Seebs escribi=F3:
=20
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.

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

Regards.

=2D-=20
I=F1aki Baz Castillo <[email protected]>
 
S

Seebs

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
 
W

Walton Hoops

El Viernes, 15 de Enero de 2010, Iñaki Baz Castillo 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.



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

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"
submit a bug! http://github.com/rdp/os

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?
 
L

Luis Lavena

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>

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.
 
P

Phillip Gawlowski

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?
 
P

Paul Mckibbin

Walton said:
In my server RbConfig::CONFIG['host_os'] = "linux-gnu" so finally it ends
if 1.size == 8
submit a bug! http://github.com/rdp/os

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?

Not on this machine
cat /proc/cpuinfo | grep lm

flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat
pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc
arch_perfmon pebs bts pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16
xtpr pdcm lahf_lm tpr_shadow
flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat
pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc
arch_perfmon pebs bts pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16
xtpr pdcm lahf_lm tpr_shadow

uname -a

Linux xxxxxxx 2.6.31-16-generic #53-Ubuntu SMP Tue Dec 8 04:01:29 UTC
2009 i686 GNU/Linux

ruby -e "require 'rbconfig'; p(Config::CONFIG['host_cpu'])"

"i486"


Mac
 
W

Walton Hoops

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.
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.
 
W

Walton Hoops

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?
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 :)
 
R

Roger Pack

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top