bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] core-count: A new program to count the number of cpu cores (was:


From: Giuseppe Scrivano
Subject: [PATCH] core-count: A new program to count the number of cpu cores (was: [PATCH] md5: accepts a new --threads option)
Date: Sun, 25 Oct 2009 19:35:02 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux)

Jim Meyering <address@hidden> writes:

>>> If nobody is already working on it, I can start doing it.
>>>
>>> What about the name?  "ncores" or "ncpus" are fine?
>>
>> Here are some longer candidates:
>>
>>   count-cores
>>   count-cpus
>>   cpu-count
>>   core-count
>
> Actually, "cpu" seems too ambiguous, so let's rule those out.
> That leaves:
>
>   ncores
>   count-cores
>   core-count
>
> Any others?
> Preferences?

I went for `core-count'.  This is the first version of the new program,
it is a simple wrapper around the gnulib nproc module, thanks to Bruno
to have done the real work.

Any comment?

Cheers,
Giuseppe



>From e3bff6a4dd2fe9560c7922e877ab57081a083c58 Mon Sep 17 00:00:00 2001
From: Giuseppe Scrivano <address@hidden>
Date: Sun, 25 Oct 2009 19:04:41 +0100
Subject: [PATCH] core-count: A new program to count the number of cpu cores

* AUTHORS: Add my name.
* NEWS: Mention it.
* bootstrap.conf (gnulib_modules): Add nproc.
* doc/coreutils.texi (core-count invocation): Add core-count info.
* src/Makefile.am (EXTRA_PROGRAMS): Add core-count.
* src/core_count.c: New file.
* tests/Makefile.am (TESTS): Add core-count/{cpuinfo, positive}.
* tests/core-count/cpuinfo: New file.
* tests/core-count/positive: New file.
---
 AUTHORS                   |    1 +
 NEWS                      |    4 ++
 bootstrap.conf            |    1 +
 doc/coreutils.texi        |   22 +++++++++++
 src/Makefile.am           |    3 ++
 src/core_count.c          |   89 +++++++++++++++++++++++++++++++++++++++++++++
 tests/Makefile.am         |    2 +
 tests/core-count/cpuinfo  |   34 +++++++++++++++++
 tests/core-count/positive |   31 ++++++++++++++++
 9 files changed, 187 insertions(+), 0 deletions(-)
 create mode 100644 src/core_count.c
 create mode 100755 tests/core-count/cpuinfo
 create mode 100755 tests/core-count/positive

diff --git a/AUTHORS b/AUTHORS
index 7095db0..0b09f6d 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -12,6 +12,7 @@ chown: David MacKenzie, Jim Meyering
 chroot: Roland McGrath
 cksum: Q. Frank Xia
 comm: Richard M. Stallman, David MacKenzie
+core-count: Giuseppe Scrivano
 cp: Torbjörn Granlund, David MacKenzie, Jim Meyering
 csplit: Stuart Kemp, David MacKenzie
 cut: David M. Ihnat, David MacKenzie, Jim Meyering
diff --git a/NEWS b/NEWS
index 1ed577f..017cdec 100644
--- a/NEWS
+++ b/NEWS
@@ -32,6 +32,10 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   with the invoked command failing with status 1.  Likewise, nohup
   fails with status 125 instead of 127.
 
+** New programs
+
+  core-count: A new program to get the number of cpu cores.
+
 ** New features
 
   md5sum --check now also accepts openssl-style checksums.
diff --git a/bootstrap.conf b/bootstrap.conf
index 1857489..5a5d76d 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -157,6 +157,7 @@ gnulib_modules="
   modechange
   mountlist
   mpsort
+  nproc
   obstack
   pathmax
   perl
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index c54ffb8..5a8167a 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -46,6 +46,7 @@
 * chroot: (coreutils)chroot invocation.         Specify the root directory.
 * cksum: (coreutils)cksum invocation.           Print POSIX CRC checksum.
 * comm: (coreutils)comm invocation.             Compare sorted files by line.
+* core-count: (coreutils)core-count invocation. Print the number of cpu cores.
 * cp: (coreutils)cp invocation.                 Copy files.
 * csplit: (coreutils)csplit invocation.         Split by context.
 * cut: (coreutils)cut invocation.               Print selected parts of lines.
@@ -408,6 +409,7 @@ User information
 System context
 
 * arch invocation::              Print machine hardware name
+* core-count invocation::        Print the number of cpu cores
 * date invocation::              Print or set system date and time
 * uname invocation::             Print system information
 * hostname invocation::          Print or set system name
@@ -13221,6 +13223,7 @@ information.
 @menu
 * date invocation::             Print or set system date and time.
 * arch invocation::             Print machine hardware name.
+* core-count invocation::        Print the number of cpu cores
 * uname invocation::            Print system information.
 * hostname invocation::         Print or set system name.
 * hostid invocation::           Print numeric host identifier.
@@ -13880,6 +13883,25 @@ The program accepts the @ref{Common options} only.
 @exitstatus
 
 
address@hidden core-count invocation
address@hidden @command{core-count}: Print the number of cpu cores
+
address@hidden core-count
address@hidden Print the number of cpu cores
address@hidden system information, printing
+
address@hidden prints the number of cpu cores.
+Synopsis:
+
address@hidden
+core-count address@hidden
address@hidden example
+
+The program accepts the @ref{Common options} only.
+
address@hidden
+
+
 @node uname invocation
 @section @command{uname}: Print system information
 
diff --git a/src/Makefile.am b/src/Makefile.am
index 67c29cc..8f722a1 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -52,6 +52,7 @@ EXTRA_PROGRAMS = \
   chown                \
   cksum                \
   comm         \
+  core-count   \
   cp           \
   csplit       \
   cut          \
@@ -189,6 +190,7 @@ chown_LDADD = $(LDADD)
 chroot_LDADD = $(LDADD)
 cksum_LDADD = $(LDADD)
 comm_LDADD = $(LDADD)
+core_count_LDADD = $(LDADD)
 cp_LDADD = $(LDADD)
 csplit_LDADD = $(LDADD)
 cut_LDADD = $(LDADD)
@@ -480,6 +482,7 @@ rmdir_SOURCES = rmdir.c prog-fprintf.c
 
 uname_SOURCES = uname.c uname-uname.c
 arch_SOURCES = uname.c uname-arch.c
+core_count_SOURCES = core_count.c
 
 md5sum_SOURCES = md5sum.c
 md5sum_CPPFLAGS = -DHASH_ALGO_MD5=1 $(AM_CPPFLAGS)
diff --git a/src/core_count.c b/src/core_count.c
new file mode 100644
index 0000000..9e1c372
--- /dev/null
+++ b/src/core_count.c
@@ -0,0 +1,89 @@
+/* core-count - print the number of online cpu cores
+   Copyright (C) 2009 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Giuseppe Scrivano.  */
+
+#include <config.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <sys/types.h>
+
+#include "system.h"
+#include "nproc.h"
+
+/* The official name of this program (e.g., no `g' prefix).  */
+#define PROGRAM_NAME "core-count"
+
+#define AUTHORS proper_name ("Giuseppe Scrivano")
+
+static struct option const longopts[] =
+{
+  {GETOPT_HELP_OPTION_DECL},
+  {GETOPT_VERSION_OPTION_DECL},
+  {NULL, 0, NULL, 0}
+};
+
+void
+usage (int status)
+{
+  if (status != EXIT_SUCCESS)
+    fprintf (stderr, _("Try `%s --help' for more information.\n"),
+             program_name);
+  else
+    {
+      printf (_("Usage: %s [OPTION]...\n"), program_name);
+      fputs (_("\
+Print the number of online cpu cores.\n\
+\n\
+"), stdout);
+      fputs (HELP_OPTION_DESCRIPTION, stdout);
+      fputs (VERSION_OPTION_DESCRIPTION, stdout);
+      emit_ancillary_info ();
+    }
+  exit (status);
+}
+
+int
+main (int argc, char **argv)
+{
+  initialize_main (&argc, &argv);
+  set_program_name (argv[0]);
+  setlocale (LC_ALL, "");
+  bindtextdomain (PACKAGE, LOCALEDIR);
+  textdomain (PACKAGE);
+
+  atexit (close_stdout);
+
+  while (1)
+    {
+      int c = getopt_long (argc, argv, "", longopts, NULL);
+      if (c == -1)
+        break;
+      switch (c)
+        {
+        case_GETOPT_HELP_CHAR;
+
+        case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
+
+        default:
+          usage (EXIT_FAILURE);
+        }
+    }
+
+  printf ("%u\n", num_processors ());
+
+  exit (EXIT_SUCCESS);
+}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 03fe6f1..6ac7f8b 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -458,6 +458,8 @@ TESTS =                                             \
   touch/read-only                              \
   touch/relative                               \
   touch/trailing-slash                         \
