Subject: system call for finding the number of cpus??

Hi!
I have a script that is using the /cpu/procinfo file to determine the number of cpus present in the system. But I would like to implement it using a system call rather than use the environment variables?? I couldn't find a system call for linux that would give me the result. Could anyone please let me know if there is one for redhat linux??

Thanks for your time,
Priya


2002-04-08 21:25:05

by Robert Love

[permalink] [raw]
Subject: Re: system call for finding the number of cpus??

On Mon, 2002-04-08 at 17:18, Kuppuswamy, Priyadarshini wrote:

> I have a script that is using the /cpu/procinfo file to determine the
> number of cpus present in the system. But I would like to implement it
> using a system call rather than use the environment variables?? I couldn't
> find a system call for linux that would give me the result. Could anyone
> please let me know if there is one for redhat linux??

Linux does not implement such a syscall. Note

cat /proc/cpuinfo | grep processor | wc -l

works and is simple; you do not have to do it via script - execute it in
your C program, save the one-line output, and atoi() it.

Robert Love

2002-04-08 21:27:48

by Christoph Hellwig

[permalink] [raw]
Subject: Re: system call for finding the number of cpus??

On Mon, Apr 08, 2002 at 05:25:08PM -0400, Robert Love wrote:
> Linux does not implement such a syscall. Note
>
> cat /proc/cpuinfo | grep processor | wc -l
>
> works and is simple; you do not have to do it via script - execute it in
> your C program, save the one-line output, and atoi() it.

I guess there is at least one architecture on which it breaks..
See http://people.nl.linux.org/~hch/cpuinfo/ for details.

2002-04-08 21:33:41

by Mark Hahn

[permalink] [raw]
Subject: Re: system call for finding the number of cpus??

> See http://people.nl.linux.org/~hch/cpuinfo/ for details.

egads. "grep -ci bogo /proc/cpuinfo" then.

2002-04-08 21:51:52

by Davide Libenzi

[permalink] [raw]
Subject: Re: system call for finding the number of cpus??

On Mon, 8 Apr 2002, Kuppuswamy, Priyadarshini wrote:

> Hi!
> I have a script that is using the /cpu/procinfo file to determine the
> number of cpus present in the system. But I would like to implement it
> using a system call rather than use the environment variables?? I
> couldn't find a system call for linux that would give me the result.
> Could anyone please let me know if there is one for redhat linux??

sysconf(_SC_NPROCESSORS_CONF);




- Davide


2002-04-08 21:52:13

by J.A. Magallon

[permalink] [raw]
Subject: Re: system call for finding the number of cpus??


On 2002.04.08 Robert Love wrote:
>On Mon, 2002-04-08 at 17:18, Kuppuswamy, Priyadarshini wrote:
>
>> I have a script that is using the /cpu/procinfo file to determine the
>> number of cpus present in the system. But I would like to implement it
>> using a system call rather than use the environment variables?? I couldn't
>> find a system call for linux that would give me the result. Could anyone
>> please let me know if there is one for redhat linux??
>
>Linux does not implement such a syscall. Note
>

How about this:

#include <sys/sysinfo.h>
...
//nproc = get_nprocs(); // Available processors
nproc = get_nprocs_conf(); // Configured processors (some can be down...)

(glibc 2.2.5, but i think it keeps working since time ago).

BTW, why linux does not implement sysconf(_SC_NPROC_[CONF,ONLN]) ??

TIA

--
J.A. Magallon # Let the source be with you...
mailto:[email protected]
Mandrake Linux release 8.3 (Cooker) for i586
Linux werewolf 2.4.19-pre6-jam1 #1 SMP Sun Apr 7 00:50:05 CEST 2002 i686

Subject: RE: system call for finding the number of cpus??

I don't think that (sysconf(_SC_NPROCESSORS_CONF)) command works on linux. It works on Unix. I tried that. It returns 1 when there are 4 processors on linux.


-----Original Message-----
From: Davide Libenzi [mailto:[email protected]]
Sent: Monday, April 08, 2002 5:57 PM
To: Kuppuswamy, Priyadarshini
Cc: [email protected]
Subject: Re: system call for finding the number of cpus??


