From patchwork Tue Aug 7 04:43:17 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Lance Taylor X-Patchwork-Id: 175502 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id D7ADA2C0085 for ; Tue, 7 Aug 2012 14:43:41 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1344919422; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:From:To:Subject:Date:Message-ID:User-Agent: MIME-Version:Content-Type:Mailing-List:Precedence:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:Sender: Delivered-To; bh=IhCS2mS1bSt3n4CIUswBso/ABb8=; b=CRtlUpnXoWV/h0W tPlesQLYsdNG7aAa08qOjBeSDYwHfrk6+2EUr7bsKoNvaBDtFtPGqvMuy6NvnWYR LNB5qxeguQNBJLMOqsqvOFiCL74cxNfEDXmLlclSKvTqZy/zWQGbXpQVXCx42kE+ ZCSeaGA5FqRoAGbOuUBSgpcupdcQ= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:X-Google-DKIM-Signature:Received:Received:Received:From:To:Subject:Date:Message-ID:User-Agent:MIME-Version:Content-Type:X-Gm-Message-State:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=aPl30YzPR31/5fjkA3shgyw8/+pLQirRwI8VbPt5nL7E5C/ghvROItN5tSlCPV +eYDSFOgCbmYIYQxA+cCpc+z7HcurgxxgSRNqWCMPmzCrlQyHFX7Abe6PDAVT/dc ehW3DWSYDhYjduTfxi8nvtgvCYDHHGumq/cqei94WpkLY=; Received: (qmail 7042 invoked by alias); 7 Aug 2012 04:43:36 -0000 Received: (qmail 7027 invoked by uid 22791); 7 Aug 2012 04:43:34 -0000 X-SWARE-Spam-Status: No, hits=-4.3 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, KHOP_RCVD_TRUST, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, TW_UF, T_RP_MATCHES_RCVD, T_TVD_MIME_NO_HEADERS X-Spam-Check-By: sourceware.org Received: from mail-pb0-f47.google.com (HELO mail-pb0-f47.google.com) (209.85.160.47) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 07 Aug 2012 04:43:20 +0000 Received: by pbcwy7 with SMTP id wy7so2748714pbc.20 for ; Mon, 06 Aug 2012 21:43:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:subject:date:message-id:user-agent:mime-version :content-type:x-gm-message-state; bh=L3r5dnYnY2mSxzuWvG1Y94t+vAoKZjq84EBUM5uI0F4=; b=SyN3KtW5OA7Mvd0D4qlFE80uFFuo0PzLZFo3/wTmVEJ+ZwVWD47GocOvwj5NwHw94+ pVtETKehEPJnb8UeUd/1dIfIaheJkYLlB4qUgigaZnqg4nPtk0x5rfHoVbQLJRUeEG5V owoUDRE049/Px4zrlVJQXqWrJSjLBiU/20dQbA/YE+JGdxgYwRD2R7jIpQ2aKzLKzTRq OoFy4e/PKjhKAhNQJ2JcztehPGaZ5/cO99ut+JbCnaUGHhJYC8L5whR+i+/oqV2jBkOP euV6mJJMoBjs6IgUC6B45PnV2l3g6jp/N29SxSRtvMtLciWDz0faYApDf60GYVSE7JDr VxJQ== Received: by 10.68.116.17 with SMTP id js17mr16193207pbb.109.1344314599674; Mon, 06 Aug 2012 21:43:19 -0700 (PDT) Received: by 10.68.116.17 with SMTP id js17mr16193191pbb.109.1344314599519; Mon, 06 Aug 2012 21:43:19 -0700 (PDT) Received: from coign.google.com ([216.239.45.130]) by mx.google.com with ESMTPS id qt6sm3404903pbb.9.2012.08.06.21.43.18 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 06 Aug 2012 21:43:18 -0700 (PDT) From: Ian Lance Taylor To: gcc-patches@gcc.gnu.org Subject: libgo patch committed: Support NumCPU on more platforms Date: Mon, 06 Aug 2012 21:43:17 -0700 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) MIME-Version: 1.0 X-Gm-Message-State: ALoCoQlpQ5ckcLJWvkOtnB0yCqNnKnqpfJYBHxEqeIHHbOL9Z+2nmLv3dCwEnpk9ciPfPxFvxo5jHBMCBEX9ybwNLpdxS0rYV8Gk5Uq0boLE4HUOYzDB6P64kSs5TzBbsKHfblyHcI1Rnnoh2g2yF4UVx2hQgs88I0UsIYdsF5osQqTO0YGNL6depV17vRY8cW4tdDYJmnKPYTHHGTZXhiIOa1vAvnSH4w== X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org This patch to libgo, from Shenghou Ma, adds support for NumCPU on additional platforms: Solaris, Irix, *BSD, Darwin. Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu. Committed to mainline. Ian diff -r 12a361bc53d0 libgo/Makefile.am --- a/libgo/Makefile.am Mon Aug 06 21:39:47 2012 -0700 +++ b/libgo/Makefile.am Mon Aug 06 21:40:21 2012 -0700 @@ -390,6 +390,32 @@ 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 @@ 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 -r 12a361bc53d0 libgo/runtime/getncpu-bsd.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libgo/runtime/getncpu-bsd.c Mon Aug 06 21:40:21 2012 -0700 @@ -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 -r 12a361bc53d0 libgo/runtime/getncpu-irix.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libgo/runtime/getncpu-irix.c Mon Aug 06 21:40:21 2012 -0700 @@ -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 -r 12a361bc53d0 libgo/runtime/getncpu-linux.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libgo/runtime/getncpu-linux.c Mon Aug 06 21:40:21 2012 -0700 @@ -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 -r 12a361bc53d0 libgo/runtime/getncpu-none.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libgo/runtime/getncpu-none.c Mon Aug 06 21:40:21 2012 -0700 @@ -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 -r 12a361bc53d0 libgo/runtime/getncpu-solaris.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libgo/runtime/getncpu-solaris.c Mon Aug 06 21:40:21 2012 -0700 @@ -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 -r 12a361bc53d0 libgo/runtime/runtime.h --- a/libgo/runtime/runtime.h Mon Aug 06 21:39:47 2012 -0700 +++ b/libgo/runtime/runtime.h Mon Aug 06 21:40:21 2012 -0700 @@ -518,3 +518,5 @@ extern uintptr runtime_stacks_sys; extern _Bool __go_file_line (uintptr, String*, String*, int *); + +int32 getproccount(void); diff -r 12a361bc53d0 libgo/runtime/thread-linux.c --- a/libgo/runtime/thread-linux.c Mon Aug 06 21:39:47 2012 -0700 +++ b/libgo/runtime/thread-linux.c Mon Aug 06 21:40:21 2012 -0700 @@ -72,42 +72,6 @@ *(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 -r 12a361bc53d0 libgo/runtime/thread-sema.c --- a/libgo/runtime/thread-sema.c Mon Aug 06 21:39:47 2012 -0700 +++ b/libgo/runtime/thread-sema.c Mon Aug 06 21:40:21 2012 -0700 @@ -138,6 +138,7 @@ void runtime_osinit (void) { + runtime_ncpu = getproccount(); } void