From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,NICE_REPLY_A,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 26792 invoked from network); 10 Jul 2021 17:29:49 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 10 Jul 2021 17:29:49 -0000 Received: (qmail 15652 invoked by uid 550); 10 Jul 2021 17:29:47 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 15622 invoked from network); 10 Jul 2021 17:29:47 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sholland.org; h= subject:to:references:cc:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=x RVutX6oIxr2ho2I5lg2EF5TmBr9ZHIg7IKuDNH5fDc=; b=G662UgU+hZS1aYB8W qUI+yc8lVrLedPqEhDiOw1HMdghUveaUVy5Xo0p0WUuR0YiUH2+EVnPQsEpZAZRn +81cRfZ3mUbZnPqDd+FAgCRFKvyZ0AlJ48wP14xvJFTMQdrUMSzYDkkcRhZD/4oW Qb8h/E1PHBq/DQSgkwVrg1eeyFHByRnV48J0NLmjLjX983SbYkAtBdNxZZJ2+n/9 uTxNY4CUoTPbCSx3dFaoEEn+M7h7l4/uuFSIovr/w8US0wY4+qZWR+sZs186GHqP sUJNN0YZbaMQzayYbrVgf/TKoEiNlk2/uTSUtsirfh0c/W77CzpI0ldgrZW3TOcE JsJIw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=xRVutX6oIxr2ho2I5lg2EF5TmBr9ZHIg7IKuDNH5f Dc=; b=Ms5fR1dtRSfusVWbDMFMxuH3IB99BV6MWFhQGEBphITAqEqEh4VOKzmM1 nZaa3yMiTus4qF5ZW6/6GVCF1E8RlghNvV4Zj8IrC3oLfMeKYO078oc0V50Qq+2x +cBwlbI/ubOuDD8V1OdZRnVGttwjrqg5WC55rq9RoxPCJZPZmtwzKaGaZgZ+0zK/ Y0uw6pJPLMHSJ/iiYORhZCMOJk95ev8FHlvsgOL8SHWFDgJiI1/7+mFwYVQKYgyl P2Egcr3NUdtlBRQKgsTA5DqmAIB1M9MS6IkFqCYu90ksiaa+5fK7Shvp3gXP9xaS IfBEEIa6N6fKRKrTgxbFTV4MhuvWw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrtdekgdduudefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefuvfhfhffkffgfgggjtgfgsehtje ertddtfeejnecuhfhrohhmpefurghmuhgvlhcujfholhhlrghnugcuoehsrghmuhgvlhes shhhohhllhgrnhgurdhorhhgqeenucggtffrrghtthgvrhhnpefgveffteelheffjeeuke dvkedviedtheevgeefkeehueeiieeuteeugfettdeggeenucevlhhushhtvghrufhiiigv pedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehsrghmuhgvlhesshhhohhllhgrnhgurd horhhg X-ME-Proxy: To: musl@lists.openwall.com References: <20210710053157.4F16D22215EC@gateway02.insomnia247.nl> Cc: jason , vincent.donnefort@arm.com From: Samuel Holland Message-ID: Date: Sat, 10 Jul 2021 12:29:34 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: <20210710053157.4F16D22215EC@gateway02.insomnia247.nl> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Subject: Re: [musl] [PATCH v3] sysconf: add _SC_NPROCESSORS_CONF support Hi all, While this topic is being discussed, I'd like to bring up another possible method for consideration. It turns out that some files in procfs have contents depending on the number of {possible,online} CPUs; we can parse them to get the count we need. The benefits are: - procfs is more likely to be mounted than sysfs, and it is already documented as being required by musl. - Some of the procfs interfaces have been around longer than the sysfs interface. - The parsing logic is somewhat simpler than for the files in /sys/devices/system/cpu, because we are just counting the number of occurrences of some string. The downsides are: - It depends on the stability of the procfs file formats. - The kernel uses more CPU to generate the contents of these files. I looked through the procfs code for uses of for_each_possible_cpu and for_each_online_cpu, and the best candidates I found are: - /proc/softirqs for _SC_NPROCESSORS_CONF. It was added in 2009 (commit d3d64df21d3d, v2.6.31). Its first line contains a column for_each_possible_cpu. - /proc/stat for _SC_NPROCESSORS_ONLN. It has been around since before git. It contains a line for_each_online_cpu. Here's an example implementation: diff --git a/src/conf/sysconf.c b/src/conf/sysconf.c index 3baaed32..8f787b81 100644 --- a/src/conf/sysconf.c +++ b/src/conf/sysconf.c @@ -193,10 +193,27 @@ long sysconf(int name) return SEM_VALUE_MAX; case JT_DELAYTIMER_MAX & 255: return DELAYTIMER_MAX; - case JT_NPROCESSORS_CONF & 255: - case JT_NPROCESSORS_ONLN & 255: ; + case JT_NPROCESSORS_CONF & 255: ; + FILE *f = fopen("/proc/softirqs", "rbe"); + int i, cnt = 0; + if (f) { + while (fscanf(f, " CPU%u", &i) > 0) ++cnt; + fclose(f); + } + if (cnt) + return cnt; + /* fallthrough */ + case JT_NPROCESSORS_ONLN & 255: + f = fopen("/proc/stat", "rbe"); + cnt = 0; + if (f) { + fscanf(f, "cpu %*[^c]"); + while (fscanf(f, "cpu%u %*[^c]", &i) > 0) ++cnt; + fclose(f); + } + if (cnt) + return cnt; unsigned char set[128] = {1}; - int i, cnt; __syscall(SYS_sched_getaffinity, 0, sizeof set, set); for (i=cnt=0; i