[Toybox] Working on nproc-do we check OMP_NUM_THREADS ?

Isaac Dunham ibid.ag at gmail.com
Sat Nov 29 17:52:29 PST 2014


Hello,
I noticed that nproc was on the TODO list now and started writing it.
I've gotten it *almost* ready, but there's one difference between current 
behavior and GNU nproc: 

GNU nproc without --all will treat OMP_NUM_THREADS as the number of CPUs,
interpreting 0x as hex and all other numbers as decimal.
The number of cpus to ignore is then subtracted, and the result printed
in decimal.

Currently, I'm ignoring OMP_NUM_THREADS, mainly because I haven't written
the code yet. If we should check it, I'd be happy to write the code.
Current code is attached.

OMP_NUM_THREADS is an environment variable that OpenMP uses to check 
how many threads to start; it overrides a check for the number of CPUs
in OpenMP-based software.

Thanks,
Isaac Dunham

-------------- next part --------------
/* nproc.c - get number of cpus
 *
 * Written by Isaac Dunham, 2014 A.D.
 *
 * No standard.
 * Based on the behavior of GNU nproc, except it does not look at 
 * $OMP_NUM_THREADS
 * GNU nproc without --all will use OMP_NUM_THREADS as the source for
 * the number of CPUs; 0x... is interpreted as hex but 010 is decimal.

USE_NPROC(NEWTOY(nproc, ">0(all)(ignore)#<0", TOYFLAG_BIN))

config NPROC
  bool "nproc"
  default n
  help
    usage: nproc [--all] [--ignore NUMCPUS]

    Show the number of cpus available.
    --all Show total number of cpus 
    --ignore Treat NUMCPUS as offline 
*/

#define FOR_nproc
#include "toys.h"

GLOBALS(
  long offline;
)

void nproc_main(void)
{
  long sysret = sysconf((toys.optflags&FLAG_all) ? _SC_NPROCESSORS_CONF :
                         _SC_NPROCESSORS_ONLN) - TT.offline;
  if (sysret < 1) sysret = 1;
  xprintf("%ld\n", sysret);
}


More information about the Toybox mailing list