runtime: support NumCPU() on more platforms Added support for Solaris, Irix, *BSD...
authorIan Lance Taylor <ian@gcc.gnu.org>
Tue, 7 Aug 2012 04:42:49 +0000 (04:42 +0000)
committerIan Lance Taylor <ian@gcc.gnu.org>
Tue, 7 Aug 2012 04:42:49 +0000 (04:42 +0000)
runtime: support NumCPU() on more platforms
        Added support for Solaris, Irix, *BSD (including Darwin).
        Still missing support for RTEMS.
        Fixes issue 3698 in Go issue tracker.

From-SVN: r190197

libgo/Makefile.am
libgo/Makefile.in
libgo/runtime/getncpu-bsd.c [new file with mode: 0644]
libgo/runtime/getncpu-irix.c [new file with mode: 0644]
libgo/runtime/getncpu-linux.c [new file with mode: 0644]
libgo/runtime/getncpu-none.c [new file with mode: 0644]
libgo/runtime/getncpu-solaris.c [new file with mode: 0644]
libgo/runtime/runtime.h
libgo/runtime/thread-linux.c
libgo/runtime/thread-sema.c

index f59b0048495034c55c437f345bd4b52192116548..3d2c8b94008ff51bc72b8c8e356e1c180f007293 100644 (file)
@@ -390,6 +390,32 @@ else
 runtime_lock_files = runtime/lock_sema.c runtime/thread-sema.c
 endif
 
+if LIBGO_IS_LINUX
+runtime_getncpu_file = runtime/getncpu-linux.c
+else
+if LIBGO_IS_DARWIN
+runtime_getncpu_file = runtime/getncpu-bsd.c
+else
+if LIBGO_IS_IRIX
+runtime_getncpu_file = runtime/getncpu-irix.c
+else
+if LIBGO_IS_SOLARIS
+runtime_getncpu_file = runtime/getncpu-solaris.c
+else
+if LIBGO_IS_FREEBSD
+runtime_getncpu_file = runtime/getncpu-bsd.c
+else
+if LIBGO_IS_NETBSD
+runtime_getncpu_file = runtime/getncpu-bsd.c
+else
+runtime_getncpu_file = runtime/getncpu-none.c
+endif
+endif
+endif
+endif
+endif
+endif
+
 runtime_files = \
        runtime/go-append.c \
        runtime/go-assert.c \
@@ -481,7 +507,8 @@ runtime_files = \
        sema.c \
        sigqueue.c \
        string.c \
-       time.c
+       time.c \
+       $(runtime_getncpu_file)
 
 goc2c.$(OBJEXT): runtime/goc2c.c
        $(CC_FOR_BUILD) -c $(CFLAGS_FOR_BUILD) $<
index 18f14141b47c8494ca10a234628e151cf6bf0ca9..14ae2a54a2781534bd986bfce04d5667f4d58681 100644 (file)
@@ -213,13 +213,23 @@ am__libgo_la_SOURCES_DIST = runtime/go-append.c runtime/go-assert.c \
        runtime/runtime.c runtime/signal_unix.c runtime/thread.c \
        runtime/yield.c runtime/rtems-task-variable-add.c iface.c \
        malloc.c map.c mprof.c reflect.c runtime1.c sema.c sigqueue.c \
-       string.c time.c
+       string.c time.c runtime/getncpu-none.c runtime/getncpu-bsd.c \
+       runtime/getncpu-solaris.c runtime/getncpu-irix.c \
+       runtime/getncpu-linux.c
 @LIBGO_IS_LINUX_FALSE@am__objects_1 = lock_sema.lo thread-sema.lo
 @LIBGO_IS_LINUX_TRUE@am__objects_1 = lock_futex.lo thread-linux.lo
 @HAVE_SYS_MMAN_H_FALSE@am__objects_2 = mem_posix_memalign.lo
 @HAVE_SYS_MMAN_H_TRUE@am__objects_2 = mem.lo
 @LIBGO_IS_RTEMS_TRUE@am__objects_3 = rtems-task-variable-add.lo
