Commit 233115ea by Ian Lance Taylor

runtime: support NumCPU() on more platforms Added support for Solaris, Irix,…

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
parent 2dbfe2c6
......@@ -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) $<
......
// 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;
}
// 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;
}
// 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;
}
// 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;
}
// 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;
}
......@@ -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);
......@@ -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)
{
......
......@@ -138,6 +138,7 @@ runtime_semawakeup (M *mp)
void
runtime_osinit (void)
{
runtime_ncpu = getproccount();
}
void
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment