SUMMARY: Can I determine the number of cpus via system call?

From: Dr. David Kirkby (drkirkby@ntlworld.com)
Date: Wed Jun 04 2003 - 05:11:34 EDT


Thanks to everyone (too many to mention you all) who answered my
question about how to find the number of active cpus in a system via a
system call from C.

I obviously did not make myself too clear, as 95% of the responses,
gave me commands to use at the command line - psrinfo, psradmin which
I knew of, hwmgr which I did not.

However, some people did suggest the use of 'getsysinfo' which was
what I was looking for. Ralf Anoil said:

> "getsysinfo" will do the job. There are several 'ops' (first argument
> to 'getsysinfo') to retrieve different characteristics of the system.
> See the manual page. Below is a program to determine the numbers of cpus
> in the machine. 'GSI_CPU_STATE' will give you the active cpus.
> Note that there are differences between Tru64 V4 und V5 regarding the
> returned structures.

Rald even gave me a program to determine the number of cpus in a
system - see below, although I will determine the state of the cpus
using GSI_CPU_STATE, rather than just the total number, since I'm
testing some multi-threaded code and want to see how well it performs
as the number of cpus is increased. The only problem is my Dec Alpha
can only take one CPU!! However, I do have other machines that have 4
cpus at my disposal and wanted to write the code so that it was
portable.

Dr. Thomas Blinn has pointed out that getsysinfo() is unique to Tru64
UNIX.

Ferens D.M. said:
> On GS160, GS320 You can use sysman for viewing, powering on and off
> cpus. With sysman You also can directly off and on power on cpu.

Unfortunately, I don't have this sort of hardware to play with.

There seems to be a problem getting this information in a portable way
(which is what I ideally wanted). Linux appears to have no sensible
way of finding the information that works across different versions of
Linux, Solaris has processor_info(), Tru64 has getsysinfo(). I don't
know about other commercial UNIX variants (AIX, IRIX, HP-UX etc.).

Thanks everyone. Here's Ralf's code.

#include <stdio.h>
#include <sys/sysinfo.h>
#include <machine/hal_sysinfo.h>

int main( int argc, char *argv[] )
{
  int start = 0;
  int buffer;

  getsysinfo( GSI_CPUS_IN_BOX, (char *)&buffer, 4, 0, 0);

  printf("cpus = %d\n",buffer);
  return 0;
}

-- 
Dr. David Kirkby,
Senior Research Fellow,
Department of Medical Physics,
University College London,
11-20 Capper St, London, WC1E 6JA.
Tel: 020 7679 6408 Fax: 020 7679 6269
Internal telephone: ext 46408
e-mail davek@medphys.ucl.ac.uk


This archive was generated by hypermail 2.1.7 : Sat Apr 12 2008 - 10:49:20 EDT