-am__objects_4 = go-append.lo go-assert.lo go-assert-interface.lo \
+@LIBGO_IS_DARWIN_FALSE@@LIBGO_IS_FREEBSD_FALSE@@LIBGO_IS_IRIX_FALSE@@LIBGO_IS_LINUX_FALSE@@LIBGO_IS_NETBSD_FALSE@@LIBGO_IS_SOLARIS_FALSE@am__objects_4 = getncpu-none.lo
+@LIBGO_IS_DARWIN_FALSE@@LIBGO_IS_FREEBSD_FALSE@@LIBGO_IS_IRIX_FALSE@@LIBGO_IS_LINUX_FALSE@@LIBGO_IS_NETBSD_TRUE@@LIBGO_IS_SOLARIS_FALSE@am__objects_4 = getncpu-bsd.lo
+@LIBGO_IS_DARWIN_FALSE@@LIBGO_IS_FREEBSD_TRUE@@LIBGO_IS_IRIX_FALSE@@LIBGO_IS_LINUX_FALSE@@LIBGO_IS_SOLARIS_FALSE@am__objects_4 = getncpu-bsd.lo
+@LIBGO_IS_DARWIN_FALSE@@LIBGO_IS_IRIX_FALSE@@LIBGO_IS_LINUX_FALSE@@LIBGO_IS_SOLARIS_TRUE@am__objects_4 = getncpu-solaris.lo
+@LIBGO_IS_DARWIN_FALSE@@LIBGO_IS_IRIX_TRUE@@LIBGO_IS_LINUX_FALSE@am__objects_4 = getncpu-irix.lo
+@LIBGO_IS_DARWIN_TRUE@@LIBGO_IS_LINUX_FALSE@am__objects_4 =  \
+@LIBGO_IS_DARWIN_TRUE@@LIBGO_IS_LINUX_FALSE@   getncpu-bsd.lo
+@LIBGO_IS_LINUX_TRUE@am__objects_4 = getncpu-linux.lo
+am__objects_5 = go-append.lo go-assert.lo go-assert-interface.lo \
        go-byte-array-to-string.lo go-breakpoint.lo go-caller.lo \
        go-callers.lo go-can-convert-interface.lo go-cgo.lo \
        go-check-interface.lo go-construct-map.lo \
@@ -244,8 +254,9 @@ am__objects_4 = go-append.lo go-assert.lo go-assert-interface.lo \
        $(am__objects_2) mfinal.lo mfixalloc.lo mgc0.lo mheap.lo \
        msize.lo print.lo proc.lo runtime.lo signal_unix.lo thread.lo \
        yield.lo $(am__objects_3) iface.lo malloc.lo map.lo mprof.lo \
-       reflect.lo runtime1.lo sema.lo sigqueue.lo string.lo time.lo
-am_libgo_la_OBJECTS = $(am__objects_4)
+       reflect.lo runtime1.lo sema.lo sigqueue.lo string.lo time.lo \
+       $(am__objects_4)
+am_libgo_la_OBJECTS = $(am__objects_5)
 libgo_la_OBJECTS = $(am_libgo_la_OBJECTS)
 libgo_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
        --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(libgo_la_LDFLAGS) \
@@ -807,6 +818,13 @@ toolexeclibgounicode_DATA = \
 @LIBGO_IS_RTEMS_TRUE@rtems_task_variable_add_file = runtime/rtems-task-variable-add.c
 @LIBGO_IS_LINUX_FALSE@runtime_lock_files = runtime/lock_sema.c runtime/thread-sema.c
 @LIBGO_IS_LINUX_TRUE@runtime_lock_files = runtime/lock_futex.c runtime/thread-linux.c
