[procps] [PATCH 2/2] Initialize smp_num_cpus only if really required

  • From: "Dr. Werner Fink" <werner@xxxxxxx>
  • To: procps@xxxxxxxxxxxxx
  • Date: Thu, 14 Apr 2011 15:16:24 +0200

An other small cahnge to be able to use the cpp
ZAP_SUSEONLY define.  This change helps to avoid
reading /proc/stat on every use of libproc.

Signed-off-by: Werner Fink <werner@xxxxxxx>
---
 proc/sysinfo.c |   32 +++++++++++++++++++++++++++++++-
 proc/sysinfo.h |    4 ++++
 2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/proc/sysinfo.c b/proc/sysinfo.c
index 34f70ec..2f26d74 100644
--- a/proc/sysinfo.c
+++ b/proc/sysinfo.c
@@ -24,7 +24,9 @@
 #include <netinet/in.h>  /* htons */
 #endif
 
+#ifndef ZAP_SUSEONLY
 long smp_num_cpus;     /* number of CPUs */
+#endif
 
 #define BAD_OPEN_MESSAGE                                       \
 "Error: /proc must be mounted\n"                               \
@@ -180,7 +182,11 @@ static void old_Hertz_hack(void){
   setlocale(LC_NUMERIC, savelocale);
   jiffies = user_j + nice_j + sys_j + other_j;
   seconds = (up_1 + up_2) / 2;
+#ifndef ZAP_SUSEONLY
   h = (unsigned)( (double)jiffies/seconds/smp_num_cpus );
+#else
+  h = (unsigned)( (double)jiffies/seconds/smp_num_cpus() );
+#endif
   /* actual values used by 2.4 kernels: 32 64 100 128 1000 1024 1200 */
   switch(h){
   case    9 ...   11 :  Hertz =   10; break; /* S/390 (sometimes) */
@@ -246,10 +252,34 @@ static int check_for_privs(void){
   return !!rc;
 }
 
+#ifdef ZAP_SUSEONLY
+long smp_num_cpus(void)
+{
+  static long _smp_num_cpus=-1;     /* number of CPUs */
+
+  if (_smp_num_cpus != -1)
+    return(_smp_num_cpus);
+
+  // ought to count CPUs in /proc/stat instead of relying
+  // on glibc, which foolishly tries to parse /proc/cpuinfo
+  //
+  // SourceForge has an old Alpha running Linux 2.2.20 that
+  // appears to have a non-SMP kernel on a 2-way SMP box.
+  // _SC_NPROCESSORS_CONF returns 2, resulting in HZ=512
+  // _SC_NPROCESSORS_ONLN returns 1, which should work OK
+
+  _smp_num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
+  if(_smp_num_cpus<1) _smp_num_cpus=1; /* SPARC glibc is buggy */
+
+  return(_smp_num_cpus);
+}
+#endif
+
 static void init_libproc(void) __attribute__((constructor));
 static void init_libproc(void){
   have_privs = check_for_privs();
   init_Linux_version(); /* Must be called before we check code */
+#ifndef ZAP_SUSEONLY
   // ought to count CPUs in /proc/stat instead of relying
   // on glibc, which foolishly tries to parse /proc/cpuinfo
   //
@@ -259,7 +289,7 @@ static void init_libproc(void){
   // _SC_NPROCESSORS_ONLN returns 1, which should work OK
   smp_num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
   if(smp_num_cpus<1) smp_num_cpus=1; /* SPARC glibc is buggy */
-
+#endif
   if(linux_version_code > LINUX_VERSION(2, 4, 0)){ 
     Hertz = find_elf_note(AT_CLKTCK);
     if(Hertz!=NOTE_NOT_FOUND) return;
diff --git a/proc/sysinfo.h b/proc/sysinfo.h
index 97ae818..9834477 100644
--- a/proc/sysinfo.h
+++ b/proc/sysinfo.h
@@ -7,7 +7,11 @@
 EXTERN_C_BEGIN
 
 extern unsigned long long Hertz;   /* clock tick frequency */
+#ifndef ZAP_SUSEONLY
 extern long smp_num_cpus;     /* number of CPUs */
+#else
+extern long smp_num_cpus(void);     /* number of CPUs */
+#endif
 extern int have_privs;     /* boolean, true if setuid or similar */
 
 #if 0
-- 
1.6.0.2


Other related posts:

  • » [procps] [PATCH 2/2] Initialize smp_num_cpus only if really required - Dr. Werner Fink