From 233115eaf547fa35ce2b442f215739f6f20647f9 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Tue, 7 Aug 2012 04:42:49 +0000 Subject: [PATCH] runtime: support NumCPU() on more platforms Added support for Solaris, Irix, *BSD (including Darwin). 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 | 29 +++++++++++++- libgo/Makefile.in | 69 ++++++++++++++++++++++++++++++--- libgo/runtime/getncpu-bsd.c | 24 ++++++++++++ libgo/runtime/getncpu-irix.c | 16 ++++++++ libgo/runtime/getncpu-linux.c | 47 ++++++++++++++++++++++ libgo/runtime/getncpu-none.c | 12 ++++++ libgo/runtime/getncpu-solaris.c | 16 ++++++++ libgo/runtime/runtime.h | 2 + libgo/runtime/thread-linux.c | 36 ----------------- libgo/runtime/thread-sema.c | 1 + 10 files changed, 210 insertions(+), 42 deletions(-) create mode 100644 libgo/runtime/getncpu-bsd.c create mode 100644 libgo/runtime/getncpu-irix.c create mode 100644 libgo/runtime/getncpu-linux.c create mode 100644 libgo/runtime/getncpu-none.c create mode 100644 libgo/runtime/getncpu-solaris.c diff --git a/libgo/Makefile.am b/libgo/Makefile.am index f59b0048495..3d2c8b94008 100644 --- a/libgo/Makefile.am +++ b/libgo/Makefile.am @@ -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) $< diff --git a/libgo/Makefile.in b/libgo/Makefile.in index 18f14141b47..14ae2a54a27 100644 --- a/libgo/Makefile.in +++ b/libgo/Makefile.in @@ -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 index 00000000000..00a81d1ddae --- /dev/null +++ b/libgo/runtime/getncpu-bsd.c @@ -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 +#include + +#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 index 00000000000..a65ca63d2ae --- /dev/null +++ b/libgo/runtime/getncpu-irix.c @@ -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 + +#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 index 00000000000..05bd4a37e52 --- /dev/null +++ b/libgo/runtime/getncpu-linux.c @@ -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 +#include +#include +#include + +#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 index 00000000000..ba6fd4e689d --- /dev/null +++ b/libgo/runtime/getncpu-none.c @@ -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 index 00000000000..5d5d7025dfe --- /dev/null +++ b/libgo/runtime/getncpu-solaris.c @@ -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 + +#include "runtime.h" +#include "defs.h" + +int32 +getproccount(void) +{ + int32 n; + n = (int32)sysconf(_SC_NPROCESSORS_ONLN); + return n > 1 ? n : 1; +} diff --git a/libgo/runtime/runtime.h b/libgo/runtime/runtime.h index dc4fc0817f5..cebc1fd9801 100644 --- a/libgo/runtime/runtime.h +++ b/libgo/runtime/runtime.h @@ -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); diff --git a/libgo/runtime/thread-linux.c b/libgo/runtime/thread-linux.c index 0014068b2cd..13d23c47b07 100644 --- a/libgo/runtime/thread-linux.c +++ b/libgo/runtime/thread-linux.c @@ -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) { diff --git a/libgo/runtime/thread-sema.c b/libgo/runtime/thread-sema.c index 7d0acfb1ce1..18827b025d7 100644 --- a/libgo/runtime/thread-sema.c +++ b/libgo/runtime/thread-sema.c @@ -138,6 +138,7 @@ runtime_semawakeup (M *mp) void runtime_osinit (void) { + runtime_ncpu = getproccount(); } void -- 2.30.2