From patchwork Fri Aug 10 06:09:18 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: 176361 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 6E6082C008D for ; Fri, 10 Aug 2012 16:09:39 +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=1345183780; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: MIME-Version:Received:Received:In-Reply-To:References:Date: Message-ID:Subject:From:To:Cc:Content-Type:Mailing-List: Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:Sender:Delivered-To; bh=svzm3EFJ35R9NQGgOqK7Y25balE=; b=DSfxEmRWY76PlUbZuN++99tNQMM46iJF4xG42sQ9x0Kmhpiz9sz/mNAIArDtV+ Z2VXCuFffFnrsUZEaMBT6CFZ9Oscl4crg0Rjnn4MsOjuyuDbCzuE7xTI3Qk8RzZq LVXCf8x/5zwCfXFEScYitVjlzD0phMJALOgrX12coLRhY= 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:MIME-Version:Received:Received:In-Reply-To:References:Date:Message-ID:Subject:From:To:Cc:Content-Type:X-System-Of-Record:X-Gm-Message-State:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=tpGdWLH10wR2Sgb3Ym8ryjEYJIX52saJByVnnyCl7QaBpzRnPbS7J9KiLHmyeY 4NxKfJoBsTg4S68NxOG8kUlu5ix4AFWmV4J8PFbWS7X9WGUtoPHGG5SAtSEwic9f 0kyYtmLZNvjN1OQEfd3KWO4h74jCTis92PQ0UsvntNivI=; Received: (qmail 9002 invoked by alias); 10 Aug 2012 06:09:35 -0000 Received: (qmail 8986 invoked by uid 22791); 10 Aug 2012 06:09:32 -0000 X-SWARE-Spam-Status: No, hits=-5.7 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, KHOP_RCVD_TRUST, KHOP_THREADED, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, TW_UF, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mail-gg0-f175.google.com (HELO mail-gg0-f175.google.com) (209.85.161.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 10 Aug 2012 06:09:19 +0000 Received: by ggmq1 with SMTP id q1so1275201ggm.20 for ; Thu, 09 Aug 2012 23:09:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:x-system-of-record:x-gm-message-state; bh=91xdrtFP8ZhSFJlM9j7ClgdNujOEeNnjntFsvogM1aY=; b=Ct777OJq2WOrXwQxoPJKCev8kHSggthuTV0RzYdVhsYJKtOwa0d/90oIk6AqT5OR9h EeK5f01kp0xQA5ER8aUEry2zaDg5kXzMDMaViXX4ueeichHsGOpNMyLcqHdajLtqlZxV T4K9eTphduXDfnbFyeZbJuUKPv7kN/F0UWqewNYeFcoM8eKBuWrTWY5iuZAHZ4W1EmVJ G3MLOPFq21gy8ifRNNOXJQZp167FdsxWZs8607WYt2VxTvKpf+fHAyyYW9xV1BSJ2q1z Lh5gHkHDaxRDAHlgFNmxTP81z1kfhqktXOkXvAYaGc2oH9s/63cyy5G3x8bJna/6cDK4 4DTg== Received: by 10.66.73.98 with SMTP id k2mr3707128pav.73.1344578958413; Thu, 09 Aug 2012 23:09:18 -0700 (PDT) MIME-Version: 1.0 Received: by 10.66.73.98 with SMTP id k2mr3707113pav.73.1344578958308; Thu, 09 Aug 2012 23:09:18 -0700 (PDT) Received: by 10.68.8.9 with HTTP; Thu, 9 Aug 2012 23:09:18 -0700 (PDT) In-Reply-To: <50210866.9010106@arm.com> References: <50210866.9010106@arm.com> Date: Thu, 9 Aug 2012 23:09:18 -0700 Message-ID: Subject: Re: libgo patch committed: Support NumCPU on more platforms From: Ian Lance Taylor To: Richard Earnshaw Cc: "gcc-patches@gcc.gnu.org" X-System-Of-Record: true X-Gm-Message-State: ALoCoQlDH7jxpCLELXe3d6Dk8zB01ZBy+/OZ5irrmFCos+kdXr/6y3qh3SKwspeQ9IRjAikMtGFz3+MpcpVVaHJe65bzCW7AsBfJOO88sc3wy7xxbdfGIq3ssdqSegwTOpG4FkMKuAI85gyShSMWc4WFauX1sqGGM2xgn7cfVdSMaQOvhVvye/Xyc0RL09AB1lfXJ2c10Gt9 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 On Tue, Aug 7, 2012 at 5:21 AM, Richard Earnshaw wrote: > > Wouldn't it be more useful on Linux to check the task's affinity > settings? Then when a task is locked to a limited set of cores it won't > overload those cores with threads. This suggestion was implemented by Shenghou Ma, as attached. Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu. Committed to mainline. Ian diff -r 8fc45a01251d libgo/runtime/getncpu-linux.c --- a/libgo/runtime/getncpu-linux.c Mon Aug 06 21:42:17 2012 -0700 +++ b/libgo/runtime/getncpu-linux.c Thu Aug 09 23:04:31 2012 -0700 @@ -2,46 +2,35 @@ // 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 +#include + +// CPU_COUNT is only provided by glibc 2.6 or higher +#if !defined(__GLIBC_PREREQ) || !__GLIBC_PREREQ(2, 6) +#define CPU_COUNT(set) _CPU_COUNT((unsigned int *)(set), sizeof(*(set))/sizeof(unsigned int)) +static int _CPU_COUNT(unsigned int *set, size_t len) { + int cnt; + + cnt = 0; + while (len--) + cnt += __builtin_popcount(*set++); + return cnt; +} +#endif #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]; + cpu_set_t set; + int32 r, cnt; - 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); + r = sched_getaffinity(0, sizeof(set), &set); + if(r == 0) + cnt += CPU_COUNT(&set); + return cnt ? cnt : 1; }