+  core-count/positive                          \
+  core-count/cpuinfo                           \
   $(root_tests)
 
 pr_data =                                      \
diff --git a/tests/core-count/cpuinfo b/tests/core-count/cpuinfo
new file mode 100755
index 0000000..ddadf11
--- /dev/null
+++ b/tests/core-count/cpuinfo
@@ -0,0 +1,34 @@
+#!/bin/sh
+# Check that the number of cores reported by core-count is equal to the number
+# of cores listed in /proc/cpuinfo, when it is available
+
+# Copyright (C) 2009 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+if test "$VERBOSE" = yes; then
+  set -x
+  core-count --version
+fi
+
+. $srcdir/test-lib.sh
+
+test -r /proc/cpuinfo || skip_test_
+
+fail=0
+cores=$(core-count)
+
+test $(grep '^core id' /proc/cpuinfo  | wc -l) -eq $cores || fail=1
+
+Exit $fail
diff --git a/tests/core-count/positive b/tests/core-count/positive
new file mode 100755
index 0000000..0c7e499
--- /dev/null
+++ b/tests/core-count/positive
@@ -0,0 +1,31 @@
+#!/bin/sh
+# Ensure that core-count returns a non-zero positive number
+
+# Copyright (C) 2009 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+if test "$VERBOSE" = yes; then
+  set -x
+  core-count --version
+fi
+
+. $srcdir/test-lib.sh
+
+fail=0
+cores=$(core-count)
+
+test $cores -gt 0 || fail=1
+
+Exit $fail
-- 
1.6.5




reply via email to

[Prev in Thread] Current Thread [Next in Thread]