Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp82830yba; Fri, 5 Apr 2019 02:27:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqxs3fdP9IVM/M6KG6dDGnqF4It9x81QOJSKVyYnM2zwlNb8TGLGwcU7wTjJuQfKZ81y8g85 X-Received: by 2002:a17:902:7247:: with SMTP id c7mr11727249pll.255.1554456476661; Fri, 05 Apr 2019 02:27:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554456476; cv=none; d=google.com; s=arc-20160816; b=wafN5s83kXRJxnXcIRcnAzXt8Zs3x4iSZ72mN4NbGF8GY0lo6JaZ6U0rv9Kl5O66wV XIUJGup8V37W5jDLEWsrlpr+eGbgjNrQnMGwO+N6JDbWZjEhl8VrwQWcueRZxlg2JN+z Om8OfOZZJ0aKQ7kn00tJtFY2kg5la1/WSN+kCB9XsNEToM/9lnheAqCw6Z6zOfd+UUvC IY8CasemIT2Gi82LBWseN+wxthQAezwQ/T2ZBgPKZW1xE3UHCvvKJcKumE3bu6o2A6DA gv7a8YbhDZZSlhuyGlsWUttWPuED1O2xwGqavv6njkb52uotljAgHU7VahC2YUkWrvuR PY1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=rmRDRrrBXgpiFDAiERO2uKmXa8X/tU5EIcxD32jyuDU=; b=W8+gkeWjEKFV9YLNV6W7hjJl/m5gMp1ccVLS1yQg8NgwwcY5SeIi+BHXG0H+Nym4jg lAX3cRRsUAVX0T+NwOjLruyRTwSAMWepeTsw3D+eszanSG/Os7VfH26emCQykL9YKNjk IHDOpD8f7DI/ollQKcJHluQk9refSc414hmZE5Pdpxv7AHfi1fWyhOFNe3L1nhTOeNnM e2AQpT9mBBrmi1O7tUpDunexDhyLSndokQvR3xs+QvoiNiuTmpdgCaggodUDjClB1IhL Ybgp3Je9eNj0NJibvPsxJB0al+Reel50hLEH2aaPvMuwZr5oto8FFr32vqTfOxzAcbI8 iRNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=DUU3S6K9; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r17si18795046pgm.52.2019.04.05.02.27.40; Fri, 05 Apr 2019 02:27:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=DUU3S6K9; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730473AbfDEJ1B (ORCPT + 99 others); Fri, 5 Apr 2019 05:27:01 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:52586 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729728AbfDEJ1A (ORCPT ); Fri, 5 Apr 2019 05:27:00 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=rmRDRrrBXgpiFDAiERO2uKmXa8X/tU5EIcxD32jyuDU=; b=DUU3S6K9fk2LZLZBwYd5G85s8 7hRxUH82o2RVAFEieQaA4AH/uYBqBwEHED+NuJYdHChfKUaknh2DFRQvt1M/OQOxnJed+rhUzcaXA uRgV6uG+PD2cujVzlPmqUhhECEM/+sokRGk/9w5+/9JG5I6tZ6im8P6ldl5lcds0K2ju8bq4zN8oH 9LWQ00e7x15kXO+RarWC2WRvOs7ws8Fiq75lutv/WNxZ/Eo6Ag7WoHRmhmvK9inqxUT+QeZk42pSO gXM2fAChDo2k2BgH2/tYr0KiumGTedf6NOfYqzdk5Kgb3mX1/6cPZ2OaIAGR/CcTK3vXtUgQK9kKU 96oPRNJ6Q==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1hCL7j-0007KR-1H; Fri, 05 Apr 2019 09:26:59 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 6A9C7200BB74D; Fri, 5 Apr 2019 11:26:57 +0200 (CEST) Date: Fri, 5 Apr 2019 11:26:57 +0200 From: Peter Zijlstra To: Alexey Dobriyan Cc: mingo@redhat.com, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org Subject: Re: [PATCH] sched/core: expand sched_getaffinity(2) to return number of CPUs Message-ID: <20190405092657.GD4038@hirez.programming.kicks-ass.net> References: <20190403200809.GA13876@avx2> <20190404084249.GS4038@hirez.programming.kicks-ass.net> <20190404180230.GA14800@avx2> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190404180230.GA14800@avx2> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Apr 04, 2019 at 09:02:30PM +0300, Alexey Dobriyan wrote: > On Thu, Apr 04, 2019 at 10:42:49AM +0200, Peter Zijlstra wrote: > > On Wed, Apr 03, 2019 at 11:08:09PM +0300, Alexey Dobriyan wrote: > > > Currently there is no easy way to get the number of CPUs on the system. > > > > And this patch doesn't change that :-) > > It does! Application or a library could do one idempotent system call > in a constructor. You said: "get the number of CPUs on the system", but what this call actually returns is: "size of CPU bitmask", these two things are distinctly not the same. > > The point is that nr_cpu_ids is the length of the bitmap, but does not > > contain information on how many CPUs are in the system. Consider the > > case where the bitmap is sparse. > > I understand that but how do you ship number of CPUs _and_ possible mask > in one go? You don't, possible mask is unrelated to number of CPUs and both are unrelated to bitmap size. In particular: nr_cpus <= nr_possible_cpus <= nr_cpumask_bits In absurdum an architecture could choose to iterate its CPUs as 1024*cpuid, just for giggles. Then suppose it has two sockets, and 16 CPUs per socket and only the second socket is populated. The we get: nr_cpumask_bits = 32k nr_possible_cpus = 32 nr_cpus = 16 see what I mean? Now, luckily we typically don't have crap like that, but I suspect that with a little creative ACPI table magic we could actually get something slightly less absurd but still non-trivial even on x86. Also see how num_possible_cpus() is defined as cpumask_weight(cpu_possible_mask), which is very much not nr_cpu_ids in the generic case. > > > Nobody seems to parse "/sys/devices/system/cpu/possible". > > > Even if someone does, parsing sysfs is much slower than necessary. > > > > True; but I suppose glibc already does lots of that anyway, right? It > > does contain the right information. > > sched_getaffinity(3) does sched_getaffinity(2) + memset() > > sysconf(_SC_NPROCESSORS_ONLN) does "/sys/devices/system/cpu/online" > > sysconf(_SC_NPROCESSORS_CONF) does readdir("/sys/devices/system/cpu") > which is 5 syscalls. I'm not sure which cpumask readdir represents. It counts the number of CPUs, which is strictly not the same as the bitmap size.