+@LIBGO_IS_DARWIN_FALSE@@LIBGO_IS_FREEBSD_FALSE@@LIBGO_IS_IRIX_FALSE@@LIBGO_IS_LINUX_FALSE@@LIBGO_IS_NETBSD_FALSE@@LIBGO_IS_SOLARIS_FALSE@runtime_getncpu_file = runtime/getncpu-none.c
+@LIBGO_IS_DARWIN_FALSE@@LIBGO_IS_FREEBSD_FALSE@@LIBGO_IS_IRIX_FALSE@@LIBGO_IS_LINUX_FALSE@@LIBGO_IS_NETBSD_TRUE@@LIBGO_IS_SOLARIS_FALSE@runtime_getncpu_file = runtime/getncpu-bsd.c
+@LIBGO_IS_DARWIN_FALSE@@LIBGO_IS_FREEBSD_TRUE@@LIBGO_IS_IRIX_FALSE@@LIBGO_IS_LINUX_FALSE@@LIBGO_IS_SOLARIS_FALSE@runtime_getncpu_file = runtime/getncpu-bsd.c
+@LIBGO_IS_DARWIN_FALSE@@LIBGO_IS_IRIX_FALSE@@LIBGO_IS_LINUX_FALSE@@LIBGO_IS_SOLARIS_TRUE@runtime_getncpu_file = runtime/getncpu-solaris.c
+@LIBGO_IS_DARWIN_FALSE@@LIBGO_IS_IRIX_TRUE@@LIBGO_IS_LINUX_FALSE@runtime_getncpu_file = runtime/getncpu-irix.c
+@LIBGO_IS_DARWIN_TRUE@@LIBGO_IS_LINUX_FALSE@runtime_getncpu_file = runtime/getncpu-bsd.c
+@LIBGO_IS_LINUX_TRUE@runtime_getncpu_file = runtime/getncpu-linux.c
 runtime_files = \
        runtime/go-append.c \
        runtime/go-assert.c \
@@ -898,7 +916,8 @@ runtime_files = \
        sema.c \
        sigqueue.c \
        string.c \
-       time.c
+       time.c \
+       $(runtime_getncpu_file)
 
 go_bufio_files = \
        go/bufio/bufio.go
@@ -2367,6 +2386,11 @@ distclean-compile:
 
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/chan.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cpuprof.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/getncpu-bsd.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/getncpu-irix.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/getncpu-linux.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/getncpu-none.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/getncpu-solaris.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/go-append.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/go-assert-interface.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/go-assert.Plo@am__quote@
@@ -3092,6 +3116,41 @@ rtems-task-variable-add.lo: runtime/rtems-task-variable-add.c
 @AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCC_FALSE@  $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o rtems-task-variable-add.lo `test -f 'runtime/rtems-task-variable-add.c' || echo '$(srcdir)/'`runtime/rtems-task-variable-add.c
 
