Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124571 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 D267E1A00B7 for ; Wed, 24 Jul 2024 13:00:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1721826131; bh=9y5sAznVXtpPTZJABZMCxXf4hcOcbaGlQS+KPAebEJw=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=ayYCghVc+S7i7GEWD6aWUOzWuynZoMR/nErQjvA6cn801+5E+WL5W/ImbTUFmwqdl 5VnFl2nRKUM/Kk33Pf52scd+29CgUj/WrRwTEKX3Y4TddwWBn9fTgqvD5xwTsWaO/B w9E6JZQWuZMSVxjBOGUWth2q2gVCr9gcM+z5xVwb8dOCO9MG0bpc+5E4b9U1eXdBWU nsK/XEWRzRC0EAdHO+3Kz9bk3dw1k8P7mYMbuOixbdI/oj8UDBt4q973z8QFdxMwu+ RZEjFpJPan0l03k2eAHhqXWr8EH0V9WzS7PfYCVMy4Y7WwhUGE62QEXTKP2Z8s4Kui c4yFB1XGWlGXg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 157E718003E for ; Wed, 24 Jul 2024 13:02:11 +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.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) (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 13:02:10 +0000 (UTC) Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-2cd5d6b2581so2171832a91.2 for ; Wed, 24 Jul 2024 06:00:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721826035; x=1722430835; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=9y5sAznVXtpPTZJABZMCxXf4hcOcbaGlQS+KPAebEJw=; b=PwFt2r3Q0hONh8M5xfqeIz8RG7yr2eOPLNc8UfDEJpbmIqXIC5R8aRO0eHk+rGN6xz HSRvBf/r863Le9Z3uwnhRiZlOP6kW+OGcWG9zpvmNveRR01JrjdVsqEygf68IBTAUVGC 8rzf+O6RtNXaUsKDT/PHIxU8W9Adr9F86da+KlQlH8sx473wU7epEqw1i0trWaMMROzQ /8+xN5NxWqIZlpJ8O5Gvq3hAwvJ9pV0GkpUdQW8h0Z78SiJH8iqU76jpRgowwyND2yid 5WQX6fyk6pDzi8X8AuckQos5mBxz8yj+FutswD6ULeHvLuSmFj3bHrrlhy7JxD/QbUle qCAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721826035; x=1722430835; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9y5sAznVXtpPTZJABZMCxXf4hcOcbaGlQS+KPAebEJw=; b=QYG2/ZThZxdwy2t7I0ZvfwBhHSISskWzUSlfnKxxU2D7UVcgXGyLtFdYieHMnU1KGh VGXmOMPZxuzq5yvrRVJ828KxZB224evfvHTwQawHmSVlfFpq1hg3dks3oUQrB6XGtsnl IcFWYiIVxLAdTdxZxR9XVZYxtF0Z92TV750NSn34DzCEGs+MWEku7vxcczKKmUWXyMgs ezPFjEqVNlmCaHUPM/co+XhWaUqNrvqJdnqTp6BUF82M58jewjQBn0Ml+UiS41x9eksa WWzlXOaHUQhNrssF7G/y3fe1E/jAdXVCgWrsCTfkaPWkfsCS4dipexO9Plo4FuL1UXzA J5hg== X-Gm-Message-State: AOJu0YwhNFui3OJnPMiiOeb+VgLfTrZywXcGXihEG5eGyTW1BCoSavMw Scqlddt/zE896BcPjQbI4hf7QQVW68qEB3IEFc2TQtHb1EGbdUrLiLgU2qLYd7lwf2+FFJeVDe8 e0rumf/t2RVAk+85yg/SogOVNdt1ENAWs+BY= X-Google-Smtp-Source: AGHT+IGiH9LHmnAKwKj9x+0OWOuhcwhAK6YNK+2rWZnHVP5yStMis4B/VVfWEg9TWnL8r4nFprYeW9P2f0pGVSOQCLs= X-Received: by 2002:a17:90a:460d:b0:2cc:f293:90a1 with SMTP id 98e67ed59e1d1-2cd1606b633mr11183070a91.25.1721826034938; Wed, 24 Jul 2024 06:00:34 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: <9828b776-660f-44ad-91df-11bea14230a4@app.fastmail.com> <889a6014-299b-4be5-956e-ae5134bb6dfb@app.fastmail.com> In-Reply-To: <889a6014-299b-4be5-956e-ae5134bb6dfb@app.fastmail.com> Date: Wed, 24 Jul 2024 20:00:23 +0700 Message-ID: Subject: Re: [PHP-DEV] tsrm question To: Rob Landers Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: pierre.php@gmail.com (Pierre Joye) On Wed, Jul 24, 2024 at 7:06=E2=80=AFPM Rob Landers wro= te: > > 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 w= rote: > > > > Hello internals, > > > > Last night I went down a rabbit hole with Frankenphp: https://github.co= m/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 performance imp= act is huge to reallocate on every request (mostly due to the global mutex = during allocation). Is anyone familiar with this and could help reviewing t= he changes there? > > I suppose you already looked, but if not, maybe it gives you some directi= on(s). > > The key part is kind of documented here: > > https://github.com/php/php-src/blob/8e93eb2e79cea5fcca6769b46a429de042660= da9/TSRM/TSRM.c#L417 > > tsrm knows nothing about requests but only threads.Its jobs are about > threads data. > > 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. > > 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: > > https://github.com/php/php-src/blob/8e93eb2e79cea5fcca6769b46a429de042660= da9/main/main.c#L2716C13-L2716C25 > > As per the last question, about when to free them: > tsrm_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 freed > anyway too but good to be clean. > > 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). > > 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. > > I hope it helps and did not say too many outdated/wrong explanations :). > > 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. > > Best, > -- > Pierre > > @pierrejoye | http://www.libgd.org > > > 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. Sorry, I forgot to copy that link, php5 but core flow is the same: https://github.com/php/php-src/blob/bcd100d812b525c982cf75d6c6dabe839f61634= a/sapi/isapi/php5isapi.c#L938 -- Pierre @pierrejoye | http://www.libgd.org