On Mon, 8 Apr 2002, Kuppuswamy, Priyadarshini wrote:

> Hi!
> I have a script that is using the /cpu/procinfo file to determine the
> number of cpus present in the system. But I would like to implement it
> using a system call rather than use the environment variables?? I
> couldn't find a system call for linux that would give me the result.
> Could anyone please let me know if there is one for redhat linux??

sysconf(_SC_NPROCESSORS_CONF);




- Davide


2002-04-08 21:58:22

by J.A. Magallon

[permalink] [raw]
Subject: Re: system call for finding the number of cpus??


On 2002.04.08 Davide Libenzi wrote:
>On Mon, 8 Apr 2002, Kuppuswamy, Priyadarshini wrote:
>
>> Hi!
>> I have a script that is using the /cpu/procinfo file to determine the
>> number of cpus present in the system. But I would like to implement it
>> using a system call rather than use the environment variables?? I
>> couldn't find a system call for linux that would give me the result.
>> Could anyone please let me know if there is one for redhat linux??
>
>sysconf(_SC_NPROCESSORS_CONF);
>

I din't really trusted you, so digged inside includes till bits/confname.h
Why the h*ll the manpage about sysconf does not talk about that ?????



--
J.A. Magallon # Let the source be with you...
mailto:[email protected]
Mandrake Linux release 8.3 (Cooker) for i586
Linux werewolf 2.4.19-pre6-jam1 #1 SMP Sun Apr 7 00:50:05 CEST 2002 i686

2002-04-08 21:59:10

by Davide Libenzi

[permalink] [raw]
Subject: RE: system call for finding the number of cpus??

On Mon, 8 Apr 2002, Kuppuswamy, Priyadarshini wrote:

> I don't think that (sysconf(_SC_NPROCESSORS_CONF)) command works on
> linux. It works on Unix. I tried that. It returns 1 when there are 4
> processors on linux.

it always worked fine for me, up to 8 CPUs ...




- Davide


2002-04-08 22:01:10

by H. Peter Anvin

[permalink] [raw]
Subject: Re: system call for finding the number of cpus??

Followup to: <[email protected]>
By author: Christoph Hellwig <[email protected]>
In newsgroup: linux.dev.kernel
>
> On Mon, Apr 08, 2002 at 05:25:08PM -0400, Robert Love wrote:
> > Linux does not implement such a syscall. Note
> >
> > cat /proc/cpuinfo | grep processor | wc -l
> >
> > works and is simple; you do not have to do it via script - execute it in
> > your C program, save the one-line output, and atoi() it.
>
> I guess there is at least one architecture on which it breaks..
> See http://people.nl.linux.org/~hch/cpuinfo/ for details.
>

Then that architecture should be fixed.

-hpa
--
<[email protected]> at work, <[email protected]> in private!
"Unix gives you enough rope to shoot yourself in the foot."
http://www.zytor.com/~hpa/puzzle.txt <[email protected]>

2002-04-08 22:04:21

by Dr. David Alan Gilbert

[permalink] [raw]
Subject: Re: system call for finding the number of cpus??

* Mark Hahn ([email protected]) wrote:
> > See http://people.nl.linux.org/~hch/cpuinfo/ for details.
>
> egads. "grep -ci bogo /proc/cpuinfo" then.

Hmm, cpuinfo is a very human readable file, I wouldn't use it to do
things like that.

I suggest /proc/stat whose format appears to be designed for machine
reading and which has a 'cpu0' and 'cpu1' line on this here dual
processor box; and from 2.4.x on it seems to have a cpu0 even on
uniprocessor; so I suggest:

grep "^cpu[0-9][0-9]* " /proc/stat

But I'd actually go and ask the CPU hotswap guys - they must have a way
of getting a handle on this (hey does that mean you might have cpu0,
cpu1, cpu3 .... ?)

Me thinks the format of these /proc files needs documenting - is there
anything already?

Dave

