Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127664 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 5A4B01A00BC for ; Sun, 15 Jun 2025 15:21:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1750000751; bh=rRivLdp9OFNJ/6S6ivbu1HsW3Ow9Kwoy6pF0HirdlZw=; h=Date:From:To:Subject:In-Reply-To:References:From; b=A+ohRJc2jsVQ/DItYkfM2rzlxydqqJXjTdR5RWFZRxBIxYFS1rWMOvp1DRCtyByMz 8f6S4Y5vjZtoj54F9r+oVA9uFk1MjBGDNMNyvC2e0I6nW+eAqYtmPEUogP05JMr5Ik iRf54xFt9G2dBtvJzNUiLJ7zWkczg6TSnw/4VjKrc7fkLEgcuOhQyfNrrkPIBIKlyJ AJihHMmedzZMFlM6hFFaWsSaEFHzrOIcpldi703d77t3ghUS9M9+VoU/ZcRDot/0wk YYbfwyuahhGKazHhCLL58skPf8p8XI70ZJQRYq9299ef4ftHR9/OiZNmu8W8nkcq2z c+VaLPM0wJtzA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6101A180041 for ; Sun, 15 Jun 2025 15:19:08 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.2 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from a.server2k.de (a.server2k.de [37.120.186.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 15 Jun 2025 15:19:07 +0000 (UTC) Received: from webmail.server2k.de (localhost [127.0.0.1]) by a.server2k.de (Postfix) with ESMTPSA id A85204011C for ; Sun, 15 Jun 2025 17:21:04 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=danielkesselberg.de; s=dkim; t=1750000864; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rBJA/IOUnshxQ9b8BjGEIrpX8i/NbiJaGh6ehZMUaAM=; b=gtRoKQNcyOnRZ8PbugiRMnXpObhcOpfWlbu0drJbc7hn/ASJWYW/1JskaI6oAGf9aX0a/q v+sQs5SxZ813AWjev+LdXohQJkRBiH8ba4ASG6GYTAO130YHiJR+YF1tuvV0Yh4ecYJLhM hJm1aVhuXfFb/BrzVWDiGDh1mPwEEx8= Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Sun, 15 Jun 2025 17:21:04 +0200 To: PHP Internals Subject: Re: [PHP-DEV] [RFC] Add num_available_processors In-Reply-To: References: <60B4033B-2E6A-4157-A698-6C58454B07C9@garsi.de> Message-ID: <41838a7899af66f89692b9c041bbe2c6@danielkesselberg.de> X-Sender: mail@danielkesselberg.de Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: mail@danielkesselberg.de (Daniel Kesselberg) Hi Rob, Thanks for your feedback. I've added a note, to the proposal section, that the cpu affinity mask is currently not considered. The current patch is rather small. Taking the CPU affinity mask into account will require a more work and introduces different code paths for linux, bsd and win. I agree, it's more correct to take into account and wonder if it's probably enough to just document for now. What do you think, is that something that should go into the voting part (take cpu affinity mask into account)? RFC: https://wiki.php.net/rfc/num_available_processors On 2025-05-25 15:28, Rob Landers wrote: > On Sun, May 25, 2025, at 12:07, Alwin Garside wrote: > >> On 24 May 2025, at 20:48, Rob Landers wrote: >>> >>> On Sat, May 24, 2025, at 19:37, Daniel Kesselberg wrote: >>>> >>>> >>>> Hi everyone, >>>> >>>> I'm happy to share my first RFC :) It proposes adding a small >>>> function >>>> to retrieve the number of available processors; a feature that's >>>> commonly found in other programming languages and one that I believe >>>> would be a useful addition to PHP. >>>> >>>> The related PR has already received a bit of early traction, and now >>>> that the RFC is complete, I'm looking forward to your feedback! >>>> >>>> RFC: https://wiki.php.net/RFC/num_available_processors >>>> Patch: https://github.com/php/php-src/pull/11137 >>>> >>>> Best >>>> Daniel >>>> >>> >>> Looks good! >>> >>> My main question is: what is this actually counting? In the RFC it >>> mentions "available processing units" ... which means, what? What >>> counts as a "processing unit"? Are we talking about CPU >>> Threads/cores; NPU cores; TPM cores; clocks? GPS? GPU? ... a modern >>> computer has many "processing units" for different purposes and >>> workloads. I'm assuming this is CPU Threads, not physical cores? I >>> will refer to CPU Threads as "Logical Cores" so we all don't get >>> confused since most of us here are programmers and saying "thread" >>> has a different meaning. >>> >>> Secondly, how is it counting "available"? If I assign PHP to a >>> specific CPU affinity mask (say one logical core), will it return 1, >>> or the total number of logical cores available on my machine? I would >>> expect it to be 1, since PHP only has access to 1, but I can also see >>> the logic in returning the total number. >>> >>> -- Rob >> >> Hi Daniel, >> >> I agree with Rob that "processor" is a bit too ambiguous. I'd use the >> phrase "cpu_core" instead. Yes, technically that's not entirely >> accurate when hyper-threading is used, but in most cases it's not >> trivial to distinguish physical cores from logical cores anyway, and >> "cpu_cores" provides the most understandable abstraction for the vast >> majority of use cases: deciding how many parallel processes one should >> use for optimal use of the CPU. > > Yes, that is why I was curious as to the method it was using to return > the number. Using the sysconf method will just return the total number > of available cores. However, in Linux there is also the > sched_getaffinity method which returns the total number of cores > allocated to the current process (which may be lower). For example, > when sharing a webserver and a worker queue on a single system, I may > only allocate 1/3 of my cores (via `taskset`) to the worker queue, and > 1/3 cores to the webserver and the last 1/3 for other tasks (such as a > database). This prevents the worker queue from taking over the entire > system and slowing down my web requests and database. > > If we have 12 cores and I've only allocated 4 cores for PHP, I would > want this function to tell my worker queue that it only has 4 cores, > using the system above -- not 12. This probably doesn't matter too much > when this is the only process on the machine. However, in any serious > production environment, you want to be able to prevent a run-away > process/job from harming other unrelated aspects of the system. > > You can also see this behaviour with nproc: > > ❯ taskset -c 0 nproc > 1 > > ❯ nproc > 16 > > -- Rob