+getncpu-none.lo: runtime/getncpu-none.c
+@am__fastdepCC_TRUE@   $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT getncpu-none.lo -MD -MP -MF $(DEPDIR)/getncpu-none.Tpo -c -o getncpu-none.lo `test -f 'runtime/getncpu-none.c' || echo '$(srcdir)/'`runtime/getncpu-none.c
+@am__fastdepCC_TRUE@   $(am__mv) $(DEPDIR)/getncpu-none.Tpo $(DEPDIR)/getncpu-none.Plo
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      source='runtime/getncpu-none.c' object='getncpu-none.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@  $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o getncpu-none.lo `test -f 'runtime/getncpu-none.c' || echo '$(srcdir)/'`runtime/getncpu-none.c
+
+getncpu-bsd.lo: runtime/getncpu-bsd.c
+@am__fastdepCC_TRUE@   $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT getncpu-bsd.lo -MD -MP -MF $(DEPDIR)/getncpu-bsd.Tpo -c -o getncpu-bsd.lo `test -f 'runtime/getncpu-bsd.c' || echo '$(srcdir)/'`runtime/getncpu-bsd.c
+@am__fastdepCC_TRUE@   $(am__mv) $(DEPDIR)/getncpu-bsd.Tpo $(DEPDIR)/getncpu-bsd.Plo
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      source='runtime/getncpu-bsd.c' object='getncpu-bsd.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@  $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o getncpu-bsd.lo `test -f 'runtime/getncpu-bsd.c' || echo '$(srcdir)/'`runtime/getncpu-bsd.c
+
+getncpu-solaris.lo: runtime/getncpu-solaris.c
+@am__fastdepCC_TRUE@   $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT getncpu-solaris.lo -MD -MP -MF $(DEPDIR)/getncpu-solaris.Tpo -c -o getncpu-solaris.lo `test -f 'runtime/getncpu-solaris.c' || echo '$(srcdir)/'`runtime/getncpu-solaris.c
+@am__fastdepCC_TRUE@   $(am__mv) $(DEPDIR)/getncpu-solaris.Tpo $(DEPDIR)/getncpu-solaris.Plo
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      source='runtime/getncpu-solaris.c' object='getncpu-solaris.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@  $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o getncpu-solaris.lo `test -f 'runtime/getncpu-solaris.c' || echo '$(srcdir)/'`runtime/getncpu-solaris.c
+
+getncpu-irix.lo: runtime/getncpu-irix.c
+@am__fastdepCC_TRUE@   $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT getncpu-irix.lo -MD -MP -MF $(DEPDIR)/getncpu-irix.Tpo -c -o getncpu-irix.lo `test -f 'runtime/getncpu-irix.c' || echo '$(srcdir)/'`runtime/getncpu-irix.c
+@am__fastdepCC_TRUE@   $(am__mv) $(DEPDIR)/getncpu-irix.Tpo $(DEPDIR)/getncpu-irix.Plo
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      source='runtime/getncpu-irix.c' object='getncpu-irix.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@  $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o getncpu-irix.lo `test -f 'runtime/getncpu-irix.c' || echo '$(srcdir)/'`runtime/getncpu-irix.c
+
+getncpu-linux.lo: runtime/getncpu-linux.c
+@am__fastdepCC_TRUE@   $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT getncpu-linux.lo -MD -MP -MF $(DEPDIR)/getncpu-linux.Tpo -c -o getncpu-linux.lo `test -f 'runtime/getncpu-linux.c' || echo '$(srcdir)/'`runtime/getncpu-linux.c
+@am__fastdepCC_TRUE@   $(am__mv) $(DEPDIR)/getncpu-linux.Tpo $(DEPDIR)/getncpu-linux.Plo
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      source='runtime/getncpu-linux.c' object='getncpu-linux.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@  $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o getncpu-linux.lo `test -f 'runtime/getncpu-linux.c' || echo '$(srcdir)/'`runtime/getncpu-linux.c
+
 mostlyclean-libtool:
        -rm -f *.lo
 
