Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124569 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 qa.php.net (Postfix) with ESMTPS id 5AE701A00B7 for ; Wed, 24 Jul 2024 12:06:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1721822896; bh=IC2F8+Rqzvy/Jhb3HvifDC0/7p1dxwgKI2Esp9yiLHI=; h=In-Reply-To:References:Date:From:To:Cc:Subject:From; b=VyX3lHyBbWj0+DEp4Sbu3DHSWkFBebcxal4MF2ZKRrzor475FHps37SGtmOyTwLrj FIYxYyRXzZAnGzADePTIhPhpJ0FUMK68UkVzIeTMFBENQiTrYYxFEMgf/tfQaVuPN7 IGQjfzvY3CJMol1vf/ORi3upZTPOerD9PsA8WfhcHU/yzf1pIfZJRxzplDVqN5dB9e OmPYCLsMP8GMp/DkoomYnhSf+WEoLF8Si/RlohkvMGX+Aet5CxxRR0+hwDzXFyLiiL w59oO8OFhInJe4hDSt1YMJSNsSh+OlJGayYC2j/blmIdDJyHhCHIWCxCMhO6ESW9gi zInZC/FbtWKlA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 0CBFE18003A for ; Wed, 24 Jul 2024 12:08:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,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.0 X-Spam-Virus: No X-Envelope-From: Received: from fout7-smtp.messagingengine.com (fout7-smtp.messagingengine.com [103.168.172.150]) (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 ; Wed, 24 Jul 2024 12:08:14 +0000 (UTC) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailfout.nyi.internal (Postfix) with ESMTP id 98716138032C; Wed, 24 Jul 2024 08:06:38 -0400 (EDT) Received: from imap49 ([10.202.2.99]) by compute3.internal (MEProxy); Wed, 24 Jul 2024 08:06:38 -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=fm1; t=1721822798; x= 1721909198; bh=IC2F8+Rqzvy/Jhb3HvifDC0/7p1dxwgKI2Esp9yiLHI=; b=p Z+Nsp4ZsNCKfT7+YM6XV8NnGOYmi789Ix3Bz5mQq0RobfAiW4Zl+3x1Z0ttUYy4f bbBIjtzwpahR4iVPSD3KSlFmYvGjR5MZeBcul6o12/M/DQAiAyE7qHt5+Vj80/CI +fIGs3U5QU2nKjSUW0iujZ5V4kkaSKRxjlteTYfTU8r2izEDfPBVS7UhjRVrEQNe XxGerAiUyJCZkgmuNO0yvsb6mpiA5PmRK+n7WkgxZ2T2biQ4AULk1QzdgwDhwU+5 OVHXoTguTbIQ3s/ilXC2PIbjYNcin6CGayRJkrlDABYJ4s2h4jXnXN+IlnN1ZRmA DuL6ILjXTXH2+hr9MfD3w== 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-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1721822798; x=1721909198; bh=IC2F8+Rqzvy/Jhb3HvifDC0/7p1d xwgKI2Esp9yiLHI=; b=mmdZ30URsrlL1LWM5d5elw8YNPnATtAN9FJMYe5N4t6F V1tsYv8Agzm+9bS6fbbw3XOqSgyyeKl/4GkhfGdjYInUVaevgKwhWRjvIr94rb2p HgTRgQJ9Iyz4TfXvpAzA/3zQqS25t3djT1rVMFQbAQm7zi8cuFlifF1toxi5Mfan slzf4dhVx7H7FCVqIAKb1NCByJzwi3UnwKWiR0r1NUVEid86CQ8HvXsBOzeB/BBE v/ifRaoByh9eqAsU6KjXbABbzheNWBeChg+8ftCfNhb4MMnsneDA5etJK+ne+1uM PtxfnxcMujOzQ0GDE93UK5qwnGHsz2KSvPdNgZWeug== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddriedugdeglecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepofgfggfkjghffffhvfevufgtsegrtd erreerreejnecuhfhrohhmpedftfhosgcunfgrnhguvghrshdfuceorhhosgessghothht lhgvugdrtghouggvsheqnecuggftrfgrthhtvghrnhepuefhgfeviedtveegudeigfeihe etffekfeeutedtheeggeeigfevhfethfekfeehnecuffhomhgrihhnpehgihhthhhusgdr tghomhdplhhisghgugdrohhrghenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpehrohgssegsohhtthhlvggurdgtohguvghspdhnsggprhgtphht thhopedt X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 52E2015A0093; Wed, 24 Jul 2024 08:06:38 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-582-g5a02f8850-fm-20240719.002-g5a02f885 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Message-ID: <889a6014-299b-4be5-956e-ae5134bb6dfb@app.fastmail.com> In-Reply-To: References: <9828b776-660f-44ad-91df-11bea14230a4@app.fastmail.com> Date: Wed, 24 Jul 2024 14:06:15 +0200 To: "Pierre Joye" Cc: internals@lists.php.net Subject: Re: [PHP-DEV] tsrm question Content-Type: multipart/alternative; boundary=72efbbd699cf4c159b1043599f20cbab From: rob@bottled.codes ("Rob Landers") --72efbbd699cf4c159b1043599f20cbab Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable On Wed, Jul 24, 2024, at 13:54, Pierre Joye wrote: > Hi Rob, >=20 >=20 > On Wed, Jul 24, 2024 at 3:17=E2=80=AFPM Rob Landers wrote: > > > > Hello internals, > > > > Last night I went down a rabbit hole with Frankenphp: https://github= .com/dunglas/frankenphp/pull/933 > > > > It isn't 100% clear to me what values `ts_resource(id)` holds and if= it needs to be freed/allocated per request or per thread. The performan= ce impact is huge to reallocate on every request (mostly due to the glob= al mutex during allocation). Is anyone familiar with this and could help= reviewing the changes there? >=20 > I suppose you already looked, but if not, maybe it gives you some dire= ction(s). >=20 > The key part is kind of documented here: >=20 > https://github.com/php/php-src/blob/8e93eb2e79cea5fcca6769b46a429de042= 660da9/TSRM/TSRM.c#L417 >=20 > tsrm knows nothing about requests but only threads.Its jobs are about > threads data. >=20 > A good start (surely already looked) are apache and litespeed (or > embed) for php8, however besides the API names change, the behavior or > goals of each ts_ or tsrm_ functions remain the same as what we had > with 5.x. One with a very similar modus operandi than Franken was > ISAPI for IIS. Mind the names changes, dapt to php8's new names (and > some behaviors) but the basics and core flows have been kept. >=20 > If you know (~) how many threads franken is going to need, tsrm allows > you to preallocate the rsc slots using tsrm_startup_ex. It uses > tsrm_startup, a more powerful version, which lets franken define how > many rsc per thread will be preallocated. I suppose that could help a > bit performance wise: >=20 > https://github.com/php/php-src/blob/8e93eb2e79cea5fcca6769b46a429de042= 660da9/main/main.c#L2716C13-L2716C25 >=20 > As per the last question, about when to free them: > tsrm_shutdown (franken shutdown) will free all rsc >=20 > When a thread is detached (franken kills it for example), > ts_free_thread needs to be called to kill its rsc. They will be freed > anyway too but good to be clean. >=20 > The tsrm resource management will also automatically purge orphan rsrc > (f.e. when a thread dies unexpectedly and ts_free_thread could not be > called). >=20 > Long story short, the TSRM API is very flexible, how and when you > alloc/free rsrc is basically up to you. It is possible to keep some > around in the root thread (and be used in other threads, given the > root thread id is known (can be 0 or else depending how franken > managed them. >=20 > I hope it helps and did not say too many outdated/wrong explanations := ). >=20 > Btw, Welting rewrote that part with Dmitry back then to fix long > standing issues (and drop TSRM_LS/DC/CC uses), not sure if he is still > around but he may help. >=20 > Best, > -- > Pierre >=20 > @pierrejoye | http://www.libgd.org >=20 Hey Pierre, Thank you! This helps tremendously. > A good start (surely already looked) are apache and litespeed (or > embed) for php8, however besides the API names change, the behavior or > goals of each ts_ or tsrm_ functions remain the same as what we had > with 5.x. One with a very similar modus operandi than Franken was > ISAPI for IIS. Mind the names changes, dapt to php8's new names (and > some behaviors) but the basics and core flows have been kept. I looked at litespeed but not apache, so I will look to that as well. > If you know (~) how many threads franken is going to need, tsrm allows > you to preallocate the rsc slots using tsrm_startup_ex. It uses > tsrm_startup, a more powerful version, which lets franken define how > many rsc per thread will be preallocated. I suppose that could help a > bit performance wise: The number of threads are static (at least for now), so we should def do= this. > Long story short, the TSRM API is very flexible, how and when you > alloc/free rsrc is basically up to you. It is possible to keep some > around in the root thread (and be used in other threads, given the > root thread id is known (can be 0 or else depending how franken > managed them. Ah, so this is where I was getting confused. It's unclear what the "id" = is and what it is used for. For example, I see the engine has a couple o= f ids (compiler/execution) but I'm unclear what the sapi should be doing= with them. Should these "ids" be different per thread, or is it just an= arbitrary key? =E2=80=94 Rob --72efbbd699cf4c159b1043599f20cbab Content-Type: text/html;charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Wed, Jul 24,= 2024, at 13:54, Pierre Joye wrote:
Hi Rob,


=
On Wed, Jul 24, 2024 at 3:17=E2=80=AFPM Rob Landers <rob@bottled.codes> wrote:
>
> Hello internals,
>
> Last night I went down a rabbit hole with Frankenphp: https://github.com= /dunglas/frankenphp/pull/933
>
> I= t isn't 100% clear to me what values `ts_resource(id)` holds and if it n= eeds to be freed/allocated per request or per thread. The performance im= pact is huge to reallocate on every request (mostly due to the global mu= tex during allocation). Is anyone familiar with this and could help revi= ewing the changes there?

I suppose you alre= ady looked, but if not, maybe it gives you some direction(s).
<= div>
The key part is kind of documented here:


tsrm knows nothing about requests but = only threads.Its jobs are about
threads data.

A good start (surely already looked) are apache and lit= espeed (or
embed) for php8, however besides the API names = change, the behavior or
goals of each ts_ or tsrm_ functio= ns remain the same as what we had
with 5.x. One with a ver= y similar modus operandi than Franken was
ISAPI for IIS. M= ind the names changes, dapt to php8's new names (and
some = behaviors) but the basics and core flows have been kept.
<= br>
If you know (~) how many threads franken is going to need,= tsrm allows
you to preallocate the rsc slots using tsrm_s= tartup_ex. It uses
tsrm_startup, a more powerful version, = which lets franken define how
many rsc per thread will be = preallocated. I suppose that could help a
bit performance = wise:


As per the last question, about when to free them:
t= srm_shutdown (franken shutdown) will free all rsc

When a thread is detached (franken kills it for example),
ts_free_thread needs to be called to kill its rsc. They will be f= reed
anyway too but good to be clean.

The tsrm resource management will also automatically purge orph= an rsrc
(f.e. when a thread dies unexpectedly and ts_free_= thread could not be
called).

= Long story short, the TSRM API is very flexible, how and when you
alloc/free rsrc is basically up to you. It is possible to keep s= ome
around in the root thread (and be used in other thread= s, given the
root thread id is known (can be 0 or else dep= ending how franken
managed them.

<= div>I hope it helps and did not say too many outdated/wrong explanations= :).

Btw, Welting rewrote that part with Dm= itry back then to fix long
standing issues (and drop TSRM_= LS/DC/CC uses), not sure if he is still
around but he may = help.

Best,
--
= Pierre

@pierrejoye | http://www.libgd.org


Hey Pierre,

Thank= you! This helps tremendously.

A good start (surely already looked) ar= e apache and litespeed (or
embed) for php8, however beside= s the API names change, the behavior or
goals of each ts_ = or tsrm_ functions remain the same as what we had
with 5.x= . One with a very similar modus operandi than Franken was
= ISAPI for IIS. Mind the names changes, dapt to php8's new names (and
=
some behaviors) but the basics and core flows have been kept.=

I looked at litespeed but not= apache, so I will look to that as well.

If you know (~) how many th= reads franken is going to need, tsrm allows
you to preallo= cate the rsc slots using tsrm_startup_ex. It uses
tsrm_sta= rtup, a more powerful version, which lets franken define how
many rsc per thread will be preallocated. I suppose that could help a=
bit performance wise:

The number of threads are static (at least for now), so we should= def do this.

Long story short, the TSRM API is very flexible, how a= nd when you
alloc/free rsrc is basically up to you. It is = possible to keep some
around in the root thread (and be us= ed in other threads, given the
root thread id is known (ca= n be 0 or else depending how franken
managed them.

Ah, so this is where I was getting co= nfused. It's unclear what the "id" is and what it is used for. For examp= le, I see the engine has a couple of ids (compiler/execution) but I'm un= clear what the sapi should be doing with them. Should these "ids" be dif= ferent per thread, or is it just an arbitrary key?

<= div id=3D"sig121229152">=E2=80=94 Rob
--72efbbd699cf4c159b1043599f20cbab--