Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127453 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 62F221A00BC for ; Sun, 25 May 2025 13:28:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1748179594; bh=8Y7Q9CvvkSEJ4JsoVcKx7F8kWMEdpBxTgT2tmNuK8So=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From; b=GsURo51mEF6kWJIptdBC5lP9fPilPTVdB4BNTAqBg8Cug81zKcjTQDNdMToJ7KCZa 8dn4hJE7vcuwa+2oeafSHgG3KxvMVHRm1MlfPtWpbbUY6V5RcM5GDV2BDjtVvVdLSj xmC3YSmAuZRv0DochGDm5TE48K2bE5hEKhgorKalDID/BjnUkvc5VqZ5MEvU2MRHuF eUKT96veh229rI4buQj77Yb8CsLq0rcB8xpSSoHmM5HJRJWfRNLe6xmyNo6bgTZeHp fP7JEtgpBNL7Df4rM4C3WpK/YGv8MASHuBGuJ1smHVWtqMrFLNZq0tLbukz2SSN0YF fJizbJfNqQInA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 88B8918006C for ; Sun, 25 May 2025 13:26:33 +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=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,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 fhigh-a4-smtp.messagingengine.com (fhigh-a4-smtp.messagingengine.com [103.168.172.155]) (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, 25 May 2025 13:26:33 +0000 (UTC) Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfhigh.phl.internal (Postfix) with ESMTP id EBAB611400DA; Sun, 25 May 2025 09:28:39 -0400 (EDT) Received: from phl-imap-09 ([10.202.2.99]) by phl-compute-05.internal (MEProxy); Sun, 25 May 2025 09:28:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:cc:content-type:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1748179719; x= 1748266119; bh=8Y7Q9CvvkSEJ4JsoVcKx7F8kWMEdpBxTgT2tmNuK8So=; b=C 7eo/7j7xN8MbPMRYiCqxFciuJito1Rfe9iO/kWL4Mg4RsNFnMUYkZcDQwXeFCYjM vLyYDdz3BRqJBi/MZkMaOC11fHnQUGvMJa2DvmSRjwepdmcTlYYUHmJpFAJzwlMU 4P3w5VeF3BqenGZdMjhg0798PtGCmwoCRL3DTW5hNrewL/9rXEoTDdmx1qEWPObX kqIN7fTE6UrVNWQQuZQprFhjwmWa12dRt5RfMpNdOJz86UlrWE08XJRS1acfTVmX aaDRqxszwZQinKDV7tLUiVWA5GK3u8/W+bFi/EK+IOUO70On47Mqv0/LibDGxo7f YPTI6dSLjIa880c5+tpQQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1748179719; x=1748266119; bh=8Y7Q9CvvkSEJ4JsoVcKx7F8kWMEdpBxTgT2 tmNuK8So=; b=PL7tVEtOboDjr5GKPbtoeZIVId1eUP3M5yyQWBFxW1BeM4R34ga HfQ8xDRlY6p0QnReteFmqawICbsfpA2fjZmokpK3wXXrIbfTCrUYCjPSsYhZuIqr NTOj0DklAQ69AqKQkp7Y1tgrF1kA49uVSguChsO485IKXonpOQjwvydu1ggnTFQU L8dzI2urDrw6Wi+r83Sl0mDr6csTY10D7TfUmo2+7qGww2Gg+7UXh2e9q6hKq3EU VBKElUuCfuJfb7BBbMU7h/rEkWpRBWcsO1uT/QxRgdOXsg5XwIkfXmsIFUuJjcxT khTRwG+/TpBRQsvVp4lLcrGTYxJlntlir+w== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtddtgddugeejvdculddtuddrgeefvddrtd dtmdcutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggft fghnshhusghstghrihgsvgdpuffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftd dtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefoggffhffvvefk jghfufgtsegrtderreertdejnecuhfhrohhmpedftfhosgcunfgrnhguvghrshdfuceorh hosgessghothhtlhgvugdrtghouggvsheqnecuggftrfgrthhtvghrnhepteefffegvddu leegkedvuedvhfeifffggfdvudejieektdeltdfgkeevfeeggfefnecuffhomhgrihhnpe hphhhprdhnvghtpdhgihhthhhusgdrtghomhenucevlhhushhtvghrufhiiigvpedtnecu rfgrrhgrmhepmhgrihhlfhhrohhmpehrohgssegsohhtthhlvggurdgtohguvghspdhnsg gprhgtphhtthhopeefpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehmrghilhes uggrnhhivghlkhgvshhsvghlsggvrhhgrdguvgdprhgtphhtthhopegrlhifihhnsehgrg hrshhirdguvgdprhgtphhtthhopehinhhtvghrnhgrlhhssehlihhsthhsrdhphhhprdhn vght X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 286F73020061; Sun, 25 May 2025 09:28:39 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 X-ThreadId: Ta0643016bd41e344 Date: Sun, 25 May 2025 15:28:18 +0200 To: "Alwin Garside" , "Daniel Kesselberg" Cc: "PHP Internals" Message-ID: In-Reply-To: <60B4033B-2E6A-4157-A698-6C58454B07C9@garsi.de> References: <60B4033B-2E6A-4157-A698-6C58454B07C9@garsi.de> Subject: Re: [PHP-DEV] [RFC] Add num_available_processors Content-Type: multipart/alternative; boundary=bbfb6805193c4d4b8b477a87d5de5cd4 From: rob@bottled.codes ("Rob Landers") --bbfb6805193c4d4b8b477a87d5de5cd4 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Sun, May 25, 2025, at 12:07, Alwin Garside wrote: > On 24 May 2025, at 20:48, Rob Landers wrote: > >=20 > > On Sat, May 24, 2025, at 19:37, Daniel Kesselberg wrote: > >>=20 > >>=20 > >> Hi everyone, > >>=20 > >> I'm happy to share my first RFC :) It proposes adding a small funct= ion=20 > >> to retrieve the number of available processors; a feature that's=20 > >> commonly found in other programming languages and one that I believ= e=20 > >> would be a useful addition to PHP. > >>=20 > >> The related PR has already received a bit of early traction, and no= w=20 > >> that the RFC is complete, I'm looking forward to your feedback! > >>=20 > >> RFC: https://wiki.php.net/RFC/num_available_processors > >> Patch: https://github.com/php/php-src/pull/11137 > >>=20 > >> Best > >> Daniel > >>=20 > >=20 > > Looks good! > >=20 > > My main question is: what is this actually counting? In the RFC it m= entions "available processing units" ... which means, what? What counts = as a "processing unit"? Are we talking about CPU Threads/cores; NPU core= s; TPM cores; clocks? GPS? GPU? ... a modern computer has many "processi= ng units" for different purposes and workloads. I=E2=80=99m assuming thi= s is CPU Threads, not physical cores? I will refer to CPU Threads as "Lo= gical Cores" so we all don=E2=80=99t get confused since most of us here = are programmers and saying "thread" has a different meaning. > >=20 > > Secondly, how is it counting "available"? If I assign PHP to a speci= fic CPU affinity mask (say one logical core), will it return 1, or the t= otal 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. > >=20 > > =E2=80=94 Rob >=20 >=20 > Hi Daniel, >=20 > 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 dis= tinguish physical cores from logical cores anyway, and "cpu_cores" provi= des the most understandable abstraction for the vast majority of use cas= es: 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 t= he 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 t= he last 1/3 for other tasks (such as a database). This prevents the work= er queue from taking over the entire system and slowing down my web requ= ests and database. If we have 12 cores and I=E2=80=99ve only allocated 4 cores for PHP, I w= ould want this function to tell my worker queue that it only has 4 cores= , using the system above =E2=80=94 not 12. This probably doesn=E2=80=99t= 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: =E2=9D=AF taskset -c 0 nproc 1 =E2=9D=AF nproc 16 =E2=80=94 Rob --bbfb6805193c4d4b8b477a87d5de5cd4 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Sun, May = 25, 2025, at 12:07, Alwin Garside wrote:
On 24 May 2025, at 20:48, Rob Landers <rob@bottled.codes> wrote:
<= div>> 
> On Sat, May 24, 2025, at 19:37, Daniel Kes= selberg 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 f= eature that's 
>> commonly found in other programmi= ng 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!
>> 
>> 
>> Best
>> Daniel
>> 
> Looks good!
> My main que= stion is: what is this actually counting? In the RFC it mentions "availa= ble processing units" ... which means, what? What counts as a "processin= g unit"? Are we talking about CPU Threads/cores; NPU cores; TPM cores; c= locks? GPS? GPU? ... a modern computer has many "processing units" for d= ifferent purposes and workloads. I=E2=80=99m assuming this is CPU Thread= s, not physical cores? I will refer to CPU Threads as "Logical Cores" so= we all don=E2=80=99t 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 expe= ct it to be 1, since PHP only has access to 1, but I can also see the lo= gic in returning the total number.
> =E2= =80=94 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 enti= rely accurate when hyper-threading is used, but in most cases it's not t= rivial to distinguish physical cores from logical cores anyway, and "cpu= _cores" provides the most understandable abstraction for the vast majori= ty 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 availabl= e cores. However, in Linux there is also the sched_getaffinity meth= od which returns the total number of cores allocated to the current proc= ess (which may be lower). For example, when sharing a webserver and a wo= rker 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 l= ast 1/3 for other tasks (such as a database). This prevents the worker q= ueue from taking over the entire system and slowing down my web requests= and database.

If we have 12 cores and I=E2=80=99= 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 =E2=80=94= not 12. This probably doesn=E2=80=99t matter too much when this is the = only process on the machine. However, in any serious production environm= ent, 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:

=E2=9D=AF t= askset -c 0 nproc
1

=E2=9D=AF nproc
16



=E2=80=94 Rob
--bbfb6805193c4d4b8b477a87d5de5cd4--