diff --git a/libgo/runtime/getncpu-bsd.c b/libgo/runtime/getncpu-bsd.c
new file mode 100644 (file)
index 0000000..00a81d1
--- /dev/null
@@ -0,0 +1,24 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#include <sys/types.h>
+#include <sys/sysctl.h>
+
+#include "runtime.h"
+#include "defs.h"
+
+int32
+getproccount(void)
+{
+       int mib[2], out;
+       size_t len;
+
+       mib[0] = CTL_HW;
+       mib[1] = HW_NCPU;
+       len = sizeof(out);
+       if(sysctl(mib, 2, &out, &len, NULL, 0) >= 0)
+               return (int32)out;
+       else
+               return 0;
+}
diff --git a/libgo/runtime/getncpu-irix.c b/libgo/runtime/getncpu-irix.c
new file mode 100644 (file)
index 0000000..a65ca63
--- /dev/null
@@ -0,0 +1,16 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#include <unistd.h>
+
+#include "runtime.h"
+#include "defs.h"
+
+int32
+getproccount(void)
+{
+       int32 n;
+       n = (int32)sysconf(_SC_NPROC_ONLN);
+       return n > 1 ? n : 1;
+}
diff --git a/libgo/runtime/getncpu-linux.c b/libgo/runtime/getncpu-linux.c
new file mode 100644 (file)
index 0000000..05bd4a3
--- /dev/null
@@ -0,0 +1,47 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#include <string.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "runtime.h"
+#include "defs.h"
+
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+
+int32
+getproccount(void)
+{
+       int32 fd, rd, cnt, cpustrlen;
+       const char *cpustr;
+       const byte *pos;
+       byte *bufpos;
+       byte buf[256];
+
+       fd = open("/proc/stat", O_RDONLY|O_CLOEXEC, 0);
+       if(fd == -1)
+               return 1;
+       cnt = 0;
+       bufpos = buf;
+       cpustr = "\ncpu";
+       cpustrlen = strlen(cpustr);
+       for(;;) {
+               rd = read(fd, bufpos, sizeof(buf)-cpustrlen);
+               if(rd == -1)
+                       break;
+               bufpos[rd] = 0;
+               for(pos=buf; (pos=(const byte*)strstr((const char*)pos, cpustr)) != nil; cnt++, pos++) {
+               }
+               if(rd < cpustrlen)
+                       break;
+               memmove(buf, bufpos+rd-cpustrlen+1, cpustrlen-1);
+               bufpos = buf+cpustrlen-1;
+       }
+       close(fd);
+       return cnt ? cnt : 1;
+}
diff --git a/libgo/runtime/getncpu-none.c b/libgo/runtime/getncpu-none.c
new file mode 100644 (file)
index 0000000..ba6fd4e
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#include "runtime.h"
+#include "defs.h"
+
+int32
+getproccount(void)
+{
+       return 0;
+}
diff --git a/libgo/runtime/getncpu-solaris.c b/libgo/runtime/getncpu-solaris.c
new file mode 100644 (file)
index 0000000..5d5d702
--- /dev/null
@@ -0,0 +1,16 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#include <unistd.h>
+
+#include "runtime.h"
+#include "defs.h"
+
+int32
+getproccount(void)
+{
+       int32 n;
+       n = (int32)sysconf(_SC_NPROCESSORS_ONLN);
+       return n > 1 ? n : 1;
+}
index dc4fc0817f5254a3b6492d0c410a9ed1bd9b2e4d..cebc1fd9801361e5e5582a54eca913b3ecaac09e 100644 (file)
@@ -518,3 +518,5 @@ void        __go_register_gc_roots(struct root_list*);
 extern uintptr runtime_stacks_sys;
 
 extern _Bool __go_file_line (uintptr, String*, String*, int *);
+
+int32 getproccount(void);
index 0014068b2cd6dcdd6ecbcda27fa81c54ef4f9996..13d23c47b077ead3280056c2496f987f98dec11b 100644 (file)
@@ -72,42 +72,6 @@ runtime_futexwakeup(uint32 *addr, uint32 cnt)
        *(int32*)0x1006 = 0x1006;
 }
 
-#ifndef O_CLOEXEC
-#define O_CLOEXEC 0
-#endif
-
-static int32
-getproccount(void)
-{
-       int32 fd, rd, cnt, cpustrlen;
-       const char *cpustr;
-       const byte *pos;
-       byte *bufpos;
-       byte buf[256];
-
-       fd = open("/proc/stat", O_RDONLY|O_CLOEXEC, 0);
-       if(fd == -1)
-               return 1;
-       cnt = 0;
-       bufpos = buf;
-       cpustr = "\ncpu";
-       cpustrlen = strlen(cpustr);
-       for(;;) {
-               rd = read(fd, bufpos, sizeof(buf)-cpustrlen);
-               if(rd == -1)
-                       break;
-               bufpos[rd] = 0;
-               for(pos=buf; (pos=(const byte*)strstr((const char*)pos, cpustr)) != nil; cnt++, pos++) {
-               }
-               if(rd < cpustrlen)
-                       break;
-               memmove(buf, bufpos+rd-cpustrlen+1, cpustrlen-1);
-               bufpos = buf+cpustrlen-1;
-       }
-       close(fd);
-       return cnt ? cnt : 1;
-}
-
 void
 runtime_osinit(void)
 {
index 7d0acfb1ce1ff78da83b55599c9db8c6bbd2d49d..18827b025d7e2b642b99d0818318c0019b272bd6 100644 (file)
@@ -138,6 +138,7 @@ runtime_semawakeup (M *mp)
 void
 runtime_osinit (void)
 {
+  runtime_ncpu = getproccount();
 }
 
 void