---------------- Have a happy GNU millennium! ----------------------
/ Dr. David Alan Gilbert | Running GNU/Linux on Alpha,68K| Happy \
\ gro.gilbert @ treblig.org | MIPS,x86,ARM, SPARC and HP-PA | In Hex /
\ _________________________|_____ http://www.treblig.org |_______/

2002-04-08 22:03:40

by Davide Libenzi

[permalink] [raw]
Subject: Re: system call for finding the number of cpus??

On Mon, 8 Apr 2002, J.A. Magallon wrote:

>
> On 2002.04.08 Davide Libenzi wrote:
> >On Mon, 8 Apr 2002, Kuppuswamy, Priyadarshini wrote:
> >
> >> Hi!
> >> I have a script that is using the /cpu/procinfo file to determine the
> >> number of cpus present in the system. But I would like to implement it
> >> using a system call rather than use the environment variables?? I
> >> couldn't find a system call for linux that would give me the result.
> >> Could anyone please let me know if there is one for redhat linux??
> >
> >sysconf(_SC_NPROCESSORS_CONF);
> >
>
> I din't really trusted you, so digged inside includes till bits/confname.h
> Why the h*ll the manpage about sysconf does not talk about that ?????

.h files usually changes ofter than man pages mainly because developers
like to code not to document



- Davide


2002-04-08 22:12:31

by J.A. Magallon

[permalink] [raw]
Subject: Re: system call for finding the number of cpus??


On 2002.04.08 "Kuppuswamy, Priyadarshini" wrote:
>I don't think that (sysconf(_SC_NPROCESSORS_CONF)) command works on linux. It works on Unix. I tried that. It returns 1 when there are 4 processors on linux.
>

Tried and works. get_nproc_conf and _SC_NPROCESSORS_CONF work the same.

--
J.A. Magallon # Let the source be with you...
mailto:[email protected]
Mandrake Linux release 8.3 (Cooker) for i586
Linux werewolf 2.4.19-pre6-jam1 #1 SMP Sun Apr 7 00:50:05 CEST 2002 i686

2002-04-08 22:56:18

by David Ford

[permalink] [raw]
Subject: Re: system call for finding the number of cpus??

# grep -c "^processor" /proc/cpuinfo
2

-d

Robert Love wrote:

>
>Linux does not implement such a syscall. Note
>
> cat /proc/cpuinfo | grep processor | wc -l
>
>works and is simple; you do not have to do it via script - execute it in
>your C program, save the one-line output, and atoi() it.
>
> Robert Love
>


2002-04-09 01:12:54

by Nicholas Miell

[permalink] [raw]
Subject: Re: system call for finding the number of cpus??

On Mon, 2002-04-08 at 14:58, J.A. Magallon wrote:
> >sysconf(_SC_NPROCESSORS_CONF);
> >
>
> I din't really trusted you, so digged inside includes till bits/confname.h
> Why the h*ll the manpage about sysconf does not talk about that ?????
>

For some odd reason, the FSF doesn't like man pages. If you read the
info documentation for glibc, you'll find the _SC_NPROCESSORS_CONF
property listed in the "libc : System Configuration : Sysconf :
Constants for Sysconf" page. The man pages tend to lag behind the actual
info documentation, because they are maintained seperately.

2002-04-09 05:36:48

by Bernd Eckenfels

[permalink] [raw]
Subject: Re: system call for finding the number of cpus??

In article <[email protected]> you wrote:
> On 2002.04.08 "Kuppuswamy, Priyadarshini" wrote:
>>I don't think that (sysconf(_SC_NPROCESSORS_CONF)) command works on linux. It works on Unix. I tried that. It returns 1 when there are 4 processors on linux.
>>

> Tried and works. get_nproc_conf and _SC_NPROCESSORS_CONF work the same.

Using "strace getconf _NPROCESSORS_CONF" it tells me, that glibc is also
parsing /proc/cpuinfo.

BTW: there are _NPROCESSORS_CONF and _NPROCESSORS_ONLN, works for me:

3ecki@calista:~> getconf _NPROCESSORS_CONF
1
3ecki@calista:~> getconf _NPROCESSORS_ONLN
1
ecki@SeeDeBrCVS:~$ getconf _NPROCESSORS_CONF
2
ecki@SeeDeBrCVS:~$ getconf _NPROCESSORS_ONLN
2

Greetings
Bernd

2002-04-09 14:28:29

by Vince Weaver

[permalink] [raw]
Subject: Re: system call for finding the number of cpus??


On 8 Apr 2002, H. Peter Anvin wrote:
> By author: Christoph Hellwig <[email protected]>
> > On Mon, Apr 08, 2002 at 05:25:08PM -0400, Robert Love wrote:
> > > Linux does not implement such a syscall. Note
> > >
> > > cat /proc/cpuinfo | grep processor | wc -l
> >
> > I guess there is at least one architecture on which it breaks..
> >
>
> Then that architecture should be fixed.

Back in 1999 or so I actually came up with a patch that more-or-less came
up with an arch-independent /proc/cpuinfo file. It touched off a
mini-flamewar on linux-kernel, none of the arch maintainers wanted to
change their pet format, and Linus silently dropped the patch.

As maintainer of linux_logo I have to have a separate cpuinfo parser for
each architecture.

For number of CPU's..... x86 uses the "count processor/bogomips" type
thing. Some architectures actually have an "active cpus:" type field.
Some have a "processors total:" type field. And often the method that
works changes from kernel to kernel, sometimes even within stable
releases.

But no, counting processors or bogomips doesn't work across the board.

Should this be rectified somehow? I think it would take a proclomation
from on high.

What to do in the meantime? Well the sysconf() method sounds promising.
If not you can dig out the sysinfo library included with linux_logo which
does a reasonable job with most of the common architectures.

Vince

--
____________
\ /\ /\ / Vince Weaver Linux 2.4.9 on a K6-2+, Up 45 days
\/__\/__\/ [email protected] http://www.deater.net/weave


2002-04-09 15:38:09

by Tigran Aivazian

[permalink] [raw]
Subject: Re: system call for finding the number of cpus??

Hello Priya,

The portable way to determine the number of cpus _online_ is:

if [ -x /usr/bin/getconf ] ; then
numprocs=$(/usr/bin/getconf _NPROCESSORS_ONLN)
if [ $numprocs -eq 0 ]; then
numprocs=1
fi
else
numprocs=1
fi

at least that is the number I usually pass to "make -j$numprocs" when
compiling large software projects.

Regards,
Tigran

On Mon, 8 Apr 2002, Kuppuswamy, Priyadarshini wrote:

> Hi!
> I have a script that is using the /cpu/procinfo file to determine the number of cpus present in the system. But I would like to implement it using a system call rather than use the environment variables?? I couldn't find a system call for linux that would give me the result. Could anyone please let me know if there is one for redhat linux??
>
> Thanks for your time,
> Priya
> -
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
>

2002-04-09 15:40:07

by Tigran Aivazian

[permalink] [raw]
Subject: RE: system call for finding the number of cpus??

On Mon, 8 Apr 2002, Kuppuswamy, Priyadarshini wrote:

> I don't think that (sysconf(_SC_NPROCESSORS_CONF)) command works on linux. It works on Unix. I tried that. It returns 1 when there are 4 processors on linux.

it works:

# /usr/bin/getconf _NPROCESSORS_ONLN
2

If it didn't work then glibc (and other rpms that take advantage of it)
would have taken a lot longer to compile :)


2002-04-11 13:20:30

by Rusty Russell

[permalink] [raw]
Subject: Re: system call for finding the number of cpus??

On Mon, 8 Apr 2002 23:02:39 +0100
"Dr. David Alan Gilbert" <[email protected]> wrote:

> But I'd actually go and ask the CPU hotswap guys - they must have a way
> of getting a handle on this (hey does that mean you might have cpu0,
> cpu1, cpu3 .... ?)

We use /proc/sys/cpu/*.

Rusty.
--
there are those who do and those who hang on and you don't see too
many doers quoting their contemporaries. -- Larry McVoy